gpt4 book ai didi

python - 用 pandas 将输出写入另一个文件中

转载 作者:行者123 更新时间:2023-12-01 01:42:00 26 4
gpt4 key购买 nike

我正在使用 LDA 来查找文本中的主题。

import pandas
import numpy as np
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.decomposition import LatentDirichletAllocation

n_components = 5
n_top_words = 10


def print_top_words(model, feature_names, n_top_words):
for topic_idx, topic in enumerate(model.components_):
message = "Topic %d: " % topic_idx
message += " ".join([feature_names[i]
for i in topic.argsort()[:-n_top_words - 1:-1]])
print(message)
print()

df = pandas.read_csv('text.csv', encoding = 'utf-8')
text = df['a']
data_samples = text.values.tolist()

# Use tf (raw term count) features for LDA.
tf_vectorizer = CountVectorizer()
tf = tf_vectorizer.fit_transform(data_samples)


lda = LatentDirichletAllocation(n_components=n_components, max_iter=5,
learning_method='online',
learning_offset=50.,
random_state=0)
lda.fit(tf)

print("\nTopics in LDA model:")
tf_feature_names = tf_vectorizer.get_feature_names()
print_top_words(lda, tf_feature_names, n_top_words)

我有一个很好的输出:

Topics in LDA model:

Topic 0: order not produced well received advance return always wishes

Topic 1: then wood color between pay broken transfer change arrival bad

Topic 2: delivery product possible package store advance date broken very good

Topic 3: misleading product france model broken open book year research association

Topic 4: address delivery change invoice deliver missing please billing advance change

但我希望用 pandas 将此输出写入 csv 文件中。

Topic 0   Topic 1   Topic 2   ...
order advance ... ...
not return ... ...
produced always ... ...
well wishes ... ...
received hello ... ...

可能吗?

最佳答案

def print_top_words(model, feature_names, n_top_words):
out_list = []
for topic_idx, topic in enumerate(model.components_):
message = "Topic%d: " % topic_idx
message += " ".join([feature_names[i]
for i in topic.argsort()[:-n_top_words - 1:-1]])
out_list.append(message.split())
print(message)
print()
return outlist
...
df_ = print_top_words(lda, tf_feature_names, n_top_words)
df_ = pd.DataFrame(df_).T
df_.to_csv('filename.csv')

关于python - 用 pandas 将输出写入另一个文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51784072/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com