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中世界最好的语言
php中世界最好的语言Original
2018-04-09 10:58:2119675browse

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!

Statement:
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