2017-07-19 13 views
0

私はバイグラムを使ってワードクラウドを生成しようとしています。私は上位30の差別的な言葉を生成することができますが、プロットしながら言葉を一緒に表示することはできません。私の言葉の雲のイメージは、ユニグラムの雲のように見えます。私は以下のスクリプトとsci-kitパッケージを使いました。Pythonを使ってnグラムのワードクラウドを作成する

def create_wordcloud(pipeline): 
""" 
Create word cloud with top 30 discriminative words for each category 
""" 

class_labels = numpy.array(['Arts','Music','News','Politics','Science','Sports','Technology']) 

feature_names =pipeline.named_steps['vectorizer'].get_feature_names() 
word_text=[] 

for i, class_label in enumerate(class_labels): 
    top30 = numpy.argsort(pipeline.named_steps['clf'].coef_[i])[-30:] 

    print("%s: %s" % (class_label," ".join(feature_names[j]+"," for j in top30))) 

    for j in top30: 
     word_text.append(feature_names[j]) 
    #print(word_text) 
    wordcloud1 = WordCloud(width = 800, height = 500, margin=10,random_state=3, collocations=True).generate(' '.join(word_text)) 

    # Save word cloud as .png file 
    # Image files are saved to the folder "classification_model" 
    wordcloud1.to_file(class_label+"_wordcloud.png") 

    # Plot wordcloud on console 
    plt.figure(figsize=(15,8)) 
    plt.imshow(wordcloud1, interpolation="bilinear") 
    plt.axis("off") 
    plt.show() 
    word_text=[] 

これは私のパイプラインコード

pipeline = Pipeline([ 

# SVM using TfidfVectorizer 
('vectorizer', TfidfVectorizer(max_features = 25000, ngram_range=(2, 2),sublinear_tf=True, max_df=0.95, min_df=2,stop_words=stop_words1)), 
('clf',  LinearSVC(loss='squared_hinge', penalty='l2', dual=False, tol=1e-3)) 
]) 

である。これらは

Arts: cosmetics businesspeople, television personality, reality television, television presenters, actors london, film producers, actresses television, indian film, set index, actresses actresses, television actors, century actors, births actors, television series, century actresses, actors television, stand comedian, television personalities, television actresses, comedian actor, stand comedians, film actresses, film actors, film directors 

答えて

0

「芸術」私はカテゴリになった機能の一部です私はあなたのn-への参加を何とか必要があると思いますスペース以外の記号を持つfeature_namesのグラム。たとえば、アンダースコアを提案します。 は今のところ、この部分は再びあなたのn-gramms別の単語を作る、私は思う:

' '.join(word_text) 

私はあなたがここにアンダースコアでスペースを代用する必要があると思う:

word_text.append(feature_names[j]) 

たが、これに変更します

word_text.append(feature_names[j].replace(' ', '_')) 
+0

動作しませんでした。すべての単語を(_)で置き換えます。 – VKB

+0

私は私の答えを編集しました。このようなことを試しましたか? – CrazyElf

+0

ありがとうございます。 – VKB

関連する問題