*はじめに [#pcdbe70c]

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

Pythonに加えて、パッケージ管理ライブラリーのpip (pip3)、パッケージ形式ライブラリーのWheel、数値計算ライブラリーのNumPy、科学計算ライブラリーのSciPy、機械学習ライブラリーのscikit-learn、データ分析支援ライブラリーのpandas、Python実行環境のJupyter Notebook、グラフ描画ライブラリーのMatplotlibをインストールします。

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


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

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

**Wheel, NumPy, SciPy [#vb260e75]
pip3でインストールします。
#geshi(sh){{
$ sudo pip3 install wheel
$ sudo pip3 install numpy
$ sudo pip3 install scipy
}}

**Matplotlib [#i0177a8c]
pip3でインストールします。
#geshi(sh){{
$ sudo pip3 install matplotlib
}}


**pandas [#ob2d6389]
pip3でインストールします。
#geshi(sh){{
$ sudo pip3 install pandas
}}


**scikit-learn [#y4702d61]
pip3でインストールします。
#geshi(sh){{
$ sudo pip3 install -U scikit-learn
}}

**Jupyter Notebook [#p49aaa0e]
pip3でインストールします。
#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