How to Exclude a Row and a Column from a NumPy Matrix in Python

NumPy is a powerful Python library used for numerical computing. It is widely used in scientific computing, data analysis, and machine learning. In this blog post, we will discuss how to exclude a row and a column from a NumPy matrix in Python using NumPy's random module.

Step 1: Import NumPy and NumPy Random Module We will start by importing the NumPy library and NumPy random module using the import statement.

import numpy as np
from numpy import random

Step 2: Create a Random Matrix Next, we will create a random matrix using the random.uniform() function. The random.uniform() function generates random numbers between 0 and 10 with the specified shape.

row_size = int(input("Enter rows ="))
column_size = int(input("Enter column ="))
a = np.random.uniform(0, 10, (row_size, column_size))

Step 3: Exclude a Row and a Column Now, we will exclude a row and a column from the matrix. We will ask the user to input the row and column numbers to be excluded. We will create a new matrix with the same shape as the original matrix, but with one row and one column less. We will then loop through the original matrix and exclude the specified row and column.

n1 = int(input("Enter row number for excluding = "))
n2 = int(input("Enter column number for excluding = "))
a1 = np.zeros((row_size-1, column_size-1))
m = []
for i in range(0, row_size):
    for j in range(0, column_size):
        if(i != n1-1 and j != n2-1):
            m.append(a[i][j])
p = 0
for i in range(0, row_size-1):
    for j in range(0, column_size-1):
        a1[i][j] = m[p]
        p = p+1

Step 4: Print the Original and Modified Matrices Finally, we will print the original and modified matrices using the print() function.

print('Original Matrix')
print(a)
print("\n")
print("Matrix after Exclusion\n")
print(a1)

The complete code is as follows:

import numpy as np
from numpy import random
row_size = int(input("Enter rows ="))
column_size = int(input("Enter column ="))
a = np.random.uniform(0, 10, (row_size, column_size))
n1 = int(input("Enter row number for excluding = "))
n2 = int(input("Enter column number for excluding = "))
a1 = np.zeros((row_size-1, column_size-1))
m = []
for i in range(0, row_size):
    for j in range(0, column_size):
        if(i != n1-1 and j != n2-1):
            m.append(a[i][j])
p = 0
for i in range(0, row_size-1):
    for j in range(0, column_size-1):
        a1[i][j] = m[p]
        p = p+1
print('Original Matrix')
print(a)
print("\n")
print("Matrix after Exclusion\n")
print(a1)

In conclusion, excluding a row and a column from a NumPy matrix in Python is a simple and straightforward process using NumPy's random module. By following the above steps, you can easily exclude a row and a column from a NumPy matrix in Python.

Here's an example output for the code:


In this example, we generated a random matrix of size 4x5 using the random.uniform() function. We then excluded row number 2 and column number 3 from the matrix, and printed both the original matrix and the modified matrix using the print() function. As a result, the output shows the original matrix and the modified matrix with the excluded row and column.

Post a Comment

0 Comments