我正在使用sklearn进行机器学习工作。这里是我的两个变量:
>>> matrix<1397x9576 sparse matrix of type '<type 'numpy.float64'>' with 44655 stored elements in Compressed Sparse Row format>>>> type(density)<type 'list'>>>> len(density)1397
matrix
是通过 TfidfVectorizer.fit_transform()
生成的。我想通过添加变量 density
作为新列来扩展变量 matrix
。有什么方法可以实现吗?
回答:
使用 scipy hstack 来将列 density
与 matrix
堆叠
from scipy.sparse import hstacknew_matrix = hstack([matrix, density])