NumPy - Data Science
1. Creating a NumPy Array
a. Basic ndarray
NumPy arrays are very easy to create given the complex problems they solve. To create a very basic ndarray, you use the np.array() method. All you have to pass are the values of the array as a list:
np.array([1,2,3,4])
Output:
This array contains integer values. You can specify the type of data in the dtype argument:
np.array([1,2,3,4],dtype=np.float32)
Output:
Since NumPy arrays can contain only homogeneous datatypes, values will be upcast if the types do not match:
np.array([1,2.0,3,4])
Output:
Here, NumPy has upcast integer values to float values.
NumPy arrays can be multi-dimensional too.
np.array([[1,2,3,4],[5,6,7,8]])
Here, we created a 2-dimensional array of values.
Note: A matrix is just a rectangular array of numbers with shape N x M where N is the number of rows and M is the number of columns in the matrix. The one you just saw above is a 2 x 4 matrix.
b. Array of zeros
NumPy lets you create an array of all zeros using the np.zeros() method. All you have to do is pass the shape of the desired array:
np.zeros(5)
The one above is a 1-D array while the one below is a 2-D array:
np.zeros((2,3))
c. Array of ones
You could also create an array of all 1s using the np.ones() method:
np.ones(5,dtype=np.int32)
d. Random numbers in ndarray
Another very commonly used method to create ndarrays is np.random.rand() method. It creates an array of a given shape with random values from [0,1):
# random np.random.rand(2,3)
array([[0.95580785, 0.98378873, 0.65133872], [0.38330437, 0.16033608, 0.13826526]])
e. An array of your choice
Or, in fact, you can create an array filled with any given value using the np.full() method. Just pass in the shape of the desired array and the value you want:
np.full((2,2),7)
f. Imatrix in NumPy
Another great method is np.eye() that returns an array with 1s along its diagonal and 0s everywhere else.
An Identity matrix is a square matrix that has 1s along its main diagonal and 0s everywhere else. Below is an Identity matrix of shape 3 x 3.
Note: A square matrix has an N x N shape. This means it has the same number of rows and columns.
# identity matrix
np.eye(3)
However, NumPy gives you the flexibility to change the diagonal along which the values have to be 1s. You can either move it above the main diagonal:
# not an identity matrix
np.eye(3,k=1)
Or move it below the main diagonal:
np.eye(3,k=-2)
Note: A matrix is called the Identity matrix only when the 1s are along the main diagonal and not any other diagonal!
g. Evenly spaced ndarray
You can quickly get an evenly spaced array of numbers using the np.arange() method:
np.arange(5)
The start, end and step size of the interval of values can be explicitly defined by passing in three numbers as arguments for these values respectively. A point to be noted here is that the interval is defined as [start,end) where the last number will not be included in the array:
np.arange(2,10,2)
Alternate elements were printed because the step-size was defined as 2. Notice that 10 was not printed as it was the last element.
Another similar function is np.linspace(), but instead of step size, it takes in the number of samples that need to be retrieved from the interval. A point to note here is that the last number is included in the values returned unlike in the case of np.arange().
np.linspace(0,1,5)
Great! Now you know how to create arrays using NumPy. But its also important to know the shape of the array.
0 Comments