Home >Web Front-end >JS Tutorial >Numpy operates two-dimensional arrays sorted by rows and columns

Numpy operates two-dimensional arrays sorted by rows and columns

php中世界最好的语言
php中世界最好的语言Original
2018-04-09 17:15:112501browse

This time I will bring you numpy operationsTwo-dimensional arraysSort by rows and columns, numpy operates two-dimensional arrays by rows and rowsWhat are the precautionsWhat are the practical cases? Get up and take a look.

How to sort according to a certain row or column in a two-dimensional array? Assume that data is a two-dimensional array of type numpy.array, which can be implemented using the argsort function in numpy. The code example is as follows:

data = data[data[:,2].argsort()] #按照第3列对行排序

Note: argsort returns only the sorted rows index and will not change the original array. To sort according to a certain row, you can use the transpose operation.

The code is as follows:

data = data.T(data.T[:,2].argsort()).T # 按照第3行对列进行排序
Also Sort directly by row,

The code is as follows:

data = data[:,data[2].argsort()]
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 perform automated unit testing with Python Unittest


Python operates excel to read and write data

The above is the detailed content of Numpy operates two-dimensional arrays sorted by rows and columns. 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