01-NumPy Arrays - Jupyter Notebook
01-NumPy Arrays - Jupyter Notebook
Using NumPy
Once you've installed NumPy you can import it as a library:
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).
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))
In [5]: my+5
In [6]: my*5
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]
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
In [21]: f.shape
Out[21]: (4, 3)
Built-in Methods
arange
Return evenly spaced values within a given interval.
Out[22]: range(5, 9)
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
In [27]: a*3
In [28]: a*2
In [29]: np.arange(0,10)
In [30]: np.arange(0,11,2)
In [ ]:
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")
In [34]: np.zeros(7)
In [35]: np.zeros(7)+3
In [36]: np.zeros((3,4))
In [37]: np.zeros((2,4))+8
In [38]: np.ones(3)
In [39]: np.ones((3,3))
In [40]: np.ones((3,3))*5
linspace
Return evenly spaced numbers over a specified interval.
In [41]: np.linspace(0,10,5)
In [42]: np.linspace(1,4,9)
In [43]: np.linspace(0,10,90)
eye
Creates an identity matrix
In [44]: np.eye(3)
In [45]: np.eye(5)
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).
In [49]: np.random.randint(1,10)
Out[49]: 8
In [52]: np.random.randint(1000,9999,5)
In [55]: np.random.randint(1000,10000)
Out[55]: 9477
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
In [64]: ranarr
Out[64]: array([13, 15, 41, 47, 4, 18, 47, 17, 16, 49])
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
In [68]: # Vector
arr.shape
Out[68]: (5, 6)
In [70]: arr.reshape(1,30).shape
In [71]: arr.reshape(30,1)
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 [ ]: