import numpy as np
nd1 = np.zeros(shape=10)
nd1
array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
nd1[4] = 1
nd1
array([0., 0., 0., 0., 1., 1., 0., 0., 0., 0.])
nd2 = np.arange(10,50)
nd2
array([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, 45, 46, 47, 48, 49])
nd3 = nd2[::-1]
nd3
array([49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33,
32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16,
15, 14, 13, 12, 11, 10])
nd4 = np.random.random(size=(10,10))
nd4
array([[0.90560377, 0.4487317 , 0.18731691, 0.39972696, 0.45401758,
0.35980696, 0.56821786, 0.16698324, 0.97431617, 0.34801765],
[0.94062016, 0.98971942, 0.66129389, 0.82909145, 0.88716164,
0.48652197, 0.52960362, 0.9288553 , 0.84153594, 0.40207861],
[0.1259168 , 0.65340804, 0.26291663, 0.58029045, 0.64215203,
0.88904972, 0.08430606, 0.80061174, 0.51394881, 0.72831859],
[0.73165525, 0.29867023, 0.18976617, 0.91708636, 0.12153946,
0.33828523, 0.35477623, 0.65124615, 0.50395779, 0.55221092],
[0.36489686, 0.3744257 , 0.06939778, 0.82299433, 0.78822478,
0.47437263, 0.0578192 , 0.65741538, 0.18507106, 0.82381541],
[0.92527842, 0.62381079, 0.93358297, 0.91197274, 0.4688368 ,
0.1299322 , 0.93256434, 0.50161041, 0.2331897 , 0.22123128],
[0.11486994, 0.44800573, 0.70364517, 0.27974851, 0.26450798,
0.34457178, 0.66977811, 0.2226335 , 0.87776231, 0.85588318],
[0.96373433, 0.88082624, 0.16675918, 0.04114781, 0.26383571,
0.01386348, 0.29874247, 0.0672143 , 0.66517088, 0.11391125],
[0.79721246, 0.6308044 , 0.99898976, 0.04284937, 0.16413283,
0.49015117, 0.30071537, 0.10647676, 0.08358498, 0.16618354],
[0.43772463, 0.27481354, 0.87846888, 0.32554241, 0.49134266,
0.8556828 , 0.05794326, 0.16425106, 0.16602536, 0.95736807]])
nd4.min()
0.013863477188800433
nd4.max()
0.9989897575713154
nd5 = np.ones(shape=(10,10),dtype=np.int32)
nd5
array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])
nd5[1:-1,1:-1] = 0
nd5
array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])
nd5 = np.zeros(shape = (10,10),dtype=np.int8)
nd5
array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=int8)
nd5[[0,-1]] = 1
nd5[:,[0,-1]] = 1
nd5
array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]], dtype=int8)
nd6_1 = np.arange(0,5,1)
nd6_1
array([0, 1, 2, 3, 4])
nd6 = np.arange(0,25).reshape(5,5)
nd6
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]])
nd6[0:5]=nd6_1
nd6
array([[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4]])
nd6 = np.array([0,1,2,3,4]*5).reshape(5,5)
nd6
array([[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4]])
nd7 = np.linspace(0,1,num=12)
nd7
array([0. , 0.09090909, 0.18181818, 0.27272727, 0.36363636,
0.45454545, 0.54545455, 0.63636364, 0.72727273, 0.81818182,
0.90909091, 1. ])
nd8 = np.random.random(10)
nd8.sort()
nd8
array([0.02057847, 0.09227163, 0.21738285, 0.23367875, 0.40642088,
0.41521377, 0.45507908, 0.53299847, 0.8719871 , 0.8837823 ])
nd8.argsort()
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=int64)
nd8[nd8.sort()]
array([[0.02057847, 0.09227163, 0.21738285, 0.23367875, 0.40642088,
0.41521377, 0.45507908, 0.53299847, 0.8719871 , 0.8837823 ]])
nd9 = np.random.randint(1,10,size = 10)
nd9
(10,)
nd9[nd9.argmax()] = 0
nd9
array([8, 3, 7, 7, 0, 6, 6, 4, 6, 2])
nd10 = np.random.randint(0,10,size=(5,5))
nd10
array([[7, 0, 6, 6, 6],
[0, 0, 5, 9, 4],
[6, 7, 4, 3, 0],
[9, 0, 3, 0, 8],
[5, 0, 4, 9, 2]])
index = [3,2,4,1,0]
nd10[index]
array([[9, 0, 3, 0, 8],
[6, 7, 4, 3, 0],
[5, 0, 4, 9, 2],
[0, 0, 5, 9, 4],
[7, 0, 6, 6, 6]])
index = nd10[:,2].argsort()
nd10[index]
array([[9, 0, 3, 0, 8],
[6, 7, 4, 3, 0],
[5, 0, 4, 9, 2],
[0, 0, 5, 9, 4],
[7, 0, 6, 6, 6]])
nd11 = np.random.randint(0,10,size=(3,4,5,6))
nd11
array([[[[8, 9, 4, 2, 6, 5],
[7, 0, 4, 5, 7, 4],
[5, 6, 0, 6, 7, 3],
[8, 8, 8, 1, 4, 6],
[1, 6, 7, 7, 5, 1]],
[[4, 5, 4, 1, 4, 0],
[4, 7, 0, 8, 7, 9],
[1, 4, 3, 9, 6, 0],
[0, 9, 0, 0, 8, 4],
[5, 4, 8, 6, 8, 4]],
[[0, 2, 6, 9, 1, 7],
[3, 8, 5, 4, 5, 0],
[9, 0, 5, 9, 0, 6],
[2, 8, 3, 9, 0, 5],
[9, 7, 2, 9, 1, 5]],
[[2, 1, 3, 6, 8, 6],
[9, 7, 4, 1, 4, 1],
[0, 6, 2, 8, 6, 0],
[2, 1, 8, 0, 9, 9],
[8, 2, 5, 7, 4, 4]]],
[[[7, 6, 5, 1, 0, 8],
[4, 4, 7, 0, 8, 7],
[5, 2, 9, 3, 4, 3],
[9, 2, 9, 3, 6, 3],
[4, 3, 2, 4, 5, 5]],
[[5, 8, 1, 7, 5, 7],
[2, 0, 6, 7, 7, 0],
[4, 4, 8, 6, 2, 1],
[7, 8, 6, 9, 6, 4],
[2, 8, 3, 4, 9, 9]],
[[0, 3, 1, 1, 4, 2],
[2, 2, 7, 3, 0, 3],
[0, 7, 3, 9, 3, 8],
[5, 6, 4, 8, 0, 7],
[1, 9, 1, 7, 3, 2]],
[[7, 3, 2, 8, 8, 3],
[6, 7, 8, 6, 7, 0],
[3, 1, 6, 2, 3, 9],
[0, 5, 6, 1, 3, 0],
[5, 1, 1, 7, 5, 3]]],
[[[8, 6, 2, 4, 5, 3],
[4, 4, 4, 6, 0, 2],
[2, 2, 5, 1, 9, 5],
[0, 2, 3, 4, 3, 4],
[0, 1, 1, 1, 9, 6]],
[[4, 4, 4, 2, 9, 5],
[8, 3, 4, 8, 3, 1],
[2, 3, 5, 7, 2, 2],
[6, 2, 7, 3, 6, 3],
[6, 3, 1, 2, 6, 8]],
[[2, 7, 4, 2, 8, 9],
[2, 3, 3, 1, 0, 2],
[7, 6, 5, 8, 2, 4],
[3, 4, 0, 1, 3, 3],
[9, 5, 9, 1, 4, 1]],
[[2, 9, 3, 5, 2, 4],
[3, 4, 9, 5, 8, 5],
[8, 3, 8, 2, 0, 6],
[3, 0, 2, 4, 4, 4],
[2, 6, 7, 0, 7, 5]]]])
nd11.sum(axis = -1).sum(axis = -1)
array([[154, 127, 127, 116],
[144, 149, 144, 136],
[127, 105, 125, 124]])
nd11.sum(axis = (-1,-2))
array([[154, 127, 127, 116],
[144, 149, 144, 136],
[127, 105, 125, 124]])
nd12_1 = np.arange(1,6)
nd12 = np.zeros(17,dtype = np.int8)
print(nd1,nd12)
nd12[::4] = nd12_1
nd12
[1 2 3 4 5] [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
array([1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0, 5], dtype=int8)
nd13 = np.random.randint(0,10,size = (3,3))
nd13
array([[1, 9, 3],
[8, 4, 7],
[3, 3, 7]])
nd13[[1,0,2]]
array([[8, 4, 7],
[1, 9, 3],
[3, 3, 7]])
nd14 = np.random.randint(0,10,size = 100000)
nd14
array([9, 5, 9, ..., 7, 3, 8])
%%time
np.power(nd14,3)
Wall time: 0 ns
array([729, 125, 729, ..., 343, 27, 512], dtype=int32)
%%time
ret = []
for i in nd14:
ret.append(i**3)
Wall time: 66.2 ms
nd15_1 = np.random.randint(1,10,size=(5,3))
nd15_1
array([[2, 1, 4],
[7, 9, 2],
[9, 1, 6],
[3, 6, 8],
[5, 3, 8]])
nd15_2 = np.random.randint(1,10,size = (3,2))
nd15_2
array([[3, 9],
[5, 6],
[3, 7]])
np.dot(nd15_1,nd15_2)
array([[ 23, 52],
[ 72, 131],
[ 50, 129],
[ 63, 119],
[ 54, 119]])
nd16 = np.random.randint(0,10,size = (3,8))
nd16
array([[7, 4, 3, 1, 1, 4, 2, 3],
[0, 4, 0, 5, 8, 2, 1, 7],
[9, 7, 1, 0, 8, 6, 7, 7]])
a = nd16.mean(axis = 1)
a
array([3.125, 3.375, 5.625])
nd16 - a.reshape(3,1)
array([[ 3.875, 0.875, -0.125, -2.125, -2.125, 0.875, -1.125, -0.125],
[-3.375, 0.625, -3.375, 1.625, 4.625, -1.375, -2.375, 3.625],
[ 3.375, 1.375, -4.625, -5.625, 2.375, 0.375, 1.375, 1.375]])
nd17 = np.zeros(shape = (8,8),dtype = np.int8)
nd17[::2,1::2] = 1
nd17[1::2,::2] = 1
nd17
array([[0, 1, 0, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 1, 0, 1, 0],
[0, 1, 0, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 1, 0, 1, 0],
[0, 1, 0, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 1, 0, 1, 0],
[0, 1, 0, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 1, 0, 1, 0]], dtype=int8)
nd18 = np.random.randint(0,10,size = (5,5))
nd18
array([[1, 6, 9, 3, 4],
[8, 3, 6, 8, 2],
[5, 6, 0, 9, 8],
[8, 5, 4, 4, 5],
[1, 0, 8, 0, 2]])
nd18_min = nd18.min()
nd18_min
0
nd18_max = nd18.max()
nd18_max
9
nd18 = (nd18 - nd18_min)/(nd18_max - nd18_min)
nd18
array([[0.11111111, 0.66666667, 1. , 0.33333333, 0.44444444],
[0.88888889, 0.33333333, 0.66666667, 0.88888889, 0.22222222],
[0.55555556, 0.66666667, 0. , 1. , 0.88888889],
[0.88888889, 0.55555556, 0.44444444, 0.44444444, 0.55555556],
[0.11111111, 0. , 0.88888889, 0. , 0.22222222]])