• 追加された行はこの色です。
  • 削除された行はこの色です。
*はじめに [#pcdbe70c]

Pythonで定番のツールを使ってデータ・マイニングを行う環境を整えます。
-pandas
-scikit-learn
-Jupyter Notebook

pandasはデータを扱うためのライブラリーです。

scikit-learnは機械学習のライブラリーです。

Jupyter NotebookはPythonのコードや出力結果をHTML形式で出力するツールです。

この記事の内容は、以下のバージョンで確認しました。
-macOS Sierra 10.12.3
-Python 3.5.1

Python 3がインストールされていない場合は、先にPython 3をインストールしてください。



*インストール [#q1f4517c]

pipを使ってインストールしますので、まず、pipを最新版にします。
まず、Python3を最新版にアップデートします。
下記のサイトからMac OS X 64-bit/32-bit installerをダウンロードして、インストール。
-https://www.python.org/downloads/

pipをインストールし、pipを最新版にします。
#geshi(sh){{
$ sudo easy_install pip
$ sudo pip3 install --upgrade pip
}}

scikit-learnに必要なWheel, NumPy, SciPyをインストールします。
#geshi(sh){{
$ sudo pip3 install wheel
$ sudo pip3 install numpy
$ sudo pip3 install scipy
}}

次に、グラフを表示するためのmatprotlibをインストールします。
#geshi(sh){{
sudo pip3 install matplotlib
}}


**pandas [#ob2d6389]
#geshi(sh){{
$ sudo pip3 install pandas
}}


**scikit-learn [#y4702d61]
#geshi(sh){{
$ sudo pip3 install -U scikit-learn
}}

**Jupyter Notebook [#p49aaa0e]
#geshi(sh){{
# sudo pip3 install jupyter
}}


*動作確認 [#a84e2c0c]

**Jupyter Notebook [#nc0f879d]
まず、macOSのTerminalからJupyter Notebookを起動します。
#geshi(sh){{
$ jupyter notebook
}}

すると、ローカルでWebサーバーが起動し、Webブラウザが自動的に起動します。
#ref(./jupyter_boot.png,50%)

''New'' から ''Python 3'' を選択します。
#ref(./jupyter_new_python.png,50%)

すると、新しいノートブックが作成されます。
#ref(./jupyter_notebook.png,50%)


**scikit-learn [#l671ced9]
標準で入っているirisデータをSVM (SVC)で学習してみます。

入力ボックスにPython 3のプログラムを入力し、ボタンを押して実行します。
#ref(./jupyter_iris.png,50%)

#geshi(python){{
from sklearn import datasets
from sklearn.svm import SVC
iris = datasets.load_iris()
clf = SVC()
clf.fit(iris.data, iris.target)
}}
#geshi(python){{
list(clf.predict(iris.data[:3]))
}}
#geshi(python){{
clf.fit(iris.data, iris.target_names[iris.target])
}}
#geshi(python){{
list(clf.predict(iris.data[:3]))
}}


**pandas [#lec3dc92]
pandasの動作を確認するため、irisデータをpandasに入れます。

まずは、irisのデータをそのまま表示してみます。
#geshi(python){{
iris.data
}}

次に、iris.dataをpandasのDataFrameに入れて表示してみます。
#geshi(python){{
import pandas as pd
iris.df = pd.DataFrame(iris.data, 
                       columns=['sepal_length', 'sepal_width', 'petal_length', 'petal_width'])
iris.df
}}

すると、Jupyter Notebookに表形式で出力されます。
#ref(./jupyter_pandas.png,50%)

**matplotlib [#b1c73801]

最後に、scikit-learnで学習した結果をmatplotlibを使って、可視化してみます。

まず、pandasのDataFrameに予測結果を追加します。
#geshi(python){{
iris.df['predict'] = clf.predict(iris.data)
iris.df
}}

次に、予測結果がsetosaのもの、versicolorのもの、virginicaのものに分けます。
#geshi(python){{
x1 = iris.df[iris.df.predict == 'setosa']['sepal_length']
y1 = iris.df[iris.df.predict == 'setosa']['sepal_width']
x2 = iris.df[iris.df.predict == 'versicolor']['sepal_length']
y2 = iris.df[iris.df.predict == 'versicolor']['sepal_width']
x3 = iris.df[iris.df.predict == 'virginica']['sepal_length']
y3 = iris.df[iris.df.predict == 'virginica']['sepal_width']
}}

これをmatplotlibで表示します。
#geshi(python){{
%matplotlib notebook
import matplotlib.pyplot as plt
fig = plt.figure()
subplt = fig.add_subplot(1, 1, 1)
subplt.scatter(x1, y1, c='red')
subplt.scatter(x2, y2, c='green')
subplt.scatter(x3, y3, c='blue')
}}

すると、こんな感じになります。
#ref(./jupyter_matplotlib.png,50%)

トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS