11.1 NumPy
11.1 NumPy
1 NumPy
An array is a data type used to store multiple values/elements using a single identifier
(variable name).
The first element has a value of '10' and is at index position [0]
NumPy arrays (also called ndarray) are used to store lists of numerical data, vectors and
matrices.
The NumPy library has a large set of routines (built-in functions) for creating,
manipulating,and transforming NumPy arrays.
#Create a list
lst=[10,15,20,25.1]
In [3]: type(arr1)
numpy.ndarray
Out[3]:
In [ ]: np.shape(arr1)
arr1.dtype
dtype('float64')
Out[6]:
In [7]: new_arr=np.array([11.0,22,33,44,55])
new_arr
In [ ]: new_arr.dtype
In [ ]: # 1-D array
arr1.ndim
Notice in the cell above, all intergers have been converted to a string datatype '
This is because the occurance of a single string in the list 'lst' makes the whole
array to be converted to string datatype
arr2=np.array([[10,15,20,25],[1.1,2.2,3.3,4.4],[100,200,300,400]])
In the cell above, notice the intergers have been converted to floats we can test
the datatype in the cell below to confirm this ...
In [9]: arr2.dtype
arr2.ndim
2
Out[10]:
In [11]: np.shape(arr2)
(3, 4)
Out[11]:
In [12]: #float
arr3 = np.array([[1,2],[3,4]],dtype=float)
arr3.dtype
dtype('float64')
Out[12]:
In [14]: #integer
arr3=np.array([[1.1,2.0],[3.3,4.7]],dtype=int)
arr3.dtype
arr3
array([[1, 2],
Out[14]:
[3, 4]])
In [15]: #string
arr3=np.array([[1.1,2.0],[3.3,4.7]],dtype=str)
arr3.dtype
dtype('<U3')
Out[15]:
In [16]: np.shape(arr3)
(2, 2)
Out[16]:
arr=np.zeros((5,1),dtype=int) # vector
arr
array([[0],
Out[18]:
[0],
[0],
[0],
[0]])
arr=np.ones((6,1)) # vector
arr
In [ ]:
In [20]: # an array with numbers in a given range and sequence using the arange() function
# 0 to 5
# 1-D array
arr=np.arange(11)
arr
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
Out[20]:
In [21]: # stepping
# 3steps
# 1-D array
arr=np.arange(-2,10,3)
arr
array([-2, 1, 4, 7])
Out[21]:
arr=np.arange(15,2,-3)
arr
arr_ident=np.eye(5)
arr_ident
NumPy calls the dimensions as axes (plural of axis). Thus, a 2-D array has two
axes.
In [24]: #dimensions
arr1=np.array([5,6,7])
arr2=np.array([[10,15,20,25],[1.1,2.2,3.3,4.4],[100,200,300,400]])
#ndim method
arr1.ndim # dimensions = 1
arr2.ndim # dimensions = 2
1, 2
arr1=np.array([5,6,7])
arr2=np.array([[10,15,20,25],[1.1,2.2,3.3,4.4],[100,200,300,400]])
arr1.shape
(3,)
Out[26]:
In [27]: arr2.shape
(3, 4)
Out[27]:
arr3=np.ones((5,1))
arr3.shape
arr2.shape
arr1=np.array([11,12])
arr2=np.array([5,6,7])
arr3=np.array([[10,15,20,25],[1.1,2.2,3.3,4.4],[100,200,300,400]])
In [29]: arr1.size
2
Out[29]:
In [30]: arr2.size
3
Out[30]:
In [31]: arr3.size
12
Out[31]:
a). Indexing
arr[0] #index 0
10
Out[33]:
arr[2] #index 2
20
Out[34]:
arr[4] #index 4
45
Out[35]:
Each element is referenced through two indexes i and j, where i represents the row
number and j represents the column number.
Example: Consider the following 2-D array consisting of student marks; create an
array called marks to store marks given in three subjects for four students given in
this table.
marks=np.array([[78,67,56],[76,75,47],[84,59,60],[67,72,54]])
marks
There are 4 students (i.e. 4 rows) and 3 subjects (i.e. 3 columns), the array will be
called marks[4][3].
Here, marks[i,j] refers to the element at (i+1)th row and (j+1)th column because
the index values start at 0.
marks[2][2]
60
Out[37]:
marks[1][0]
76
Out[38]:
marks[3][1]
b). Slicing
Sometimes we need to extract part of an array.
We can define which part of the array to be sliced by specifying the start and end
index values using [start : end] along with the array name.
array([-2, 2])
Out[40]:
array([-2, 2, 6])
Out[41]:
In [44]: arr[3:]
arr[2:,0:2]
array([[-1, 1]])
Out[47]:
In [48]: arr
In [ ]: # method 1
# access all the elements in the 3rd column
# access all rows (i.e. row index 1 to row index 2 - inclusive)
# access index 2 (NOTE: this is not a range therefore index is specific to the elem
arr[0:3,2]
In [ ]: # method 2
# access all the elements in the 3rd column
# access all rows (i.e. row index 1 to row index 2 - inclusive)
# access index 2 (NOTE: this is not a range therefore index is specific to the elem
arr[:,2]
In [ ]: # access elements of 2nd and 3rd row from 1st and 2nd column
# method 1
arr[1:3,0:2]
In [ ]: # access elements of 2nd and 3rd row from 1st and 2nd column
# method 2
arr[1:,:2]
In [ ]: # access elements of 1st and 2nd row from 3rd and 4th column
# method 1
arr[0:2,2:4]
In [ ]: # access elements of 1st and 2nd row from 3rd and 4th column
# method 2
arr[:2,2:4]
If row indices are not specified, it means all the rows are to be considered.
Likewise, if column indices are not specified, all the columns are to be considered.
For instance, adding two arrays will result in the first element in the first array to
be added to the first element in the second array, and so on.
arr1.shape==arr2.shape
True
Out[49]:
In [50]: arr1
array([[3, 6],
Out[50]:
[4, 2]])
In [51]: arr2
array([[10, 20],
Out[51]:
[15, 12]])
arr1+arr2
array([[13, 26],
Out[52]:
[19, 14]])
arr1-arr2
arr1*arr2
arr1/arr2
array([[0.3 , 0.3 ],
Out[54]:
[0.26666667, 0.16666667]])
arr1**arr2
arr1**3.5
arr2 % arr1
array([[1, 2],
Out[57]:
[3, 0]])
b). Transposition
Transposing an array turns its rows into columns and columns into rows just like
matrices in mathematics.
arr.transpose()
c). Sorting
Sorting is to arrange the elements of an array in hierarchical order either
ascending or descending.
arr = np.array([1,0,2,-3,6,8,4,7])
arr
arr.sort()
arr
array([-3, 0, 1, 2, 4, 6, 7, 8])
Out[61]:
In 2-D array, sorting can be done along either of the axes i.e., row-wise or column-
wise.
By default, sorting is done per row (i.e., on axis = 1). It means to arrange elements
in each row in ascending order.
When axis=0, sorting is done per column, which means each column is sorted in
ascending order.
arr1=np.array([[10,-7,0, 20],[-5,1,200,40],[30,1,-1,4]])
arr2=np.array([[10,-7,0, 20],[-5,1,200,40],[30,1,-1,4]])
arr3=np.array([[10,-7,0, 20],[-5,1,200,40],[30,1,-1,4]])
arr1
In [64]: arr2
arr2.sort(axis=1)
arr2
In [66]: arr3
arr3.sort(axis=0)
arr3
d). Concatenation
Concatenating 1-D arrays means appending the sequences one after another.
In [74]: arr1=np.zeros(3)
arr2=np.ones(3)
arr1
In [69]: arr2
1-D arrays only allow concatenation on the same row, otherwise an error is
returned
In [75]: # concatenate 1-D arrays on the same columns when there is only 1 row returns an er
arr3=np.concatenate((arr1,arr2),axis=1)
arr3
#this is like forcefully creating a new row on arr1/ trying to stack 1-D arrays (py
---------------------------------------------------------------------------
AxisError Traceback (most recent call last)
Cell In[75], line 3
1 # concatenate 1-D arrays on the same columns when there is only 1 row retu
rns an error
----> 3 arr3=np.concatenate((arr1,arr2),axis=1)
4 arr3
arr1=np.eye(3)
arr2=np.ones((3,3))
print(arr1)
print()
print(arr2)
[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]
arr3=np.concatenate((arr1,arr2),axis=0)
arr3
In [ ]: arr3.shape
arr3=np.concatenate((arr1,arr2),axis=1)
arr3
In [ ]: arr3.shape
arr=np.arange(1,11)
arr
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
Out[79]:
In [80]: arr.shape
(10,)
Out[80]:
array([[ 1, 2, 3, 4, 5],
Out[81]:
[ 6, 7, 8, 9, 10]])
In [82]: arr1.shape
(2, 5)
Out[82]:
In [83]: # 1D to 2D
arr1=arr.reshape(5,2)
arr1
array([[ 1, 2],
Out[83]:
[ 3, 4],
[ 5, 6],
[ 7, 8],
[ 9, 10]])
In [84]: # 2D to 1D
arr3=arr1.reshape(10,)
arr3
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
Out[84]:
In [ ]: arr3.shape
arr=np.arange(1,11)
arr1=arr.reshape(5,2)
arr1
array([[ 1, 2],
Out[85]:
[ 3, 4],
[ 5, 6],
[ 7, 8],
[ 9, 10]])
In [86]: # The max() function finds the maximum element from an array
arr1.max()
10
Out[86]:
In [87]: # The max() function finds the maximum element from an array by row
arr1.max(axis=0)
array([ 9, 10])
Out[87]:
In [88]: # The min() function finds the minimum element from an array
arr1.min()
In [89]: # The min() function finds the minimum element from an array by column
arr1.min(axis=1)
array([1, 3, 5, 7, 9])
Out[89]:
In [90]: # The sum() function finds the sum of all elements of an array
arr1.sum()
55
Out[90]:
In [91]: # The mean() function finds the average of elements of the array
arr1.mean()
5.5
Out[91]:
In [92]: # The std() function is used to find standard deviation of an array of elements
arr1.std()
2.8722813232690143
Out[92]:
ASSIGNMENT
1. Using create a 1-D array with 12 odd numbers between 1 and 24
2. Reshape the array to a 3 by 4 ndarray
3. Transpose the array
4. Create a new 3 by 4 identity ndarray
5. Join the first array to the second array along the columns
6. Calculate the mean and standard deviation of each column and store them in a
Python list
In [ ]: