python-3.x - 如何将包含稀疏矩阵的 nd 数组拟合到 sklearn
问题描述
首先,我在我的数据框中的文本列上应用 TfidfVectorization 之后我使用 hsatck 合并它们
train_gene_var_tfidf =
hstack((train_gene_feature_tfidf,train_variation_feature_tfidf))
test_gene_var_tfidf =
hstack((test_gene_feature_tfidf,test_variation_feature_tfidf))
cv_gene_var_tfidf =
hstack((cv_gene_feature_tfidf,cv_variation_feature_tfidf))
# Adding the train_text feature
train_x_tfidf = hstack((train_gene_var_tfidf, train_text))
train_x_tfidf = hstack((train_x_tfidf, train_text_feature_tfidf))
train_y = np.array(list(train_df['Class']))
结果我得到了一个包含稀疏矩阵的 numpy ndarray 我想将我的函数放入 sklearn .fit() 中,如下所示
clf = SGDClassifier(class_weight='balanced',alpha=i,penalty='l2', loss='log', random_state=42)
clf.fit(train_x_tfidf, train_y)
形状如下:
print(train_x_tfidf.shape)
print(train_y.shape)
>>>(4,)
>>>(2124,)
print(train_x_tfidf)
>>>[<2124x227 sparse matrix of type '<class 'numpy.float64'>'
with 2124 stored elements in Compressed Sparse Row format>
<2124x1000 sparse matrix of type '<class 'numpy.float64'>'
with 1259 stored elements in Compressed Sparse Row format>
<2124x1000 sparse matrix of type '<class 'numpy.float64'>'
with 31088 stored elements in Compressed Sparse Row format>
<2124x1000 sparse matrix of type '<class 'numpy.float64'>'
with 1129081 stored elements in Compressed Sparse Column format>]
我在尝试适应时遇到的错误是
ValueError:使用序列设置数组元素。
如何将此 ndarray 拟合到 sklearn 拟合函数中?
解决方案
推荐阅读
- node.js - 保持节点在后台运行的最佳方法是什么 - nohup vs. forever vs. pm2?
- html - Pikaday 和 moment.js
- servlets - 如何通过 URL 指定 servlet 输入数据:如 http://example.com/servlet/ReverseServlet "Reverse Me"
- c# - 无法在服务器上检索上传的 Excel 文档的上次保存时间
- javascript - 在 React Native FlatList 中按下组件时如何更改组件的颜色
- django - 如何将查找转换为要在 `QuerySet.annotate` 中使用的表达式?
- javascript - 使用“createElement”后如何激活类点击功能
- symfony - EasyAdmin 2.0.* 和 Symfony 3.4 作曲家错误
- ios - 电话返回后从 notificationCenter 获取通知
- c# - Visual Studio 在某些设计器上崩溃