简体   繁体   中英

how to save Sklearn lda model output to csv?

How to save Sklearn LDA model output to csv ? it does not have show_topics command as genism lDA model.

def selected_topics(model, vectorizer, top_n=10):
for idx, topic in enumerate(model.components_):
    print("Topic %d:" % (idx))
    print([(vectorizer.get_feature_names()[i], topic[i])
                    for i in topic.argsort()[:-top_n - 1:-1]])

This is good for print but how to save these results to csv?

def selected_topics(model, vectorizer, top_n=10):
    results={}
    for idx, topic in enumerate(model.components_):
        topicId='Topic'+str(idx)
        print("Topic %d:" % (idx))
        topic_name = " ".join([(vectorizer.get_feature_names()[i]
                    for i in topic.argsort()[:-top_n - 1:-1]])
        results[topicId]=topic_name
    return results

you can Write the results to a Json and then CSV file

To Json

import json,csv
results = selected_topics(model, vectorizer, top_n=10)
res_file = open(outputFile,'w')
res_file.write(json.dumps(results))
res_file.close()

Json to csv

input = open(res_file)
data = json.load(input)
input.close()

output = csv.writer("output_csv.csv")

output.writerow(data[0].keys())  # header row

for item in data:
    output.writerow(item.values())

Let me know if this doesn't help you

I found a solution on my own .Running the loop works for me .

 def show_topics(vectorizer=vectorizer, lda_model=lda, n_words=20):
    keywords = np.array(vectorizer.get_feature_names())
    topic_keywords = []
    for topic_weights in lda_model.components_:
        top_keyword_locs = (-topic_weights).argsort()[:n_words]
        topic_keywords.append(keywords.take(top_keyword_locs))
    return topic_keywords

topic_keywords = show_topics(vectorizer=vectorizer, lda_model=lda, n_words=15)        

# Topic - Keywords Dataframe
df_topic_keywords = pd.DataFrame(topic_keywords)
df_topic_keywords.columns = ['Word '+str(i) for i in range(df_topic_keywords.shape[1])]
df_topic_keywords.index = ['Topic '+str(i) for i in range(df_topic_keywords.shape[0])]
df_topic_keywords

You can export the results by first creating a pandas dataframe and saving the LDA model results to that dataframe(through a loop). Later on exporting it to a csv file.

import pandas as pd
import csv
pd.DataFrame(savedresults).to_csv("all_model_ouput.csv") 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM