Bikash Santra
Indian Statistical Institute, Kolkata
import numpy as np
print(np.__version__)
1.20.1
x = np.array([1,2,3,4,5])
x1 = np.array([[6,7,8,9,0]])
# print(x1)
# x1 = np.array((5,3))
# print(np.shape(x1))
print('x =', x, type(x))
x = [1 2 3 4 5] <class 'numpy.ndarray'>
print('x1 =', x1)
x1 = [[6 7 8 9 0]]
print(np.shape(x), np.shape(x1))#, x.shape(), x1.shape())
(5,) (1, 5)
y = x1 + 5
print('y =', y)
y = [[11 12 13 14 5]]
z = x1 * 2
print('z =', z)
z = [[12 14 16 18 0]]
A = np.array([
[1, 2, 3, 4],
[4, 5, 6, 6],
[7, 8, 9, 7]
])
print(A, A.shape, np.shape(A))
[[1 2 3 4] [4 5 6 6] [7 8 9 7]] (3, 4) (3, 4)
C = A + 10
print(C)
[[11 12 13 14] [14 15 16 16] [17 18 19 17]]
print('')
D = A * 3
print(D)
[[ 3 6 9 12] [12 15 18 18] [21 24 27 21]]
A = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(A, A.shape)
[[1 2 3] [4 5 6] [7 8 9]] (3, 3)
x = np.array([1,2,3])
print(x)
print(A + x)
[1 2 3] [[ 2 4 6] [ 5 7 9] [ 8 10 12]]
x = np.array([
[1],
[2],
[3]
])
print(x)
print(A + x)
[[1] [2] [3]] [[ 2 3 4] [ 6 7 8] [10 11 12]]
x = np.array([1, 2, 3])
y = np.array([1, 2, 3])
inner = np.dot(x, y)
print(inner)
14
outer = np.outer(x,y)
print(outer)
[[1 2 3] [2 4 6] [3 6 9]]
A = np.random.rand(5,3)
print(A)
[[0.77132099 0.20269011 0.38458103] [0.995501 0.31162557 0.9699157 ] [0.67891847 0.36808584 0.61636367] [0.32266934 0.23058523 0.68805568] [0.96393289 0.69779723 0.85389311]]
print('')
x = np.random.rand(5)
print(x, type(x), x.shape)
# Will not work
#print(A + x)
[0.14138948 0.50072268 0.14359971 0.66582373 0.74436788] <class 'numpy.ndarray'> (5,)
x = np.random.randint(-1000,1000,(5,5))
print(x)
[[ 604 -342 -655 722 262] [-691 799 918 -59 294] [-671 103 -590 512 230] [ 157 244 -499 128 16] [-109 448 255 123 986]]
x = np.random.rand(12)
print(x, type(x), x.shape)
[0.51900327 0.55527827 0.07304653 0.90244838 0.22817104 0.96889324 0.39072028 0.74573034 0.27101815 0.05025909 0.44034638 0.4174488 ] <class 'numpy.ndarray'> (12,)
y = np.reshape(x,(3,4))
print(y)
[[0.51900327 0.55527827 0.07304653 0.90244838] [0.22817104 0.96889324 0.39072028 0.74573034] [0.27101815 0.05025909 0.44034638 0.4174488 ]]
x = np.array([x])
print(x,x.shape)
[[0.51900327 0.55527827 0.07304653 0.90244838 0.22817104 0.96889324 0.39072028 0.74573034 0.27101815 0.05025909 0.44034638 0.4174488 ]] (1, 12)
print('')
y = np.reshape(x,(3,4))
print(y)
[[0.51900327 0.55527827 0.07304653 0.90244838] [0.22817104 0.96889324 0.39072028 0.74573034] [0.27101815 0.05025909 0.44034638 0.4174488 ]]
A = np.random.rand(5,3)
print(A, type(A), A.shape)
[[0.32421144 0.29237086 0.1880635 ] [0.37535086 0.64426259 0.17127461] [0.35832559 0.79104414 0.43732181] [0.16209518 0.28795531 0.9242762 ] [0.55722131 0.3951814 0.80055236]] <class 'numpy.ndarray'> (5, 3)
y = np.reshape(A,(3,5))
print(y)
[[0.32421144 0.29237086 0.1880635 0.37535086 0.64426259] [0.17127461 0.35832559 0.79104414 0.43732181 0.16209518] [0.28795531 0.9242762 0.55722131 0.3951814 0.80055236]]
print('')
x = np.random.rand(5)
print(x)
# Will not work
#print(A + x)
y = np.reshape(x,(5,1))
print(y)
[0.69831831 0.8823071 0.29498539 0.08224456 0.91234731] [[0.69831831] [0.8823071 ] [0.29498539] [0.08224456] [0.91234731]]
print('')
print(A + y)
[[1.02252975 0.99068917 0.88638181] [1.25765797 1.52656969 1.05358171] [0.65331098 1.08602954 0.7323072 ] [0.24433974 0.37019987 1.00652076] [1.46956862 1.30752872 1.71289967]]
x = np.random.rand(3)
print(x)
y = np.random.rand(3)
print(y)
[0.82855427 0.21050694 0.98698451] [0.2528599 0.32296625 0.14575312]
z = np.vstack((x, y))
print(z, z.shape)
[[0.82855427 0.21050694 0.98698451] [0.2528599 0.32296625 0.14575312]] (2, 3)
z = np.hstack((x, y))
print(z, type(z), z.shape)
[0.82855427 0.21050694 0.98698451 0.2528599 0.32296625 0.14575312] <class 'numpy.ndarray'> (6,)
Exercise: Create two arrays of size 3x5 initialized with random integers drawn from (-100,100) and then vertically & horizontally stack them.
x1 = np.random.randint(-100,100,(3,5))
x2 = np.random.randint(-100,100,(3,5))
vX = np.vstack((x1,x2))
hX = np.hstack((x1,x2))
print(vX, '\n', hX)
[[-25 31 28 -2 22] [ 10 -62 -91 72 56] [ -9 76 -29 60 -91] [-30 99 96 92 -25] [-21 31 -86 27 -53] [-15 76 -57 -76 98]] [[-25 31 28 -2 22 -30 99 96 92 -25] [ 10 -62 -91 72 56 -21 31 -86 27 -53] [ -9 76 -29 60 -91 -15 76 -57 -76 98]]
A = np.random.rand(5,1)
print(A.ndim)
print(A.shape)
2 (5, 1)
print('')
x = np.random.rand(5,3,10,25)
print(x.ndim)
print(x.shape)
print(x)
4 (5, 3, 10, 25) [[[[8.95695602e-01 8.15368342e-01 8.45767140e-01 ... 8.77039090e-01 4.33004827e-01 6.22352699e-01] [3.82431357e-01 7.63140405e-01 8.92406786e-01 ... 9.82396024e-01 8.84651303e-01 9.91303581e-01] [2.58877584e-01 5.73070305e-01 2.86779348e-01 ... 7.00848078e-01 1.18170599e-01 4.96898632e-01] ... [7.30733214e-01 2.63227255e-01 1.82467427e-01 ... 2.11366676e-01 5.46586609e-02 3.75403443e-01] [6.81490313e-01 8.07221326e-01 1.39705647e-01 ... 1.01607993e-01 6.64976122e-01 3.29220978e-01] [5.82690715e-01 1.16190722e-01 4.29195310e-01 ... 8.30976678e-01 6.61114678e-01 2.91433141e-01]] [[5.88253165e-01 8.03571114e-01 9.65933776e-01 ... 7.94160037e-01 1.23871908e-01 1.52416986e-01] [8.89599797e-01 4.57048397e-02 3.43983659e-01 ... 5.48503783e-03 6.66787730e-01 5.95746856e-01] [1.51872350e-01 3.44405080e-01 3.37998644e-01 ... 2.04336011e-01 2.17213738e-01 1.16659366e-01] ... [8.92358112e-01 7.29022003e-01 6.45893827e-01 ... 3.66694859e-01 6.28423044e-01 4.53831818e-01] [1.38871938e-01 4.20415058e-01 9.82822046e-02 ... 8.28496532e-01 1.81738100e-01 2.62280948e-01] [8.09058606e-02 5.23208542e-01 3.47730654e-01 ... 8.81028823e-01 3.94882780e-02 3.52949775e-01]] [[2.10103155e-01 7.65123834e-01 2.80890103e-01 ... 8.96173063e-01 6.69471537e-01 9.92904035e-01] [4.36204407e-01 2.78529333e-01 3.51532663e-01 ... 4.15010618e-01 6.29289771e-03 3.22978618e-01] [9.68158468e-01 3.98260004e-01 7.32255808e-01 ... 3.53018239e-01 1.17861643e-01 2.33099832e-01] ... [2.06686908e-01 7.09751211e-01 9.95315695e-01 ... 9.44515036e-01 3.76958753e-01 7.78784000e-01] [5.35750940e-02 5.79616049e-01 6.29261714e-01 ... 8.16209879e-01 9.33446027e-01 6.00900052e-01] [4.39493235e-01 1.35299558e-01 4.98826853e-01 ... 4.50969193e-01 2.75809237e-01 8.54907876e-01]]] [[[4.37121027e-01 6.36867543e-01 7.25900774e-01 ... 5.82402501e-01 1.18159243e-01 7.09964563e-01] [8.88428560e-01 3.75719523e-02 3.63838358e-01 ... 9.83256556e-01 2.91143369e-01 4.51173559e-01] [1.83592841e-01 2.44370555e-01 4.53878061e-01 ... 1.41128964e-01 8.53593942e-01 7.77352557e-01] ... [5.98623291e-01 1.02320327e-01 6.38175086e-01 ... 2.71884936e-01 9.87100988e-01 8.49348743e-01] [6.22503778e-01 5.42816701e-01 6.60303505e-01 ... 2.24251631e-01 4.30118899e-02 4.85905288e-01] [6.33125229e-01 9.48578820e-01 1.20677851e-01 ... 7.83642694e-02 9.70796277e-01 9.86676933e-01]] [[9.13691024e-01 8.28732114e-01 8.07102611e-02 ... 9.33945286e-01 2.54468582e-01 1.14224204e-01] [1.77572805e-01 8.36233393e-01 6.78522669e-01 ... 8.26880088e-01 3.23563417e-01 5.57734369e-01] [5.86067019e-01 4.32900545e-01 6.50218713e-02 ... 3.79823720e-01 3.08703943e-01 4.60138421e-01] ... [5.29487156e-02 8.68314654e-01 5.99247356e-01 ... 2.08858184e-01 6.86842140e-01 9.08609416e-01] [2.60785231e-01 2.17619011e-01 9.60832653e-01 ... 5.34659118e-02 7.86880259e-01 7.53150540e-01] [6.62334669e-01 9.79409301e-01 7.55219509e-01 ... 9.06530917e-01 7.42505372e-01 9.52741064e-01]] [[9.12594008e-01 1.36541893e-01 8.85567970e-01 ... 4.72284382e-01 1.06737308e-01 7.37195444e-01] [8.31185592e-03 8.56806851e-02 5.06451558e-01 ... 5.56920190e-01 8.10669310e-01 3.95129516e-01] [6.68254877e-01 5.61454651e-01 9.36890906e-01 ... 4.55112249e-01 2.87392254e-01 8.96866989e-01] ... [3.77319395e-01 2.98541676e-01 6.98561413e-01 ... 6.46833814e-01 6.79929064e-01 8.76357064e-01] [3.91458645e-01 7.72820268e-02 7.73212532e-01 ... 2.41780826e-02 9.24251008e-02 4.06921833e-01] [8.00717933e-03 4.99099282e-01 8.30865739e-01 ... 9.74804935e-01 7.08596852e-01 8.70670102e-01]]] [[[4.81186907e-01 7.95203919e-02 4.71556166e-01 ... 6.24328719e-01 8.37398920e-01 5.48848156e-01] [8.78072350e-01 8.85102015e-01 5.17618260e-01 ... 9.97261378e-01 9.77764196e-01 5.48731422e-01] [3.38277545e-01 8.89988213e-01 2.25409991e-02 ... 6.58818986e-02 8.46197781e-01 6.69339375e-01] ... [3.88495792e-01 9.85306161e-01 8.26551876e-02 ... 7.20732620e-01 2.29327359e-01 6.64155003e-01] [2.35006552e-02 1.14201039e-01 9.69674028e-01 ... 6.59538735e-01 2.69416366e-02 8.26990876e-01] [7.91718198e-01 9.77062986e-06 8.58336695e-01 ... 1.68158423e-01 2.78519201e-01 5.21627722e-01]] [[2.69127776e-03 3.27241253e-01 1.37816642e-01 ... 2.89870601e-01 8.12911765e-01 3.10662296e-01] [4.62825156e-02 9.27826072e-01 9.30940802e-01 ... 2.45584193e-02 9.87859315e-01 4.30675817e-01] [2.84253795e-01 3.59738620e-02 7.14234931e-01 ... 2.34293494e-01 9.12472550e-01 7.39215290e-01] ... [3.02970035e-01 7.05212348e-01 8.20270752e-01 ... 6.55439883e-01 1.63408568e-02 3.22502094e-02] [9.07447565e-01 8.58064783e-01 4.78855229e-01 ... 9.60271965e-01 2.02056737e-01 1.44045450e-01] [8.19707744e-01 4.60870340e-01 1.77082358e-01 ... 2.64145423e-01 8.67208769e-01 3.75294832e-01]] [[1.12036595e-01 2.12184491e-01 7.29685222e-01 ... 5.41233823e-01 9.03639285e-01 1.01392132e-01] [7.84202415e-01 7.12662075e-01 7.75981602e-01 ... 9.13637867e-01 3.22250902e-01 5.43820959e-01] [2.66511442e-01 7.04016200e-02 5.89354893e-01 ... 2.21924348e-01 5.69407648e-02 8.11105688e-01] ... [3.67034135e-01 1.02051247e-01 3.67407312e-02 ... 5.38658096e-01 3.50990589e-02 1.65193620e-02] [4.30514576e-01 1.22086615e-01 5.44350018e-01 ... 7.69060158e-01 3.78346729e-01 4.01916049e-03] [4.98151705e-01 4.28007057e-01 6.29373480e-01 ... 6.09532422e-01 9.02579152e-01 1.25090669e-01]]] [[[9.37712262e-01 5.83369784e-03 8.25311300e-01 ... 5.17461385e-01 6.22171338e-02 9.09286813e-01] [8.93352374e-02 5.00699591e-01 4.27932896e-01 ... 9.96640642e-01 3.11608189e-01 8.47294513e-02] [1.73619519e-01 5.00132373e-02 7.83873003e-01 ... 2.80782893e-02 9.99438734e-01 9.31266588e-02] ... [3.57568295e-01 4.04965218e-01 5.37127712e-01 ... 9.78747781e-01 3.25942097e-01 6.02403287e-01] [9.79798097e-01 4.65783127e-01 6.98375883e-01 ... 7.60102257e-01 1.33672708e-02 8.45061452e-01] [9.84706230e-01 7.19981415e-01 1.71324039e-01 ... 3.40277929e-01 5.90424859e-01 5.93385126e-01]] [[3.89230774e-02 4.84747943e-01 3.53970890e-01 ... 9.15089547e-01 5.44857469e-01 1.61731760e-01] [9.04811646e-02 2.13532441e-01 1.91517078e-01 ... 2.68656409e-01 2.65978958e-01 4.03388978e-01] [7.42739330e-02 5.95250110e-01 1.32321521e-01 ... 4.37007994e-01 3.96790480e-02 8.94397397e-01] ... [7.69347679e-01 9.38317057e-01 4.26726833e-01 ... 1.24657017e-03 2.16042095e-01 8.86048622e-01] [6.97310892e-02 4.46981497e-01 4.79385570e-01 ... 6.46545513e-01 1.91156655e-01 1.70194190e-01] [6.58620802e-01 3.42876550e-01 4.15465392e-01 ... 2.56012856e-01 1.30084819e-01 5.28343540e-02]] [[3.61550392e-01 7.26389298e-01 6.72291836e-01 ... 3.95190622e-01 7.19498287e-01 3.81240411e-01] [1.59183595e-01 9.58064066e-01 5.16980372e-01 ... 8.60160246e-01 6.80440244e-01 1.15171277e-01] [2.68983478e-01 6.47561063e-01 9.33551283e-02 ... 1.90819021e-01 8.25791064e-01 5.10840705e-01] ... [7.01748865e-01 2.52699280e-01 1.23056429e-01 ... 5.74278237e-01 8.81469881e-01 4.11206009e-01] [2.24067763e-01 8.04466293e-02 1.18162783e-01 ... 8.64473333e-01 9.72278043e-01 8.81176734e-01] [5.23304222e-01 2.36049866e-01 7.39136878e-02 ... 3.91297539e-01 5.98274279e-01 2.38712375e-01]]] [[[2.69953587e-01 9.72797720e-01 2.72781496e-01 ... 5.93712294e-01 8.29098571e-01 5.94390949e-01] [1.39124550e-01 9.46545726e-01 7.99343652e-01 ... 9.33604809e-01 4.66387997e-01 7.36257054e-01] [9.94551184e-02 4.31374824e-01 9.74218070e-01 ... 1.67459280e-02 7.27468566e-01 3.32875523e-01] ... [5.83919313e-01 5.35319082e-01 1.86732720e-01 ... 7.91447052e-01 5.23289264e-01 1.42674446e-01] [1.93793211e-01 5.38467454e-01 9.20518914e-01 ... 7.52284353e-01 9.74018796e-01 2.54985534e-01] [3.61001014e-01 4.53271865e-01 8.32004928e-01 ... 1.94282932e-01 2.06869033e-01 6.12120486e-01]] [[7.38838880e-01 2.66671011e-01 5.36645710e-02 ... 1.10892029e-01 2.69407785e-01 8.28746040e-01] [9.86423872e-02 8.34902141e-01 3.35940199e-01 ... 2.73106214e-01 7.17745481e-02 3.02877364e-01] [4.75614904e-01 8.18536526e-01 4.75044260e-01 ... 8.73568273e-01 8.72928115e-01 6.69726761e-02] ... [1.84699264e-01 9.25437043e-02 2.76537923e-01 ... 8.59049199e-01 4.32848867e-02 9.74536049e-01] [3.29136312e-01 5.68045333e-01 2.20497130e-01 ... 9.73344337e-02 8.27351813e-01 9.03261549e-01] [9.57208702e-03 9.98509893e-01 1.53001256e-01 ... 8.37899064e-01 4.32604288e-01 9.26695411e-01]] [[5.36515451e-01 8.55055349e-02 6.82627447e-02 ... 2.83029377e-02 1.13706343e-01 5.99048592e-01] [5.23502139e-01 3.89160587e-01 3.66744689e-01 ... 1.39818482e-01 3.67560624e-01 5.75490022e-01] [1.46143184e-01 2.56433264e-02 2.08498938e-01 ... 4.80108314e-01 4.71175949e-02 2.72202907e-01] ... [7.05383430e-01 3.53398033e-01 4.86669821e-01 ... 1.99899254e-03 6.45934106e-02 9.22805414e-01] [8.17317402e-01 8.91230591e-03 9.30824778e-01 ... 1.03679748e-01 9.61337881e-01 7.07830997e-01] [5.57809287e-01 1.82054633e-01 4.96130086e-01 ... 5.33018446e-01 6.15565939e-01 4.08628949e-01]]]]
x = np.array([[1,2,3,4], [5,6,7,8]])
y = np.array(((1,2,3,4), (5,6,7,8)))
print(x, type(x), x.shape)
print(y, type(y), y.shape)
[[1 2 3 4] [5 6 7 8]] <class 'numpy.ndarray'> (2, 4) [[1 2 3 4] [5 6 7 8]] <class 'numpy.ndarray'> (2, 4)
print(np.zeros((3,2), dtype=int))
[[0 0] [0 0] [0 0]]
zf = np.zeros((3,2))
zi = np.zeros((3,2),dtype=int)
print(type(zf), type(zi), zf.dtype, zi.dtype)
<class 'numpy.ndarray'> <class 'numpy.ndarray'> float64 int32
print(np.ones((5,5)))
print('')
[[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.]]
print(np.eye(6))
[[1. 0. 0. 0. 0. 0.] [0. 1. 0. 0. 0. 0.] [0. 0. 1. 0. 0. 0.] [0. 0. 0. 1. 0. 0.] [0. 0. 0. 0. 1. 0.] [0. 0. 0. 0. 0. 1.]]
x = list(range(0,9,2))
print(x)
[0, 2, 4, 6, 8]
A = np.arange(0,9,2)
print(A, A.ndim, A.shape, type(A), A.dtype)
[0 2 4 6 8] 1 (5,) <class 'numpy.ndarray'> int32
A = np.arange(9)
B = np.reshape(A, (3,3))
print(B)
[[0 1 2] [3 4 5] [6 7 8]]
Exercise: Use np.arrange() function to create an array of size 4x2 where the elements will start from 0 to 14 (with step size 2) in row major oder
A = np.arange(0,15,2)
B = np.reshape(A, (4,2))
print(B)
[[ 0 2] [ 4 6] [ 8 10] [12 14]]
# x = np.array([5,4,1,8,7,3,9,2,6])
x = np.random.randint(1,100,15)
print(x)
[34 19 2 79 93 36 48 61 99 60 15 38 27 64 78]
print(np.max(x))
99
print(np.argmax(x))
8
print('')
print(np.min(x))
print(np.argmin(x))
2 2
print('')
print(np.sort(x))
print(np.argsort(x))
[ 2 15 19 27 34 36 38 48 60 61 64 78 79 93 99] [ 2 10 1 12 0 5 11 6 9 7 13 14 3 4 8]
# Colon operator
C = np.random.rand(10)
print(C)
print(C[1:5])
print(C[5:])
print(C[:])
[0.27187084 0.0747467 0.17597604 0.26606344 0.76891328 0.55502234 0.94510928 0.28873734 0.22924237 0.18497965] [0.0747467 0.17597604 0.26606344 0.76891328] [0.55502234 0.94510928 0.28873734 0.22924237 0.18497965] [0.27187084 0.0747467 0.17597604 0.26606344 0.76891328 0.55502234 0.94510928 0.28873734 0.22924237 0.18497965]
# Colon operator
C = np.random.rand(3,5)
print(C)
print(C[1:3,1:4])
[[0.97123951 0.25819102 0.12180608 0.85181182 0.15302344] [0.72496527 0.98198125 0.82083567 0.57963518 0.95631042] [0.9972878 0.02472627 0.80750702 0.45007341 0.38939842]] [[0.98198125 0.82083567 0.57963518] [0.02472627 0.80750702 0.45007341]]
Exercise: Generate an 1D array A with 20 elements randomly initialized with the numbers in [0,15]. Create another 1D array B with 20 elements randomly initialized with the numbers in [0,1]. Sort A and apply the sorted indices of A on B.
A = np.random.randint(0,15,(3,4))
B = np.random.rand(3,4)
print(A,B)
ind = np.argsort(A)
print(np.sort(A), ind)
[[ 2 14 0 5] [13 5 0 8] [12 0 4 13]] [[0.57481448 0.43348965 0.28229281 0.79151234] [0.78533208 0.68472968 0.1906933 0.42718898] [0.46429199 0.39130593 0.12781012 0.02011126]] [[ 0 2 5 14] [ 0 5 8 13] [ 0 4 12 13]] [[2 0 3 1] [2 1 3 0] [1 2 0 3]]
print(B)
B2 = B
B2[0,:] = B2[0,ind[0,:]]
B2[1,:] = B2[1,ind[1,:]]
B2[2,:] = B2[2,ind[2,:]]
print(B2)
[[0.57481448 0.43348965 0.28229281 0.79151234] [0.78533208 0.68472968 0.1906933 0.42718898] [0.46429199 0.39130593 0.12781012 0.02011126]] [[0.28229281 0.57481448 0.79151234 0.43348965] [0.1906933 0.68472968 0.42718898 0.78533208] [0.39130593 0.12781012 0.46429199 0.02011126]]
temp = np.random.randint(1, 205, (5,5))
print(temp)
[[ 7 5 135 88 177] [ 86 6 189 127 87] [ 19 9 152 135 39] [ 13 161 172 136 52] [184 27 182 1 99]]
print(temp[0,0])
7
x = np.array([5,4,1,8,7,3,9,2,6])
print(np.mean(x))
print(np.var(x))
print(np.std(x))
5.0 6.666666666666667 2.581988897471611
x = np.random.rand(3,3)
print(x)
[[0.39383391 0.16565132 0.97948321] [0.84604666 0.85137788 0.70332652] [0.40836269 0.69414368 0.39238418]]
y = (x>0.5)
print(y,y.dtype)
[[False False True] [ True True True] [False True False]] bool
x = np.random.rand(3,3)
print(x)
[[0.99763724 0.69392924 0.3695234 ] [0.63712887 0.66475011 0.59136071] [0.52068796 0.46456325 0.25027268]]
mask = x > 0.5
print(mask)
[[ True True False] [ True True True] [ True False False]]
print(x[mask])
# print(x)
[0.99763724 0.69392924 0.63712887 0.66475011 0.59136071 0.52068796]
x[mask] = 0
print(x)
[[0. 0. 0.3695234 ] [0. 0. 0. ] [0. 0.46456325 0.25027268]]
x = np.random.rand(3,3)
mask = x > 0.5
x[mask] = 0
print(x)
[[0.14221903 0. 0. ] [0.27875248 0.42196899 0.28312714] [0.20207925 0.44765824 0.11389233]]
x = np.random.rand(3,3)
print(x)
[[0.09155755 0.61782912 0.50929165] [0.00844614 0.1317633 0.22713813] [0.53484759 0.44429539 0.36511703]]
r, c = np.where(x==0)
print(r, c)
[] []
x = np.array([1,2,3,4,5])
def f(x):
return x**2
for i in x:
print(i,f(i))
1 1 2 4 3 9 4 16 5 25
print(np.random.rand(2,2,3))
[[[0.09463859 0.05851054 0.75652589] [0.73365895 0.85643617 0.00149973]] [[0.57486344 0.82975722 0.38382674] [0.46909837 0.10671569 0.30895629]]]
x = np.array( [
[[2,1],
[3,5]],
[[0,1],
[2,3]],
[[1,2],
[9,5]]
])
print(x)
[[[2 1] [3 5]] [[0 1] [2 3]] [[1 2] [9 5]]]
r2 = np.random.randint(1,10,(4,5))
print(r2)
[[3 4 2 9 3] [3 5 8 8 9] [9 6 8 6 9] [9 9 3 5 7]]
print(np.empty((5,5)))
[[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.]]
y = np.empty_like(r2)
print(y)
[[ -121982180 1070687828 -1707134120 1068704409 46700424] [ 1069975138 -288207842 1070663470 84757885 1072208624] [ 940933035 1071760062 -752711451 1072578133 760747720] [ 1070758572 1454091324 1070421968 -1116444800 1070050665]]
y = np.zeros_like(r2)
print(y)
[[0 0 0 0 0] [0 0 0 0 0] [0 0 0 0 0] [0 0 0 0 0]]
y = np.ones_like(r2)
print(y)
[[1 1 1 1 1] [1 1 1 1 1] [1 1 1 1 1] [1 1 1 1 1]]
print(r2.T) # Transpose
print('\n')
print(np.transpose(r2))
[[3 3 9 9] [4 5 6 9] [2 8 8 3] [9 8 6 5] [3 9 9 7]] [[3 3 9 9] [4 5 6 9] [2 8 8 3] [9 8 6 5] [3 9 9 7]]
x = np.array([[1,2],[3,4]])
print(x)
[[1 2] [3 4]]
print(np.sum(x)) # Compute sum of all elements; prints "10"
10
x = np.random.randint(1,100,(6,8))
print(x)
print(np.sum(x))
print(x.sum())
[[35 98 17 9 12 26 26 88] [99 45 47 66 33 67 5 75] [43 14 43 66 53 68 55 38] [ 8 4 51 95 53 63 82 18] [34 58 14 56 50 17 42 41] [86 98 30 97 76 45 92 89]] 2427 2427
print(np.sum(x, axis=0)) # Compute sum of each column; prints "[4 6]"
[305 317 202 389 277 286 302 349]
print(np.sum(x, axis=1)) # Compute sum of each row; prints "[3 7]"
[311 437 380 374 312 613]
print(np.sqrt(x))
[[5.91607978 9.89949494 4.12310563 3. 3.46410162 5.09901951 5.09901951 9.38083152] [9.94987437 6.70820393 6.8556546 8.1240384 5.74456265 8.18535277 2.23606798 8.66025404] [6.55743852 3.74165739 6.55743852 8.1240384 7.28010989 8.24621125 7.41619849 6.164414 ] [2.82842712 2. 7.14142843 9.74679434 7.28010989 7.93725393 9.05538514 4.24264069] [5.83095189 7.61577311 3.74165739 7.48331477 7.07106781 4.12310563 6.4807407 6.40312424] [9.2736185 9.89949494 5.47722558 9.8488578 8.71779789 6.70820393 9.59166305 9.43398113]]
x = np.array([[1,2],[3,4]])
y = np.array([[5,6], [7,8]])
print(x)
print(y)
[[1 2] [3 4]] [[5 6] [7 8]]
print(np.add(x,y))
[[ 6 8] [10 12]]
print(x+y)
[[ 6 8] [10 12]]
a2d = np.array([[1,2,3],[4,5,6]])
b2d = np.array([[10,4,6],[2,4,6]])
print(a2d, a2d.shape)
print(b2d, b2d.shape)
[[1 2 3] [4 5 6]] (2, 3) [[10 4 6] [ 2 4 6]] (2, 3)
print(a2d.shape[1])
r, c = b2d.shape
print(r,c)
3 2 3
Exercise: Define a random matrix of shape 7x8. Fetch the number of rows and columns programatically. Then manipulate each element of the matrix with the following formula: x = sqrt((x+5)/10), x refers to the element of the matrix.
A = np.random.randint(1,50,(7,8))
print(A)
row, col = A.shape
print(row, col)
for i in range(row):
for j in range(col):
A[i,j] = np.sqrt((A[i,j]+5)/10)
print(A)
[[ 4 19 12 17 42 3 49 20] [41 15 20 33 36 17 47 49] [15 35 1 31 36 5 22 18] [33 47 2 38 17 9 26 28] [21 18 49 41 43 25 49 47] [ 8 40 38 24 32 33 16 43] [47 3 10 33 29 3 27 40]] 7 8 [[0 1 1 1 2 0 2 1] [2 1 1 1 2 1 2 2] [1 2 0 1 2 1 1 1] [1 2 0 2 1 1 1 1] [1 1 2 2 2 1 2 2] [1 2 2 1 1 1 1 2] [2 0 1 1 1 0 1 2]]
c2d = a2d.T # Transpose
print(c2d)
print(a2d)
[[1 4] [2 5] [3 6]] [[1 2 3] [4 5 6]]
# Element wise multiplication
d2d = a2d * b2d
print(d2d)
[[10 8 18] [ 8 20 36]]
d2d = np.multiply(a2d, b2d)
print(d2d)
[[10 8 18] [ 8 20 36]]
print(a2d.shape, b2d.shape, c2d.shape)
(2, 3) (2, 3) (3, 2)
# Matrix multiplication
e2d = np.dot(a2d,c2d)
print(e2d)
[[14 32] [32 77]]
e2d = np.matmul(a2d,c2d)
print(e2d)
[[14 32] [32 77]]
det1 = np.linalg.det(e2d)
print(det1)
54.00000000000001
eig, eig1 = np.linalg.eig(e2d)
print(eig)
print(eig1)
[ 0.59732747 90.40267253] [[-0.92236578 -0.3863177 ] [ 0.3863177 -0.92236578]]
inv1 = np.linalg.inv(e2d)
print(inv1)
[[ 1.42592593 -0.59259259] [-0.59259259 0.25925926]]
a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
print(a)
[[ 1 2 3 4] [ 5 6 7 8] [ 9 10 11 12]]
b = a[:2, 1:3] #Last exclusive
print(b)
[[2 3] [6 7]]
b[0, 1] = 77
print(b)
[[ 2 77] [ 6 7]]
print(a)
[[ 1 2 77 4] [ 5 6 7 8] [ 9 10 11 12]]
c = np.copy(a)
print(c)
[[ 1 2 77 4] [ 5 6 7 8] [ 9 10 11 12]]
c[0,0] = 105
print(c)
print(a)
[[105 2 77 4] [ 5 6 7 8] [ 9 10 11 12]] [[ 1 2 77 4] [ 5 6 7 8] [ 9 10 11 12]]
# We will add the vector v to each row of the matrix x,
# storing the result in the matrix y
x = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]])
print(x)
v = np.array([1, 0, 1])
print(v)
[[ 1 2 3] [ 4 5 6] [ 7 8 9] [10 11 12]] [1 0 1]
print(x+v)
[[ 2 2 4] [ 5 5 7] [ 8 8 10] [11 11 13]]
vv = np.tile(v, (4, 1)) # Stack 4 copies of v on top of each other
print(vv) # Prints "[[1 0 1]
# [1 0 1]
# [1 0 1]
# [1 0 1]]"
[[1 0 1] [1 0 1] [1 0 1] [1 0 1]]
y = x + vv # Add x and vv elementwise
print(y) # Prints "[[ 2 2 4
# [ 5 5 7]
# [ 8 8 10]
# [11 11 13]]"
[[ 2 2 4] [ 5 5 7] [ 8 8 10] [11 11 13]]
# We will add the vector v to each row of the matrix x,
# storing the result in the matrix y
x = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]])
v = np.array([1, 0, 1])
y = x + v # Add v to each row of x using broadcasting
print(y) # Prints "[[ 2 2 4]
# [ 5 5 7]
# [ 8 8 10]
# [11 11 13]]"
[[ 2 2 4] [ 5 5 7] [ 8 8 10] [11 11 13]]
Exercise: Create a random array A of size 5x4. Using np.tile() function, add a random vector of size 4 with first three rows of A and add another random vector of size 4 with last two rows of A.
A = np.random.randint(1,100,(5,4))
vec1 = np.random.randint(0,2,4)
vec2 = np.random.randint(0,2,4)
print(A)
print(vec1)
print(vec2)
[[ 4 34 75 36] [81 83 63 68] [14 15 21 22] [39 56 58 59] [90 60 16 72]] [1 1 1 0] [0 0 1 0]
A[:3,:] = A[:3,:]+vec1
A[3:,:] = A[3:,:]+vec2
print(A)
[[ 5 35 76 36] [82 84 64 68] [15 16 22 22] [39 56 59 59] [90 60 17 72]]
v_1 = np.tile(vec1,(3,1))
print(v_1)
v_2 = np.tile(vec2,(2,1))
print(v_2)
[[1 1 1 0] [1 1 1 0] [1 1 1 0]] [[0 0 1 0] [0 0 1 0]]
v = np.vstack((v_1, v_2))
print(v)
[[1 1 1 0] [1 1 1 0] [1 1 1 0] [0 0 1 0] [0 0 1 0]]
print(A+v)
[[ 6 36 77 36] [83 85 65 68] [16 17 23 22] [39 56 60 59] [90 60 18 72]]