numpy合併矩陣操作ValueError: all the input arrays must have same number of dimensions

當使用numpy對稀疏矩陣進行合併時出現異常(對於密集(dense)矩陣沒有問題)

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

操作涉及到np.hstack((m1,m2)),np.column_stack((m1,m2)),np.concatenate([m1, m2], axis=1)等

解決方案來源於:http://www.it1352.com/239795.html

m1 = sparse.rand(10, 10000)
m2 = np.random.random((10, 1))
print 'm1 shape:', m1.shape
print 'm2 shape:', m2.shape
print 'Stacked shape:', np.hstack((m1,m2)).shape

異常出現

解決方案:

    X = sparse.rand(10, 10000)
    xt = np.random.random((10, 1))
    res = sparse.hstack((X,xt)).toarray()
    print('X shape:', X.shape)
    print('xt shape:', xt.shape)
    print('Stacked shape:', res.shape)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章