Scipy hstack results in “TypeError: no supported conversion for types: (dtype('float64'), dtype('O')

 X =  sp.sparse.hstack((X, OtherColumn.values)) #error here
  sc = preprocessing.StandardScaler().fit(X)
  X = sc.transform(X)
  X_test = sc.transform(X_test)

Full error message:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-13-79d1e70bc1bc> in <module>()
---> 47 X =  sp.sparse.hstack((X, OtherColumn.values))
     48 sc = preprocessing.StandardScaler().fit(X)
     49 X = sc.transform(X)

C:\Users\Simon\Anaconda\lib\site-packages\scipy\sparse\construct.pyc in hstack(blocks, format, dtype)
    421 
    422     """
--> 423     return bmat([blocks], format=format, dtype=dtype)
    424 
    425 

C:\Users\Simon\Anaconda\lib\site-packages\scipy\sparse\construct.pyc in bmat(blocks, format, dtype)
    537     nnz = sum([A.nnz for A in blocks[block_mask]])
    538     if dtype is None:
--> 539         dtype = upcast(*tuple([A.dtype for A in blocks[block_mask]]))
    540 
    541     row_offsets = np.concatenate(([0], np.cumsum(brow_lengths)))

C:\Users\Simon\Anaconda\lib\site-packages\scipy\sparse\sputils.pyc in upcast(*args)
     58             return t
     59 
---> 60     raise TypeError('no supported conversion for types: %r' % (args,))
     61 
     62 

TypeError: no supported conversion for types: (dtype('float64'), dtype('O'))

修改方法如下:

sparse.hstack(X, A) # error
sparse.hstack(X.astype(object), A) # cast X to object; return object
sparse.hstack(X, A.astype(float)) # cast A to float; return float
hstack(X.A, A) # make X dense, result will be type object


阅读更多

没有更多推荐了,返回首页