Jupyter Notebook Viewer
Jupyter Notebook Viewer
N- Dimensional array
Arrays allows you to perform mathematical operations on whole blocks of data.
In [1]:
In [22]:
# Nested lists with equal length, will be converted into a multidimensional array
scores_1 = [[34,56,23,89], [11,45,76,34]]
second_arr = np.array(scores_1)
print second_arr
print second_arr.ndim #.ndim gives you the dimensions of an array.
print second_arr.shape #(number of rows, number of columns)
print second_arr.dtype
[[34 56 23 89]
[11 45 76 34]]
2
(2L, 4L)
int32
In [28]:
Out[28]:
array([ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
http://nbviewer.jupyter.org/gist/manujeevanprakash/7e47301f0b50a98232ca 1/19
9/8/2018 Jupyter Notebook Viewer
In [30]:
Out[30]:
In [34]:
np.arange(15)
Out[34]:
In [36]:
np.eye(6) # Create a square N x N identity matrix (1’s on the diagonal and 0’s elsewhe
Out[36]:
In [10]:
#Batch operations on data can be performed without using for loops, this is called vec
scores = [89,56.34, 76,89, 98]
first_arr =np.array(scores)
print first_arr
print first_arr * first_arr
print first_arr - first_arr
print 1/(first_arr)
print first_arr ** 0.5
http://nbviewer.jupyter.org/gist/manujeevanprakash/7e47301f0b50a98232ca 2/19
9/8/2018 Jupyter Notebook Viewer
In [26]:
# you may want to select a subset of your data, for which Numpy array indexing is real
new_arr = np.arange(12)
print new_arr
print new_arr[5]
print new_arr[4:9]
new_arr[4:9] = 99 #assign sequence of values from 4 to 9 as 99
print new_arr
[ 0 1 2 3 4 5 6 7 8 9 10 11]
5
[4 5 6 7 8]
[ 0 1 2 3 99 99 99 99 99 9 10 11]
In [27]:
# A major diffence between lists and array is that, array slices are views on the orig
# the data is not copied, and any modifications to the view will be reflected in the s
# array.
modi_arr = new_arr[4:9]
modi_arr[1] = 123456
print new_arr # you can see the changes are refelected in main array.
modi_arr[:] # the sliced variable
[ 0 1 2 3 99 123456 99 99 99 9
10 11]
Out[27]:
array([ 99, 123456, 99, 99, 99])
http://nbviewer.jupyter.org/gist/manujeevanprakash/7e47301f0b50a98232ca 3/19
9/8/2018 Jupyter Notebook Viewer
In [9]:
[[3 4 5]
[6 7 8]
[9 5 1]]
[6 7 8]
5
5
Out[9]:
In [8]:
C:\Users\tk\Desktop\pics
In [37]:
[[[ 1 2 3]
[ 4 5 6]]
[[ 7 8 9]
[10 11 12]]]
returns the second list inside first list [4 5 6]
http://nbviewer.jupyter.org/gist/manujeevanprakash/7e47301f0b50a98232ca 4/19
9/8/2018 Jupyter Notebook Viewer
In [39]:
three_d_arr = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
print three_d_arr[0]
#if you omit later indices, the returned object will be a lowerdimensional
# ndarray consisting of all the data along the higher dimensions
[[1 2 3]
[4 5 6]]
In [62]:
[[ 7 8 9]
[10 11 12]]]
three_d_arr again: [[[99 99 99]
[99 99 99]]
[[ 7 8 9]
[10 11 12]]]
In [76]:
matrix_arr =np.array([[3,4,5],[6,7,8],[9,5,1]])
print "The original matrix {}:".format(matrix_arr)
print "slices the first two rows:{}".format(matrix_arr[:2]) # similar to list slicing.
print "Slices the first two rows and two columns:{}".format(matrix_arr[:2, 1:])
print "returns 6 and 7: {}".format(matrix_arr[1,:2])
print "Returns first column: {}".format(matrix_arr[:,:1]) #Note that a colon by itself
http://nbviewer.jupyter.org/gist/manujeevanprakash/7e47301f0b50a98232ca 5/19
9/8/2018 Jupyter Notebook Viewer
In [80]:
Out[80]:
In [4]:
In [5]:
In [10]:
cd C:\Users\Manu\Desktop
C:\Users\Manu\Desktop
http://nbviewer.jupyter.org/gist/manujeevanprakash/7e47301f0b50a98232ca 6/19
9/8/2018 Jupyter Notebook Viewer
In [11]:
Out[11]:
In [12]:
random_no[personals == 'Manu', 2:] #First two columns and first two rows.
Out[12]:
array([[-0.15747451, -0.1196816 ],
[-0.48825407, 1.32425359]])
In [13]:
# To select everything except 'Manu', you can != or negate the condition using -:
print personals != 'Manu'
random_no[-(personals == 'Manu')] #get everything except 1st and 4th rows
Out[13]:
array([[-0.35946571, -1.23477985, 1.08186057, -0.61596683],
[ 1.67096505, 1.11183755, -0.39640455, 0.22848279],
[-0.04493194, -1.10371501, -0.52742166, -1.06265549],
[ 1.16938298, -0.60478133, 1.40615125, -1.35350336],
[ 0.86325448, 1.97577081, 0.05339779, 0.71515521]])
In [18]:
Out[18]:
array([[-0.129557 , 0.3684001 , -0.15747451, -0.1196816 ],
[-0.35946571, -1.23477985, 1.08186057, -0.61596683],
[-0.27989438, -1.51275966, -0.48825407, 1.32425359],
[ 1.16938298, -0.60478133, 1.40615125, -1.35350336]])
http://nbviewer.jupyter.org/gist/manujeevanprakash/7e47301f0b50a98232ca 7/19
9/8/2018 Jupyter Notebook Viewer
In [22]:
random_no[random_no < 0] =0
random_no # This will set all negative values to zero
Out[22]:
array([[ 0. , 0.3684001 , 0. , 0. ],
[ 0. , 0. , 1.08186057, 0. ],
[ 1.67096505, 1.11183755, 0. , 0.22848279],
[ 0. , 0. , 0. , 1.32425359],
[ 0. , 0. , 0. , 0. ],
[ 1.16938298, 0. , 1.40615125, 0. ],
[ 0.86325448, 1.97577081, 0.05339779, 0.71515521]])
In [24]:
random_no[ personals != 'Manu'] = 9 # This will set all rows except 1 and 4 to 9.
random_no
Out[24]:
array([[ 0. , 0.3684001 , 0. , 0. ],
[ 9. , 9. , 9. , 9. ],
[ 9. , 9. , 9. , 9. ],
[ 0. , 0. , 0. , 1.32425359],
[ 9. , 9. , 9. , 9. ],
[ 9. , 9. , 9. , 9. ],
[ 9. , 9. , 9. , 9. ]])
In [19]:
Out[19]:
http://nbviewer.jupyter.org/gist/manujeevanprakash/7e47301f0b50a98232ca 8/19
9/8/2018 Jupyter Notebook Viewer
In [23]:
# To select a subset of rows in particular order, you can simply pass a list.
algebra[[4,5,1]] #returns a subset of rows
Out[23]:
array([[ 4., 4., 4., 4.],
[ 5., 5., 5., 5.],
[ 1., 1., 1., 1.]])
In [33]:
[[ 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]]
Out[33]:
array([ 7, 18, 13, 8])
In [39]:
fancy[[1, 4, 8, 2]][:, [0, 3, 1, 2]] # entire first row is selected, but the elements
Out[39]:
array([[ 4, 7, 5, 6],
[16, 19, 17, 18],
[32, 35, 33, 34],
[ 8, 11, 9, 10]])
In [42]:
Out[42]:
array([[ 4, 7, 5, 6],
[16, 19, 17, 18],
[32, 35, 33, 34],
[ 8, 11, 9, 10]])
Transposing Arrays
http://nbviewer.jupyter.org/gist/manujeevanprakash/7e47301f0b50a98232ca 9/19
9/8/2018 Jupyter Notebook Viewer
In [47]:
transpose= np.arange(12).reshape(3,4)
transpose.T # the shape has changed to 4,3
Out[47]:
array([[ 0, 4, 8],
[ 1, 5, 9],
[ 2, 6, 10],
[ 3, 7, 11]])
In [48]:
#you can use np.dot function to perform matrix computations. You can calculate X trans
np.dot(transpose.T, transpose)
Out[48]:
array([[ 80, 92, 104, 116],
[ 92, 107, 122, 137],
[104, 122, 140, 158],
[116, 137, 158, 179]])
universal functions
They perform element wise operations on data in arrays.
In [53]:
funky =np.arange(8)
print np.sqrt(funky)
print np.exp(funky) #exponent of the array
# these are called as unary functions
http://nbviewer.jupyter.org/gist/manujeevanprakash/7e47301f0b50a98232ca 10/19
9/8/2018 Jupyter Notebook Viewer
In [62]:
In [67]:
Out[67]:
http://nbviewer.jupyter.org/gist/manujeevanprakash/7e47301f0b50a98232ca 11/19
9/8/2018 Jupyter Notebook Viewer
In [69]:
Out[69]:
mtrices =np.arange(-5,5,1)
x, y = np.meshgrid(mtrices, mtrices) #mesh grid function takes two 1 d arrays and prod
print "Matrix values of y: {}".format(y)
print "Matrix values of x: {}".format(x)
http://nbviewer.jupyter.org/gist/manujeevanprakash/7e47301f0b50a98232ca 12/19
9/8/2018 Jupyter Notebook Viewer
In [124]:
x1= np.array([1,2,3,4,5])
y1 = np.array([6,7,8,9,10])
cond =[True, False, True, True, False]
#If you want to take a value from x1 whenever the corresponding value in cond is true,
z1 = [(x,y,z) for x,y,z in zip(x1, y1, cond)] # I have used zip function To illustrate
print z1
np.where(cond, x1, y1)
[(1, 6, True), (2, 7, False), (3, 8, True), (4, 9, True), (5, 10, False)]
Out[124]:
array([ 1, 7, 3, 4, 10])
In [132]:
ra = np.random.randn(5,5)
# If you want to replace negative values in ra with -1 and positive values with 1. You
print ra
print np.where(ra>0, 1, -1) # If values in ra are greater than zero, replace it with 1
# to set only positive values
np.where(ra >0, 1, ra) # same implies to negative values
Statistical methods
http://nbviewer.jupyter.org/gist/manujeevanprakash/7e47301f0b50a98232ca 13/19
9/8/2018 Jupyter Notebook Viewer
In [136]:
thie = np.random.randn(5,5)
print thie.mean() # calculates the mean of thie
print np.mean(thie) # alternate method to calculate mean
print thie.sum()
0.286291297223
0.286291297223
7.15728243058
In [36]:
jp =np.arange(12).reshape(4,3)
print"The arrays are: {}".format(jp)
print "The sum of rows are :{}".format(np.sum(jp, axis =0)) #axis =0, gives you sum of
# remember this zero is for columns and one is for rows.
In [35]:
[ 3 12 21 30]
In [37]:
Out[37]:
array([[ 0, 1, 2],
[ 3, 5, 7],
[ 9, 12, 15],
[18, 22, 26]])
In [38]:
Out[38]:
array([[ 0, 1, 3],
[ 3, 7, 12],
[ 6, 13, 21],
[ 9, 19, 30]])
http://nbviewer.jupyter.org/gist/manujeevanprakash/7e47301f0b50a98232ca 14/19
9/8/2018 Jupyter Notebook Viewer
In [47]:
xp =np.random.randn(100)
print(xp > 0).sum() # sum of all positive values
print (xp < 0).sum()
tandf =np.array([True,False,True,False,True,False])
print tandf.any()#checks if any of the values are true
print tandf.all() #returns false even if a single value is false
#These methods also work with non-boolean arrays, where non-zero elements evaluate to
45
55
True
False
Sorting
In [55]:
lp = np.random.randn(8)
print lp
lp.sort()
lp
In [57]:
tp = np.random.randn(4,4)
tp
Out[57]:
http://nbviewer.jupyter.org/gist/manujeevanprakash/7e47301f0b50a98232ca 15/19
9/8/2018 Jupyter Notebook Viewer
In [60]:
Out[60]:
array([[-0.65497365, -0.43687651, 0.4968525 , 0.51706412],
[-1.39148137, -0.82572908, -0.0166924 , 2.20839298],
[-1.92611011, -0.8311936 , -0.5400157 , 0.04556166],
[-1.7181857 , -1.1659837 , 0.15529182, 0.41679611]])
In [61]:
Out[61]:
array(['Jeevan', 'Manu', 'Prakash'],
dtype='|S7')
In [65]:
Out[65]:
In [67]:
np.in1d(personals, ['Manu']) #in1d function checks for the value 'Manu' and returns Tr
Out[67]:
array([ True, False, False, True, False, False, False], dtype=bool)
Linear Algebra
http://nbviewer.jupyter.org/gist/manujeevanprakash/7e47301f0b50a98232ca 16/19
9/8/2018 Jupyter Notebook Viewer
In [75]:
cp = np.array([[1,2,3],[4,5,6]])
dp = np.array([[7,8],[9,10],[11,12]])
print "CP array :{}".format(cp)
print "DP array :{}".format(dp)
CP array :[[1 2 3]
[4 5 6]]
DP array :[[ 7 8]
[ 9 10]
[11 12]]
In [73]:
Out[73]:
In [77]:
np.dot(cp, np.ones(3))
Out[77]:
array([ 6., 15.])
In [84]:
[[17 22 27]
[22 29 36]
[27 36 45]]
In [90]:
sp = np.random.randn(5,5)
print inv(sp)
rt = inv(sp)
http://nbviewer.jupyter.org/gist/manujeevanprakash/7e47301f0b50a98232ca 17/19
9/8/2018 Jupyter Notebook Viewer
In [91]:
Out[91]:
array([[ 1.00000000e+00, -6.66133815e-16, -3.88578059e-16,
-4.44089210e-16, -5.77315973e-15],
[ -8.88178420e-16, 1.00000000e+00, 1.11022302e-16,
4.44089210e-16, 8.88178420e-16],
[ -2.66453526e-15, 2.22044605e-16, 1.00000000e+00,
-3.55271368e-15, 2.22044605e-15],
[ 8.88178420e-16, 0.00000000e+00, -1.11022302e-16,
1.00000000e+00, -8.88178420e-16],
[ 0.00000000e+00, -6.66133815e-16, 1.66533454e-16,
8.88178420e-16, 1.00000000e+00]])
In [95]:
q,r = qr(sp)
print q
r
diag : Return the diagonal (or off-diagonal) elements of a square matrix as a 1D array, or convert a 1D
array into a square
matrix with zeros on the off-diagonal
http://nbviewer.jupyter.org/gist/manujeevanprakash/7e47301f0b50a98232ca 18/19
9/8/2018 Jupyter Notebook Viewer
http://nbviewer.jupyter.org/gist/manujeevanprakash/7e47301f0b50a98232ca 19/19