NumPy is a powerful Python library designed for executing mathematical operations on arrays, which are multidimensional collections of elements of the same size. We use the indexing operator “[row, column]” to access an element at a specific index. NumPy.load() loads data from the .npy files into NumPy arrays. NumPy.matrix.transpose() is used to transpose matrices which means interchanging rows and columns.
To access NumPy
1. Open your Python interpreter or a Python script in a text editor.
2. On the first line, type “import NumPy and press enter to import the NumPy library.
3. We can give a nickname to NumPy to make it easier; we can call it “np”. To do this, we need to mention this at the start of the program like “import NumPy as np”.
4. Now we can use NumPy functions and methods by calling with “np”.
5. to access elements we use indexing.
program:
import numpy as np array= np.array([[[7,8,91],[6,5,43],[3,2,[1111]]]], dtype=object) element=array[0,2,2][0] print(element)
output: 1111
NumPy.load()
1. first, You need to import the NumPy library in your Python script.
2. we need to use ‘NumPy.load()’ to load data from the ‘.npy’ file into an array.
3. once loaded you can perform various operations on the data.
program:
import numpy as np my_arr=np.array([[1,2,4],[5,3,8]]) np.save('my_arr_temp.npy',my_arr) loaded_arr=np.load('my_arr_temp.npy') print(loaded_arr)
output: [[1 2 4] [5 3 8]]
NumPy matrix. transpose()
1. first you need import NumPy in your library.
2. Now, create matrix using NumPy.
3. Then , transpose the created matrix using ‘transpose()’.
Program:
import numpy as np matrix=np.matrix([[10,20],[30,40],[50,60]]) transposed_matrix=matrix.transpose() print("old matrix:") print(matrix) print("transposed matrix:") print(transposed_matrix)
output: old matrix: [[10 20] [30 40] [50 60]] transposed matrix: [[10 30 50] [20 40 60]]