选择numpy数组,最后一个元素是1。

时间:2022-11-11 18:04:09

Suppose we have a dataset like this:

假设我们有这样的数据集:

X = 
6  2   1
-2 4  -1
4  1  -1
1  6   1
2  4   1
6  2   1

I would like to get two data from this one having last digit 1 and another having last digit -1.

我想从这个中得到两个数据一个是最后一个数字1另一个是最后一个数字-1。

X0 = 
-2 4  -1
4  1  -1

And,

而且,

X1 = 
6  2   1
1  6   1
2  4   1
6  2   1

How can we do this in numpy efficiently?

我们怎么能有效地做这个呢?

In simple python, I could do this like this:

在简单的python中,我可以这样做:

dataset = np.loadtxt('data.txt')

X0, X1 = [], []
for i in range(len(X)):
    if X[i][-1] == 1:
        X0.append(X[i])
    else:
        X1.append(X[i])

This is slow and cumbersome, Numpy is fast and easy so, I would appreciate if there is easier way in numpy. Thanks.

这是缓慢和笨重,Numpy是快速和容易,所以,如果有更容易的方法在Numpy。谢谢。

4 个解决方案

#1


1  

You may want this:

你可以这样:

import numpy as np
X = np.array([
       [ 6,  2,  1],
       [-2,  4, -1],
       [ 4,  1, -1],
       [ 1,  6,  1],
       [ 2,  4,  1],
       [ 6,  2,  1]
])

X0 = X[X[:, -1] == -1]
X1 = X[X[:, -1] == 1]

print(X0)

#2


2  

Suppose you have an array:

假设你有一个数组:

>>> arr
array([[ 6,  2,  1],
       [-2,  4, -1],
       [ 4,  1, -1],
       [ 1,  6,  1],
       [ 2,  4,  1],
       [ 6,  2,  1]])

Then simply:

然后简单的:

>>> mask1 = arr[:, -1] == 1
>>> mask2 = arr[:, -1] == -1
>>> X1 = arr[mask1]
>>> X2 = arr[mask2]

Results:

结果:

>>> X1
array([[6, 2, 1],
       [1, 6, 1],
       [2, 4, 1],
       [6, 2, 1]])
>>> X2
array([[-2,  4, -1],
       [ 4,  1, -1]])

#3


1  

You could just use numpy and use slicing to access your data e.g.:

你可以用numpy和slicing来访问你的数据,例如:

X[X[:, 2] == 1]  # Returns all rows where the third column equals 1

or as a complete example:

或者作为一个完整的例子:

import numpy as np

# Random data set
X = np.zeros((6, 3))
X[:3, 2] = 1
X[3:, 2] = -1
np.random.shuffle(X)

print(X[X[:, 2] == 1])
print('-')
print(X[X[:, 2] == -1])

#4


1  

import numpy as np
x = np.array(x)
x0 = x[np.where(a[:,2]==-1)]
x1 = x[np.where(a[:,2]==1)]

#1


1  

You may want this:

你可以这样:

import numpy as np
X = np.array([
       [ 6,  2,  1],
       [-2,  4, -1],
       [ 4,  1, -1],
       [ 1,  6,  1],
       [ 2,  4,  1],
       [ 6,  2,  1]
])

X0 = X[X[:, -1] == -1]
X1 = X[X[:, -1] == 1]

print(X0)

#2


2  

Suppose you have an array:

假设你有一个数组:

>>> arr
array([[ 6,  2,  1],
       [-2,  4, -1],
       [ 4,  1, -1],
       [ 1,  6,  1],
       [ 2,  4,  1],
       [ 6,  2,  1]])

Then simply:

然后简单的:

>>> mask1 = arr[:, -1] == 1
>>> mask2 = arr[:, -1] == -1
>>> X1 = arr[mask1]
>>> X2 = arr[mask2]

Results:

结果:

>>> X1
array([[6, 2, 1],
       [1, 6, 1],
       [2, 4, 1],
       [6, 2, 1]])
>>> X2
array([[-2,  4, -1],
       [ 4,  1, -1]])

#3


1  

You could just use numpy and use slicing to access your data e.g.:

你可以用numpy和slicing来访问你的数据,例如:

X[X[:, 2] == 1]  # Returns all rows where the third column equals 1

or as a complete example:

或者作为一个完整的例子:

import numpy as np

# Random data set
X = np.zeros((6, 3))
X[:3, 2] = 1
X[3:, 2] = -1
np.random.shuffle(X)

print(X[X[:, 2] == 1])
print('-')
print(X[X[:, 2] == -1])

#4


1  

import numpy as np
x = np.array(x)
x0 = x[np.where(a[:,2]==-1)]
x1 = x[np.where(a[:,2]==1)]