Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

N Umpy Notebook

Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

In 

[1]: # The first step of using numpy is to tell python to import it


import numpy as np

2.1 NumPy Arrays ¶

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.

In [2]: # 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]

In [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.

In [4]: # 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'>

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]]

In [6]: print('Data type of matrix: ',type(matrix))

Data type of matrix: <class 'numpy.ndarray'>

We see that all the NumPy objects have data type as ndarray

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

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])

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

In [9]: matrix2 = np.linspace(0,5) # by default 50 evenly spaced values will be generat


matrix2
Out[9]: array([0. , 0.10204082, 0.20408163, 0.30612245, 0.40816327,
0.51020408, 0.6122449 , 0.71428571, 0.81632653, 0.91836735,
1.02040816, 1.12244898, 1.2244898 , 1.32653061, 1.42857143,
1.53061224, 1.63265306, 1.73469388, 1.83673469, 1.93877551,
2.04081633, 2.14285714, 2.24489796, 2.34693878, 2.44897959,
2.55102041, 2.65306122, 2.75510204, 2.85714286, 2.95918367,
3.06122449, 3.16326531, 3.26530612, 3.36734694, 3.46938776,
3.57142857, 3.67346939, 3.7755102 , 3.87755102, 3.97959184,
4.08163265, 4.18367347, 4.28571429, 4.3877551 , 4.48979592,
4.59183673, 4.69387755, 4.79591837, 4.89795918, 5. ])

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.

In [10]: # generating 10 evenly spaced values between 10 and 20


matrix3 = np.linspace(10,20,10)
matrix3
Out[10]: 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.

In [11]: matrix4 = np.zeros([3,5])


matrix4
Out[11]: 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 .

In [12]: matrix5 = np.ones([3,5])


matrix5
Out[12]: 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 .

In [13]: matrix6 = np.eye(5)


matrix6
Out[13]: 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.]])

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 [14]: # defining an array with values 0 to 9


arr4 = np.arange(0,10)
arr4
Out[14]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

In [15]: # reshaping the array arr4 to a 2 x 5 matrix


arr4_reshaped = arr4.reshape((2,5))
arr4_reshaped
Out[15]: array([[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9]])

In [16]: arr4

Out[16]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

In [17]: # reshaping the array arr4 to a 2 x 6 matrix


arr4.reshape((2,6))
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-17-d52ee4fd36fa> in <module>()
1 # reshaping the array arr4 to a 2 x 6 matrix
----> 2 arr4.reshape((2,6))

ValueError: cannot reshape array of size 10 into shape (2,6)

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

Exponents

In [19]: np.exp(2)

Out[19]: 7.38905609893065

In [20]: arr5 = np.array([2,4,6])


np.exp(arr5)
Out[20]: array([ 7.3890561 , 54.59815003, 403.42879349])

Logarithms

In [21]: # by default NumPy takes the base of log as e


np.log(2)
Out[21]: 0.6931471805599453

In [22]: np.log(arr5)

Out[22]: array([0.69314718, 1.38629436, 1.79175947])

In [23]: ## log with base 10


np.log10(8)
Out[23]: 0.9030899869919435

Arithmetic Operations on arrays

In [24]: # 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]
In [25]: # 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]

In [26]: 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 ^!

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

In [27]: 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.]]
In [28]: 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.]]

/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.

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


In [29]: 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
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

In [30]: print(matrix9)

[[1 2 3]
[4 5 6]
[7 8 9]]

In [31]: # taking transpose of matrix


np.transpose(matrix9)
Out[31]: array([[1, 4, 7],
[2, 5, 8],
[3, 6, 9]])

In [32]: # another way of taking a transpose


matrix9.T
Out[32]: array([[1, 4, 7],
[2, 5, 8],
[3, 6, 9]])

Function to find minimum and maximum values

In [33]: print(matrix9)

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

Minimum value: 1

In [35]: 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 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.

In [36]: # Generating random values in an array


rand_mat = np.random.rand(5)
print(rand_mat)
[0.1849803 0.40907032 0.43320279 0.20128129 0.14866145]

In [37]: # * Generating random values in a matrix


rand_mat = np.random.rand(5,5) # uniform random variable
print(rand_mat)
[[0.74674765 0.39426306 0.83013246 0.59158138 0.33519229]
[0.30061878 0.74846432 0.85288519 0.22903368 0.77846736]
[0.03130384 0.25880234 0.28800138 0.39070634 0.74455262]
[0.1001066 0.2882508 0.5651407 0.66282803 0.08676726]
[0.61007231 0.95430991 0.75879184 0.29681776 0.22827461]]

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.

In [38]: # Generating random values in an array


rand_mat2 = np.random.randn(5)
print(rand_mat2)
[0.66222394 2.44126474 0.41205704 0.04786961 1.04811775]
In [39]: # Generating random values in a matrix
rand_mat2 = np.random.randn(5,5)
print(rand_mat2)
[[ 0.2820466 0.31424354 -0.51386568 -0.45097053 -1.12771526]
[ 0.05229145 -2.12906168 0.78210721 0.74668925 -0.73695471]
[ 0.70736688 0.41313492 -0.84423005 -0.11143511 1.8729083 ]
[ 0.5017195 0.71736157 1.49309021 0.45919776 -0.13238773]
[ 1.10234237 -0.20650805 -0.91874445 1.11226684 -0.45414538]]

In [40]: # 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.11722991137038243
Standard Deviation: 0.883066783507808

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.

In [41]: # Generating random values in an array


rand_mat3 = np.random.randint(1,5,10)
print(rand_mat3)
[3 2 3 1 2 4 3 3 4 4]

In [42]: # Generating random values in a matrix


rand_mat3 = np.random.randint(1,10,[5,5])
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]]
2.3 Accessing the entries of a Numpy Array

In [43]: # let's generate an array with 10 random values


rand_arr = np.random.randn(10)
print(rand_arr)
[-0.61610084 0.256255 -1.02474372 1.69663775 0.14830608 0.0587465
-0.58451129 0.09827507 1.38926594 0.52114238]

Accessing one element from an array

In [44]: # accessing the 6 th entry of rand_arr


print(rand_arr[6])
-0.5845112936165019

Accessing multiple elements from an array

In [45]: # we can access multiple entries at once using


print(rand_arr[4:9])
[ 0.14830608 0.0587465 -0.58451129 0.09827507 1.38926594]

In [46]: # 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]
[ 1.69663775 -0.58451129 0.52114238]

Accessing arrays using logical operations

In [47]: print(rand_arr)

[-0.61610084 0.256255 -1.02474372 1.69663775 0.14830608 0.0587465


-0.58451129 0.09827507 1.38926594 0.52114238]

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]

Accessing the entries of a Matrix

In [50]: # let's generate an array with 10 random values


rand_mat = np.random.randn(5,5)
print(rand_mat)
[[ 0.66008467 -0.91860835 -0.91768744 -0.56565779 -0.19013084]
[ 0.68692112 1.20042327 -0.55648032 0.51675983 -1.13325252]
[ 1.76883638 -0.85498119 1.89998922 -1.0658905 1.50531893]
[ 0.77388464 -1.24206763 0.75631041 1.01940576 0.89747448]
[ 0.37852758 -1.36846366 -0.91566544 -0.47943544 -0.20226054]]

In [51]: # acessing the second row of the rand_mat


rand_mat[1]
Out[51]: array([ 0.68692112, 1.20042327, -0.55648032, 0.51675983, -1.13325252])

In [52]: # acessing third element of the second row


print(rand_mat[1][2])

#or

print(rand_mat[1,2])
-0.5564803167132045
-0.5564803167132045

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]]

Accessing matrices using logical operations

In [54]: print(rand_mat)

[[ 0.66008467 -0.91860835 -0.91768744 -0.56565779 -0.19013084]


[ 0.68692112 1.20042327 -0.55648032 0.51675983 -1.13325252]
[ 1.76883638 -0.85498119 1.89998922 -1.0658905 1.50531893]
[ 0.77388464 -1.24206763 0.75631041 1.01940576 0.89747448]
[ 0.37852758 -1.36846366 -0.91566544 -0.47943544 -0.20226054]]
In [55]: # 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.66008467 0.68692112 1.20042327 0.51675983 1.76883638 1.89998922
1.50531893 0.77388464 0.75631041 1.01940576 0.89747448 0.37852758]
Values less than 0:
[-0.91860835 -0.91768744 -0.56565779 -0.19013084 -0.55648032 -1.13325252
-0.85498119 -1.0658905 -1.24206763 -1.36846366 -0.91566544 -0.47943544
-0.20226054]

Modifying the entries of an Array

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 [57]: # 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)
[-0.61610084 0.256255 -1.02474372 5. 5. 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 [59]: # modify entries using logical references


rand_arr[rand_arr>0] = 65
rand_arr
Out[59]: array([65. , 65. , -1.02474372, 65. , 65. ,
65. , -0.58451129, 65. , 65. , 65. ])

Modifying the entries of a Matrix

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]]

In [63]: # changing all the values of the extracted matrix to 3


sub_mat[:] = 3
print(sub_mat)
[[3. 3. 3.]
[3. 3. 3.]]

In [64]: # what happened to rand_mat when we change sub_mat?


rand_mat
Out[64]: array([[ 3. , 3. , 3. , -0.56565779, -0.19013084],
[ 3. , 3. , 3. , 0.51675983, -1.13325252],
[ 1.76883638, -0.85498119, 1.89998922, -1.0658905 , 1.50531893],
[ 0.77388464, -1.24206763, 0.75631041, 1.01940576, 0.89747448],
[ 0.37852758, -1.36846366, -0.91566544, -0.47943544, -0.20226054]])
In [65]: # to prevent this behavior we need to use the .copy() method when we assign sub
# 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()
sub_mat[:] = 3
print(sub_mat)
print(rand_mat)
[[ 2.0112384 0.49785818 -1.25183808 1.68261636 0.51405209]
[ 0.19861572 -1.02405923 1.47588997 0.9095827 1.44913937]
[ 1.17797906 2.2230628 1.0897073 -0.61658736 1.30247375]
[-0.72899737 0.35296055 -1.28267073 -0.12390565 0.74816716]
[ 1.54172729 0.23114564 -0.06203034 -0.00718632 -0.32182532]]
[[3. 3. 3.]
[3. 3. 3.]]
[[ 2.0112384 0.49785818 -1.25183808 1.68261636 0.51405209]
[ 0.19861572 -1.02405923 1.47588997 0.9095827 1.44913937]
[ 1.17797906 2.2230628 1.0897073 -0.61658736 1.30247375]
[-0.72899737 0.35296055 -1.28267073 -0.12390565 0.74816716]
[ 1.54172729 0.23114564 -0.06203034 -0.00718632 -0.32182532]]

2.4 Saving and Loading a NumPy array

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

In [66]: from google.colab import drive


drive.mount('/content/drive')
Mounted at /content/drive

In [67]: # 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)
[[7 2 9 7 2]
[6 6 5 7 2]]

[[10 16 12 17 18]
[19 12 11 13 17]]

Using np.save() function

In [ ]: np.save('/content/drive/MyDrive/Python Course/saved_file_name',randint_matrix1

Using np.savez() function


In [ ]: np.savez('/content/drive/MyDrive/Python Course/multiple_files',randint_matrix1=

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.

In [ ]: # now let's load it


loaded_arr = np.load('/content/drive/MyDrive/Python Course/saved_file_name.npy
loaded_multi = np.load('/content/drive/MyDrive/Python Course/multiple_files.npz

print(loaded_arr)
print('')
print(loaded_multi)

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 [ ]: 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)

In [ ]: # we can also save/load text files...but only single variables


np.savetxt('/content/drive/MyDrive/Python Course/text_file_name.txt',randint_ma
rand_mat_txt = np.loadtxt('/content/drive/MyDrive/Python Course/text_file_name
print(randint_matrix1)
print('')
print(rand_mat_txt)

In [ ]: ​

You might also like