Numpy
Generally used for working with arrays.
NumPy helps to provide an array object that is faster than traditional Python lists.
The array object in NumPy is called ndarray, it provides a lot of supporting functions that
make working with ndarray very easy.
Arrays are very frequently used in data science, where speed and resources are very important.
NumPy Array Indexing
• Can access an array element by referring to its index number.
• The indexes in NumPy arrays start with 0, meaning that the first element has index 0,
and the second has index 1 etc.
E.g.
import numpy as np
arr = [Link]([1, 2, 3, 4])
print(arr[0])---o/p is 1
Access 2-D Arrays
To access elements from 2-D arrays we can use comma separated integers representing the
dimension and the index of the element.
Think of 2-D arrays like a table with rows and columns, where the dimension represents the
row and the index represents the column.
import numpy as np
arr = [Link]([[1,2,3,4,5], [6,7,8,9,10]])
print('2nd element on 1st row: ', arr[0, 1])
NumPy Input Output
NumPy offers input/output (I/O) functions for loading and saving data to and from files.
Input/output functions support a variety of file formats, including binary and text formats.
• The binary format is designed for efficient storage and retrieval of large arrays.
• The text format is more human-readable and can be easily edited in a text editor.
Most Commonly Used I/O Functions
Here are some of the commonly used NumPy Input/Output functions:
Function Description
save() saves an array to a binary file in the NumPy .npy format.
load() loads data from a binary file in the NumPy .npy format
savetxt() saves an array to a text file in a specific format
loadtxt() loads data from a text file.
NumPy save() Function
In NumPy, the save() function is used to save an array to a binary file in the
NumPy .npy format.
Here's the syntax of the save() function,
[Link](file, array)
• file - specifies the file name (along with path if required)
• array - specifies the NumPy array to be saved
NumPy load() Function
loaded_array = [Link]('[Link]')
# display the loaded array
print(loaded_array)
NumPy savetxt() Function
In NumPy, we use the savetxt() function to save an array to a text file.
Here's the syntax of the savetxt() function:
[Link](file, array)
• file - specifies the file name
• array - specifies the NumPy array to be saved
import numpy as np
# create a NumPy array
array2 = [Link]([[1, 3, 5],
[7, 9, 11]])
# save the array to a file
[Link]('[Link]', array2)
NumPy loadtxt() Function
We use the loadtxt() function to load the saved txt file.
Let's see an example to load the [Link] file that we saved earlier.
import numpy as np
# load the saved NumPy array
loaded_array = [Link]('[Link]')
# display the loaded array
print(loaded_array)
NumPy add()
The add() function performs element-wise addition of two arrays
import numpy as np
# create two arrays
array1 = [Link]([1, 2, 3])
array2 = [Link]([4, 5, 6])
# perform element-wise addition of the two arrays
result = [Link](array1, array2)
print(result)
Add NumPy Array by scalar (Single Value)
import numpy as np
# create an array
array1 = [Link]([1, 2, 3])
# add a scalar value to the array
result = [Link](array1, 10)
print(result)
Matrix Addition
import numpy as np
# Creating two 2x2 matrices
A = [Link]([[1, 2], [3, 4]])
B = [Link]([[5, 6], [7, 8]])
# Adding two matrices using the + operator
C =A+ B
# Print the result
print("Matrix C (A + B):")
print(C)
NumPy Universal Functions
The universal functions in NumPy include
• Trigonometric functions like sin(), cos(), and tan()
• Arithmetic functions like add(), subtract(), and multiply()
• Rounding functions like floor(), ceil() and around()
• Aggregation functions like mean(), min(), and max().
transpose() Syntax
The syntax of transpose() is:
[Link](array, axes = None)
transpose() Arguments
The transpose() method takes two arguments:
• array - the array to be transposed
• axes(optional) - the axes of the transposed matrix ( tuple or list of integers )
import numpy as np
originalArray = [Link]([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
# if axes argument is skipped, the shape of array is reversed
transposedArray = [Link](originalArray)
print(transposedArray)