NumPy Library
NumPy is a library in python that is created to work efficiently with arrays in python. It is fast, easy to learn, and provides efficient storage. It also provides a better way of handling data for the process. We can create an n-dimensional array in NumPy. To use NumPy simply have to import it into our program and then we can easily use the functionality of NumPy in our program.
NumPy is a Python library that is frequently used for scientific and statistical analysis. NumPy arrays are grids of the same datatype’s values.
Numpy matrix.itemset() Function:
We can set the items in a given matrix using the matrix.itemset() method of the Numpy module by passing the index number and the item as arguments.
Syntax:
matrix.itemset(index, item)
Return Value:
A new matrix containing item is returned by the itemset() function.
Numpy matrix.itemset() Function in Python
For 2-Dimensional (2D) Matrix
Approach:
- Import numpy module using the import keyword
- Create a matrix(2-Dimensional) using the matrix() function of numpy module by passing some random 2D matrix as arguments to it and store it in a variable
- Pass the index(row, column) and item/value as an argument to the itemset() function and apply it to the given matrix.
- Here it inserts the given item at the specified index in a matrix(inserts 50 at 0th row, 1st col).
- Store it in another variable
- Print the given matrix after inserting an item at the specified index.
- The Exit of the Program.
Below is the implementation:
# Import numpy module using the import keyword import numpy as np # Create a matrix(2-Dimensional) using the matrix() function of numpy module by passing # some random 2D matrix as arguments to it and store it in a variable gvn_matrx = np.matrix('[2, 1; 6, 3]') # Pass the index(row, column) and item/value as an argument to the itemset() function and # apply it to the given matrix. # Here it inserts the given item at the specified index in a matrix(inserts 50 at 0th row, 1st col). # Store it in another variable gvn_matrx.itemset((0, 1), 50) # Print the given matrix after inserting an item at the specified index print("The given matrix after inserting an item {50} at the specified index(0th row, 1st col):") print(gvn_matrx)
Output:
The given matrix after inserting an item {50} at the specified index(0th row, 1st col): [[ 2 50] [ 6 3]]
For 3-Dimensional (3D) Matrix
Approach:
- Import numpy module using the import keyword
- Create a matrix(3-Dimensional) using the matrix() function of numpy module by passing some random 3D matrix as arguments to it and store it in a variable
- Pass the index(row, column) and item/value as an argument to the itemset() function and apply it to the given matrix.
- Here it inserts the given item at the specified index in a matrix(inserts 50 at 0th row, 1st col).
- Store it in another variable
- Print the given matrix after inserting an item at the specified index.
- The Exit of the Program.
Below is the implementation:
# Import numpy module using the import keyword import numpy as np # Create a matrix(3-Dimensional) using the matrix() function of numpy module by passing # some random 3D matrix as an argument to it and store it in a variable gvn_matrx = np.matrix('[2, 4, 1; 8, 7, 3; 10, 9, 5]') # Pass the index(row, column) and item/value as an argument to the itemset() function and # apply it to the given matrix. # Here it inserts the given item at the specified index in a matrix(inserts 100 at 1st row, 2nd col). # Store it in another variable gvn_matrx.itemset((1, 2), 100) # Print the given matrix after inserting an item at the specified index print("The given matrix after inserting an item {100} at the specified index(1st row, 2nd col):") print(gvn_matrx)
Output:
The given matrix after inserting an item {100} at the specified index(1st row, 2nd col): [[ 2 4 1] [ 8 7 100] [ 10 9 5]]