• 追加された行はこの色です。
  • 削除された行はこの色です。
*はじめに [#g55e2e45]
WindowsでPythonの定番ツールを使ってデータ分析を行う環境を整えます。

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

この記事の内容は、以下のバージョンで確認しました。
-Windows 10
-Python 3.6.2
-Wheel 0.30.0
-NumPy 1.13.1
-SciPy 0.19.1
-scikit-learn 0.19.0
-Jupyter Notebook 1.0.0
-Matplotlib 2.0.2



*インストール [#vd1d1ad7]

**Python 3 64ビット版 [#td00681b]
まず、Pythonのオフィシャル・サイトからPython 3の64ビット版インストーラーをダウンロードします。
-[[Python.org:http://python.org/]]

「Downloads」の「Windows」をクリックし、リストの中にある''Windows x86-64 executable installer''をダウンロードします。
#ref(./download_python.png,nolink,50%)

インストーラーを実行したら、''Add Python 3.6 to PATH''にチェックを入れて、''Install Now''をクリックします。
#ref(./install_python.png,nolink,50%)

**Wheel [#ubec5c76]
Pythonのライブラリーは、pipを使ってインストールします。

pipはコマンド・プロンプト上で実行しますが、インストールを行いますので、管理者として実行する必要があります。

そこで、Windowsメニューの''Windowsシステムツール''にある''コマンド プロンプト''を右クリックし、''管理者として実行''を選択します。
#ref(./command_prompt.png,nolink,50%)

標準のライブラリーをインストールすればいい場合は、次のようにしてインストールできます。
#geshi(sh){{
> pip install wheel
}}


**NumPy [#o70ff223]
次のサイトから、''numpy‑1.13.1+mkl‑cp36‑cp36m‑win_amd64.whl''をダウンロードします。
-http://www.lfd.uci.edu/~gohlke/pythonlibs/

pipでダウンロードしたファイルを指定し、インストールします。
#geshi(sh){{
> pip install "C:\Users\tohgoroh\Downloads\numpy‑1.13.1+mkl‑cp36‑cp36m‑win_amd64.whl"
}}
ここで、tohgorohはユーザー名なので、自分のユーザー名に置き換えてください。


**SciPy [#e02b4dc2]
NumPyと同じように、''scipy‑0.19.1‑cp36‑cp36m‑win_amd64.whl''をダウンロードしてインストールします。
#geshi(sh){{
> pip install "C:\Users\tohgoroh\Downloads\scipy‑0.19.1‑cp36‑cp36m‑win_amd64.whl"
}}


**pandas [#c564d222]
Wheelと同じように、通常のライブラリーをインストールできます。
#geshi(sh){{
> pip install pandas
}}

**scikit-learn [#x357571c]
Wheelと同じように、通常のライブラリーをインストールできます。
#geshi(sh){{
> pip install scikit-learn
}}

**Jupyter Notebook [#kd28ee9b]
Wheelと同じように、通常のライブラリーをインストールできます。
#geshi(sh){{
> pip install jupyter
}}

**Matplotlib [#g5ae4f52]
Wheelと同じように、通常のライブラリーをインストールできます。
#geshi(sh){{
> pip install matplotlib
}}

これでインストールは終わりです。
管理者権限で起動したコマンド・プロンプトを終了します。


*動作確認 [#rcdf53e5]
**Jupyter Notebook [#t9eab852]
まず、コマンド・プロンプトから、Jupyter Notebookを起動します。
ユーザー権限でコマンド・プロンプトを起動し、そこからJupyter Notebookを起動します。
#geshi(sh){{
> jupyter notebook
}}

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

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

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


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

入力ボックスにPythonのプログラムを入力し、実行ボタンをクリックするか、Shiftキーを押しながらreturnキーを押して実行します。
#ref(./scikit_learn.png,nolink,50%)
#geshi(python){{
from sklearn import datasets
from sklearn.svm import SVC
clf = SVC()
iris = datasets.load_iris()
clf.fit(iris.data, iris.target)
}}


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

まずは、irisのデータをそのまま表示してみます。
#geshi(python){{
iris.data
}}
#ref(./numpy.png,nolink,50%)

次に、iris.dataをpandasのDataFrameに入れて表示してみます。
次に、iris.dataをpandasのデータフレームに入れて表示してみます。
#geshi(python){{
import pandas as pd
df_iris = pd.DataFrame(iris.data, 
                       columns=['sepal_length', 'sepal_width', 'petal_length', 'petal_width'])
df_iris
}}
#ref(./pandas.png,nolink,50%)


**Matplotlib [#ke5826eb]
Matplotlibを使って、グラフを描いてみます。

まず、予測値をpandasのデータフレームに追加します。
#geshi(python){{
iris.df['predict'] = clf.predict(iris.data)
iris.df
}}

次に、予測した値によって、3つのグループに分けます。
#geshi(python){{
x0 = df_iris[df_iris.predict==0]['sepal_length']
y0 = df_iris[df_iris.predict==0]['sepal_width']
x1 = df_iris[df_iris.predict==1]['sepal_length']
y1 = df_iris[df_iris.predict==1]['sepal_width']
x2 = df_iris[df_iris.predict==2]['sepal_length']
y2 = df_iris[df_iris.predict==2]['sepal_width']
}}

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


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