当前位置:   article > 正文

TF-IDF文本向量化_将文本转换为tf-idf向量

将文本转换为tf-idf向量
import pandas as pd
from sklearn.feature_extraction.text import TfidfVectorizer

# 读取处理后的数据
data = pd.read_excel('cleaned_Laos_news.xlsx')

# 创建一个TF-IDF向量化器
tfidf_vectorizer = TfidfVectorizer(max_features=1000)  # 选择前1000个最重要的词汇

# 将清洗后的文本进行向量化
tfidf_matrix = tfidf_vectorizer.fit_transform(data['cleaned_content'])

# 将TF-IDF矩阵转换为DataFrame
tfidf_df = pd.DataFrame(tfidf_matrix.toarray(), columns=tfidf_vectorizer.get_feature_names_out())

# 将TF-IDF特征保存到新的Excel文件
tfidf_output_path = 'tfidf_features.xlsx'
tfidf_df.to_excel(tfidf_output_path, index=False)
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/350082
推荐阅读
相关标签
  

闽ICP备14008679号