[Python] Sorting Numpy data

Sort numpy.sort(a, axis=-1, kind='quicksort', order=None)

In the case of one dimension

Use sort to sort in ascending order. If you want to sort in descending order, use [:: -1] as shown below.

In [3]: x = np.random.randint(0, 100, size=10)

In [4]: print x
[63 82 80 93 65 96 97 75  2 61]

In [5]: print np.sort(x)
[ 2 61 63 65 75 80 82 93 96 97]

In [6]: print np.sort(x)[::-1]
[97 96 93 82 80 75 65 63 61  2]

For multidimensional

In the case of multidimensional, specify which axis data to sort. If omitted, it will be the last axis.

In [7]: x = np.random.randint(0, 100, size=(2,10))

In [8]: print x
[[43 70 88 99  0 83 51 17 19 58]
 [11 89 94 11  8 28  9 54 74 67]]

In [9]: print np.sort(x)
[[ 0 17 19 43 51 58 70 83 88 99]
 [ 8  9 11 11 28 54 67 74 89 94]]

In [10]: print np.sort(x,axis=0)
[[11 70 88 11  0 28  9 17 19 58]
 [43 89 94 99  8 83 51 54 74 67]]

If you want to sort in descending order, use [:: -1] as in 1D.

In [11]: print x
[[43 70 88 99  0 83 51 17 19 58]
 [11 89 94 11  8 28  9 54 74 67]]

In [12]: print np.sort(x)[:,::-1]
[[99 88 83 70 58 51 43 19 17  0]
 [94 89 74 67 54 28 11 11  9  8]]

In [13]: print np.sort(x,axis=0)[::-1]
[[43 89 94 99  8 83 51 54 74 67]
 [11 70 88 11  0 28  9 17 19 58]]

argsort numpy.argsort(a, axis=-1, kind='quicksort', order=None) If you don't want to sort the actual data, you can use argsort to return the index.

In the case of one dimension

Sorted data can be created by using the index obtained by argsort for x [index]. If you want to sort in descending order, you can invert the index.

In [14]: x = np.random.randint(0, 100, size=10)

In [15]: print x
[16 52 64 37 50 68  5 87 99 69]

In [16]: print np.argsort(x)
[6 0 3 4 1 2 5 9 7 8]

In [17]: print x[np.argsort(x)]
[ 5 16 37 50 52 64 68 69 87 99]

In [18]: print x[np.argsort(x)[::-1]]
[99 87 69 68 64 52 50 37 16  5]

For multidimensional

In the case of multiple dimensions, it is basically the same as one dimension. However, even if the obtained index is used as it is as x [index], it will not work, so It is good to pass it one by one and retrieve the data as shown below.


In [31]: x = np.random.randint(0, 100, size=(2,10))

In [32]: print x
[[31 72 21 18 47 33 88 12 92 19]
 [55 62 19 69 43 65 95 28 20 33]]

In [33]: index = np.argsort(x)

In [34]: print index
[[7 3 9 2 0 5 4 1 6 8]
 [2 8 7 9 4 0 1 5 3 6]]

In [35]: print np.array([x[0,index[0,:]],x[1,index[1,:]]])
[[12 18 19 21 31 33 47 72 88 92]
 [19 20 28 33 43 55 62 65 69 95]]

In [36]: print np.array([x[0,index[0,::-1]],x[1,index[1,::-1]]])
[[92 88 72 47 33 31 21 19 18 12]
 [95 69 65 62 55 43 33 28 20 19]]

transpose numpy.transpose(a, axes=None)

If you want transposed data, use transpose or T. The obtained data has an inverted axis. For example, in the case of 2D data A, the transposed TA is as follows. TA[x,y] = A[y,x] It is converted in the same way for 3 dimensions and above. TA[x,y,z] = A[z,y,x]

In [38]: x = np.array([[0,1,2]])

In [39]: print x
[[0 1 2]]

In [40]: print x.T
[[0]
 [1]
 [2]]

In [41]: x = np.array(range(4)).reshape(2,2)

In [42]: print x
[[0 1]
 [2 3]]

In [43]: print x.transpose()
[[0 2]
 [1 3]]

The following is the case of 3D, and you can see that the same value can be obtained by inverting the index and passing it.

In [44]: x = np.array(range(8)).reshape(2,2,2)

In [45]: xT = x.T

In [46]: print 'x', x
x [[[0 1]
  [2 3]]

 [[4 5]
  [6 7]]]

In [47]: print 'x.T', xT
x.T [[[0 4]
  [2 6]]

 [[1 5]
  [3 7]]]

In [48]: idx = np.array([[i//4,(i//2)%2,i%2] for i in range(8)])

In [49]: x[idx[:,0],idx[:,1],idx[:,2]]
Out[49]: array([0, 1, 2, 3, 4, 5, 6, 7])

In [50]: xT[idx[:,2],idx[:,1],idx[:,0]]
Out[50]: array([0, 1, 2, 3, 4, 5, 6, 7])

When specifying the order to sort

In transpose, you can specify the order of the axes to sort.

The following is an example when converting in the order of 2,0,1. This is effective when the image data of the Interleaved array that is lined up with RGBRGB is made into a Plane array like R, G, B.

In [53]: print x
[[[0 1]
  [2 3]]

 [[4 5]
  [6 7]]]

In [54]: print x.transpose(2,0,1)
[[[0 2]
  [4 6]]

 [[1 3]
  [5 7]]]
In [63]: I = sp.misc.imread('./data/SIDBA/Lenna.bmp')

In [64]: r, g, b = I.transpose(2,0,1)

In [65]: np.sum(np.abs(r - I[:,:,0])) + np.sum(np.abs(g - I[:,:,1])) + np.sum(np.abs(b - I[:,:,2]))
Out[65]: 0

transpose.png

import matplotlib.pyplot as plt
import matplotlib.cm as cm

fig, axes = plt.subplots(ncols=4, figsize=(12,8))
axes[0].set_title('color')
axes[0].imshow(I)
axes[0].get_xaxis().set_visible(False)
axes[0].get_yaxis().set_visible(False)

axes[1].set_title('R')
axes[1].imshow(r, cmap=cm.Greys_r, vmin=0,vmax=255)
axes[1].get_xaxis().set_visible(False)
axes[1].get_yaxis().set_visible(False)

axes[2].set_title('G')
axes[2].imshow(g, cmap=cm.Greys_r, vmin=0,vmax=255)
axes[2].get_xaxis().set_visible(False)
axes[2].get_yaxis().set_visible(False)

axes[3].set_title('B')
axes[3].imshow(b, cmap=cm.Greys_r, vmin=0,vmax=255)
axes[3].get_xaxis().set_visible(False)
axes[3].get_yaxis().set_visible(False)

roll numpy.roll(a, shift, axis=None) Rotate the elements of the array

In the case of one dimension

sign direction
+ forward
- backward
In [88]: x = np.arange(10)

In [89]: print x
[0 1 2 3 4 5 6 7 8 9]

In [90]: print np.roll(x, 1)
[9 0 1 2 3 4 5 6 7 8]

In [91]: print np.roll(x, -1)
[1 2 3 4 5 6 7 8 9 0]

For multidimensional

The axis can be selected to rotate. If nothing is specified, it will be flattened and then rotated to return to the original shape.

In [92]: x = np.arange(20).reshape(2,10)

In [93]: print x
[[ 0  1  2  3  4  5  6  7  8  9]
 [10 11 12 13 14 15 16 17 18 19]]

In [94]: print np.roll(x, 2)
[[18 19  0  1  2  3  4  5  6  7]
 [ 8  9 10 11 12 13 14 15 16 17]]

In [95]: print np.roll(x, 1, axis=0)
[[10 11 12 13 14 15 16 17 18 19]
 [ 0  1  2  3  4  5  6  7  8  9]]

In [96]: print np.roll(x, 1, axis=1)
[[ 9  0  1  2  3  4  5  6  7  8]
 [19 10 11 12 13 14 15 16 17 18]]

rollaxis numpy.rollaxis(a, axis, start=0)

Sort the size of each dimension of a ex.) If a is 2D and 3x4, then 4x3.

The movement is to move the axis to the start position.

It is difficult to understand intuitively, but if you break down the processing one by one, the processing will be as follows.

a. When the shape is 4 dimensions of (3,4,5,6) np.rollaxis(a, axis=3, start=1)

  1. axes = list(range(a.ndim)) ... [0,1,2,3]
  2. axes.remove(axis) ... [0,1,2]
  3. axes.insert(start, axis) ... [0,3,1,2]
  4. return a.transpose(axes) ... a.shape (3,6,4,5)

This is the process of taking out the size of the axis th dimension and putting it in the start th.

In [15]: x = np.arange(3*4*5*6).reshape(3,4,5,6)

In [16]: print x.shape
(3, 4, 5, 6)

In [17]: print np.rollaxis(x, 3, 1).shape
(3, 6, 4, 5)

a. When the shape is 4 dimensions of (3,4,5,6) np.rollaxis(a, axis=1, start=3)

  1. axes = list(range(a.ndim)) ... [0,1,2,3]
  2. axes.remove(axis) ... [0,2,3]
  3. axes.insert (start, axis) ... [0,2,1,3] ... Be careful where you insert
  4. return a.transpose(axes) ... a.shape (3,5,4,6)
print np.rollaxis(x, 1, 3).shape
(3, 5, 4, 6)

swapaxes numpy.swapaxes(a, axis1, axis2)

Swap axis1 and axis2

In [4]: x = np.arange(3*4).reshape(3,4)

In [5]: x
Out[5]: 
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])

In [6]: y = np.swapaxes(x,0,1)

In [7]: y
Out[7]: 
array([[ 0,  4,  8],
       [ 1,  5,  9],
       [ 2,  6, 10],
       [ 3,  7, 11]])

Recommended Posts

[Python] Sorting Numpy data
My Numpy (Python)
Data analysis python
#Python basics (#Numpy 2/2)
Python #Numpy basics
[Python] Numpy memo
[python] Read data
[Python] Various data processing using Numpy arrays
Python and numpy tips
Basic sorting in Python
Python Data Visualization Libraries
Data analysis using Python 0
Data analysis overview python
Data cleaning using Python
[Python] Search (NumPy) ABC165C
python numpy array calculation
Re: Human-powered sorting [Python]
Python data analysis template
[Python tutorial] Data structure
Data analysis with Python
Python Basic --Pandas, Numpy-
[Python] Create structured array (store heterogeneous data with NumPy)
Sample data created with python
My python data analysis container
Handle Ambient data in Python
Python / Numpy np.newaxis thinking tips
data structure python push pop
Artificial data generation with numpy
Convert numpy int64 to python int
Python for Data Analysis Chapter 4
[Python] Calculation method with numpy
Get Youtube data with python
Implemented SMO with Python + NumPy
Sorting image files with Python (2)
Data Science Cheat Sheet (Python)
[Python] Notes on data analysis
Techniques for sorting in Python
Matrix multiplication in python numpy
My python data analytics environment
Python application: data visualization # 2: matplotlib
Sorting image files with Python
Python data analysis learning notes
Create a python numpy array
Python data type summary memo
[Python] Plot time series data
Python for Data Analysis Chapter 2
Image data type conversion [Python]
[Python] Numpy reference, extraction, combination
Data analysis using python pandas
Python3 | Getting Started with numpy
python> tuple> data, address = s.recvfrom (10000)
Python for Data Analysis Chapter 3
Read json data with python
Get Leap Motion data in Python.
[Python] How to FFT mp3 data
Python: Exclude tags from html data
Python Application: Data Cleansing Part 1: Python Notation
Data acquisition using python googlemap api
Introduction to Python Numerical Library NumPy
Python
Read Protocol Buffers data in Python3