Reputation: 638
I am trying to concat two valid array via np.concat()
method.
My code:
print X_train.shape, train_names.shape
X_train = np.concatenate([train_names,X_train], axis=0)
The output:
(3545, 93355) (3545, 692)
ValueError Traceback (most recent call last)
<ipython-input-58-59dc66874663> in <module>()
1 print X_train.shape, train_names.shape
----> 2 X_train = np.concatenate([train_names,X_train], axis=0)
ValueError: zero-dimensional arrays cannot be concatenated
As you can see, the shapes of arrays align, still I am getting this weird error. Why?
EDIT: I have tried with axis=1
as well. Same result
EDIT 2: Eqauted data types using .astype(np.float64)
. Same result.
Upvotes: 6
Views: 11472
Reputation: 185
In my case, the problem was due to sparse nature of matrix returned by LabelEncoder()
so this line gave me an error:
np.append(airlineTrain, train_transformed,axis =1)
To fix it, I used this:
np.append(airlineTrain.toarray(), train_transformed.toarray(),axis =1 )
Alternately, you may be using NLTK, where the sparse storage of matrices can be converted by using todense()
Upvotes: 1
Reputation: 231395
Applying np.concatenate
to scipy
sparse
matrices produces this error:
In [162]: from scipy import sparse
In [163]: x=sparse.eye(3)
In [164]: x
Out[164]:
<3x3 sparse matrix of type '<class 'numpy.float64'>'
with 3 stored elements (1 diagonals) in DIAgonal format>
In [165]: np.concatenate((x,x))
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-165-0b67d0029ca6> in <module>()
----> 1 np.concatenate((x,x))
ValueError: zero-dimensional arrays cannot be concatenated
There are sparse
functions to do this:
In [168]: sparse.hstack((x,x)).A
Out[168]:
array([[ 1., 0., 0., 1., 0., 0.],
[ 0., 1., 0., 0., 1., 0.],
[ 0., 0., 1., 0., 0., 1.]])
In [169]: sparse.vstack((x,x)).A
Out[169]:
array([[ 1., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 1.],
[ 1., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 1.]])
Upvotes: 15
Reputation: 169
Pass the arrays as a tuple rather than a list.
X_train = np.concatenate((train_names,X_train), axis=0)
Upvotes: 1