我有一个scipy稀疏矩阵,例如:
import scipy as sp
from scipy import sparse
X = sparse.csr_matrix(np.random.randint(0, 10, (100, 10)))
我需要在此矩阵中添加K
行。应该从原始矩阵中的同一列中获取这些新行的每一列。
所以举个例子。期望的结果应该是:
Z = np.concat(X, X_sampled, axis=0)
其中X_sampled[:,i] = np.random.choice(X[:,i], k)
如果不移动到密集矩阵,我怎么能这样做?
编辑:密集阵列的示例
import numpy as np
import scipy as sp
k = 20
X = np.random.randint(0, 10, (100, 10))
X2 = np.zeros(shape=(k, X.shape[1]))
for col_id in range(X.shape[1]):
X2[:, col_id] = np.random.choice(X[:, col_id], k)
res = np.concatenate([X, X2])