(pytho2.7.7)
今さら『データサイエンティスト養成読本 』を購入。
「第4章 Python による機械学習」で一部エラーやらで動かなかったりしたのでメモ。
Google先生に訊いても引っかからず正誤表も無いような。
この問題って自分だけなのかしらん?
修正・変更
リスト10
ここはそのままでも動くけど、実行すると「rows 廃止になるよ」メッセージが表示されちゃうので。
# リスト10 ピポットテーブルの作成 # pd.pivot_table(iris, rows="Name", aggfunc=np.mean) # original pd.pivot_table(iris, index="Name", aggfunc=np.mean) # fixed (rows : kwarg only alias of index [deprecated])
リスト23
『.../site-packages/sklearn/preprocessing/label.py:99: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().』
とかエラー出る。
# リスト23 ロジスティクス回帰モデル import sklearn import numpy as np usedata = np.logical_or(iris["Name"] == "Iris-setosa", iris["Name"] == "Iris-virginica") setosa_virginica = iris[usedata] X = setosa_virginica[["SepalLength", "SepalWidth"]] # Y = setosa_virginica[["Iris-setosa"]] # original Y = setosa_virginica["Iris-setosa"] # fixed LogRegr = sklearn.linear_model.LogisticRegression(C=1.0) LogRegr.fit(X, Y) print(LogRegr.coef_) print(LogRegr.intercept_) # print(pd.crosstab(Y["Iris-setosa"], LogRegr, predict(X))) # original print(pd.crosstab(Y, LogRegr.predict(X))) # fixed
これでいいのかな?
リスト26
# リスト26 決定木の可視化 # NOTE: 実行すると"Couldn't import dot_parser, loading of dot files will not be possible." とエラーが出るので、下記リンクを参照して対処 # http://stackoverflow.com/questions/15951748/pydot-and-graphviz-error-couldnt-import-dot-parser-loading-of-dot-files-will # pip uninstall pyparsing # pip install -Iv https://pypi.python.org/packages/source/p/pyparsing/pyparsing-1.5.7.tar.gz#md5=9be0fcdcc595199c646ab317c1d9a709 # pip install pydot import StringIO import pydot dot_data = StringIO.StringIO() tree.export_graphviz(treeClf, out_file=dot_data, feature_names=["SepalLength", "SepalWidth", "PetalLength", "PetalWidth"]) graph = pydot.graph_from_dot_data(dot_data.getvalue()) graph.write_pdf("iris_decision_tree.pdf")