決定木を作る
集合知プログラミング
これを図書館から借りて,決定木のサンプルコードを実行し,画像を出力した.
サンプルコード
http://examples.oreilly.com/9780596529321/
決定木とは
https://ja.wikipedia.org/wiki/%E6%B1%BA%E5%AE%9A%E6%9C%A8
ようは,条件によって,分岐させる木.選択の過程が理解しやすいという利点がある.
実行結果
サンプルコードのディレクトリ(chapter7)に入って実行する.
% python Python 2.7.12 (default, Oct 11 2016, 05:16:02) [GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import treepredict >>> reload(treepredict) <module 'treepredict' from 'treepredict.pyc'> >>> treepredict.drawtree(tree,jpeg='treeview.jpg') Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'tree' is not defined
このように詰まるかも,だが少し読み返して
>>> tree=treepredict.buildtree(treepredict.my_data) >>> reload(treepredict) <module 'treepredict' from 'treepredict.py'> >>> treepredict.printtree(tree) 0:google? T-> 3:21? T-> {'Premium': 3} F-> 2:yes? T-> {'Basic': 1} F-> {'None': 1} F-> 0:slashdot? T-> {'None': 3} F-> 2:yes? T-> {'Basic': 4} F-> 3:21? T-> {'Basic': 1} F-> {'None': 3} >>> reload(treepredict) <module 'treepredict' from 'treepredict.pyc'> >>> treepredict.drawtree(tree,jpeg='treeview.jpg')
結果
これにより,決定木ができた. 自分のデータに合わせて実行した結果treeview.jpg
詰まるかもしれないところ
計算に必要な,あるいは画像出力に必要なパッケージがないかもしれない.そのときは,エラーメッセージにかかれたパッケージを調べて,ダウンロードすればよい.
自分の場合
easy_install PIL
pip install pillow