2015-03-02 67 views
0

我试图使用numpy(和numpy)使用cutoffs中的边来分割矢量y。将yg定义为列向量,但将a作为行向量返回。运行g += a.transpose()通过引入100x100阵列生成形状一致性错误(复制如下)。必须有一个更加优雅的方式来完成这个任务。谢谢。将矢量值分配给numpy中的段

y = np.random.uniform(0,1,100) 
cutoffs = np.random.uniform(0,1,3) 
cutoffs.sort() 
g = np.zeros(y.size) 
for c in np.hstack([ cutoffs , 1. ]): 
    a = np.array([ y < c ]) 
    g += a.transpose() 


Traceback (most recent call last): 
    File "<stdin>", line 3, in <module> 
ValueError: non-broadcastable output operand with shape (100,) doesn't match the broadcast shape (100,100) 
+0

你到底要完成什么? ...关于错误,您需要修改代码如下... – plonser 2015-03-02 17:24:34

+0

感谢您的答案。目标是将段分配给'y',然后'将'函数映射到这些段的每一个,类似于R中的'tapply()'。 – user2105469 2015-03-02 18:15:59

回答

1

使用

a = (y < c).reshape(-1) 
g += a