機械学習/Pythonでデータ分析するはじめの一歩(Mac編) のバックアップの現在との差分(No.4)


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

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

pandasはデータを扱うためのライブラリーです。
以前は、ツールを一つずつインストールしていましたが、今では、''Anaconda''というパッケージだけでOKです。

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

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

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


*ダウンロード [#t08f958d]

Anacondaの公式サイトから''Python 3.7 version''をダウンロードします。
-https://www.anaconda.com/download/#macos

*インストール [#b200c27b]

*インストール [#q1f4517c]
ダウンロードしたインストーラーを起動します。

pipを使ってインストールしますので、まず、pipを最新版にします。
#geshi(sh){{
$ sudo pip3 install --upgrade pip
}}
インストール先を聞かれます。自分だけで使う場合は「自分専用にインストール」、他のユーザーも使えるようにするには「特定のディスクにインストール」を選びます。

scikit-learnに必要なWheel, NumPy, SciPyをインストールします。
#geshi(sh){{
$ sudo pip3 install wheel
$ sudo pip3 install numpy
$ sudo pip3 install spicy
}}
ここでは、ほかのユーザーも使えるように「特定のディスクにインストール」を選択し、ディスクをMacBookのSSD、ディレクトリーを共有ユーザーの /Users/Shared/tools にします(ユーザー → 共有 → tools、toolsがなければ新規作成)。

次に、グラフを表示するためのmatprotlibをインストールします。
#geshi(sh){{
sudo pip3 install matplotlib
}}
最後に、MicrosoftのVisual Studioのコードをインストールするかどうかを聞かれます。
ここでは、スキップします。

これでインストールは完了です。

**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
}}
アプリケーションに追加された''Anaconda-Navigator.app''を起動します。
#ref(anaconda_navi.png,nolink,50%)

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

すると、ターミナルが起動し、その上でWebサーバーが自動的に起動し、さらにWebブラウザが自動的に起動します。
#ref(./jupyter_boot.png,nolink,50%)

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

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


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

入力ボックスにPython 3のプログラムを入力し、ボタンを押して実行します。
#ref(./jupyter_iris.png,50%)
#ref(./jupyter_iris.png,nolink,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%)
#ref(./jupyter_pandas.png,nolink,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%)
#ref(./jupyter_matplotlib.png,nolink,50%)



*終わり方 [#m9d32ebf]


Jupyter Notebookが動いているターミナルをアクティブにして、''control+C'' でサーバーのプロセスを終了します。

Anaconda Navigatorを終了します。

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