2017-05-09 85 views
0

我有一列数组包含相同的行,但不同的列。 我打印出数组的形状并检查它们是否有相同的行。Numpy连接为相同的行但列不同的数组

print ("Type x_test : actual",type(x_dump),x_dump.shape, type(actual), actual.shape, pred.shape) 
cmp = np.concatenate([x_test,actual,pred],axis = 1) 

('Type x_test : actual', <type 'numpy.ndarray'>, (2420L, 4719L), <type 'numpy.ndarray'>, (2420L,), (2420L,)) 

这给了我一个错误:

ValueError: all the input arrays must have same number of dimensions 

我试图复制使用下面的命令,这个错误:

x.shape,x1.shape,x2.shape 
Out[772]: ((3L, 1L), (3L, 4L), (3L, 1L)) 

np.concatenate([x,x1,x2],axis=1) 
Out[764]: 
array([[ 0, 0, 1, 2, 3, 0], 
     [ 1, 4, 5, 6, 7, 1], 
     [ 2, 8, 9, 10, 11, 2]]) 

我没有在这里得到任何错误。有没有人面临类似的问题?

编辑1:在写完这个问题后,我发现尺寸是不同的。 @Gareth Rees:精美地解释了numpy数组(R,1)和(R,)here之间的差别。

上的使用:

# Reshape and concatenate 
actual = actual.reshape(len(actual),1) 
pred = pred.reshape(len(pred),1) 

编辑2:标记关闭这个答案的Difference between numpy.array shape (R, 1) and (R,)重复。

+0

这是什么问题?结果似乎是正确的。你能更准确地知道你想要做什么吗? – fonfonx

+0

@fonfonx我的第一个命令是给出错误。形状也是(2420L,)而不是(2420L,1L)。即使我使用x_dump.reshape(2420,1)它不起作用。为什么col值在x_dump.shape中为空 – Tammy

+1

“形状也是(2420L,)而不是(2420L,1L)” - 这是你的问题。你需要重塑形状为'(2420L,)'的物体以具有形状'(2420L,1)'。 – Eric

回答

-2

编辑

发布后,OP发现错误。除非需要看到带有(R,1)和(R,)形状的结构,否则这可以被忽略。无论如何,这将会给选民的练习空间。

ORIGINAL

鉴于你的形状,答案是正确的。

a = np.arange(3).reshape(3,1) 

b = np.arange(12).reshape(3,4) 

c = np.arange(3).reshape(3,1) 

np.concatenate([a, b, c], axis=1) 
Out[4]: 
array([[ 0, 0, 1, 2, 3, 0], 
     [ 1, 4, 5, 6, 7, 1], 
     [ 2, 8, 9, 10, 11, 2]]) 

a 
Out[5]: 
array([[0], 
     [1], 
     [2]]) 

b 
Out[6]: 
array([[ 0, 1, 2, 3], 
     [ 4, 5, 6, 7], 
     [ 8, 9, 10, 11]]) 

c 
Out[7]: 
array([[0], 
     [1], 
     [2]]) 
相关问题