LA ILAHA ILLA HU
Allah, Your Lord There Is No Deity Except Him.
Python Data Science NumPy Creating Arrays
Creating a NumPy ndarray Object: NumPy is used to work with arrays. The array object in NumPy is called ndarray.
A NumPy ndarray object can be created by using the array() function.
Example 1:
Code
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6])
print(arr)
print(type(arr))
the output will be
[1 2 3 4 5 6]
class 'numpy.ndarray'
type(): This built-in Python function tells us the type of the object passed to it. Like in above code it shows that arr is numpy.ndarray type.
To create an ndarray, we can pass a list, tuple or any array-like object into the array() method, and it will be converted into an ndarray.
Example 2: Use a tuple to create a NumPy array.
Code
import numpy as np
arr = np.array((1, 2, 3, 4, 5))
print(arr)
the output will be
[1 2 3 4 5]
Dimensions in Arrays: A dimension in arrays is one level of array depth (nested arrays).
nested arrays are arrays that have arrays as their elements.
0-D Arrays
0-D arrays, or Scalars, are the elements in an array. Each value in an array is a 0-D array.
Example 3: Create a 0-D array with value 41.
Code
import numpy as np
arr = np.array(41)
print(arr)
the output will be
41
1-D Arrays Or Uni-dimensional: An array that has 0-D arrays as its elements is called uni-dimensional or 1-D array.
These are the most common and basic arrays.
Example 4: Create a 1-D array containing the values 1,2,3,4
Code
import numpy as np
arr = np.array([1, 2, 3, 4])
print(arr)
the output will be
[1 2 3 4]
2-D Arrays: An array that has 1-D arrays as its elements is called a 2-D array.
These are often used to represent matrix or 2nd order tensors.
NumPy has a whole sub module dedicated towards matrix operations called numpy.mat
Example 5: Create a 2-D array containing two arrays with the values 1,2,3,4 and 5,6,7,8
Code
import numpy as np
arr = np.array([[1,2,3,4], [5,6,7,8]])
print(arr)
the output will be
[[1 2 3 4]
[5 6 7 8]]
3-D arrays: An array that has 2-D arrays (matrices) as its elements is called 3-D array.
These are often used to represent a 3rd order tensor.
Example 6: Create a 3-D array with two 2-D arrays, both containing two arrays with the values 8,9,10 and 11,12,13
Code
import numpy as np
arr = np.array([[[8,9,10], [11,12,13]], [[8,9,10], [11,12,13]]])
print(arr)
the output will be
[[[ 8 9 10]
[11 12 13]]
[[ 8 9 10]
[11 12 13]]]
Check Number of Dimensions?
NumPy Arrays provides the ndim attribute that returns an integer that tells us how many dimensions the array has.
Example 7: Check how many dimensions the arrays has?
Code
import numpy as np
a = np.array(41)
b = np.array([1, 2, 3, 4])
c = np.array([[1,2,3,4], [5,6,7,8]])
d = np.array([[[8,9,10], [11,12,13]], [[8,9,10], [11,12,13]]])
print(a.ndim)
print(b.ndim)
print(c.ndim)
print(d.ndim)
the output will be
0
1
2
3
Higher Dimensional Arrays: An array can have any number of dimensions.
When the array is created, you can define the number of dimensions by using the ndmin argument.
Example 8: Create an array with 5 dimensions and verify that it has 5 dimensions.
Code
import numpy as np
arr = np.array([1, 2, 3, 4,5], ndmin=6)
print(arr)
print('number of dimensions :', arr.ndim)
the output will be
[[[[[[1 2 3 4 5]]]]]]
number of dimensions : 6
In this array the innermost dimension (5th dim) has 4 elements, the 4th dim has 1 element that is the vector,
the 3rd dim has 1 element that is the matrix with the vector, the 2nd dim has 1 element that is 3D array and 1st dim has 1 element that is a 4D array.