【发布时间】:2017-06-15 03:08:11
【问题描述】:
我想在我的模型中使用 X 变量的其他派生值。
XAll = pd_data[['title','wordcount','sumscores','length']]
y = pd_data['sentiment']
X_train, X_test, y_train, y_test = train_test_split(XAll, y, random_state=1)
当我处理标题中的文本数据时,我首先将其单独转换为 dtm:
vect = CountVectorizer(max_df=0.5)
vect.fit(X_train['title'])
X_train_dtm = vect.transform(X_train['title'])
column_index = X_train_dtm.indices
print(type(X_train_dtm)) # This is <class 'scipy.sparse.csr.csr_matrix'>
print("X_train_dtm shape",X_train_dtm.get_shape()) # This is (856, 2016)
print("column index:",column_index) # This is column index: [ 533 754 859 ..., 633 950 1339]
现在我将文本作为文档术语矩阵,我想将“wordcount”、“sumscores”、“length”等其他数字特征添加到 X_train_dtm。这我将使用新的 dtm 创建模型,因此会更准确,因为我会插入附加特征。
如何将 pandas 数据帧的其他数字列添加到稀疏 csr 矩阵?
【问题讨论】:
标签: python pandas scikit-learn sklearn-pandas