NUMPY ARRAYS
OBJECTIVE: To print the dimension, shape and the number of bytes of an array.
1 IMPORT
[27]: import numpy as np
np.random.seed(0)
x1 = np.random.randint(10, size=6)
x2 = np.random.randint(10, size=(3, 4))
x3 = np.random.randint(10, size=(3, 4, 5))
[41]: import numpy as np
np.random.seed()
a=np.random.randint(100)
print(a)
print("dtype: ", x3.dtype)
3
dtype: int32
[55]: x1
[55]: array([5, 0, 3, 3, 7, 9])
2 POSITIVE INDEXING
[57]: x1
[57]: array([5, 0, 3, 3, 7, 9])
[60]: x1[5]
[60]: 9
3 NEGATIVE INDEXING
[64]: x1[-2]
[64]: 7
1
[66]: x1[-4]
[66]: 3
4 INDEXING TWO DIM ARRAY
[68]: x2[1,3]
[68]: 8
[70]: x2[0,0]
[70]: 3
5 MODIFYING IN ARRAY
[83]: x2[1, 2] = 11
x2
[83]: array([[ 3, 5, 2, 4],
[ 7, 6, 11, 8],
[ 1, 6, 7, 7]])
[90]: x1[0] = 7.1254
x1
[90]: array([7, 0, 3, 3, 7, 9])
6 SUB ARRAY
[98]: x= np.arange(15)
x
[98]: array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])
7 SLICING
[102]: x[:4]
[102]: array([0, 1, 2, 3])
[123]: x[8:16]
[123]: array([ 8, 9, 10, 11, 12, 13, 14])
2
8 MULTI DIM ARRAY
[125]: x2
[125]: array([[ 3, 5, 2, 4],
[ 7, 6, 11, 8],
[ 1, 6, 7, 7]])
[126]: x2[::2, ::3]
[126]: array([[3, 4],
[1, 7]])
[127]: x2
[127]: array([[ 3, 5, 2, 4],
[ 7, 6, 11, 8],
[ 1, 6, 7, 7]])
9 ACCESSING ROW & COLOUMNS
print(x2[:, :2])
[[3 5]
[134]: [7 6]
[1 6]]
print(x2[:, 0])
[3 7 1]
[141]: 10 Reshaping of Arrays
[154]: x = np.array([1, 2, 3])
x
[154]: array([1, 2, 3])
[156]: x.reshape((3, 1))
[156]: array([[1],
[2],
[3]])
3
11 Array Concatenation and Splitting
[162]: x=np.array([1])
y=np.array([4,3])
np.concatenate([x,y])
[162]: array([1, 4, 3])
[168]: m=np.array([3])
np.concatenate([x,y,])
[168]: array([1, 4, 3])
[169]: np.concatenate([x,y,m])
[169]: array([1, 4, 3, 3])
12 Splitting of arrays
[176]: x = [1, 2, 3, 99, 99, 3, 2, 1]
x1, x2, x3 = np.split(x, [3, 5])
print(x1, x2, x3)
[1 2 3] [99 99] [3 2 1]
[194]: grid= np.arange(45).reshape((5, 9))
grid
[194]: array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8],
[ 9, 10, 11, 12, 13, 14, 15, 16, 17],
[18, 19, 20, 21, 22, 23, 24, 25, 26],
[27, 28, 29, 30, 31, 32, 33, 34, 35],
[36, 37, 38, 39, 40, 41, 42, 43, 44]])
[201]: upper, lower = np.vsplit(grid, [2])
print(upper)
print(lower)
[[ 0 1 2 3 4 5 6 7 8]
[ 9 10 11 12 13 14 15 16 17]]
[[18 19 20 21 22 23 24 25 26]
[27 28 29 30 31 32 33 34 35]
[36 37 38 39 40 41 42 43 44]]
[206]: left, right = np.hsplit(grid, [1])
print(left)
4
print(right)
[[ 0]
[ 9]
[18]
[27]
[36]]
[[ 1 2 3 4 5 6 7 8]
[10 11 12 13 14 15 16 17]
[19 20 21 22 23 24 25 26]
[28 29 30 31 32 33 34 35]
[37 38 39 40 41 42 43 44]]
13 The Slowness of Loops
[246]: import numpy as np
np.random.seed(0)
def compute_reciprocals(values):
output = np.empty(len(values))
for i in range(len(values)):
output[i] = 1.0 / values[i]
return output
values = np.random.randint(1, 10, size=5)
compute_reciprocals(values)
[246]: array([0.16666667, 1. , 0.25 , 0.25 , 0.125 ])
14 UFuncs
[30]: import numpy as np
np.random.seed(0)
def compute_reciprocals(values):
output = np.empty(len(values))
for i in range(len(values)):
output[i] = 1.0 / values[i]
return output
values = np.random.randint(1, 10, size=5)
result = compute_reciprocals(values)
print(result)
[0.16666667 1. 0.25 0.25 0.125 ]
5
[253]: print(compute_reciprocals(values))
print(3.1 / values)
[0.16666667 1. 0.25 0.25 0.125 ]
[0.51666667 3.1 0.775 0.775 0.3875 ]
15 Array arithmetic
[8]: import numpy as np
x = np.arange(4)
print("x =", x)
print("x + 5 =", x + 5)
print("x - 5 =", x - 5)
print("x * 2 =", x * 2)
x = [0 1 2 3]
x + 5 = [5 6 7 8]
x - 5 = [-5 -4 -3 -2]
x * 2 = [0 2 4 6]
[9]: -(0.5*x + 1) ** 2
[9]: array([-1. , -2.25, -4. , -6.25])
16 Absolute value
[13]: X = np.array([-2, -1, 0, 1, 2])
abs(x)
[13]: array([0, 1, 2, 3])
# Trignometric functions
[16]: theta = np.linspace(0, np.pi, 3)
[17]: print("theta = ", theta)
print("sin(theta) = ", np.sin(theta))
print("cos(theta) = ", np.cos(theta))
print("tan(theta) = ", np.tan(theta))
theta = [0. 1.57079633 3.14159265]
sin(theta) = [0.0000000e+00 1.0000000e+00 1.2246468e-16]
cos(theta) = [ 1.000000e+00 6.123234e-17 -1.000000e+00]
tan(theta) = [ 0.00000000e+00 1.63312394e+16 -1.22464680e-16]
6
[22]: x = [-1, 0, 1]
print("x = ", x)
print("arcsin(x) = ", np.arcsin(x))
print("arccos(x) = ", np.arccos(x))
print("arctan(x) = ", np.arctan(x))
x = [-1, 0, 1]
arcsin(x) = [-1.57079633 0. 1.57079633]
arccos(x) = [3.14159265 1.57079633 0. ]
arctan(x) = [-0.78539816 0. 0.78539816]
[0.16666667 1. 0.25 0.25 0.125 ]
[28]:
[ ]:
INFERENCE: Numpy, pandas, scipy, seaborn and statsmodels packages
were installed