Creating NumPy Arrays

Before performing any analysis, the first step is to store data in a proper structure. In NumPy, this structure is called an array. Creating arrays correctly is very important because the entire data analysis process depends on how data is stored and organized.


In this module, you will learn different ways to create NumPy arrays that are commonly used in real-world data analytics.



Creating Arrays from Python Lists and Tuples

The most basic way to create a NumPy array is by converting a Python list or tuple into an array.

Example : Creating an Array from a List

import numpy as np
data = [10, 20, 30, 40]
arr = np.array(data)
print(arr)  # Output: [10 20 30 40]

This converts a normal Python list into a NumPy array.


Example: Creating an Array from a Tuple

values = (5, 15, 25, 35)
arr = np.array(values)
print(arr)
# Output: [ 5 15 25 35]

Both lists and tuples can be used, but NumPy arrays are faster and more efficient for calculations.



Creating Arrays with a Specific Data Type

Sometimes, you need data in a specific format (for example, integers or floating-point values).

arr = np.array([1, 2, 3, 4], dtype=float)
print(arr)
# Output: [1. 2. 3. 4.]

This is useful when working with :



Creating Multidimensional Arrays

Real-world data is often not one-dimensional. It may contain rows and columns like a table.

Example : 2D Array (Matrix)

arr = np.array([
    [1, 2, 3],
    [4, 5, 6]
])
print(arr)
# Output:
# [[1 2 3]
#  [4 5 6]]

This type of structure is very common in :



Creating Special NumPy Arrays

NumPy provides built-in functions to create arrays quickly.

Array of Zeros

arr = np.zeros(5)
print(arr)
# Output: [0. 0. 0. 0. 0.]

Useful for :


Array of Ones

arr = np.ones(5)
print(arr)
# Output: [1. 1. 1. 1. 1.]

Array with a Fixed Value

arr = np.full(5, 10)
print(arr)
# Output: [10 10 10 10 10]


Creating Arrays Using Ranges

Using arange( )

arr = np.arange(1, 10)
print(arr)
# Output: [1 2 3 4 5 6 7 8 9]

Using linspace( )

arr = np.linspace(0, 1, 5)
print(arr)
# Output: [0.   0.25 0.5  0.75 1.  ]

Generates evenly spaced values between two numbers.

Real-Life Use Case :

Used in data visualization and mathematical modeling.



Creating Identity and Diagonal Matrices

Identity Matrix

arr = np.eye(4)
print(arr)
# Output:
# [[1. 0. 0. 0.]
#  [0. 1. 0. 0.]
#  [0. 0. 1. 0.]
#  [0. 0. 0. 1.]]

Useful for :


Diagonal Matrix

arr = np.diag([1, 2, 3])
print(arr)
# Output:
# [[1 0 0]
#  [0 2 0]
#  [0 0 3]]

Useful for :



Creating Arrays with Random Values

Random data is often used for :

Random Values Between 0 and 1

arr = np.random.rand(5)
print(arr)
# Output: [0.123 0.456 0.789 0.234 0.567]

Random Integers

arr = np.random.randint(1, 10, size=5)
print(arr)
# Output: [3 7 1 9 5]

Real-Life Example: Sales Data Creation

daily_sales = np.array([1200, 1500, 1800, 1600, 2000])
print(daily_sales)
# Output: [1200 1500 1800 1600 2000]

This array can later be used to :



Common Mistakes Beginners Make

Avoiding these mistakes improves performance and clarity.



Conclusion

Understanding how to create NumPy arrays is fundamental for data analysis and scientific computing. By mastering these techniques, you can efficiently manipulate and analyze large datasets.