N Umpy Notebook
N Umpy Notebook
N Umpy Notebook
NumPy Array
The resuts look similar to a list but arr_str and arr_num have been converted to NumPy arrays.
Let's check the data type to confirm this.
The above output confirms that both the lists were successfully converted to arrays
The above output confirms that both the lists were successfully converted to arrays
NumPy Matrix
A matrix is a two-dimensional data structure where elements are arranged into rows and
columns.
A matrix can be created by using list of lists
In [5]: # let's say we have information of different number of cylinders in a car and w
matrix = np.array([[1,2,1],[4,5,9],[1,8,9]])
print(matrix)
[[1 2 1]
[4 5 9]
[1 8 9]]
We see that all the NumPy objects have data type as ndarray
There are different ways to create NumPy arrays using the functions available in NumPy
library
The np.arange() function returns an array with evenly spaced elements as per the interval.
The interval mentioned is half-opened i.e. start is included but stop is excluded.
It has the following paramaters:
start : start of interval range. By default start = 0
stop : end of interval range
step : step size of interval. By default step size = 1
In [7]: arr2 = np.arange(start = 0, stop = 10) # 10 will be excluded from the output
print(arr2)
# or
arr2 = np.arange(0,10)
print(arr2)
[0 1 2 3 4 5 6 7 8 9]
[0 1 2 3 4 5 6 7 8 9]
In [8]: # adding a step size of 5 to create an array
arr3 = np.arange(start = 0, stop = 20, step = 5)
arr3
Out[8]: array([ 0, 5, 10, 15])
The np.linspace() function returns numbers which are evenly distributed with respect to
interval. Here the start and stop both are included.
*It has the following parameters:
start: start of interval range. By default start = 0
stop: end of interval range
num : No. of samples to generate. By default num = 50
The step size or the difference between each element will be decided by the following formula:
The first value will be 0.10204082, the second value will be 0.10204082 + 0.10204082, the third
value will be 0.10204082 + 0.10204082 +0.10204082, and so on.
Similarly we can create matrices using the functions available in NumPy library
Using np.zeros()
The np.zeros() is a function for creating a matrix and performing matrix operations in
NumPy.
It returns a matrix filled with zeros of the given shape.
It has the following parameters:
shape : Number of rows and columns in the output matrix.
Using np.ones()
The np.ones() is another function for creating a matrix and performing matrix operations in
NumPy.
It returns a matrix of given shape and type, filled with ones.
It has the following parameters:
shape : Number of rows and columns in the output matrix.
dtype: data type of the elements in the matrix, by default the value is set to float .
Using np.eye()
The np.eye() is a function for creating a matrix and performing matrix operations in NumPy.
It returns a matrix with ones on the diagonal and zeros elsewhere.
It has the following parameters:
n: Number of rows and columns in the output matrix
dtype: data type of the elements in the matrix, by default the value is set to float .
We can also convert a one dimension array to a matrix. This can be done by using the
np.reshape() function.
The shape of an array basically tells the number of elements and dimensions of the array.
Reshaping a Numpy array simply means changing the shape of the given array.
By reshaping an array we can add or remove dimensions or change number of elements in
each dimension.
In order to reshape a NumPy array, we use the reshape method with the given array.
Syntax: array.reshape(shape)
shape: a tuple given as input, the values in tuple will be the new shape of the array.
In [16]: arr4
This did not work because we have 10 elements which we are trying to fit in a 2 X 6 shape
which will require 12 elements.
NumPy can also perform a large number of different mathematical operations and it
provides different functions to do so.
NumPy provides:
1. Trigonometric functions
2. Exponents and Logarithmic functions
3. Functions for arithmetic operations between arrays and matrices
Trigonometric functions
In [18]: print('Sine Function:',np.sin(4))
print('Cosine Function:',np.cos(4))
print('Tan Function',np.tan(4))
Sine Function: -0.7568024953079282
Cosine Function: -0.6536436208636119
Tan Function 1.1578212823495775
Exponents
In [19]: np.exp(2)
Out[19]: 7.38905609893065
Logarithms
In [22]: np.log(arr5)
Operations on Matrices
/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:4: RuntimeWarnin
g: divide by zero encountered in true_divide
after removing the cwd from sys.path.
Multiplication:
[[ 90 96 102]
[216 231 246]
[342 366 390]]
Transpose of a matrix
In [30]: print(matrix9)
[[1 2 3]
[4 5 6]
[7 8 9]]
In [33]: print(matrix9)
[[1 2 3]
[4 5 6]
[7 8 9]]
In [34]: print('Minimum value: ',np.min(matrix9))
Minimum value: 1
Maximum value: 9
The np.random.rand returns a random NumPy array whose element(s) are drawn randomly
from the normal distribution over [0,1). (including 0 but excluding 1).
Syntax - np.random.rand(d0,d1)
d0,d1 – It represents the dimension of the required array given as int, where d1 is
optional.
The np.random.randn returns a random numpy array whose sample(s) are drawn randomly
from the standard normal distribution (Mean as 0 and standard deviation as 1)
Syntax - np.random.randn(d0,d1)
d0,d1 – It represents the dimension of the output, where d1 is optional.
We observe that the mean is very close to 0 and standard deviation is very close to 1.
The np.random.randint returns a random numpy array whose element(s) are drawn
randomly from low (inclusive) to the high (exclusive) range.
Syntax - np.random.randint(low, high, size)
low – It represents the lowest inclusive bound of the distribution from where the sample
can be drawn.
high – It represents the upper exclusive bound of the distribution from where the
sample can be drawn.
size – It represents the shape of the output.
In [47]: print(rand_arr)
In [48]: rand_arr>0
Out[48]: array([False, True, False, True, True, True, False, True, True,
True])
In [49]: # accessing all the values of rand_arr which are greater than 0
print('Values greater than 0: ',rand_arr[rand_arr>0])
# accessing all the values of rand_arr which are less than 0
print('Values less than 0: ',rand_arr[rand_arr<0])
Values greater than 0: [0.256255 1.69663775 0.14830608 0.0587465 0.098275
07 1.38926594
0.52114238]
Values less than 0: [-0.61610084 -1.02474372 -0.58451129]
In [53]: # accessing first two rows with second and third column
print(rand_mat[0:2,1:3])
[[-0.91860835 -0.91768744]
[ 1.20042327 -0.55648032]]
In [54]: print(rand_mat)
In [56]: print(rand_arr)
[-0.61610084 0.256255 -1.02474372 1.69663775 0.14830608 0.0587465
-0.58451129 0.09827507 1.38926594 0.52114238]
In [58]: # changing the values of index value 0 and index value 1 to 2 and 3 respective
rand_arr[0:2] = [2,3]
print(rand_arr)
[ 2. 3. -1.02474372 5. 5. 0.0587465
-0.58451129 0.09827507 1.38926594 0.52114238]
In [60]: print(rand_mat3)
[[1 5 2 1 2]
[6 1 7 1 8]
[6 6 5 2 5]
[4 3 7 3 1]
[2 6 1 1 5]]
In [61]: # changing the values of the 4th and 5th element of the second and third rows o
print('Matrix before modification: \n',rand_mat3)
rand_mat3[1:3,3:5] = 0
print('Matrix after modification: \n',rand_mat3)
Matrix before modification:
[[1 5 2 1 2]
[6 1 7 1 8]
[6 6 5 2 5]
[4 3 7 3 1]
[2 6 1 1 5]]
Matrix after modification:
[[1 5 2 1 2]
[6 1 7 0 0]
[6 6 5 0 0]
[4 3 7 3 1]
[2 6 1 1 5]]
In [62]: # extracting the first 2 rows and first 3 columns from the matrix
sub_mat = rand_mat[0:2,0:3]
print(sub_mat)
[[ 0.66008467 -0.91860835 -0.91768744]
[ 0.68692112 1.20042327 -0.55648032]]
Let's save some NumPy objects on the disk for use later!
[[10 16 12 17 18]
[19 12 11 13 17]]
The files will be saved in the directory where the Jupyter Notebook is located.
With np.save() function, we can save an array/matrix to a NumPy .npy format.
np.savez() function has an advantage over np.save() function because with np.savez(), we
can store several arrays/matrices into a single file in uncompressed .npz format.
We see that .npy file has been loaded but the .npz file is returning a memory location.
Let's see how to load the values stored in .npz file.
In [ ]: