In [123]: from scipy import sparse
Создайте матрицу scipy.sparse:
In [124]: M = sparse.random(5,4,.2)
In [125]: M
Out[125]:
<5x4 sparse matrix of type '<class 'numpy.float64'>'
with 4 stored elements in COOrdinate format>
In [126]: print(M)
(0, 3) 0.006222105671732758
(1, 0) 0.7198559134274957
(2, 0) 0.3603986399431639
(4, 2) 0.9519927602284366
In [127]: M.A
Out[127]:
array([[0. , 0. , 0. , 0.00622211],
[0.71985591, 0. , 0. , 0. ],
[0.36039864, 0. , 0. , 0. ],
[0. , 0. , 0. , 0. ],
[0. , 0. , 0.95199276, 0. ]])
In [128]: type(M)
Out[128]: scipy.sparse.coo.coo_matrix
Попытка использовать hstack
:
In [129]: np.hstack([M, np.arange(5)[:,None]])
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-129-f06fc972039d> in <module>
----> 1 np.hstack([M, np.arange(5)[:,None]])
<__array_function__ internals> in hstack(*args, **kwargs)
/usr/local/lib/python3.6/dist-packages/numpy/core/shape_base.py in hstack(tup)
341 # As a special case, dimension 0 of 1-dimensional arrays is "horizontal"
342 if arrs and arrs[0].ndim == 1:
--> 343 return _nx.concatenate(arrs, 0)
344 else:
345 return _nx.concatenate(arrs, 1)
<__array_function__ internals> in concatenate(*args, **kwargs)
ValueError: all the input arrays must have same number of dimensions,
but the array at index 0 has 1 dimension(s) and the array at index 1
has 2 dimension(s)
Правильное использование sparse.hstack
:
In [130]: sparse.hstack([M, np.arange(5)[:,None]])
Out[130]:
<5x5 sparse matrix of type '<class 'numpy.float64'>'
with 8 stored elements in COOrdinate format>
In [131]: _.A
Out[131]:
array([[0. , 0. , 0. , 0.00622211, 0. ],
[0.71985591, 0. , 0. , 0. , 1. ],
[0.36039864, 0. , 0. , 0. , 2. ],
[0. , 0. , 0. , 0. , 3. ],
[0. , 0. , 0.95199276, 0. , 4. ]])
Если второй массив имеет форму (5,) вместо (5,1), я получу вашу последнюю ошибку:
In [132]: sparse.hstack([M, np.arange(5)])
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-132-defd4158f59e> in <module>
----> 1 sparse.hstack([M, np.arange(5)])
/usr/local/lib/python3.6/dist-packages/scipy/sparse/construct.py in hstack(blocks, format, dtype)
463
464 """
--> 465 return bmat([blocks], format=format, dtype=dtype)
466
467
/usr/local/lib/python3.6/dist-packages/scipy/sparse/construct.py in bmat(blocks, format, dtype)
584 exp=brow_lengths[i],
585 got=A.shape[0]))
--> 586 raise ValueError(msg)
587
588 if bcol_lengths[j] == 0:
ValueError: blocks[0,:] has incompatible row dimensions. Got blocks[0,1].shape[0] == 1, expected 5.