Home > Backend Development > Python Tutorial > How to extract the specified rows and columns of a matrix in Python numpy

How to extract the specified rows and columns of a matrix in Python numpy

php中世界最好的语言
Release: 2018-04-09 10:58:21
Original
19641 people have browsed it

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)
Copy after login
a
Out[31]: 
array([[0, 1, 2],
  [3, 4, 5],
  [6, 7, 8]])
Copy after login

A certain row of the matrix

a[1]
Out[32]: array([3, 4, 5])
Copy after login

A certain column of the matrix

a[:,1]
Out[33]: array([1, 4, 7])
Copy after login
b=np.eye(3,3)
b
Out[36]: 
array([[ 1., 0., 0.],
  [ 0., 1., 0.],
  [ 0., 0., 1.]])
Copy after login

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.]])
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template