LoginSignup
2
4

More than 1 year has passed since last update.

Python nlplot で共起ネットワーク表示

Posted at

Pythonのnlplotパッケージで共起ネットワークを表示しました。
例えば「木下ふみこ」と検索したTwitter結果を取得してnlplotで表示すると以下のような図が出てきます(前処理をいくつか加えているので、単純にやっても以下のとおりにはならないです)。
image.png

環境

Python/パッケージ Version
Python 3.9.13
nlplot 1.4.0
pandas 1.4.3

Python Script

コードはシンプルでこんな感じ。save=Trueにしたことで、HTMLファイルとして保存されます。

import pandas as pd
import nlplot

df = pd.read_table('test.txt', names=['tokens'])

# target_col as a list type or a string separated by a space.
npt = nlplot.NLPlot(df, target_col='tokens')
stopwords = npt.get_stopword(top_n=15, min_freq=2)
npt.build_graph(stopwords=stopwords, min_edge_frequency=5)
npt.co_network(title='Co-occurrence network', width=600, height=500, save=True)

読込ファイルの中身は分かち書き済みのテキスト(スペース区切り)です。

test.txt(一部抜粋)
建築 エコノミスト 肩書き 自称 人物 小池 都政 当時 主張 豊洲 デマ メディア 共犯 関係 政局 都議選 問題 こと 小池 新党 こと 都民 ファースト 圧勝 ちなみ
こと 木下 ふみこ
写真 右 木下 ふみこ 笑
無免許 ひき逃げ 木下 ふみこ 板橋 選出 都 比較 申し訳 木下 違い 木下 板橋区 地元 愛 松田 都 板橋区 板橋 区民 松田 都 本当 前
都議会 ホームページ 死亡 退職 お知らせ 木下 ふみこ 辞職 荒木 ちはる 退職時 西山 議員 紹介 ページ 現員 会派 議員 西山 議員 分
木下 ふみこ 都民 白紙 投票
A 経済産業省 金融 課 Q 昨日 電話 議会 今 電話 A Q 崖 上 A ポニョ ポニョ 会話 結局 議員
2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4