2016-01-04 138 views
0

加入两个python数组我在Python中遇到了问题。我从字典条目创建两个numpy数组。我想加入这两个numpy的阵列以特定的方式是这样的:通过索引

# create array with classes 
probVec = filePickle['classID'] 
a = np.empty([0, 1]) 

for x in np.nditer(probVec): 
    a = np.append(a,x) 

timeVec = filePickle['start'] 
timeVec = np.asarray(timeVec) 
b = np.empty([0, 1]) 

for x in np.nditer(timeVec): 
    b = np.append(b,x) 

# create input-vectors for clustering 
c = np.vstack((b,a)).transpose() 

现在,如果我想加入他们在一个更具体的方式,喜欢拍照的阵列“probVec”一起加入他们只有特定项目的数组“timeVec”像这样的对应条目:

for x in np.nditer(probVec): 
    if x == 3.0: 
     a = np.append(a,x) 

for x in np.nditer(timeVec): 
    b = append with x values that have the same indices as the ones appended in the first loop 

因为两个阵列含有对应于各其它数值它们具有相同的长度。所以我的目标是这样的:

probVec = [2.0, 1.0, 3.0, 3.0, 4.0, 3.0...] 

timeVec = [t1, t2, t3, t4, t5, t6...] 

c = [[3.0 t3] 
    [3.0 t4] 
    [3.0 t6] 
     . 
     . 
     . 
    ] 

我只是不知道什么是要认识到的最好方式。

回答

0

在数组上使用比较运算符(如a == 3.0),可以得到一个布尔数组,用于索引,选择条件为true的行。

In [87]: a = np.random.randint(low=1, high=4, size=10) # example data 

In [88]: a 
Out[88]: array([3, 1, 3, 1, 1, 3, 2, 2, 2, 2]) 

In [89]: b = np.arange(10) 

In [90]: c = np.column_stack((a, b)) 

In [91]: c[a == 3] 
Out[91]: 
array([[3, 0], 
     [3, 2], 
     [3, 5]]) 
+0

完全没有把戏。对不起,迟到接受! – Grmrk