将稀疏矩阵转换为表

问题描述 投票:0回答:1

我的数据集是这样的 然后我做了我的 tf_idf 矢量器tf idf code 并得到了这个tf idf result

但是当我想让它看起来像使用 pandas 的表格时,结果是 0 result when i try to transform with pandas

你们能帮我吗? :(

我尝试使用 toarray() 但结果全为零

python pandas sentiment-analysis
1个回答
0
投票

一旦处理稀疏矩阵,请使用 pandas from_spmatrix。

这是一个例子:

import pandas as pd
from sklearn.feature_extraction.text import TfidfVectorizer
corpus = ['This is the first document.','This document is the second     document.','And this is the third one.','Is this the first document?']
vectorizer = TfidfVectorizer()
X = vectorizer.fit_transform(corpus)
# Show the type of X
print(type(X))
# Convert to pandas dataframe
pd.DataFrame.sparse.from_spmatrix(X) 
© www.soinside.com 2019 - 2024. All rights reserved.