Various methods for retrieving columns in a two-dimensional array.
import
python
import numpy as np
First, generate a 5x4 array appropriately
python
a = np.arange(5*4).reshape([5, 4])
print(a)
# [[ 0 1 2 3]
# [ 4 5 6 7]
# [ 8 9 10 11]
# [12 13 14 15]
# [16 17 18 19]]
python
#2nd row only
b = a[:, 2]
print(b)
# [ 2 6 10 14 18]
If ʻa [row, column]is specified, the row is specified in the
row part and the column is specified in the
columnpart. Since we want to retrieve columns this time, the
row part is always
: . By writing only
: , it means that no particular row is specified. In other words, ʻa [:, 2]
Means, "From all rows, only elements with an index of 2".
Extract multiple consecutive columns, such as the 1st, 2nd, and 0th to 2nd columns.
python
# 1,2nd row
b = a[:, 1:3]
print(b)
# [[ 1 2]
# [ 5 6]
# [ 9 10]
# [13 14]
# [17 18]]
Since 1: 3
means 1 or more and less than 3, the 1st and 2nd columns can be extracted.
If you specify like 1:
or : 3
, you can get all columns above and below the first column.
python
#1st row~
b = a[:, 1:]
print(b)
# [[ 1 2 3]
# [ 5 6 7]
# [ 9 10 11]
# [13 14 15]
# [17 18 19]]
# 0~2nd row
b = a[:, :3]
print(b)
# [[ 0 1 2]
# [ 4 5 6]
# [ 8 9 10]
# [12 13 14]
# [16 17 18]]
python
#Generate a 4x8 array
a = np.arange(4*8).reshape([4, 8])
# [[ 0 1 2 3 4 5 6 7]
# [ 8 9 10 11 12 13 14 15]
# [16 17 18 19 20 21 22 23]
# [24 25 26 27 28 29 30 31]]
#Even columns
b = a[:, ::2]
print(b)
# [[ 0 2 4 6]
# [ 8 10 12 14]
# [16 18 20 22]
# [24 26 28 30]]
#Odd column
b = a[:, 1::2]
print(b)
# [[ 1 3 5 7]
# [ 9 11 13 15]
# [17 19 21 23]
# [25 27 29 31]]
In this way, since it basically follows the operation method of the list, it is possible to obtain it in multiples or in reverse order.
python
#Columns that are multiples of 3 from the back
b = a[:, ::-3]
print(b)
# [[ 7 4 1]
# [15 12 9]
# [23 20 17]
# [31 28 25]]
#Skip one row from the first row to less than the seventh row
b = a[:, 1:7:2]
print(b)
# [[ 1 3 5]
# [ 9 11 13]
# [17 19 21]
# [25 27 29]]
Personally, the main subject is from here. There are times when you want to retrieve columns irregularly, such as columns 1, 5, and 6. It takes a lot of work, but it can be achieved by preparing a filter with the same length as the number of columns.
python
#Index 1, 5,The value of 6 is True,Other than that, a list of False lengths 8
f = [False, True, False, False, False, True, True, False]
b = a[:, f]
print(b)
# [[ 1 5 6]
# [ 9 13 14]
# [17 21 22]
# [25 29 30]]
#Generate a boolean 1x8 zero matrix,A pattern that changes only the index value of the desired column to True
f = np.zeros(8, dtype=bool)
columns = [1, 5, 6]
f[columns] = True
b = a[:, f]
print(b)
# [[ 1 5 6]
# [ 9 13 14]
# [17 21 22]
# [25 29 30]]
#If you want to get other than the specified column, generate an array of all True with ones,Change only unnecessary columns to False
f = np.ones(8, dtype=bool)
columns = [1, 5, 6]
f[columns] = False
b = a[:, f]
print(b)
# [[ 0 2 3 4 7]
# [ 8 10 11 12 15]
# [16 18 19 20 23]
# [24 26 27 28 31]]
I think that most of the arrays handled by ~~ numpy have a large number of columns, so I think the second method will be the main one.
The example uses np.zeros
, but in some situations it may be easier to create an array of np.ones
with all the elements True
and then change only the columns you don't need to False
. . ~~
np.zeros
, so I'll show it below.python
#Generate a 4x8 array
a = np.arange(4*8).reshape([4, 8])
# [[ 0 1 2 3 4 5 6 7]
# [ 8 9 10 11 12 13 14 15]
# [16 17 18 19 20 21 22 23]
# [24 25 26 27 28 29 30 31]]
# 1, 5,Take out 6 rows
b = a[:, [1, 5, 6]]
print(b)
# [[ 1 5 6]
# [ 9 13 14]
# [17 21 22]
# [25 29 30]]
I thought that it would be difficult to understand even if the story was expanded too much, so this time I focused on columns, but it is also possible to extract a specific column of a specific row by specifying a row as well as a column. In the case of 3D or more By applying these methods, you can extract the desired elements.
The method of creating a zero matrix and extracting the columns, which I explained at the very end, seems to have been pretty clean for me, but I think there may be other easier methods ~~.
I added it because @nkay told me a smarter way to retrieve multiple columns in the comments. (July 4, 2020)
Recommended Posts