Created
December 15, 2013 04:49
-
-
Save katryo/7969030 to your computer and use it in GitHub Desktop.
Python3.3でナイーブベイズを実装する ref: http://qiita.com/katryo/items/6a2266ffafb7efa9a46c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def score(self, words, category): | |
# logを取るのは、word_probが0.000....01くらいの小数になったりするため | |
score = math.log(self.prior_prob(category)) | |
for word in words: | |
score += math.log(self.word_prob(word, category)) | |
return score | |
# logを取らないと値が小さすぎてunderflowするかも。 | |
def score_without_log(self, words, category): | |
score = self.prior_prob(category) | |
for word in words: | |
score *= self.word_prob(word, category) | |
return score |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
グイド・ヴァンロッサムが作ったオープンソース => 推定カテゴリ: Python | |
Pythonカテゴリである確率: 0.27307692307692305 | |
Rubyカテゴリである確率: 0.2662866449511401 | |
プログラミング言語のRubyは純粋なオブジェクト指向言語です. => 推定カテゴリ: Ruby | |
Rubyカテゴリである確率: 0.3346905537459284 | |
Pythonカテゴリである確率: 0.3371794871794871 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
グイド・ヴァンロッサムが作ったオープンソース => 推定カテゴリ: Python | |
Pythonカテゴリである確率: 5.05740150710565e-08 | |
Rubyカテゴリである確率: 2.592066486298008e-08 | |
プログラミング言語のRubyは純粋なオブジェクト指向言語です. => 推定カテゴリ: Ruby | |
Rubyカテゴリである確率: 1.0568043348436783e-20 | |
Pythonカテゴリである確率: 1.4013428667584096e-21 | |
コランダム => 推定カテゴリ: Gem | |
Gemカテゴリである確率: 0.0018248175182481751 | |
Rubyカテゴリである確率: 0.0008143322475570033 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if __name__ == '__main__': | |
nb = NaiveBayes() | |
nb.train('''Python(パイソン)は,オランダ人のグイド・ヴァンロッサムが作ったオープンソースのプログラミング言語。 | |
オブジェクト指向スクリプト言語の一種であり,Perlとともに欧米で広く普及している。イギリスのテレビ局 BBC が製作したコメディ番組『空飛ぶモンティパイソン』にちなんで名付けられた。 | |
Python は英語で爬虫類のニシキヘビの意味で,Python言語のマスコットやアイコンとして使われることがある。Pythonは汎用の高水準言語である。プログラマの生産性とコードの信頼性を重視して設計されており,核となるシンタックスおよびセマンティクスは必要最小限に抑えられている反面,利便性の高い大規模な標準ライブラリを備えている。 | |
Unicode による文字列操作をサポートしており,日本語処理も標準で可能である。多くのプラットフォームをサポートしており(動作するプラットフォーム),また,豊富なドキュメント,豊富なライブラリがあることから,産業界でも利用が増えつつある。 | |
''', | |
'Python') | |
nb.train('''ヘビ(蛇)は、爬虫綱有鱗目ヘビ亜目(Serpentes)に分類される爬虫類の総称。 | |
体が細長く、四肢がないのが特徴。ただし、同様の形の動物は他群にも存在する。 | |
''', 'Snake') | |
nb.train('''Ruby(ルビー)は,まつもとゆきひろ(通称Matz)により開発されたオブジェクト指向スクリプト言語であり, | |
従来 Perlなどのスクリプト言語が用いられてきた領域でのオブジェクト指向プログラミングを実現する。 | |
Rubyは当初1993年2月24日に生まれ, 1995年12月にfj上で発表された。 | |
名称のRubyは,プログラミング言語Perlが6月の誕生石であるPearl(真珠)と同じ発音をすることから, | |
まつもとの同僚の誕生石(7月)のルビーを取って名付けられた。 | |
''', | |
'Ruby') | |
nb.train('''ルビー(英: Ruby、紅玉)は、コランダム(鋼玉)の変種である。赤色が特徴的な宝石である。 | |
天然ルビーは産地がアジアに偏っていて欧米では採れないうえに、 | |
産地においても宝石にできる美しい石が採れる場所は極めて限定されており、 | |
3カラットを超える大きな石は産出量も少ない。 | |
''', 'Gem') | |
doc = 'グイド・ヴァンロッサムが作ったオープンソース' | |
print('%s => 推定カテゴリ: %s' % (doc, nb.classify(doc))) # 推定カテゴリ: Pythonになるはず | |
print('Pythonカテゴリである確率: %s' % nb.score_without_log(['グイド・ヴァンロッサム', 'が', '作った'], 'Python')) | |
print('Rubyカテゴリである確率: %s' % nb.score_without_log(['グイド・ヴァンロッサム', 'が', '作った'], 'Ruby')) | |
doc = 'プログラミング言語のRubyは純粋なオブジェクト指向言語です.' | |
print('%s => 推定カテゴリ: %s' % (doc, nb.classify(doc))) # 推定カテゴリ: Rubyになるはず | |
print('Rubyカテゴリである確率: %s' % nb.score_without_log(['プログラミング言語', 'の', 'Ruby', 'は', '純粋', 'な', 'オブジェクト指向言語', 'です', '。'], 'Ruby')) | |
print('Pythonカテゴリである確率: %s' % nb.score_without_log(['プログラミング言語', 'の', 'Ruby', 'は', '純粋', 'な', 'オブジェクト指向言語', 'です', '。'], 'Python')) | |
doc = 'コランダム' | |
print('%s => 推定カテゴリ: %s' % (doc, nb.classify(doc))) # 推定カテゴリ: Gemになるはず | |
print('Gemカテゴリである確率: %s' % nb.score_without_log(['コランダム'], 'Gem')) | |
print('Rubyカテゴリである確率: %s' % nb.score_without_log(['コランダム'], 'Ruby')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment