10/9/23, 9:42 PM 01-NumPy Arrays - Jupyter Notebook
Using NumPy
Once you've installed NumPy you can import it as a library:
In [ ]: #!pip install numpy pandas matplotlib
In [1]: import numpy as np
Numpy has many built-in functions and capabilities. We won't cover them all but instead we
will focus on some of the most important aspects of Numpy: vectors,arrays,matrices, and
number generation. Let's start by discussing arrays.
Numpy Arrays
NumPy arrays are the main way we will use Numpy throughout the course. Numpy arrays
essentially come in two flavors: vectors and matrices. Vectors are strictly 1-d arrays and
matrices are 2-d (but you should note a matrix can still have only one row or one column).
Let's begin our introduction by exploring how to create NumPy arrays.
Creating NumPy Arrays
From a Python List
We can create an array by directly converting a list or list of lists:
In [2]: my_list = [1,2,3]
print("data :",my_list,
"\t type :",
type(my_list))
data : [1, 2, 3] type : <class 'list'>
In [3]: my_list*3
Out[3]: [1, 2, 3, 1, 2, 3, 1, 2, 3]
In [4]: my=np.array(my_list)
print("data :",my,"\t type :",type(my))
data : [1 2 3] type : <class 'numpy.ndarray'>
In [5]: my+5
Out[5]: array([6, 7, 8])
In [6]: my*5
Out[6]: array([ 5, 10, 15])
localhost:8888/notebooks/Downloads/01-NumPy Arrays.ipynb 1/10
10/9/23, 9:42 PM 01-NumPy Arrays - Jupyter Notebook
In [7]: my.shape
Out[7]: (3,)
In [15]: n=np.array([12,4,657,87,453,76,43,32])
n.shape
Out[15]: (8,)
In [ ]: (1,2,3,43,5)
In [ ]: [ list ]
{ dict }
In [ ]:
In [16]: x=90
y="90"
print(x)
print(y)
90
90
In [17]: print(np.array(my_list))
[1 2 3]
In [18]: my_matrix = [[1,2,3],[4,5,6],[7,8,9],[11,12,13]]
my_matrix
Out[18]: [[1, 2, 3], [4, 5, 6], [7, 8, 9], [11, 12, 13]]
In [19]: len(my_matrix)
Out[19]: 4
In [20]: f=np.array(my_matrix)
f
Out[20]: array([[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9],
[11, 12, 13]])
In [21]: f.shape
Out[21]: (4, 3)
Built-in Methods
localhost:8888/notebooks/Downloads/01-NumPy Arrays.ipynb 2/10
10/9/23, 9:42 PM 01-NumPy Arrays - Jupyter Notebook
arange
Return evenly spaced values within a given interval.
In [22]: range(5,9) #5678
Out[22]: range(5, 9)
In [23]: for i in range(5,9):
print(i)
5
6
7
8
In [24]: l=list(range(2,7))
l
Out[24]: [2, 3, 4, 5, 6]
In [25]: l*3
Out[25]: [2, 3, 4, 5, 6, 2, 3, 4, 5, 6, 2, 3, 4, 5, 6]
In [26]: a=np.arange(2,7)
a
Out[26]: array([2, 3, 4, 5, 6])
In [27]: a*3
Out[27]: array([ 6, 9, 12, 15, 18])
In [28]: a*2
Out[28]: array([ 4, 6, 8, 10, 12])
In [29]: np.arange(0,10)
Out[29]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [30]: np.arange(0,11,2)
Out[30]: array([ 0, 2, 4, 6, 8, 10])
In [ ]:
localhost:8888/notebooks/Downloads/01-NumPy Arrays.ipynb 3/10
10/9/23, 9:42 PM 01-NumPy Arrays - Jupyter Notebook
In [31]: print("welcome")
for i in range(2,9):
print(i)
print("thanks")
welcome
2
3
4
5
6
7
8
thanks
In [32]: print("welcome")
for i in range(2,9):
print(i)
print("thanks")
welcome
2
3
4
5
6
7
8
thanks
In [ ]: print("welcome")
for i in np.arange(19,12,-1):
print(i)
print("thanks")
In [ ]: print("welcome")
for i in range(9,2,1):
print(i)
print("thanks")
In [ ]: print("welcome")
for i in np.arange(-9,-2,1):
print(i)
print("thanks")
In [ ]: print("welcome")
for i in np.arange(-9,-2,-1):
print(i)
print("thanks")
zeros and ones
Generate arrays of zeros or ones
localhost:8888/notebooks/Downloads/01-NumPy Arrays.ipynb 4/10
10/9/23, 9:42 PM 01-NumPy Arrays - Jupyter Notebook
In [33]: import numpy as np
In [34]: np.zeros(7)
Out[34]: array([0., 0., 0., 0., 0., 0., 0.])
In [35]: np.zeros(7)+3
Out[35]: array([3., 3., 3., 3., 3., 3., 3.])
In [36]: np.zeros((3,4))
Out[36]: array([[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]])
In [37]: np.zeros((2,4))+8
Out[37]: array([[8., 8., 8., 8.],
[8., 8., 8., 8.]])
In [38]: np.ones(3)
Out[38]: array([1., 1., 1.])
In [39]: np.ones((3,3))
Out[39]: array([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])
In [40]: np.ones((3,3))*5
Out[40]: array([[5., 5., 5.],
[5., 5., 5.],
[5., 5., 5.]])
linspace
Return evenly spaced numbers over a specified interval.
In [41]: np.linspace(0,10,5)
Out[41]: array([ 0. , 2.5, 5. , 7.5, 10. ])
In [42]: np.linspace(1,4,9)
Out[42]: array([1. , 1.375, 1.75 , 2.125, 2.5 , 2.875, 3.25 , 3.625, 4. ])
localhost:8888/notebooks/Downloads/01-NumPy Arrays.ipynb 5/10
10/9/23, 9:42 PM 01-NumPy Arrays - Jupyter Notebook
In [43]: np.linspace(0,10,90)
Out[43]: array([ 0. , 0.11235955, 0.2247191 , 0.33707865, 0.4494382 ,
0.56179775, 0.6741573 , 0.78651685, 0.8988764 , 1.01123596,
1.12359551, 1.23595506, 1.34831461, 1.46067416, 1.57303371,
1.68539326, 1.79775281, 1.91011236, 2.02247191, 2.13483146,
2.24719101, 2.35955056, 2.47191011, 2.58426966, 2.69662921,
2.80898876, 2.92134831, 3.03370787, 3.14606742, 3.25842697,
3.37078652, 3.48314607, 3.59550562, 3.70786517, 3.82022472,
3.93258427, 4.04494382, 4.15730337, 4.26966292, 4.38202247,
4.49438202, 4.60674157, 4.71910112, 4.83146067, 4.94382022,
5.05617978, 5.16853933, 5.28089888, 5.39325843, 5.50561798,
5.61797753, 5.73033708, 5.84269663, 5.95505618, 6.06741573,
6.17977528, 6.29213483, 6.40449438, 6.51685393, 6.62921348,
6.74157303, 6.85393258, 6.96629213, 7.07865169, 7.19101124,
7.30337079, 7.41573034, 7.52808989, 7.64044944, 7.75280899,
7.86516854, 7.97752809, 8.08988764, 8.20224719, 8.31460674,
8.42696629, 8.53932584, 8.65168539, 8.76404494, 8.87640449,
8.98876404, 9.1011236 , 9.21348315, 9.3258427 , 9.43820225,
9.5505618 , 9.66292135, 9.7752809 , 9.88764045, 10. ])
eye
Creates an identity matrix
In [44]: np.eye(3)
Out[44]: array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
In [45]: np.eye(5)
Out[45]: array([[1., 0., 0., 0., 0.],
[0., 1., 0., 0., 0.],
[0., 0., 1., 0., 0.],
[0., 0., 0., 1., 0.],
[0., 0., 0., 0., 1.]])
In [46]: np.zeros((9,9))
Out[46]: 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.]])
randint
Return random integers from low (inclusive) to high (exclusive).
localhost:8888/notebooks/Downloads/01-NumPy Arrays.ipynb 6/10
10/9/23, 9:42 PM 01-NumPy Arrays - Jupyter Notebook
In [49]: np.random.randint(1,10)
Out[49]: 8
In [52]: np.random.randint(1000,9999,5)
Out[52]: array([4185, 3075, 3396, 1365, 6638])
In [55]: np.random.randint(1000,10000)
Out[55]: 9477
Array Attributes and Methods
Let's discuss some useful attributes and methods or an array:
In [56]: arr = np.arange(30)
arr
Out[56]: 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, 25, 26, 27, 28, 29])
In [59]: ranarr = np.random.randint(0,50,10)
In [60]: ranarr
Out[60]: array([13, 15, 41, 47, 4, 18, 47, 17, 16, 49])
In [61]: ranarr.shape
Out[61]: (10,)
Reshape
Returns an array containing the same data with a new shape.
In [62]: arr=arr.reshape(5,6)
In [63]: arr
Out[63]: 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, 25, 26, 27, 28, 29]])
In [64]: ranarr
Out[64]: array([13, 15, 41, 47, 4, 18, 47, 17, 16, 49])
localhost:8888/notebooks/Downloads/01-NumPy Arrays.ipynb 7/10
10/9/23, 9:42 PM 01-NumPy Arrays - Jupyter Notebook
In [65]: arr.max()
Out[65]: 29
In [66]: arr.min()
Out[66]: 0
Shape
Shape is an attribute that arrays have (not a method):
In [67]: arr
Out[67]: 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, 25, 26, 27, 28, 29]])
In [68]: # Vector
arr.shape
Out[68]: (5, 6)
In [69]: # Notice the two sets of brackets
arr.reshape(1,30)
Out[69]: 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, 25, 26, 27, 28, 29]])
In [70]: arr.reshape(1,30).shape
Out[70]: (1, 30)
localhost:8888/notebooks/Downloads/01-NumPy Arrays.ipynb 8/10
10/9/23, 9:42 PM 01-NumPy Arrays - Jupyter Notebook
In [71]: arr.reshape(30,1)
Out[71]: 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],
[25],
[26],
[27],
[28],
[29]])
In [72]: arr.reshape(30,1).shape
Out[72]: (30, 1)
dtype
You can also grab the data type of the object in the array:
In [73]: arr.dtype
Out[73]: dtype('int32')
In [ ]:
In [77]: np.ones( (3,5) )
Out[77]: array([[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]])
localhost:8888/notebooks/Downloads/01-NumPy Arrays.ipynb 9/10
10/9/23, 9:42 PM 01-NumPy Arrays - Jupyter Notebook
In [ ]:
localhost:8888/notebooks/Downloads/01-NumPy Arrays.ipynb 10/10