Numpy
Numpy
Numpy
• NumPy arrays are stored at one continuous place in memory unlike lists so processes
can access and manipulate them very efficiently
• First NumPy should be imported to a program before it is used
• import numpy as np
• Now the NumPy package is referred to as np instead of numpy
• NumPy arrays have a fixed size at creation unlike Python lists which can grow
dynamically
• The following program create a NumPy ndarray object by using the array() function
Numpy
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr)
print(type(arr))
Output
[1 2 3 4 5]
<class 'numpy.ndarray'>
Two Dimensional Array
import numpy as np
x = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15]])
print(x)
print(x.shape)
Output
[[ 1 2 3 4 5]
[ 6 7 8 9 10]
[11 12 13 14 15]]
(3, 5)
Three Dimensional Array
import numpy as np
arr = np.array([[[1, 2, 3], [4, 5, 6],[7, 8, 9]], [[10, 11, 12],[13, 14, 15], [16, 17, 18]]])
print(x)
print(arr.shape)
Output
[[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]]
[[10 11 12]
[13 14 15]
[16 17 18]]]
Accessing Array Elements
import numpy as np
arr = np.array([1, 2, 3, 4])
print(arr[2] + arr[3])
Output
7
Accessing Array Element in 3D Array
import numpy as np
arr = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
print(arr[0, 1, 2] + arr[1, 1, 1]) @ 6 + 11
Output
17
Negative Indexing
import numpy as np
arr = np.array([[1,2,3,4,5], [6,7,8,9,10]])
print('Last element from 2nd dim: ', arr[1, -1])
Output
10
Array creation
import numpy as np
>>>np.zeros(10)
array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
>>> np.zeros((3, 6))
array([[0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0.]])
empty() creates an array without initializing its values to any particular value and
return garbage values
>>> np.empty((2, 3, 2))
Array creation
array([[[4.45057637e-308, 1.78021527e-306],
[8.45549797e-307, 1.37962049e-306],
[1.11260619e-306, 1.78010255e-306]],
[[9.79054228e-307, 4.45057637e-308],
[8.45596650e-307, 9.34602321e-307],
[4.94065646e-322, 1.11261027e-306]]])
• arange() is an array-valued version of the built-in Python range function
>>> np.arange(15)
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])
Array creation
import numpy as np
a = np.arange(20).reshape(4, 5)
>>> a
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19]])
>>> a.shape
(4, 5)
>>> a.ndim
2
>>> a.dtype.name
'int64'
Array creation
>>> a.itemsize
8 @ size in bytes
>>> a.size
20
>>> b = np.array([1.2, 3.5, 5.1])
>>> b.dtype
dtype('float64’)
>>> np.ones((3, 6))
array([[1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1.]])
Array creation
>>> np.eye(4)
array([[1., 0., 0., 0.],
[0., 1., 0., 0.],
[0., 0., 1., 0.],
[0., 0., 0., 1.]])
>>> np.identity(3)
array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
Array operations
import numpy as np
arr1 = np.array([[21, 22, 23], [24, 25, 26], [27, 28, 29]])
arr2 = np.array([[2, 3, 4], [5, 6, 7], [8, 9, 3]])
>>> arr3 = arr1 + arr2
>>> arr3
array([[23, 25, 27],
[29, 31, 33],
[35, 37, 32]])
>>> arr4 = arr1 - arr2
Array operations
>>> arr4
array([[19, 19, 19],
[19, 19, 19],
[19, 19, 26]])
>>> arr6 = np.dot(arr1, arr2)
>>> arr6
array([[336, 402, 307],
[381, 456, 349],
[426, 510, 391]])
Transpose of a Matrix
import numpy as np
a = np.array([[1, 2, 3, 4],[5, 6, 7, 8], [9, 10, 11, 12]])
at =a.transpose()
print(a)
print(at)
Output
[[ 1 2 3 4] [[ 1 5 9]
[ 5 6 7 8] [ 2 6 10]
[ 9 10 11 12]] [ 3 7 11]
[ 4 8 12]]
Indexing and Slicing
• NumPy array indexing is a way to select a subset of data or individual elements
• One-dimensional arrays are similar to Python lists
arr1 = np.arange(10)
>>> arr1
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> arr1[5:8]
array([5, 6, 7])
>>> arr1[5:8] = 12
>>> arr1
array([ 0, 1, 2, 3, 4, 12, 12, 12, 8, 9])
Indexing and Slicing
arr1 = np.arange(15)
>>> arr1
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])
>>> arr1[5:7]
array([5, 6])
>>> arr1[5:6] = 12
>>> arr1
array([ 0, 1, 2, 3, 4, 12, 6, 7, 8, 9, 10, 11, 12, 13, 14])
>>> arr_slice = arr1[5:8]
>>> arr_slice
array([12, 6, 7])
Multidimensional Arrays
Output
[[10 27 62 46 1]
[58 2 5 0 74]
[18 80 53 37 3]]
Random Numbers
Output
[[0.50393499 0.0055657 0.11829967 0.67372635 0.10152183]
[0.78642396 0.38139114 0.95172678 0.80690577 0.82478002]
[0.46092385 0.98753774 0.39051375 0.33470791 0.68467847]]