Home >Backend Development >Python Tutorial >How to extract the specified rows and columns of a matrix in Python numpy
This time I will bring you Python How does numpy extract the specified rows and columns of a matrix? What are the precautions for Python numpy to extract the specified rows and rows of a matrix. Here is a practical case. Let’s take a look. one time.
is as follows:
import numpy as np a=np.arange(9).reshape(3,3)
a Out[31]: array([[0, 1, 2], [3, 4, 5], [6, 7, 8]])
A certain row of the matrix
a[1] Out[32]: array([3, 4, 5])
A certain column of the matrix
a[:,1] Out[33]: array([1, 4, 7])
b=np.eye(3,3) b Out[36]: array([[ 1., 0., 0.], [ 0., 1., 0.], [ 0., 0., 1.]])
Assign the second column of matrix a to the first column of matrix b
b[:,0]=a[:,1] b Out[38]: array([[ 1., 0., 0.], [ 4., 1., 0.], [ 7., 0., 1.]])
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
How to implement Mahalanobis distance in Python
##How to batch read txt files into DataFrame format in Python
The above is the detailed content of How to extract the specified rows and columns of a matrix in Python numpy. For more information, please follow other related articles on the PHP Chinese website!