2.1 NumPy Arrays

# The first step of using numpy is to tell python to import it
import numpy as np

NumPy Array * An array is a data structure that stores values of same data type. * While python lists can contain values corresponding to different data types, arrays in python can only contain values corresponding to the same data type. * However python lists fail to deliver the performance required while computing large sets of numerical data. To address this issue we use NumPy arrays. * We can create NumPy arrays by converting a list to an array.

# defining a list of different car companies or string elements
arr_str = ['Mercedes', 'BMW', 'Audi', 'Ferrari', 'Tesla']

# defining a list of number of cylinders in car or numerical elements
arr_num = [5, 4, 6, 7, 3]
# connverting the list arr_str to a NumPy array
np_arr_str = np.array(arr_str)


# connverting the list arr_num to a NumPy array
np_arr_num = np.array(arr_num)

# checking the output
print('Numpy Array (arr_str): ',np_arr_str)
print('Numpy Array (arr_num): ',np_arr_num)
Numpy Array (arr_str):  ['Mercedes' 'BMW' 'Audi' 'Ferrari' 'Tesla']
Numpy Array (arr_num):  [5 4 6 7 3]

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.

# printing the data type of lists
print('Data type of arr_str: ',type(arr_str))
print('Data type of arr_num: ',type(arr_num))

# printing the data type after conversion of lists to array
print('Data type of np_arr_str: ',type(np_arr_str))
print('Data type of np_arr_num: ',type(np_arr_num))
Data type of arr_str:  <class 'list'>
Data type of arr_num:  <class 'list'>
Data type of np_arr_str:  <class 'numpy.ndarray'>
Data type of np_arr_num:  <class 'numpy.ndarray'>

NumPy Matrix

# let's say we have information of different number of cylinders in a car and we want to display them in a matrix format
matrix = np.array([[1,2,1],[4,5,9],[1,8,9]])
print(matrix)
matrix
[[1 2 1]
 [4 5 9]
 [1 8 9]]
array([[1, 2, 1],
       [4, 5, 9],
       [1, 8, 9]])
print('Data type of matrix: ',type(matrix))
Data type of matrix:  <class 'numpy.ndarray'>

Example:

sports = ['Cricket', 'Football', 'Tennis', 'Golf', 'Baseball']
sports_new = np.array(sports)
print(sports_new)
sports_new
['Cricket' 'Football' 'Tennis' 'Golf' 'Baseball']
array(['Cricket', 'Football', 'Tennis', 'Golf', 'Baseball'], dtype='<U8')

2.2 NumPy Functions

There are different ways to create NumPy arrays using the functions available in NumPy library

Using np.arange() function * 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

Remember we already know the range function:

range(0,10)
list(range(0,10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
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]
# adding a step size of 5 to create an array
arr3  = np.arange(start = 0, stop = 20, step = 5)
arr3
array([ 0,  5, 10, 15])

Using np.linspace() function * 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

matrix2 = np.linspace(0,5) # by default 50 evenly spaced values will be generated between 0 and 5
matrix2
type(matrix2)
numpy.ndarray

How are these values getting generated?

The step size or the difference between each element will be decided by the following formula:

(stop - start) / (total elements - 1)

So, in this case: (5 - 0) / 49 = 0.10204082

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.

# generating 10 evenly spaced values between 10 and 20
matrix3 = np.linspace(10,20,10)
matrix3
array([10.        , 11.11111111, 12.22222222, 13.33333333, 14.44444444,
       15.55555556, 16.66666667, 17.77777778, 18.88888889, 20.        ])

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.
    • dtype: data type of the elements in the matrix, by default the value is set to float.
matrix4 = np.zeros([3,5])
matrix4
array([[0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.]])

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.
matrix5 = np.ones([3,5])
matrix5
array([[1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.]])
matrix5 = np.ones([3,5], int)
matrix5
array([[1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1]])

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.

matrix6 = np.eye(5)
matrix6
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.]])
matrix = np.linspace(10,20,10)
matrix
array([10.        , 11.11111111, 12.22222222, 13.33333333, 14.44444444,
       15.55555556, 16.66666667, 17.77777778, 18.88888889, 20.        ])

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.
# defining an array with values 0 to 9
arr4 = np.arange(0,10)
arr4
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
# reshaping the array arr4 to a 2 x 5 matrix
arr4_reshaped = arr4.reshape((2,5))
arr4_reshaped
array([[0, 1, 2, 3, 4],
       [5, 6, 7, 8, 9]])
arr4
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
# reshaping the array arr4 to a 2 x 6 matrix
arr4.reshape((2,6))
ValueError: ignored
  • 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.
arr4.reshape((5,2))
array([[0, 1],
       [2, 3],
       [4, 5],
       [6, 7],
       [8, 9]])

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

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 and Logarithmic functions

  • Exponents
np.exp(2)
7.38905609893065
arr5 = np.array([2,4,6])
np.exp(arr5)
array([  7.3890561 ,  54.59815003, 403.42879349])
  • Logarithms
# by default NumPy takes the base of log as e
np.log(2)
0.6931471805599453
np.log(arr5)
array([0.69314718, 1.38629436, 1.79175947])
## log with base 10
np.log10(8)
0.9030899869919435

Arithmetic Operations on arrays

# arithmetic on lists

l1 = [1,2,3]
l2 = [4,5,6]
print(l1+l2)
# this does not behave as you would expect!
[1, 2, 3, 4, 5, 6]

When we add lists it just concatenates them

# we can +-*/ arrays together

# defining two arrays
arr7 = np.arange(1,6)
print('arr7:', arr7)

arr8 = np.arange(3,8)
print('arr8:', arr8)
arr7: [1 2 3 4 5]
arr8: [3 4 5 6 7]

The following are element wise operations:

print('Addition: ',arr7+arr8)
print('Subtraction: ',arr8-arr7)
print('Multiplication:' , arr7*arr8)
print('Division:', arr7/arr8)
print('Inverse:', 1/arr7)
print('Powers:', arr7**arr8) # in python, powers are achieved using **, NOT ^!!! ^ does something completely different!
Addition:  [ 4  6  8 10 12]
Subtraction:  [2 2 2 2 2]
Multiplication: [ 3  8 15 24 35]
Division: [0.33333333 0.5        0.6        0.66666667 0.71428571]
Inverse: [1.         0.5        0.33333333 0.25       0.2       ]
Powers: [    1    16   243  4096 78125]

Operations on Matrices

matrix7 = np.arange(1,10).reshape(3,3)
print(matrix7)

matrix8 = np.eye(3)
print(matrix8)
[[1 2 3]
 [4 5 6]
 [7 8 9]]
[[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]
print('Addition: \n', matrix7+matrix8)
print('Subtraction: \n ', matrix7-matrix8)
print('Multiplication: \n', matrix7*matrix8)
print('Division: \n', matrix7/matrix8)
Addition: 
 [[ 2.  2.  3.]
 [ 4.  6.  6.]
 [ 7.  8. 10.]]
Subtraction: 
  [[0. 2. 3.]
 [4. 4. 6.]
 [7. 8. 8.]]
Multiplication: 
 [[1. 0. 0.]
 [0. 5. 0.]
 [0. 0. 9.]]
Division: 
 [[ 1. inf inf]
 [inf  5. inf]
 [inf inf  9.]]
RuntimeWarning: divide by zero encountered in divide
  print('Division: \n', matrix7/matrix8)
  • RuntimeWarning: Errors which occur during program execution(run-time) after successful compilation are called run-time errors.
  • One of the most common run-time error is division by zero also known as Division error.
  • Due to division by zero error, we are getting inf (infinity) values because 1/0 is not a defined operation.

Linear algebra matrix multiplication

matrix9 = np.arange(1,10).reshape(3,3)
print('First Matrix: \n',matrix9)

matrix10 = np.arange(11,20).reshape(3,3)
print('Second Matrix: \n',matrix10)
print('')
# taking linear algebra matrix multiplication (some may have heard this called the dot product)
print('Multiplication: \n', matrix9 @ matrix10)
First Matrix: 
 [[1 2 3]
 [4 5 6]
 [7 8 9]]
Second Matrix: 
 [[11 12 13]
 [14 15 16]
 [17 18 19]]

Multiplication: 
 [[ 90  96 102]
 [216 231 246]
 [342 366 390]]

Transpose of a matrix

print(matrix9)
[[1 2 3]
 [4 5 6]
 [7 8 9]]
# taking transpose of matrix
np.transpose(matrix9)
array([[1, 4, 7],
       [2, 5, 8],
       [3, 6, 9]])
# another way of taking a transpose
matrix9.T
array([[1, 4, 7],
       [2, 5, 8],
       [3, 6, 9]])

Function to find minimum and maximum values

print(matrix9)
[[1 2 3]
 [4 5 6]
 [7 8 9]]
print('Minimum value: ',np.min(matrix9))
Minimum value:  1
print('Maximum value: ',np.max(matrix9))
Maximum value:  9

Function to generate random samples

Using np.random.rand function

  • The np.random.rand returns a random NumPy array whose element(s) are drawn randomly from the uniform 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.
# Generating random values in an array
rand_mat = np.random.rand(5)
print(rand_mat)
[0.61119152 0.63874214 0.81412005 0.11046744 0.05085582]
# * Generating random values in a matrix
rand_mat = np.random.rand(5,5) # uniform random variable
print(rand_mat)
[[0.54149921 0.24989529 0.68436833 0.56160127 0.04672559]
 [0.90418561 0.62221662 0.53526713 0.37262735 0.40287867]
 [0.18249637 0.88662311 0.73748125 0.37634647 0.8783134 ]
 [0.12490506 0.33295567 0.10921213 0.31563883 0.03451156]
 [0.72559714 0.56869017 0.42403095 0.64943207 0.0212128 ]]

Using np.random.randn function

  • 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.
# Generating random values in an array
rand_mat2 = np.random.randn(5)
print(rand_mat2)
[ 0.50337763 -0.49592368 -0.54954059 -0.57635331  0.77430004]
# Generating random values in a matrix
rand_mat2 = np.random.randn(5,5)
print(rand_mat2)
[[ 1.02952777  0.17156967 -0.38967882  0.97064069 -0.4977873 ]
 [-0.6554173   0.09778448  0.02065062  0.24890587  0.96219723]
 [-2.1752861  -0.77920612  0.19608711  0.26009534 -0.87234805]
 [-1.60647075 -1.56615086  2.59093804 -0.70987238 -0.68224213]
 [-0.72795919 -0.89102125 -0.49189801 -0.1577149  -0.99756595]]
# Let's check the mean and standard deviation of rand_mat2
print('Mean:',np.mean(rand_mat2))
print('Standard Deviation:',np.std(rand_mat2))
Mean: -0.2660888911305895
Standard Deviation: 0.971519539817963
  • We observe that the mean is very close to 0 and standard deviation is very close to 1.

Using np.random.randint function

  • 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.
# Generating random values in an array
rand_mat3 = np.random.randint(1,5,10)
print(rand_mat3)
[2 1 4 2 3 3 1 4 2 3]
# Generating random values in a matrix
rand_mat3 = np.random.randint(1,10,[5,5])
print(rand_mat3)
[[8 2 6 9 9]
 [4 3 2 1 3]
 [3 2 6 3 9]
 [5 5 8 3 3]
 [3 1 2 8 4]]

2.3 Accessing the entries of a Numpy Array

# let's generate an array with 10 random values
rand_arr = np.random.randn(10)
print(rand_arr)
[-2.47429316  0.72450183 -0.35536103 -0.18125309 -0.41814497 -1.68162322
 -1.27917712 -0.35279965 -0.36827598 -0.70568934]
  • Accessing one element from an array
# accessing the 6 th entry of rand_arr
print(rand_arr[6])
-1.2791771211266523
  • Accessing multiple elements from an array
# we can access multiple entries at once using
print(rand_arr[4:9])
[-0.41814497 -1.68162322 -1.27917712 -0.35279965 -0.36827598]
# we can also access multiple non-consecutive entries using np.arange
print('Index of values to access: ',np.arange(3,10,3))
print(rand_arr[np.arange(3,10,3)])
Index of values to access:  [3 6 9]
[-0.18125309 -1.27917712 -0.70568934]

Accessing arrays using logical operations

print(rand_arr)
[-2.47429316  0.72450183 -0.35536103 -0.18125309 -0.41814497 -1.68162322
 -1.27917712 -0.35279965 -0.36827598 -0.70568934]
rand_arr>0
array([False,  True, False, False, False, False, False, False, False,
       False])
print(rand_arr>0)
[False  True False False False False False False False False]
# 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.72450183]
Values less than 0:  [-2.47429316 -0.35536103 -0.18125309 -0.41814497 -1.68162322 -1.27917712
 -0.35279965 -0.36827598 -0.70568934]
vec = np.array([4,7,8,9,10,6,1])
print(vec[vec>6])
[ 7  8  9 10]
vec1 = np.array([4, 7, 8, 9, 10, 6, 1])
vec1[vec1>6] = 2
print(vec1)
[4 2 2 2 2 6 1]

Accessing the entries of a Matrix

# let's generate an array with 10 random values
rand_mat = np.random.randn(5,5)
print(rand_mat)
[[ 0.7392347   1.18219165  1.41198691  1.75049955  0.74367584]
 [-1.15032837  0.23079247 -0.06174375  1.10037696  0.41069768]
 [ 0.61493313 -0.03418034 -1.23791069 -0.11883395 -0.38064714]
 [ 1.66156107  0.55040996 -0.23320651 -0.88322353 -0.57090148]
 [ 0.96685567 -0.80259471 -0.8503211   1.26409647  0.44283458]]
# acessing the second row of the rand_mat
rand_mat[1]
array([-1.15032837,  0.23079247, -0.06174375,  1.10037696,  0.41069768])
# acessing third element of the second row
print(rand_mat[1][2])

#or

print(rand_mat[1,2])
-0.06174375212141622
-0.06174375212141622
# accessing first two rows with second and third column
print(rand_mat[0:2,1:3])
[[ 1.18219165  1.41198691]
 [ 0.23079247 -0.06174375]]

Accessing matrices using logical operations

print(rand_mat)
[[ 0.7392347   1.18219165  1.41198691  1.75049955  0.74367584]
 [-1.15032837  0.23079247 -0.06174375  1.10037696  0.41069768]
 [ 0.61493313 -0.03418034 -1.23791069 -0.11883395 -0.38064714]
 [ 1.66156107  0.55040996 -0.23320651 -0.88322353 -0.57090148]
 [ 0.96685567 -0.80259471 -0.8503211   1.26409647  0.44283458]]
# accessing all the values of rand_mat which are greater than 0
print('Values greater than 0: \n ',rand_mat[rand_mat>0])

# accessing all the values of rand_mat which are less than 0
print('Values less than 0: \n',rand_mat[rand_mat<0])
Values greater than 0: 
  [0.7392347  1.18219165 1.41198691 1.75049955 0.74367584 0.23079247
 1.10037696 0.41069768 0.61493313 1.66156107 0.55040996 0.96685567
 1.26409647 0.44283458]
Values less than 0: 
 [-1.15032837 -0.06174375 -0.03418034 -1.23791069 -0.11883395 -0.38064714
 -0.23320651 -0.88322353 -0.57090148 -0.80259471 -0.8503211 ]

Modifying the entries of an Array

print(rand_arr)
[-2.47429316  0.72450183 -0.35536103 -0.18125309 -0.41814497 -1.68162322
 -1.27917712 -0.35279965 -0.36827598 -0.70568934]
# let's change some values in an array!
# changing the values of index value 3 and index value 4 to 5
rand_arr[3:5] = 5
print(rand_arr)
[-2.47429316  0.72450183 -0.35536103  5.          5.         -1.68162322
 -1.27917712 -0.35279965 -0.36827598 -0.70568934]
# changing the values of index value 0 and index value 1 to 2 and 3 respectively
rand_arr[0:2] = [2,3]
print(rand_arr)
[ 2.          3.         -0.35536103  5.          5.         -1.68162322
 -1.27917712 -0.35279965 -0.36827598 -0.70568934]
# modify entries using logical references
rand_arr[rand_arr>0] = 65
rand_arr
array([65.        , 65.        , -0.35536103, 65.        , 65.        ,
       -1.68162322, -1.27917712, -0.35279965, -0.36827598, -0.70568934])

Modifying the entries of a Matrix

print(rand_mat3)
[[8 2 6 9 9]
 [4 3 2 1 3]
 [3 2 6 3 9]
 [5 5 8 3 3]
 [3 1 2 8 4]]
# changing the values of the 4th and 5th element of the second and third rows of the matrix to 0
print('Matrix before modification: \n',rand_mat3)
rand_mat3[1:3,3:5] = 0
print('Matrix after modification: \n',rand_mat3)
Matrix before modification: 
 [[8 2 6 9 9]
 [4 3 2 1 3]
 [3 2 6 3 9]
 [5 5 8 3 3]
 [3 1 2 8 4]]
Matrix after modification: 
 [[8 2 6 9 9]
 [4 3 2 0 0]
 [3 2 6 0 0]
 [5 5 8 3 3]
 [3 1 2 8 4]]
# extracting the first 2 rows and first 3 columns from the matrix
print(rand_mat)
sub_mat = rand_mat[0:2,0:3]
print('Sub Matrix: \n',sub_mat)
[[ 0.7392347   1.18219165  1.41198691  1.75049955  0.74367584]
 [-1.15032837  0.23079247 -0.06174375  1.10037696  0.41069768]
 [ 0.61493313 -0.03418034 -1.23791069 -0.11883395 -0.38064714]
 [ 1.66156107  0.55040996 -0.23320651 -0.88322353 -0.57090148]
 [ 0.96685567 -0.80259471 -0.8503211   1.26409647  0.44283458]]
Sub Matrix: 
 [[ 0.7392347   1.18219165  1.41198691]
 [-1.15032837  0.23079247 -0.06174375]]

The [:] means all the rows and all the columns

# changing all the values of the extracted matrix to 3
sub_mat[:] = 3
print(sub_mat)
[[3. 3. 3.]
 [3. 3. 3.]]
# what happened to rand_mat when we change sub_mat?
rand_mat
array([[ 3.        ,  3.        ,  3.        ,  1.75049955,  0.74367584],
       [ 3.        ,  3.        ,  3.        ,  1.10037696,  0.41069768],
       [ 0.61493313, -0.03418034, -1.23791069, -0.11883395, -0.38064714],
       [ 1.66156107,  0.55040996, -0.23320651, -0.88322353, -0.57090148],
       [ 0.96685567, -0.80259471, -0.8503211 ,  1.26409647,  0.44283458]])

This happens because Python is a reference based programming language.

# to prevent this behavior we need to use the .copy() method when we assign sub_mat
# this behavior is the source of MANY errors for early python users!!!

rand_mat = np.random.randn(5,5)
print(rand_mat)
sub_mat = rand_mat[0:2,0:3].copy()   # This .copy breaks the conection bewtween rand_mat and sub_mat.
sub_mat[:] = 3
print(sub_mat)
print(rand_mat)
[[ 0.82092924 -0.59815803  0.02854417 -0.23940479  0.12059752]
 [-1.34586476 -0.66854391 -0.05915812 -2.94774861  0.52788252]
 [-0.02661493  0.32532514  0.21700486  0.15709506  0.7808568 ]
 [-1.29389382 -0.35506882 -1.30828677 -0.94010785 -0.22061429]
 [ 0.83973608 -0.6642331  -0.90050473  0.78779374  0.43902847]]
[[3. 3. 3.]
 [3. 3. 3.]]
[[ 0.82092924 -0.59815803  0.02854417 -0.23940479  0.12059752]
 [-1.34586476 -0.66854391 -0.05915812 -2.94774861  0.52788252]
 [-0.02661493  0.32532514  0.21700486  0.15709506  0.7808568 ]
 [-1.29389382 -0.35506882 -1.30828677 -0.94010785 -0.22061429]
 [ 0.83973608 -0.6642331  -0.90050473  0.78779374  0.43902847]]
matrix = np.arange(1,10).reshape(3,3)
matrix[0:2,1:2] = 0
print(matrix)
[[1 0 3]
 [4 0 6]
 [7 8 9]]

2.4 Saving and Loading a NumPy array

Let’s save some NumPy objects on the disk for use later!

from google.colab import drive
drive.mount('/content/drive')
Mounted at /content/drive
# creating a random matrices
randint_matrix1 = np.random.randint(1,10,10).reshape(2,5)
print(randint_matrix1)
print('')
randint_matrix2 = np.random.randint(10,20,10).reshape(2,5)
print(randint_matrix2)
[[8 1 9 6 6]
 [3 4 8 9 6]]

[[11 14 16 15 11]
 [18 17 11 17 12]]

Using np.save() function

np.save('/content/drive/MyDrive/MIT Machine Learning/Week_1/saved_file_name',randint_matrix1)

Using np.savez() function

np.savez('/content/drive/MyDrive/MIT Machine Learning/Week_1/multiple_files',randint_matrix1=randint_matrix1,randint_matrix2=randint_matrix2)
  • 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.
# now let's load it
loaded_arr = np.load('/content/drive/MyDrive/MIT Machine Learning/Week_1/saved_file_name.npy')
loaded_multi = np.load('/content/drive/MyDrive/MIT Machine Learning/Week_1/multiple_files.npz')

print(loaded_arr)
print('')
print(loaded_multi)
[[8 1 9 6 6]
 [3 4 8 9 6]]

<numpy.lib.npyio.NpzFile object at 0x789090393f40>
  • 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.
print('1st Matrix: \n',loaded_multi['randint_matrix1'])
print('2nd Matrix: \n',loaded_multi['randint_matrix2'])

new_matrix  = loaded_multi['randint_matrix1']
print('New Matrix: \n',new_matrix)
1st Matrix: 
 [[8 1 9 6 6]
 [3 4 8 9 6]]
2nd Matrix: 
 [[11 14 16 15 11]
 [18 17 11 17 12]]
New Matrix: 
 [[8 1 9 6 6]
 [3 4 8 9 6]]
# we can also save/load text files...but only single variables
np.savetxt('/content/drive/MyDrive/MIT Machine Learning/Week_1/text_file_name.txt',randint_matrix1,delimiter=',')
rand_mat_txt = np.loadtxt('/content/drive/MyDrive/MIT Machine Learning/Week_1/text_file_name.txt',delimiter=',')
print(randint_matrix1)
print('')
print(rand_mat_txt)
[[8 1 9 6 6]
 [3 4 8 9 6]]

[[8. 1. 9. 6. 6.]
 [3. 4. 8. 9. 6.]]