import numpy as np
# Array creation
shape = (3,4)
dtype = np.int32
order = 'C'
# Creates array without initializing entries
array1 = np.empty(shape, dtype)
array1
array([[-2043364424, 32730, 1725429712, 21959], [ 0, 0, 0, 0], [ 0, 0, 0, 0]], dtype=int32)
# Returns array with same shape and type as given array
array2 = np.empty_like(array1)
array2
array([[-2043364488, 32730, -2043364488, 32730], [ 0, 0, 0, 0], [ 1760273608, 32730, 1760273672, 32730]], dtype=int32)
# Returns a 2-D matrix with ones on diagonal and zeros elsewhere
array3 = np.eye(N=4)
print(array3)
array4 = np.eye(N=4,M=2)
print(array4)
array5 = np.eye(N=4, k=1)
print(array5)
# k defines the amount by which you want to shift the main diagonal
[[ 1. 0. 0. 0.] [ 0. 1. 0. 0.] [ 0. 0. 1. 0.] [ 0. 0. 0. 1.]] [[ 1. 0.] [ 0. 1.] [ 0. 0.] [ 0. 0.]] [[ 0. 1. 0. 0.] [ 0. 0. 1. 0.] [ 0. 0. 0. 1.] [ 0. 0. 0. 0.]]
# To get a identity matrix
np.identity(4)
array([[ 1., 0., 0., 0.], [ 0., 1., 0., 0.], [ 0., 0., 1., 0.], [ 0., 0., 0., 1.]])
np.ones((4,3,2), dtype=np.float32)
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.]]], dtype=float32)
np.ones_like(array1)
array([[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]], dtype=int32)
np.zeros_like(array1)
array([[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], dtype=int32)
np.zeros(shape)
array([[ 0., 0., 0., 0.], [ 0., 0., 0., 0.], [ 0., 0., 0., 0.]])
# To create an array with your specified value in all the cells
np.full((4,3), fill_value=10.)
array([[ 10., 10., 10.], [ 10., 10., 10.], [ 10., 10., 10.], [ 10., 10., 10.]])
np.full_like(array1, fill_value=1+9j, dtype=complex)
array([[ 1.+9.j, 1.+9.j, 1.+9.j, 1.+9.j], [ 1.+9.j, 1.+9.j, 1.+9.j, 1.+9.j], [ 1.+9.j, 1.+9.j, 1.+9.j, 1.+9.j]])
# So far we discussed a new array creation and
# now we discuss how to create a new array using existing data
ref_array = 10*np.random.rand(3,4)
ref_array
array([[ 3.87194212, 6.0227274 , 9.40008761, 1.55617568], [ 1.62449474, 8.24526602, 8.9153371 , 0.13569454], [ 1.31895575, 5.89019258, 8.51156338, 2.12524318]])
array1 = np.array(ref_array, dtype=np.int, copy=True, order='C')
print(array1)
# Now why we copied
array1[0,1] = 10
print(array1)
print(ref_array)
# Now if we didn't copy
array2 = np.array(ref_array, copy=False)
array2[0,2] = 15
print('\n', array2)
print(ref_array)
# As you can notice that if we didn.t copy than the array from which
# we copied also got the changes
[[ 3 6 15 1] [ 1 8 8 0] [ 1 5 8 2]] [[ 3 10 15 1] [ 1 8 8 0] [ 1 5 8 2]] [[ 3.87194212 6.0227274 15. 1.55617568] [ 1.62449474 8.24526602 8.9153371 0.13569454] [ 1.31895575 5.89019258 8.51156338 2.12524318]] [[ 3.87194212 6.0227274 15. 1.55617568] [ 1.62449474 8.24526602 8.9153371 0.13569454] [ 1.31895575 5.89019258 8.51156338 2.12524318]] [[ 3.87194212 6.0227274 15. 1.55617568] [ 1.62449474 8.24526602 8.9153371 0.13569454] [ 1.31895575 5.89019258 8.51156338 2.12524318]]
# To create an array from objects that can be converted to arrays
# like lists, tuples, ndarrays
array2 = np.asarray(ref_array)
print(array2)
array3 = np.asarray([[4,5,6],[1,2,3]])
print(array3)
[[ 3.87194212 6.0227274 15. 1.55617568] [ 1.62449474 8.24526602 8.9153371 0.13569454] [ 1.31895575 5.89019258 8.51156338 2.12524318]] [[4 5 6] [1 2 3]]
# To get a contiguous array in memory
array4 = np.ascontiguousarray(ref_array)
array4
array([[ 3.87194212, 6.0227274 , 15. , 1.55617568], [ 1.62449474, 8.24526602, 8.9153371 , 0.13569454], [ 1.31895575, 5.89019258, 8.51156338, 2.12524318]])
# To simply copy an array
array5 = np.copy(ref_array)
array5
array([[ 3.87194212, 6.0227274 , 15. , 1.55617568], [ 1.62449474, 8.24526602, 8.9153371 , 0.13569454], [ 1.31895575, 5.89019258, 8.51156338, 2.12524318]])
# if you want to create an array from a buffer say a srtring
dt = np.dtype(int)
dt = dt.newbyteorder('>')
np.frombuffer('hello', dtype=dt)
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-73-b66b204e14a8> in <module>() 2 dt = np.dtype(int) 3 dt = dt.newbyteorder('>') ----> 4 np.frombuffer('hello', dtype=dt) AttributeError: 'str' object has no attribute '__buffer__'
# To create an array from a file
dt = np.dtype(int)
dt = dt.newbyteorder('>')
np.fromfile(file='vimTutorial.txt', dtype=dt)
array([5432882060864155506, 2336646335973647726, 8391061973054070892, 7306655128079005285, 7882828167451910189, 2338616291577788005, 2337208112243304814, 7286934626993310315, 2318545067618364783, 8531260732088852588, 7597120811504241260, 2318545058932418676, 2336646335973647726, 8361506904289124397, 4476706050239985769, 8367818302828869481, 8388076856227689313, 8532472353939220577, 7955438811540518769, 2318577692278988901, 8676593839527851109, 8223699782014168679, 2334669371110750240, 3260078864715506789, 7810777106429012069, 2333823799648150388, 7309940812453342836, 7306000102812836725, 8247057878230919456, 3260078864715507557, 8367804030775140457, 7958816508742757743, 7234224000819227936, 8388065495758692722, 7017580623428612207, 7597137600240968313, 2333838166331453962, 6998643565789474592, 7450488841110975599, 2335519323670606880, 7885631858047087215, 7863412941110931045, 8679597993174856289, 7166464447898218345, 7959097922184968480, 7166760222146562570, 7239290304709293167, 2334102023117759776, 7382085253699171698, 8317992520494181484, 2334393358181426720, 8606223186924938272, 3260078864715506789, 7810777106428097135, 7863394305471377266, 2338610020903580776, 7286935721886117734, 2336358228040115232, 3260078864715506789, 7810777106429012069, 2333831547736319348, 7286943397164943882, 8439795446548033312, 8461811178324387941, 2336349455195857251, 8388358299364696109, 4476706050241031780, 8007507255763170408, 7286933497047836517, 8295746435327008876, 7597120715852098878, 2338616291577788005, 2338616291694765344, 8319381555862597478, 2336358229560920665, 8031361121693606005, 8315087945798083173, 8246970979836780908, 7791355347073000303, 7885065649355169908, 8007525917467697524, 2338608891343959144, 7022273377766040165, 8223695404000373101, 7310197658759689330, 7794999917943857268, 8007513822902379381, 8246765392737889385, 7954799975608443493, 8223680046332667497, 7810684787197043978, 5125146120803348256, 7885651649257303840, 7093015981566271599, 7358994394099550823, 7430989130017042208, 7885651649257303840, 8319381555862597478, 2334669371110732857, 3478784615854929007, 2336646335968605295, 2336358229562388085, 7881973838692759856, 723442862983570783, 8606223187290439200, 8389960259432641647, 2334669397122359411, 8029185171371553380, 2333258732058845300, 7522454419870542196, 7597412181984288867, 8462953572274433568, 3260078864715509103, 8531260753575964261, 8679598062012556639, 8606223186923495469, 4476706050240898401, 8242546231384961568, 8388065496026672485, 6879089492928522606, 2338042715741254501, 2334106446932964457, 8029390821770210848, 7237970109630082415, 8289507829152048416, 8099849692699238445, 4476706050240898401, 7163010831661277299, 8029184900670714465, 8315063571842231341, 8007449882320465696, 7453211592774153320, 7286947833716304239, 8463143792037554548, 7597412181984288867, 8462953572274422644, 8245014809904234016, 8389960285208077600, 8389960315154276462, 7311721677710127988, 7597412181984288867, 8462953572274407973, 2318577692278988903, 8007528129186786657, 8386661733127102576, 8030878509237759520, 8027138987362640242, 7020676860844665203, 737028136615699830, 7597420233974769016, 8367737852509971824, 7809632516211504232, 7286936800163230752, 8026368316985929315, 7286946681507050085, 8532473519263479657, 8387989990317301870, 7311721677592600673, 2336358229560932979, 3418357897278091125, 8299974020358745959, 2318577692244799596, 7017564189512264812, 2337196010451920238, 7166107094903451762, 7311146972515344503, 7598812959781255284, 2339461068539062638, 2333181731952026890, 4189881675908146223, 7954895362297179454, 2338616291409551726, 7450396821335274866, 7308888531255911968, 8028902326299882600, 2336916818971160096, 8388065496094240620, 7286936800061622842, 2698552757331439470, 7311364641150479678, 2338616291459952228, 2334402159945785455, 7161696966637675365, 2337199021122937888, 8604536553637569911, 2333267458274059636, 2333181749282958704, 8367819397849686132, 8007510565540471596, 2554437569255583598, 7311364640027262496, 8389960306514750561, 7162166479645075488, 8028902326299882600, 2336916818971160096, 8388065496009895527, 7286951123660007782, 7594586436884045859, 2842626828842132335, 7885065649349733694, 2338616291695882341, 2333181693397921121, 7954518444758559086, 2333181749282958704, 8367795234408523128, 7305812163298353523, 2333181761999432812, 2333831547685269092, 723454973463382348, 4994000836928482622, 2338616291677533797, 2333181706182092064, 8389960315154559776, 5064663116504190277, 736938264369317449, 5495884962651775021, 4476706050240832877, 8031718175528215840, 5064663116504190277, 753825366319068271, 2335232299646085479, 7503123901613154416, 7021802451680764020, 7522454368329889133, 7308906927691293728, 8388065829642596965, 8319028370339143777, 7954518526182911520, 4212871355116319855, 2338319792666080360, 7022273386020762656, 8027139070174240870, 7596557766071820358, 5281672665404097824, 3260078864715510373, 8390884940243607668, 7522454436882969632, 8027139070174524192, 5064663116504190277, 2333273764695401760, 8101821151424638830, 2337199021206562080, 7166760222146562618, 8223609669250198846, 2338616291627917667, 7286952188807507829, 8390335247412061728, 8388065495759154541, 7881702199194839406, 7286940118797281381, 2339172973881485676, 7280643219995311648, 8389960298041271141, 2333181740475424876, 7597120811168068709, 8223682275661016946, 2333267458273930868, 7309940644394517842, 6061965423633828431, 2318577692278988912, 7809632514823233646, 7311348170056688928, 7090185817211347043, 8462953572275872110, 7214878127870469490, 2326476802065847328, 7885631857677901869, 4476706050240507766, 7286955517629702263, 7598528186186342497, 2336358229560942880, 3260078864715508078, 8315178130095763232, 8388065495943051380, 2337490748086446447, 7935465027847550322, 8317992425144462654, 2338616291510612837, 8247252502074913896, 7286935721886117734, 2338608890057091438, 7280643262944984608, 8389960242258016544, 8388065495842645864, 7811888726977897504, 8387242320834076717, 4476706050240700787, 8387121397853334846, 2338616291426398821, 7166472174543531888, 8367816099376428911, 8242748880955929704, 7286933553167953778, 723454957444145257, 7142758753865330464, 7595160721523286132, 7522454363801085216, 8604243002365342565, 7021784069120616202, 4211821625955871859, 2335524463803506804, 8007527017041913961, 7451324783446750240, 8315159430931423329, 7954518444797620000, 7597118964399695214, 8367814986918159208, 737028368605671023, 7593948775364719727, 2334106373868774688, 7595160721523286115, 7022067434117425516, 7286951076349240168, 7597122915048580468, 2336927750938632237, 4476706050239916385, 7092154703066785640, 7811888726973379685, 8679573420895659372, 8079507476358393632, 7450488841093606512, 739562798608897824, 4860635706782326829, 4476706050239850593, 7955438456873250080, 8604529940713927434, 4208725368189772142, 8745818331605200225, 7954518220531069807, 7882742334225214817, 7954609424754173216, 8587274200660792402, 5489118477147532915, 7310032883566928997, 8659331872345911853, 7881702273299802730])
# To create a character array
np.chararray
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-92-48335dcfc097> in <module>() 1 # To create a character array ----> 2 np.chararray(['a','a','b']) ~/anaconda3/lib/python3.6/site-packages/numpy/core/defchararray.py in __new__(subtype, shape, itemsize, unicode, buffer, offset, strides, order) 1833 if buffer is None: 1834 self = ndarray.__new__(subtype, shape, (dtype, itemsize), -> 1835 order=order) 1836 else: 1837 self = ndarray.__new__(subtype, shape, (dtype, itemsize), TypeError: 'str' object cannot be interpreted as an integer
# it creates an array from starting=1 to end=10 taking strides=3
np.arange(start=1, stop=10, step=3)
array([1, 4, 7])
# To divide the number into equal number of sample
np.linspace(start=1,stop=10, num=10, endpoint=True)
# endp[oint specifies whether to include the endpoint in the smaples
array([ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.])
# To get numbers on a logspace
# It starts at base**start and ends with base**stop
array1= np.logspace(start=1, stop=10, num=10, endpoint=True, base=10)
print (array1, '\n')
array2 = np.logspace(start=1, stop=10, num=10, base=2)
print(array2)
[ 1.00000000e+01 1.00000000e+02 1.00000000e+03 1.00000000e+04 1.00000000e+05 1.00000000e+06 1.00000000e+07 1.00000000e+08 1.00000000e+09 1.00000000e+10] [ 2. 4. 8. 16. 32. 64. 128. 256. 512. 1024.]
# Each output is a constant multiple of previous output
array3 = np.geomspace(start=1, stop=10, num=10)
print(array3, '\n')
for a in range(1,10):
print(array3[a]/array3[a-1])
array4 = np.geomspace(start=1, stop=1000, num=4)
print('\n', array4)
[ 1. 1.29154967 1.66810054 2.15443469 2.7825594 3.59381366 4.64158883 5.9948425 7.74263683 10. ] 1.29154966501 1.29154966501 1.29154966501 1.29154966501 1.29154966501 1.29154966501 1.29154966501 1.29154966501 1.29154966501 [ 1. 10. 100. 1000.]
# To extract a diagonal from a matrix
print(ref_array, '\n')
np.diag(ref_array, k=1)
[[ 3.87194212 6.0227274 15. 1.55617568] [ 1.62449474 8.24526602 8.9153371 0.13569454] [ 1.31895575 5.89019258 8.51156338 2.12524318]]
array([ 6.0227274 , 8.9153371 , 2.12524318])
array5 = np.diagflat(ref_array , k=0)
for a in array5:
print(a, '\n')
[ 3.87194212 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 6.0227274 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 15. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [ 0. 0. 0. 1.55617568 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 1.62449474 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 8.24526602 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 8.9153371 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0.13569454 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 1.31895575 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 5.89019258 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 8.51156338 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 2.12524318]
# To generate a vandemonde matrix
# The columns of output matrix are powers of input vector
x = np.vander(np.array([1,2,3,5]), N=3, increasing=True)
x
array([[ 1, 1, 1], [ 1, 2, 4], [ 1, 3, 9], [ 1, 5, 25]])
# Matrix
# Interpret the input as a matrix
x = np.array([[1,2], [3,4]])
m = np.mat(x)
print(x[0,0])
print(m[0,0])
# What this allows is allow you to use matrix operations on the numpy array
1 1
# Basic operations
ref_array = 10*np.random.rand(3,4)
ref_array
array([[ 3.46824036, 7.67681693, 8.2756161 , 7.32539822], [ 4.8000464 , 6.80180895, 5.63463621, 8.51450354], [ 0.82627203, 0.50071077, 1.99507292, 6.79505002]])
# Chaning array shape
ref_array = np.arange(10)
print(ref_array)
[0 1 2 3 4 5 6 7 8 9]
ref_array = ref_array.reshape(5,2)
ref_array
array([[0, 1], [2, 3], [4, 5], [6, 7], [8, 9]])
# To get a contiguous flattened array
np.ravel(ref_array)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
# Transposiing
ref_array = np.transpose(ref_array)
ref_array
array([[0, 2, 4, 6, 8], [1, 3, 5, 7, 9]])
# if you want to expand shape of an array
ref_array = np.expand_dims(ref_array, axis=2)
ref_array
array([[[0]], [[1]], [[2]], [[3]], [[4]], [[5]], [[6]], [[7]], [[8]], [[9]]])
# Joining arrays
a1 = np.random.randint(50, size=(4,3,2))
a2 = np.random.randint(50, size=(4,3,3))
print(a1, '\n', a2)
[[[14 37] [49 2] [37 23]] [[23 47] [47 46] [14 21]] [[27 10] [49 42] [45 23]] [[49 29] [22 20] [17 24]]] [[[ 7 46 21] [39 32 11] [23 25 7]] [[22 39 20] [25 22 7] [15 30 20]] [[43 11 6] [29 39 44] [30 25 0]] [[14 35 12] [ 3 18 5] [41 48 44]]]
np.concatenate((a1,a2), axis=2)
array([[[14, 37, 7, 46, 21], [49, 2, 39, 32, 11], [37, 23, 23, 25, 7]], [[23, 47, 22, 39, 20], [47, 46, 25, 22, 7], [14, 21, 15, 30, 20]], [[27, 10, 43, 11, 6], [49, 42, 29, 39, 44], [45, 23, 30, 25, 0]], [[49, 29, 14, 35, 12], [22, 20, 3, 18, 5], [17, 24, 41, 48, 44]]])
# Stacking two same dimensional arrays along a new axis
a1 = np.random.randint(50, size=(4,3))
a2 = np.random.randint(50, size=(4,3))
print(a1, '\n', a2, '\n')
np.stack((a1, a2), axis=0)
[[ 1 41 42] [20 12 18] [ 0 25 22] [41 43 22]] [[46 27 2] [48 10 27] [ 0 7 40] [35 11 20]]
array([[[ 1, 41, 42], [20, 12, 18], [ 0, 25, 22], [41, 43, 22]], [[46, 27, 2], [48, 10, 27], [ 0, 7, 40], [35, 11, 20]]])
# To stack arrays horizontally i.e. columsn wise)
np.hstack((a1, a2))
array([[ 1, 41, 42, 46, 27, 2], [20, 12, 18, 48, 10, 27], [ 0, 25, 22, 0, 7, 40], [41, 43, 22, 35, 11, 20]])
# To stack arrays vertically i.e. row wise
np.vstack((a1, a2))
array([[ 1, 41, 42], [20, 12, 18], [ 0, 25, 22], [41, 43, 22], [46, 27, 2], [48, 10, 27], [ 0, 7, 40], [35, 11, 20]])
# to create blocks of