Numpy Basics: Array Creation & Functions
Numpy Basics: Array Creation & Functions
Numpy Array
In [2]: import numpy as np
np.array([1, 2, 3])
Out[2]: array([1, 2, 3])
In [3]: #Upcasting:
In [9]: #dtype
In [12]: x = np.array([(1,2),(3,4)],dtype=[('a','<i4'),('b','<i4')])
In [13]: x['a']
http://localhost:8888/notebooks/Numpy%2BBasics.ipynb 1/50
2/28/2019 Numpy+Basics
numpy.asarray
In [17]: #Convert the input to an array.
In [21]: a = [1, 2]
In [22]: np.asarray(a)
Out[22]: array([1, 2])
In [24]: np.asarray(a) is a
Out[24]: True
In [25]: #If dtype is set, array is copied only if dtype does not match:
Out[27]: True
In [32]: np.asarray(a) is a
Out[32]: False
http://localhost:8888/notebooks/Numpy%2BBasics.ipynb 2/50
2/28/2019 Numpy+Basics
In [33]: np.asanyarray(a) is a
Out[33]: True
numpy.asanyarray
In [35]: a = [1, 2]
np.asanyarray(a)
Out[35]: array([1, 2])
In [37]: np.asanyarray(a) is a
Out[37]: True
#numpy.copy
In [42]: y = x
In [43]: z = np.copy(x)
In [45]: x[0] = 10
Out[47]: False
http://localhost:8888/notebooks/Numpy%2BBasics.ipynb 3/50
2/28/2019 Numpy+Basics
In [49]: x
In [50]: import os
In [55]: np.fromfile('C:/Datasets/College_Data')
In [57]: np.save('C:/Datasets/College_Data', x)
numpy.fromfunction
In [58]: #Construct an array by executing a function over each coordinate.
In [71]: np.loadtxt(c)
In [74]: c = StringIO("1,0,2\n3,0,4")
In [76]: x
In [77]: y
Out[77]: array([ 2., 4.])
#Parameters::
http://localhost:8888/notebooks/Numpy%2BBasics.ipynb 5/50
2/28/2019 Numpy+Basics
ndmin : int, optional:::The returned array will have at least ndmin dimensions.
Otherwise mono-dimensional axes will be squeezed. Legal values: 0 (default), 1
or 2. New in version 1.6.0.
In [79]: # How to create create a record array from a (flat) list of arrays
In [80]: x1=np.array([1,2,3,4])
In [81]: x2=np.array(['a','dd','xyz','12'])
In [82]: x3=np.array([1.1,2,3,4])
In [83]: r = np.core.records.fromarrays([x1,x2,x3],names='a,b,c')
In [84]: print(r[1])
x1[1]=34
In [85]: r.a
Out[85]: array([1, 2, 3, 4])
data types
http://localhost:8888/notebooks/Numpy%2BBasics.ipynb 6/50
2/28/2019 Numpy+Basics
http://localhost:8888/notebooks/Numpy%2BBasics.ipynb 7/50
2/28/2019 Numpy+Basics
Matrix creation
In [96]: print("Vector of zeroes\n---------------------")
print(np.zeros(5))
Vector of zeroes
---------------------
[ 0. 0. 0. 0. 0.]
Vector of ones
---------------------
[ 1. 1. 1. 1. 1.]
http://localhost:8888/notebooks/Numpy%2BBasics.ipynb 8/50
2/28/2019 Numpy+Basics
Matrix of ones
---------------------
[[ 1. 1.]
[ 1. 1.]
[ 1. 1.]
[ 1. 1.]
[ 1. 1.]]
Matrix of 5's
---------------------
[[ 5. 5. 5. 5. 5.]
[ 5. 5. 5. 5. 5.]
[ 5. 5. 5. 5. 5.]]
Empty matrix
-------------
[[ 5. 5. 5. 5. 5.]
[ 5. 5. 5. 5. 5.]
[ 5. 5. 5. 5. 5.]]
In [103]: np.arange(3)
Out[103]: array([0, 1, 2])
In [104]: np.arange(3.0)
In [105]: np.arange(3,7)
In [106]: np.arange(3,7,2)
Out[106]: array([3, 5])
http://localhost:8888/notebooks/Numpy%2BBasics.ipynb 9/50
2/28/2019 Numpy+Basics
In [116]: x = np.arange(9).reshape((3,3))
In [117]: x
In [118]: np.diag(x)
In [121]: np.diag(np.diag(x))
http://localhost:8888/notebooks/Numpy%2BBasics.ipynb 10/50
2/28/2019 Numpy+Basics
In [124]: np.diagflat([1,2], 1)
Out[124]: array([[0, 1, 0],
[0, 0, 2],
[0, 0, 0]])
In [125]: #An array with ones at and below the given diagonal and zeros elsewhere.
http://localhost:8888/notebooks/Numpy%2BBasics.ipynb 11/50
2/28/2019 Numpy+Basics
In [133]: print("Numbers from Normal distribution with zero mean and standard deviation 1 i
print(np.random.randn(4,3))
Numbers from Normal distribution with zero mean and standard deviation 1 i.e. s
tandard normal
[[-0.7234741 0.32885333 -0.60040597]
[ 0.54863879 1.76326167 -0.97475272]
[ 0.30910746 0.30583897 0.95082336]
[-1.52208987 0.47583592 1.18738672]]
Reshaping
In [136]: from numpy.random import randint as ri
a = ri(1,100,30)
b = a.reshape(2,3,5)
c = a.reshape(6,5)
Shape of a: (30,)
Shape of b: (2, 3, 5)
Shape of c: (6, 5)
http://localhost:8888/notebooks/Numpy%2BBasics.ipynb 12/50
2/28/2019 Numpy+Basics
a looks like
--------------------
[22 5 3 82 52 48 38 5 4 75 45 74 34 58 36 54 12 18 70 80 50 82 33 50 67
33 75 32 17 35]
--------------------
b looks like
--------------------
[[[22 5 3 82 52]
[48 38 5 4 75]
[45 74 34 58 36]]
[[54 12 18 70 80]
[50 82 33 50 67]
[33 75 32 17 35]]]
--------------------
c looks like
--------------------
[[22 5 3 82 52]
[48 38 5 4 75]
[45 74 34 58 36]
[54 12 18 70 80]
[50 82 33 50 67]
[33 75 32 17 35]]
--------------------
http://localhost:8888/notebooks/Numpy%2BBasics.ipynb 13/50
2/28/2019 Numpy+Basics
Max of a: 82
Max of b: 82
Max of a location: 3
Max of b location: 3
Max of c location: 3
Array: [ 0 1 2 3 4 5 6 7 8 9 10]
http://localhost:8888/notebooks/Numpy%2BBasics.ipynb 14/50
2/28/2019 Numpy+Basics
In [150]: print("Elements at 2nd, 4th, and 9th index are:", arr[[2,4,9]]) # Pass a list as
http://localhost:8888/notebooks/Numpy%2BBasics.ipynb 15/50
2/28/2019 Numpy+Basics
Subsetting sub-matrices
--------------------------
Matrix with row indices 1 and 2 and column indices 3 and 4
[[17 53]
[99 95]]
In [156]: print("Matrix with row indices 0 and 1 and column indices 1 and 3\n", mat[0:2,[1,
Subseting
In [157]: mat = np.array(ri(10,100,15)).reshape(3,5)
print("Matrix of random 2-digit numbers\n--------------------------------\n",mat
http://localhost:8888/notebooks/Numpy%2BBasics.ipynb 16/50
2/28/2019 Numpy+Basics
Slicing
In [159]: mat = np.array([[11,12,13],[21,22,23],[31,32,33]])
print("Original matrix")
print(mat)
Original matrix
[[11 12 13]
[21 22 23]
[31 32 33]]
Sliced matrix
[[11 12]
[21 22]]
[[1000 12]
[ 21 22]]
Original matrix
[[11 12 13]
[21 22 23]
[31 32 33]]
http://localhost:8888/notebooks/Numpy%2BBasics.ipynb 17/50
2/28/2019 Numpy+Basics
Sliced matrix
[[11 12]
[21 22]]
Universal Functions
In [169]: mat1 = np.array(ri(1,10,9)).reshape(3,3)
mat2 = np.array(ri(1,10,9)).reshape(3,3)
print("\n1st Matrix of random single-digit numbers\n----------------------------
print("\n2nd Matrix of random single-digit numbers\n----------------------------
http://localhost:8888/notebooks/Numpy%2BBasics.ipynb 18/50
2/28/2019 Numpy+Basics
Addition
------------------
[[13 14 11]
[ 3 11 16]
[ 9 13 8]]
Multiplication
------------------
[[42 45 18]
[ 2 18 63]
[ 8 42 15]]
Division
------------------
[[ 0.85714286 1.8 4.5 ]
[ 2. 0.22222222 0.77777778]
[ 8. 1.16666667 0.6 ]]
http://localhost:8888/notebooks/Numpy%2BBasics.ipynb 19/50
2/28/2019 Numpy+Basics
Broadcasting
In [174]: #NumPy operations are usually done on pairs of arrays on an element-by-element ba
#In the simplest case, the two arrays must have exactly the same shape.
#NumPy’s broadcasting rule relaxes this constraint when the arrays’ shapes meet c
#When operating on two arrays, NumPy compares their shapes element-wise. It start
#dimensions, and works its way forward. Two dimensions are compatible when
#they are equal, or one of them is 1
[[ 1. 0. 2.]
[ 1. 0. 2.]
[ 1. 0. 2.]
[ 1. 0. 2.]]
http://localhost:8888/notebooks/Numpy%2BBasics.ipynb 20/50
2/28/2019 Numpy+Basics
Array Math
In [181]: mat1 = np.array(ri(1,10,9)).reshape(3,3)
mat2 = np.array(ri(1,10,9)).reshape(3,3)
print("\n1st Matrix of random single-digit numbers\n----------------------------
print("\n2nd Matrix of random single-digit numbers\n----------------------------
http://localhost:8888/notebooks/Numpy%2BBasics.ipynb 21/50
2/28/2019 Numpy+Basics
In [187]: A = np.linspace(0,12*np.pi,1001)
In [188]: A
http://localhost:8888/notebooks/Numpy%2BBasics.ipynb 22/50
2/28/2019 Numpy+Basics
In [5]: b = a.T
In [6]: c = b.view()
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-7-7f6a843e0253> in <module>()
----> 1 c.shape = (20)
In [8]: #It is not always possible to change the shape of an array without copying the da
#If you want an error to be raised when the data is copied, you should assign the
#shape attribute of the array
In [10]: #The order keyword gives the index ordering both for fetching the values from a,
#into the output array
http://localhost:8888/notebooks/Numpy%2BBasics.ipynb 23/50
2/28/2019 Numpy+Basics
In [12]: a
In [18]: #examples
a = np.array([[1,2,3], [4,5,6]])
a
Out[18]: array([[1, 2, 3],
[4, 5, 6]])
In [19]: np.reshape(a, 6)
http://localhost:8888/notebooks/Numpy%2BBasics.ipynb 24/50
2/28/2019 Numpy+Basics
In [23]: print(np.ravel(x))
[1 2 3 4 5 6]
In [24]: print(x.reshape(-1))
[1 2 3 4 5 6]
[1 4 2 5 3 6]
In [26]: #When order is ‘A’, it will preserve the array’s ‘C’ or ‘F’ ordering
In [27]: print(np.ravel(x.T))
[1 4 2 5 3 6]
[1 2 3 4 5 6]
In [29]: #When order is ‘K’, it will preserve orderings that are neither ‘C’ nor ‘F’, but
In [30]: a = np.arange(3)[::-1]; a
In [31]: a.ravel(order='C')
In [32]: a.ravel(order='K')
In [33]: a = np.arange(12).reshape(2,3,2).swapaxes(1,2); a
[[ 6, 8, 10],
[ 7, 9, 11]]])
In [34]: a.ravel(order='C')
In [35]: a.ravel(order='K')
http://localhost:8888/notebooks/Numpy%2BBasics.ipynb 25/50
2/28/2019 Numpy+Basics
Out[38]: (4, 5, 3)
Out[39]: (5, 3, 4)
In [40]: np.transpose(x).shape
Out[40]: (5, 4, 3)
Out[41]: (5, 4, 3)
Out[42]: (5, 4, 3)
Out[43]: (5, 4, 3)
In [44]: x = np.arange(4).reshape((2,2))
In [45]: x
In [46]: np.transpose(x)
In [47]: x
Out[49]: (2, 1, 3)
http://localhost:8888/notebooks/Numpy%2BBasics.ipynb 26/50
2/28/2019 Numpy+Basics
In [51]: x = np.arange(9.0).reshape(3,3)
In [52]: np.atleast_1d(x)
In [53]: np.atleast_1d(x) is x
Out[53]: True
In [56]: np.atleast_2d(3.0)
In [57]: x = np.arange(3.0)
In [58]: np.atleast_2d(x)
In [59]: np.atleast_2d(x).base is x
Out[59]: True
In [62]: x = np.arange(12.0).reshape(4,3)
In [63]: x
In [64]: np.atleast_3d(x).shape
Out[64]: (4, 3, 1)
http://localhost:8888/notebooks/Numpy%2BBasics.ipynb 27/50
2/28/2019 Numpy+Basics
Out[65]: True
[[[1]
[2]]] (1, 2, 1)
[[[1]
[2]]] (1, 2, 1)
[[[1 2]]] (1, 1, 2)
In [107]: x
In [104]: x
In [110]: x = np.array([[1,2,3]])
http://localhost:8888/notebooks/Numpy%2BBasics.ipynb 28/50
2/28/2019 Numpy+Basics
In [111]: x
In [112]: y = np.array([[1],[2],[3]])
In [113]: y
Out[113]: array([[1],
[2],
[3]])
In [114]: np.broadcast_arrays(x, y)
In [117]: x = np.array([1,2])
In [118]: x
In [119]: x.shape
Out[119]: (2,)
In [121]: y
In [122]: y.shape
Out[122]: (1, 2)
http://localhost:8888/notebooks/Numpy%2BBasics.ipynb 29/50
2/28/2019 Numpy+Basics
In [124]: y
Out[124]: array([[1],
[2]])
In [125]: y.shape
Out[125]: (2, 1)
Out[126]: True
In [129]: x
Out[129]: array([[[0],
[1],
[2]]])
In [130]: x.shape
Out[130]: (1, 3, 1)
In [131]: np.squeeze(x).shape
Out[131]: (3,)
Out[132]: (3, 1)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-133-03d2df5273b7> in <module>()
----> 1 np.squeeze(x, axis=1).shape
ValueError: cannot select an axis to squeeze out which has size not equal to on
e
http://localhost:8888/notebooks/Numpy%2BBasics.ipynb 30/50
2/28/2019 Numpy+Basics
Out[134]: (1, 3)
In [141]: a
In [143]: b
In [146]: a = np.ma.arange(3)
In [147]: a
Out[147]: masked_array(data = [0 1 2],
mask = False,
fill_value = 999999)
http://localhost:8888/notebooks/Numpy%2BBasics.ipynb 31/50
2/28/2019 Numpy+Basics
In [149]: a
In [150]: b = np.arange(2, 5)
In [151]: b
http://localhost:8888/notebooks/Numpy%2BBasics.ipynb 32/50
2/28/2019 Numpy+Basics
Out[157]: (10, 3, 4)
In [161]: a
http://localhost:8888/notebooks/Numpy%2BBasics.ipynb 33/50
2/28/2019 Numpy+Basics
In [163]: b
In [173]: x = np.arange(12.0)
In [174]: x
Out[174]: array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.,
11.])
http://localhost:8888/notebooks/Numpy%2BBasics.ipynb 34/50
2/28/2019 Numpy+Basics
In [177]: np.split(x,[5])
In [178]: np.array_split(x,[5])
In [179]: x = np.arange(16.0).reshape(2, 2, 4)
In [180]: x
In [182]: x = np.arange(16.0).reshape(2, 2, 4)
In [183]: x
In [184]: np.dsplit(x, 2)
[[ 8., 9.],
[ 12., 13.]]]), array([[[ 2., 3.],
[ 6., 7.]],
[[ 10., 11.],
[ 14., 15.]]])]
http://localhost:8888/notebooks/Numpy%2BBasics.ipynb 35/50
2/28/2019 Numpy+Basics
[[ 11.],
[ 15.]]]), array([], shape=(2, 2, 0), dtype=float64)]
In [186]: np.hsplit(x, 2)
Out[186]: [array([[[ 0., 1., 2., 3.]],
In [188]: np.hsplit(x, 2)
In [189]: np.vsplit(x, 2)
http://localhost:8888/notebooks/Numpy%2BBasics.ipynb 36/50
2/28/2019 Numpy+Basics
In [191]: np.tile(x, 2)
Out[191]: array([[[ 0., 1., 2., 3., 0., 1., 2., 3.],
[ 4., 5., 6., 7., 4., 5., 6., 7.]],
In [192]: np.tile(x,(4,1))
In [194]: np.repeat(x, 2)
Out[194]: array([ 0., 0., 1., 1., 2., 2., 3., 3., 4., 4., 5.,
5., 6., 6., 7., 7., 8., 8., 9., 9., 10., 10.,
11., 11., 12., 12., 13., 13., 14., 14., 15., 15.])
http://localhost:8888/notebooks/Numpy%2BBasics.ipynb 37/50
2/28/2019 Numpy+Basics
In [198]: np.delete(x, 1, 0)
Out[200]: array([ 0., 2., 4., 6., 7., 8., 9., 10., 11., 12., 13.,
14., 15.])
In [202]: np.insert(x, 1, 5)
Out[202]: array([ 0., 5., 1., 2., 3., 4., 5., 6., 7., 8., 9.,
10., 11., 12., 13., 14., 15.])
http://localhost:8888/notebooks/Numpy%2BBasics.ipynb 38/50
2/28/2019 Numpy+Basics
In [209]: y = x.flatten()
Out[210]: array([ 0., 1., 5., 6., 2., 3., 4., 5., 6., 7., 8.,
9., 10., 11., 12., 13., 14., 15.])
Out[211]: array([ 0., 1., 5., 2., 6., 3., 4., 5., 6., 7., 8.,
9., 10., 11., 12., 13., 14., 15.])
In [221]: np.resize(x,(2,4))
http://localhost:8888/notebooks/Numpy%2BBasics.ipynb 39/50
2/28/2019 Numpy+Basics
In [225]: a
In [227]: np.unique(a)
In [229]: a
In [238]: a
In [241]: u
In [242]: indices
In [243]: a[indices]
http://localhost:8888/notebooks/Numpy%2BBasics.ipynb 40/50
2/28/2019 Numpy+Basics
In [245]: a
In [247]: u
In [248]: indices
In [249]: u[indices]
basic stats
In [252]: from numpy.random import randint as ri
mat1 = np.array(ri(1,10,9)).reshape(3,3)
mat2 = np.array(ri(1,10,9)).reshape(3,3)
print("\n1st Matrix of random single-digit numbers\n","-"*50,"\n",mat1)
print("\n2nd Matrix of random single-digit numbers\n","-"*50,"\n",mat2)
http://localhost:8888/notebooks/Numpy%2BBasics.ipynb 41/50
2/28/2019 Numpy+Basics
In [258]: print("\nStandard deviation of all numbers in the modified matrix, a larger numbe
http://localhost:8888/notebooks/Numpy%2BBasics.ipynb 42/50
2/28/2019 Numpy+Basics
http://localhost:8888/notebooks/Numpy%2BBasics.ipynb 43/50
2/28/2019 Numpy+Basics
Correlation
In [269]: A = ri(1,5,20) # 20 random integeres from a small range (1-10)
B = 2*A+5*np.random.randn(20) # B is twice that of A plus some random noise
print("\nB is twice that of A plus some random noise")
http://localhost:8888/notebooks/Numpy%2BBasics.ipynb 44/50
2/28/2019 Numpy+Basics
[[ 1. 0.68171563]
[ 0.68171563 1. ]]
http://localhost:8888/notebooks/Numpy%2BBasics.ipynb 45/50
2/28/2019 Numpy+Basics
In [277]: print("")
print(np.corrcoef(A,B)) # Correleation coefficient matrix between A and B
[[ 1. -0.98606912]
[-0.98606912 1. ]]
http://localhost:8888/notebooks/Numpy%2BBasics.ipynb 46/50
2/28/2019 Numpy+Basics
In [281]: A = np.arange(1,6)
B = ri(1,10,5)
http://localhost:8888/notebooks/Numpy%2BBasics.ipynb 47/50
2/28/2019 Numpy+Basics
In [287]: A = ri(1,10,9).reshape(3,3)
In [288]: A
Matrix transpose
--------------------------------------------------
[[4 7 8]
[5 2 5]
[6 3 3]]
In [291]: B = ri(1,10,6).reshape(3,2)
In [292]: B
Out[292]: array([[4, 5],
[4, 4],
[4, 2]])
http://localhost:8888/notebooks/Numpy%2BBasics.ipynb 48/50
2/28/2019 Numpy+Basics
In [297]: A = ri(1,10,16).reshape(4,4)
In [298]: A
Matrix trace
--------------------------------------------------
18
http://localhost:8888/notebooks/Numpy%2BBasics.ipynb 49/50
2/28/2019 Numpy+Basics
In [ ]:
http://localhost:8888/notebooks/Numpy%2BBasics.ipynb 50/50