Extracting Specific Columns in Numpy Array

Extracting specific columns in numpy array

I assume you wanted columns 1 and 9?

To select multiple columns at once, use

X = data[:, [1, 9]]

To select one at a time, use

x, y = data[:, 1], data[:, 9]

With names:

data[:, ['Column Name1','Column Name2']]

You can get the names from data.dtype.names

How to extract slices and specific columns of a numpy array with one command?

As ombk suggested, you can use r_.
It is a perfect tool to concatenate slice expressions.

In your case:

A[:, np.r_[0:3, 4]]

retrieves the intended part of your array.

Just the same way you can concatenate more slice expressions.

Extract Specific RANGE of columns in numpy array Python

You can just use e[:, 1:5] to retrive what you want.

In [1]: import numpy as np

In [2]: e = np.array([[ 0, 1, 2, 3, 5, 6, 7, 8],
...: [ 4, 5, 6, 7, 5, 3, 2, 5],
...: [ 8, 9, 10, 11, 4, 5, 3, 5]])

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

https://docs.scipy.org/doc/numpy/reference/arrays.indexing.html

How to select specific columns in numpy array?

One way to get an R-like syntax here would be to use np.r_:

>>> Z = np.arange(2000).reshape(20, 100)
>>> Z.shape
(20, 100)
>>> x = Z[:,np.r_[:49,50:100]]
>>> x.shape
(20, 99)
>>> x[0,48:52]
array([48, 50, 51, 52])

and we see that the 50th column (with number 49) is missing from x.

Extracting data from specific columns of numpy array for each row

Since you want to sequentially index the rows of A, you can index with np.arange(len(A)) in addition to b to get your desired output:

A[np.arange(len(A)), b]

# array([35, 21, 21, 17, 18, 35])

Showing how this works:


# A np.arange(len(A)) b
array([[35, 2, 23, 22], [0, 0]
[44, 21, 15, 4], [1, 1]
[44, 21, 15, 4], [2, 1]
[37, 4, 17, 41], [3, 2]
[33, 4, 4, 18], [4, 3]
[35, 2, 23, 22]]) [5, 0]

Extracting specific columns in numpy array by condition

In Python, the expression -0.4 < x_y_z[2] < 0.1 is roughly equivalent to -0.4 < x_y_z[2] and x_y_z[2] < 0.1. The and operator decides the truth value of each part of the expression by converting it into a bool. Unlike Python lists and tuples, numpy arrays do not support the conversion.

The correct way to specify the condition is with bitwise & (which is unambiguous and non-short-circuiting), rather than the implicit and (which short circuits and is ambiguous in this case):

condition = ((x_y_z[2, :] > - 0.4) & (x_y_z[2, :] < 0.1))

condition is a boolean mask that selects the columns you want. You can select the rows with a simple slice:

selection = x_y_z[:, condition] 

Selecting specific rows and columns from NumPy array

Fancy indexing requires you to provide all indices for each dimension. You are providing 3 indices for the first one, and only 2 for the second one, hence the error. You want to do something like this:

>>> a[[[0, 0], [1, 1], [3, 3]], [[0,2], [0,2], [0, 2]]]
array([[ 0, 2],
[ 4, 6],
[12, 14]])

That is of course a pain to write, so you can let broadcasting help you:

>>> a[[[0], [1], [3]], [0, 2]]
array([[ 0, 2],
[ 4, 6],
[12, 14]])

This is much simpler to do if you index with arrays, not lists:

>>> row_idx = np.array([0, 1, 3])
>>> col_idx = np.array([0, 2])
>>> a[row_idx[:, None], col_idx]
array([[ 0, 2],
[ 4, 6],
[12, 14]])

Extracting multiple sets of rows/ columns from a 2D numpy array

IIUC, you can use numpy.r_ to generate the indices from the slice:

img[np.r_[0,2:4][:,None],2] 

output:

array([[ 2],
[12],
[17]])

intermediates:

np.r_[0,2:4]
# array([0, 2, 3])

np.r_[0,2:4][:,None] # variant: np.c_[np.r_[0,2:4]]
# array([[0],
# [2],
# [3]])


Related Topics



Leave a reply



Submit