發表文章

目前顯示的是 11月, 2015的文章

Python: Using pydot and Graphviz to generate PDF file for Decision Tree

from sklearn import datasets from sklearn import tree from sklearn .externals.six import StringIO import pydot clf = tree.DecisionTreeClassifier() clf = clf.fit(iris.data, iris.target) dot_data = StringIO() tree.export_graphviz(clf, out_file=dot_data) graph = pydot.graph_from_dot_data(dot_data.getvalue()) graph.write_pdf("iris.pdf") Remark: (OS: OS X El Capitan) 1. You must install pydot2 firstly. pip install pydot2 2. Then, you must install Graphviz by using "brew" brew install graphviz 3. How to install Homebrew ruby -e " $( curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install ) " Ref: http://stackoverflow.com/questions/18438997/why-is-pydot-unable-to-find-graphvizs-executables-in-windows-8 http://stackoverflow.com/questions/27666846/pydot-invocationexception-graphvizs-executables-not-found http://hackercodex.com/guide/python-development-environment-on-mac-osx/ http://hackercodex.com/guide/mac-osx-maveri...

Python: List Comprehensions

If I'd like to create a list based on the existing objects (maybe a list or a sequence of numbers), it's a good way to achieve the goal. I often use this to build a list of files. For example, output = [open('output_%s' %(i), 'w+') for i in range(6, 10, 1)] The program will generate four files named output_6, output_7, output_8, and output_9. Reference: http://www.swaroopch.com/notes/python/#list_comprehension