学生の備忘録なブログ

日々のことを忘れないためのブログです。一日一成果物も目標。技術系はQiitaにあげるように変更しました。

ゼロから作るDeep Learning ~L2正規化~

L2正規化

L2正規化について,あまり詳しく書かれていなかったので

6.4.2 Weight decay

Weight decay 荷重減衰とは 学習の過程において、大きな重みを持つことに対してペ ナルティを課すことで、過学習を抑制すること.


L2正則化,L1正規化とは

機械学習でよく用いられる正則化には、L1正則化とL2正則化がある

ノルムとは

n 次元ベクトル x→=(x1,x2,⋯,xn) および 1≤p<∞ なる p に対して $\sqrt[p]{|x_1|^p+|x_2|^p+\cdots +|x_n|^p}$ を x→ の Lp ノルムと言い,||x||p と書く.

定義

$\lambda$は正則化パラメータ(regularization parameter)

$L2=\lambda ||w||^{2}{2}=\lambda \sum ^{n}{j=1}w^{2}_{j}$ 各成分の自乗の和, 2乗ノルム     

  $L1=\lambda \left| w\right| ^{1}{1}=\lambda \sum ^{n}{j=1}\left| w_{j}\right|$ 各成分の絶対値の和

以上をペナルティ項として加える.


改めてL2正則化,L1正規化正規化とは

正規化したい対象E(w)にペナルティ項λE_1(w)を加えて, E(w)+λE_1(w)を最小化するように最適化問題を解く問題に置き換えることで,過学習を避けること.


グラフで見る

L2ノルム L2 =$\sqrt[2]{|x_1|^2+|x_2|^2+\cdots +|x_n|^2}$ normf:id:forhighlow:20180611231329j:plain


f:id:forhighlow:20180611231326p:plain 六本木で働くデータサイエンティストのブログより

ペナルティ項を付けておくことでw1,w2の定まり方が変わるというお話です。左側のL1ノルム正則化であれば尖った形の領域に接するようにしなければならないため自ずとw2の軸上に最適点が定まってw1=0となり、右側のL2ノルム正則化であれば円状の領域に接するようにしなければならないためw1,w2ともにnon-zeroな値が入る、という感じになるわけです。


グラフから分かること

重みwは正規化により円状の領域にのみ制限される. よってwを項に含む関数E(D)はある程度制限される. wの値に制約を付け加えることで,意図しない域までパラメータが行くことを防ぐことができる.


制度の比較

図より,学習済みデータとテストデータとの差が狭まっている.つまり過学習が抑制されている.

正規化のないグラフ

正規化したグラフf:id:forhighlow:20180611231443p:plain


正規化したグラフ

正規化したグラフf:id:forhighlow:20180611231321p:plain


参考

六本木で働くデータサイエンティストのブログ(https://tjo.hatenablog.com/entry/2015/03/03/190000)

過学習を防ぐ正則化 (https://www.techcrowd.jp/machinelearning/regularization/)

ゼロから作るDeepLearning

pdf埋め込み

ターミナルのコマンドで消す方法

ターミナルのコマンドで消す方法

Macの起動音を消す方法にコマンド実行による方法があります。Finderからアプリケーションを選択し、ユーティリティを開き、ターミナルを開きます。ターミナルを開いたら「sudo nvram SystemAudioVolume=%80」と入力します。

そしてパスワードを入力し実行すれば完了です。これでMacの起動音が消音になります。もし、Macの起動音を通常通りに戻したい場合は、同じようにターミナルを開いて「sudo nvram-d SystemAudioVolume」を入力します。

sudo nvram SystemAudioVolume=%80

戻すときは

sudo nvram-d SystemAudioVolume

http://minto.tech/mac-kidouon-kesu/

統計学全体の目次

本を買った

研究室仮配属したので,興味をそそられて買った.生協10%offで.

書籍詳細 - なぜあなたの研究は進まないのか? | メディカルレビュー社

重要だと感じたこと

本の中で(全部は読めていないが)重要だと感じたのは,分野全体についての知識だ.

研究分野の全体像を知ること,これを怠れば車輪を二度発明する(すでにあるものを生み出すことに労力を使う)ことになりかねない.

統計学

以下はwikipediaを参照した.

機械学習 - Wikipedia

統計学
標本調査 標本,母集団,無作為抽出,層化抽出法 |
要約統計学 連続データ | 位置 | \multicolumn{1}{c|}{平均(算術,幾何,調和),中央値,最頻値,階級値}
| 分散 | 範囲,標準偏差,変動係数,百分率
| モーメント | 分散,歪度,尖度
カテゴリーデータ | 頻度,分割表 | 頻度,分割表
統計的推測 仮説検定 | 帰無仮説 対立仮説 有意 棄却 ノンパラメトリック手法 スチューデントのt検定 ウェルチのt検定 カイ二乗検定 イェイツのカイ二乗検定 累積カイ二乗検定 F検定 G検定 マン・ホイットニーのU検定 Z検定 フィッシャーの正確確率検定 二項検定 尤度比検定 マンテル検定 コクラン・マンテル・ヘンツェルの統計量 ウィルコクソンの符号順位検定 アンダーソン–ダーリング検定 カイパー検定 ジャック–ベラ検定 シャピロ–ウィルク検定 コルモゴロフ–スミルノフ検定 分散分析 共分散分析
区間推定 | 信頼区間,予測区間
その他 | 最尤推定 最大事後確率 ベイズ推定 尤度関数カーネル密度推定 最小距離推定 メタアナリシス
生存時間分析 生存時間関数 カプラン=マイヤー推定量 ログランク検定故障率 比例ハザードモデル |
相関 相関交絡変数 ピアソンの積率相関係数 順位相関(スピアマンの順位相関係, ケンドールの順位相関係数) |
モデル 一般線形モデル 一般化線形モデル 混合モデル一般化線形混合モデル |
回帰 線形 | 線形回帰 リッジ回帰 Lasso エラスティックネット
非線形 | k近傍法 回帰木 ランダムフォレスト ニューラルネットワーク サポートベクター回帰 射影追跡回帰
分類 線形 | 線形判別分析 ロジスティック回帰単純ベイズ分類器 単純パーセプトロン 線形サポートベクターマシン |
二次 | 二次判別分析
非線形 | 非線形k近傍法 決定木 ランダムフォレスト ニューラルネットワーク サポートベクターマシン ベイジアンネットワーク 隠れマルコフモデル
その他 | 二項分類 多クラス分類 第一種過誤と第二種過誤
教師なし学習 クラスタリング | k平均法 (k-means++法)
その他 | 主成分分析 独立成分分析 自己組織化写像(SOM)

このように思った理由

これを読んで, qiita.com ここへ飛んで Deep Learning Monitor - Find new Arxiv papers, tweets and Reddit posts for you あまりにも分野が多岐にわたっていて応用事例だけ追うことも難しいと感じた. (そりゃキュレーション全部英語だよね,,,ともなりました)

これに肉付けして頭を整理していきたい.

MindMapを書くほうが良いかと思ったけれど,先に手を動かしてみた.

ブログ再開

blogをお休みしていた理由

学校の実験講義が忙しかったため,

やる気を失っていたため,

blogを再開する理由

技術的にいろいろやってはいたけれど,残していないと忘れてしまうため,

やる気を取り戻したため,

DeepLearningにおける勾配

損失関数の出力をfとすると,出力の全てのパラメータ$w_i$に関する偏微分を計算すると,偏微分を並べたベクトルを定義することができる.このベクトル

f = f d w 1 , . . . , · f W i

を勾配と呼ぶ.

f = f d w 1 , . . . , · f W i

f = f W 1 , f W i

TeXが上手に描画できないので,一旦stop

機械学習入門_第2章 途中まで

import sys
print("Python version: {}".format(sys.version))

import pandas as pd
print("pandas version: {}".format(pd.__version__))

import matplotlib
print("matplotlib version: {}".format(matplotlib.__version__))

import numpy as np
print("NumPy version: {}".format(np.__version__))

import scipy as sp
print("SciPy version: {}".format(sp.__version__))

import IPython
print("IPython version: {}".format(IPython.__version__))

import sklearn
print("scikit-learn version: {}".format(sklearn.__version__))

import mglearn
import matplotlib.pyplot as plt

Python version: 3.6.1 |Continuum Analytics, Inc.| (default, May 11 2017, 13:04:09) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)]
pandas version: 0.20.3
matplotlib version: 2.0.2
NumPy version: 1.13.1
SciPy version: 0.19.1
IPython version: 6.1.0
scikit-learn version: 0.19.0
%matplotlib inline
from preamble import *
---------------------------------------------------------------------------

ModuleNotFoundError                       Traceback (most recent call last)

<ipython-input-10-e31a7faecc4a> in <module>()
      1 get_ipython().magic('matplotlib inline')
----> 2 from preamble import *


ModuleNotFoundError: No module named 'preamble'

教師あり学習は,ある入力に対しての特定の出力を予測したい場合で,入力出力のペアのデータが入手可能な際に用いられる.

2.1 クラス分類と回帰

教師あり機械学習問題は,2つに大別することができる. * クラス分類(classification) * 2クラス分類(binary classification) * 多クラス分類(multiclass classification) * 回帰(regression)

クラス分類問題と回帰問題を区別するには,出力に何らかの連続性があるかを考えてみれば良い.出力に連続性があるなら回帰問題である.年収を予測する場合を考えてみる.40,000ドルと40,001ドルは近似できる.しかし,webサイトの言語を認識するタスク(クラス分類問題)では,量の大小は問題ではない.言語には連続性がない.英語とフランス語の中間の言語は存在しない.

汎化,過剰適合,適合不足

非常に複雑なモデルを作ることを許せば,訓練データに対してはいくらでも正確な予測を行うようにできてしまう.

[f:id:forhighlow:20171006222712p:plain]

教師あり機械学習アルゴリズム

これから,最も一般的な機械学習アルゴリズムについて,どのようにデータから学習し,どのように予想を行うん丘を見ていく.モデルの複雑さという概念が個々のモデルで果たす役割について述べ,個々のアルゴリズムがモデルを構築する方法の概念を示す.更に,それぞれのアルゴリズムの長所と短所,適しているデータの種類について述べる.重要なパラメータとオプションについても説明する.多くのアルゴリズムは,クラス分類と回帰のバリエーションがあるので両方とも説明する.

個々のアルゴリズムの説明を詳しく読む必要はないが,モデルを理解することで個々の機械学習アルゴリズムの働き方についてより良く理解することができるだろう.本章はリファレンスガイドとしても利用できる.

サンプルデータセット

合成した2クラス分類データセットの例として,forgeデータセットを見てみる.

第1特徴量をx軸に、第2特徴量はy軸にプロットしている.

# generate dataset
X, y = mglearn.datasets.make_forge()
# plot dataset
mglearn.discrete_scatter(X[:, 0], X[:, 1], y)
plt.legend(["Class 0", "Class 1"], loc=4)
plt.xlabel("First feature")
plt.ylabel("Second feature")
print("X.shape: {}".format(X.shape))
X.shape: (26, 2)

[f:id:forhighlow:20171006222715p:plain]

X.shapeからわかるようにこのデータセットは2つの特徴量を持つ26のデータポイントで構成されている.

回帰あるを紹介する際には,合成したwaveデータセットを用いる.このwaveデータセットは入力として1つの特徴量と,モデルの対象となる連続地のターゲット変数(もしくは(response))を持つ.次のグラフは特徴量をx軸に,回帰のターゲット(出力)をy軸に取る.

X, y = mglearn.datasets.make_wave(n_samples=40)
plt.plot(X, y, 'o')
plt.ylim(-3, 3)
plt.xlabel("Feature")
plt.ylabel("Target")
<matplotlib.text.Text at 0x114641160>

[f:id:forhighlow:20171006222719p:plain]

学習にこれから用いるデータセットの一つに乳がんの腫瘍を測定したデータが有る.

from sklearn.datasets import load_breast_cancer
cancer= load_breast_cancer()
print("cancer.keys(): \n{}".format(cancer.keys()))
cancer.keys(): 
dict_keys(['data', 'target', 'target_names', 'DESCR', 'feature_names'])
print("shape of cancer data: {}".format(cancer.data.shape))
shape of cancer data: (569, 30)
print("Sample counts per class:\n{}".format(
      {n: v for n, v in zip(cancer.target_names, np.bincount(cancer.target))}))
Sample counts per class:
{'malignant': 212, 'benign': 357}

212が悪性で,357が良性である.

個々の特徴量の意味を示す記述は,feature_names属性に格納されている.

print("Feature names:^n{}".format(cancer.feature_names))##特徴量名
Feature names:^n['mean radius' 'mean texture' 'mean perimeter' 'mean area'
 'mean smoothness' 'mean compactness' 'mean concavity'
 'mean concave points' 'mean symmetry' 'mean fractal dimension'
 'radius error' 'texture error' 'perimeter error' 'area error'
 'smoothness error' 'compactness error' 'concavity error'
 'concave points error' 'symmetry error' 'fractal dimension error'
 'worst radius' 'worst texture' 'worst perimeter' 'worst area'
 'worst smoothness' 'worst compactness' 'worst concavity'
 'worst concave points' 'worst symmetry' 'worst fractal dimension']
## DESCRを見れば,詳しい情報が得られる.
print("Feature names:^n{}".format(cancer.DESCR))
Feature names:^nBreast Cancer Wisconsin (Diagnostic) Database
=============================================

Notes
-----
Data Set Characteristics:
    :Number of Instances: 569

    :Number of Attributes: 30 numeric, predictive attributes and the class

    :Attribute Information:
        - radius (mean of distances from center to points on the perimeter)
        - texture (standard deviation of gray-scale values)
        - perimeter
        - area
        - smoothness (local variation in radius lengths)
        - compactness (perimeter^2 / area - 1.0)
        - concavity (severity of concave portions of the contour)
        - concave points (number of concave portions of the contour)
        - symmetry 
        - fractal dimension ("coastline approximation" - 1)

        The mean, standard error, and "worst" or largest (mean of the three
        largest values) of these features were computed for each image,
        resulting in 30 features.  For instance, field 3 is Mean Radius, field
        13 is Radius SE, field 23 is Worst Radius.

        - class:
                - WDBC-Malignant
                - WDBC-Benign

    :Summary Statistics:

    ===================================== ====== ======
                                           Min    Max
    ===================================== ====== ======
    radius (mean):                        6.981  28.11
    texture (mean):                       9.71   39.28
    perimeter (mean):                     43.79  188.5
    area (mean):                          143.5  2501.0
    smoothness (mean):                    0.053  0.163
    compactness (mean):                   0.019  0.345
    concavity (mean):                     0.0    0.427
    concave points (mean):                0.0    0.201
    symmetry (mean):                      0.106  0.304
    fractal dimension (mean):             0.05   0.097
    radius (standard error):              0.112  2.873
    texture (standard error):             0.36   4.885
    perimeter (standard error):           0.757  21.98
    area (standard error):                6.802  542.2
    smoothness (standard error):          0.002  0.031
    compactness (standard error):         0.002  0.135
    concavity (standard error):           0.0    0.396
    concave points (standard error):      0.0    0.053
    symmetry (standard error):            0.008  0.079
    fractal dimension (standard error):   0.001  0.03
    radius (worst):                       7.93   36.04
    texture (worst):                      12.02  49.54
    perimeter (worst):                    50.41  251.2
    area (worst):                         185.2  4254.0
    smoothness (worst):                   0.071  0.223
    compactness (worst):                  0.027  1.058
    concavity (worst):                    0.0    1.252
    concave points (worst):               0.0    0.291
    symmetry (worst):                     0.156  0.664
    fractal dimension (worst):            0.055  0.208
    ===================================== ====== ======

    :Missing Attribute Values: None

    :Class Distribution: 212 - Malignant, 357 - Benign

    :Creator:  Dr. William H. Wolberg, W. Nick Street, Olvi L. Mangasarian

    :Donor: Nick Street

    :Date: November, 1995

This is a copy of UCI ML Breast Cancer Wisconsin (Diagnostic) datasets.
https://goo.gl/U2Uwz2

Features are computed from a digitized image of a fine needle
aspirate (FNA) of a breast mass.  They describe
characteristics of the cell nuclei present in the image.

Separating plane described above was obtained using
Multisurface Method-Tree (MSM-T) [K. P. Bennett, "Decision Tree
Construction Via Linear Programming." Proceedings of the 4th
Midwest Artificial Intelligence and Cognitive Science Society,
pp. 97-101, 1992], a classification method which uses linear
programming to construct a decision tree.  Relevant features
were selected using an exhaustive search in the space of 1-4
features and 1-3 separating planes.

The actual linear program used to obtain the separating plane
in the 3-dimensional space is that described in:
[K. P. Bennett and O. L. Mangasarian: "Robust Linear
Programming Discrimination of Two Linearly Inseparable Sets",
Optimization Methods and Software 1, 1992, 23-34].

This database is also available through the UW CS ftp server:

ftp ftp.cs.wisc.edu
cd math-prog/cpo-dataset/machine-learn/WDBC/

References
----------
   - W.N. Street, W.H. Wolberg and O.L. Mangasarian. Nuclear feature extraction 
     for breast tumor diagnosis. IS&T/SPIE 1993 International Symposium on 
     Electronic Imaging: Science and Technology, volume 1905, pages 861-870,
     San Jose, CA, 1993.
   - O.L. Mangasarian, W.N. Street and W.H. Wolberg. Breast cancer diagnosis and 
     prognosis via linear programming. Operations Research, 43(4), pages 570-577, 
     July-August 1995.
   - W.H. Wolberg, W.N. Street, and O.L. Mangasarian. Machine learning techniques
     to diagnose breast cancer from fine-needle aspirates. Cancer Letters 77 (1994) 
     163-171.
print("Feature names:^n{}".format(cancer.data))
Feature names:^n[[  1.79900000e+01   1.03800000e+01   1.22800000e+02 ...,   2.65400000e-01
    4.60100000e-01   1.18900000e-01]
 [  2.05700000e+01   1.77700000e+01   1.32900000e+02 ...,   1.86000000e-01
    2.75000000e-01   8.90200000e-02]
 [  1.96900000e+01   2.12500000e+01   1.30000000e+02 ...,   2.43000000e-01
    3.61300000e-01   8.75800000e-02]
 ..., 
 [  1.66000000e+01   2.80800000e+01   1.08300000e+02 ...,   1.41800000e-01
    2.21800000e-01   7.82000000e-02]
 [  2.06000000e+01   2.93300000e+01   1.40100000e+02 ...,   2.65000000e-01
    4.08700000e-01   1.24000000e-01]
 [  7.76000000e+00   2.45400000e+01   4.79200000e+01 ...,   0.00000000e+00
    2.87100000e-01   7.03900000e-02]]

詳しく見てもよくわからない.

ボストン住宅価格

実世界の回帰データセットとして,boston_housingデータセットを用いる.これはボストン郊外の住宅地の住宅価格の中央世知を犯罪率チャールズ川からの距離,高速道路への利便性などから予測するものだ.

13notokutyouryouwomotu 506のデータポイントが含まれる.

from sklearn.datasets import load_boston
boston = load_boston()
print("Data shape: {}".format(boston.data.shape))
Data shape: (506, 13)

このデータセットを拡張子,13の測定結果だけを特徴量とするのではなく,特徴量間の積(交互作用(interaction)と呼ぶ)も見ることにする.つまり,犯罪率と高速道路への利便性を特徴量として見るだけでなく,それらの積も特徴量として考える.このように導出された特徴量を含めることを特徴量エンジニアリング(feature engineering)と呼ぶ.

X, y = mglearn.datasets.load_extended_boston()
print("X.shape: {}".format(X.shape))
X.shape: (506, 104)

104の特徴量とは,もとの13の特徴量に,13の特徴量から2つの特徴量を選ぶ重複ありの組合せ91を足したものである.

これから,様々な機械学習アルゴリズムの特徴を説明していく.

k-最近傍法

k-NNアルゴリズムは,最も単純な学習アルゴリズムであると言われる.新しいデータポイントに対する予測を行う際には,訓練データセットのんかから一番近い点つまり最近傍点を見つける.

k-最近傍法によるクラス分類

一番簡単な倍位には,k-NNアルゴリズムは,1つの近傍点,つまり訓練データに含まれる天の中で,予測したいデータポイントに一番近いものだけみる.予測には,この点に対する出力をそのまま用いる.

mglearn.plots.plot_knn_classification(n_neighbors=1)

f:id:forhighlow:20171006222723p:plain

ここでは,星印で示される3つの新しいデータポイントを加えている.それぞれに対して訓練データのうちで,最も近いものに印を付けた.

最近傍法アルゴリズムでの予測では,近傍店のラベルが予測されたラベルになる.(星印の色で表されている).

1つ以上の近傍店を考慮にいれる場合は,投票でラベルを決める.つまり,個々のテストする点に対して,近傍点のうちいくつがクラス0に属し,いくつが暮らすに属するのかを数える.そして最も多く現れたクラスをその点に与える.言い換えればk-最近傍点の多数はのクラスを採用する.

要は多数決的に近しいものと同じになる.朱に交われば赤くなる,ということだ.

mglearn.plots.plot_knn_classification(n_neighbors=3)

png

ここでも,予測された結果は,星印の色で示されている.左上の新しいデータポイントに対する予測は,1つの最近傍点だけを使った場合とことなっている.

scikit-learnを用いてk-最近傍点アルゴリズムが適用できるか見てみたい.まず,データを訓練セットとテストセットに分割し,汎化性能を評価可能にする.

from sklearn.model_selection import train_test_split
X, y = mglearn.datasets.make_forge()
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)

次にクラスをインポートして,インスタンスを作成する.この際に,近傍店の数などのパラメータを渡すことができる.ここでは3にしている.

from sklearn.neighbors import KNeighborsClassifier
clf = KNeighborsClassifier(n_neighbors=3)

次に,訓練データセットを用いてクラス分類器を訓練する. for KNeighborsClassifier this means storing the dataset, so we can compute neighbors during prediction.

clf.fit(X_train, y_train)
KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski',
           metric_params=None, n_jobs=1, n_neighbors=3, p=2,
           weights='uniform')

finds the most common class among these

clf.predict(X_test)
array([1, 0, 1, 0, 1, 0, 0])
clf.score(X_test, y_test)
0.8571428571428571

85%の正確さである.

機械学習入門_第1章

機械学習の世界へ入門

www.oreilly.co.jp

各所でおすすめされている本.scikit-learnの作者の書いた本で,信頼がおけそう.理論的な部分より,scikit-learnの使ってできる解析としての機械学習を説明している.

データ分析の世界,というよりライブラリの進歩は著しく,基礎的説明より実践を重視する本は2,3年で通用しなくなる.(というより学習効率が下がる.)

www.oreilly.co.jp

こちらの本は2013年の本で,2017年現在はいささか古い.

この次にこの本で数式を踏まえた理論的な部分を勉強したい.

book.impress.co.jp

上記サイトに学習の手引がある.必読.

この分野では,PLMLがバイブルとして有名だが,少し見てめまいがするほど数式で埋め尽くされていた.よって,上記の2つの本で勉強していきたい.

python/numpy - 機械学習の「朱鷺の杜Wiki」

DeepLearning

www.oreilly.co.jp

ざっくりとした知識を付けた最大の要因は,KerasのMNISTチュートリアルをやってみること.怖がらずに四苦八苦しながら写経を恐れず書いてみた.わからない概念は,上記の本で分かりやすく解説されている.Kerasはラッパーとしてとても優秀なのでドキュメントをみれば,参考書内の概念が理解できる.

1.4.2

NumPy

import numpy as np
x = np.array([[1,2,3],[4,5,6]])#括弧は大外にも必要
print("x:\n{}".format(x))
x:
[[1 2 3]
 [4 5 6]]
print(x)
[[1 2 3]
 [4 5 6]]

SciPy

scikit-learnはアルゴリズムの実装をscipyの関数を用いている.scipy.sparseで疎行列を表現する.疎行列はone-hotの2次元配列を格納するのに使う.

from scipy import sparse

# 対角成分が1でそれ以外が0の2次元NumPy配列を作る
eye = np.eye(4)
print("NumPy array:\n{}".format(eye))
NumPy array:
[[ 1.  0.  0.  0.]
 [ 0.  1.  0.  0.]
 [ 0.  0.  1.  0.]
 [ 0.  0.  0.  1.]]
# NumPy配列をSciPyのCSR形式の疎行列に変換する
# 非ゼロ要素のみが格納される
sparse_matrix = sparse.csr_matrix(eye)
print("\nSciPy sparse CSR matrix:\n{}".format(sparse_matrix))
SciPy sparse CSR matrix:
  (0, 0)    1.0
  (1, 1)    1.0
  (2, 2)    1.0
  (3, 3)    1.0
# 疎なデータ表現を直接作る必要がある.ここでは,上のものと同じ疎行列をCOO形式で作っている.
data = np.ones(4)
row_indices = np.arange(4)
col_indices = np.arange(4)
eye_coo = sparse.coo_matrix((data, (row_indices, col_indices)))
print("COO representation:\n{}".format(eye_coo))
COO representation:
  (0, 0)    1.0
  (1, 1)    1.0
  (2, 2)    1.0
  (3, 3)    1.0
%matplotlib inline

import matplotlib.pyplot as plt
# -10から10までを100ステップに区切った列を配列として生成
# Generate a sequence numbers from -10 to 10 with 100 steps in between
x = np.linspace(-10, 10, 100)
print(x)
# create a second array using sinus
y = np.sin(x)
print(y)
# The plot function makes a line chart of one array against another
plt.plot(x, y, marker="x")

[f:id:forhighlow:20170927002441p:plain]
[-10.          -9.7979798   -9.5959596   -9.39393939  -9.19191919
  -8.98989899  -8.78787879  -8.58585859  -8.38383838  -8.18181818
  -7.97979798  -7.77777778  -7.57575758  -7.37373737  -7.17171717
  -6.96969697  -6.76767677  -6.56565657  -6.36363636  -6.16161616
  -5.95959596  -5.75757576  -5.55555556  -5.35353535  -5.15151515
  -4.94949495  -4.74747475  -4.54545455  -4.34343434  -4.14141414
  -3.93939394  -3.73737374  -3.53535354  -3.33333333  -3.13131313
  -2.92929293  -2.72727273  -2.52525253  -2.32323232  -2.12121212
  -1.91919192  -1.71717172  -1.51515152  -1.31313131  -1.11111111
  -0.90909091  -0.70707071  -0.50505051  -0.3030303   -0.1010101
   0.1010101    0.3030303    0.50505051   0.70707071   0.90909091
   1.11111111   1.31313131   1.51515152   1.71717172   1.91919192
   2.12121212   2.32323232   2.52525253   2.72727273   2.92929293
   3.13131313   3.33333333   3.53535354   3.73737374   3.93939394
   4.14141414   4.34343434   4.54545455   4.74747475   4.94949495
   5.15151515   5.35353535   5.55555556   5.75757576   5.95959596
   6.16161616   6.36363636   6.56565657   6.76767677   6.96969697
   7.17171717   7.37373737   7.57575758   7.77777778   7.97979798
   8.18181818   8.38383838   8.58585859   8.78787879   8.98989899
   9.19191919   9.39393939   9.5959596    9.7979798   10.        ]
[ 0.54402111  0.36459873  0.17034683 -0.03083368 -0.23076008 -0.42130064
 -0.59470541 -0.74392141 -0.86287948 -0.94674118 -0.99209556 -0.99709789
 -0.96154471 -0.8868821  -0.77614685 -0.63384295 -0.46575841 -0.27872982
 -0.0803643   0.12126992  0.31797166  0.50174037  0.66510151  0.80141062
  0.90512352  0.97202182  0.99938456  0.98609877  0.93270486  0.84137452
  0.7158225   0.56115544  0.38366419  0.19056796 -0.01027934 -0.21070855
 -0.40256749 -0.57805259 -0.73002623 -0.85230712 -0.93992165 -0.98930624
 -0.99845223 -0.96698762 -0.8961922  -0.78894546 -0.64960951 -0.48385164
 -0.2984138  -0.10083842  0.10083842  0.2984138   0.48385164  0.64960951
  0.78894546  0.8961922   0.96698762  0.99845223  0.98930624  0.93992165
  0.85230712  0.73002623  0.57805259  0.40256749  0.21070855  0.01027934
 -0.19056796 -0.38366419 -0.56115544 -0.7158225  -0.84137452 -0.93270486
 -0.98609877 -0.99938456 -0.97202182 -0.90512352 -0.80141062 -0.66510151
 -0.50174037 -0.31797166 -0.12126992  0.0803643   0.27872982  0.46575841
  0.63384295  0.77614685  0.8868821   0.96154471  0.99709789  0.99209556
  0.94674118  0.86287948  0.74392141  0.59470541  0.42130064  0.23076008
  0.03083368 -0.17034683 -0.36459873 -0.54402111]





[<matplotlib.lines.Line2D at 0x10d96bb38>]

png

pandas

import pandas as pd
from IPython.display import display

# create asimple dataset of people
data = {'Name': ["Jone", "Anna","Peter", "Linda"],
        'Location': ["Nes York", "Paris", "Berlin", "London"],
        'Age': [24, 13, 53, 33]
       }
data_pandas = pd.DataFrame(data)
# Ipython.displayを用いると,きれいに表示できる.
display(data_pandas)
Age Location Name
0 24 Nes York Jone
1 13 Paris Anna
2 53 Berlin Peter
3 33 London Linda
# One of many possible ways to query the table:
# selecting all rows that have an age column greate than 30
display(data_pandas[data_pandas.Age > 30])
Age Location Name
2 53 Berlin Peter
3 33 London Linda

mglearn

import sys
print("Python version: {}".format(sys.version))

import pandas as pd
print("pandas version: {}".format(pd.__version__))

import matplotlib
print("matplotlib version: {}".format(matplotlib.__version__))

import numpy as np
print("NumPy version: {}".format(np.__version__))

import scipy as sp
print("SciPy version: {}".format(sp.__version__))

import IPython
print("IPython version: {}".format(IPython.__version__))

import sklearn
print("scikit-learn version: {}".format(sklearn.__version__))
Python version: 3.6.1 |Continuum Analytics, Inc.| (default, May 11 2017, 13:04:09) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)]
pandas version: 0.20.3
matplotlib version: 2.0.2
NumPy version: 1.13.1
SciPy version: 0.19.1
IPython version: 6.1.0
scikit-learn version: 0.19.0

A First Application: Classifying iris species¶

アイリスのクラス分析

irisデータセットはscikit-learnのload_iris関数で読み込むことができる.

from sklearn.datasets import load_iris
iris_dataset = load_iris()
# load_irisが返すirisオブジェクトは,ディクショナリによく似たBunchクラスのオブジェクトで,キーと値を持つ.
print("Keys of iris_dataset: {}".format(iris_dataset.keys()))
Keys of iris_dataset: dict_keys(['data', 'target', 'target_names', 'DESCR', 'feature_names'])
print(iris_dataset['DESCR'][:193] + "\n...")
Iris Plants Database
====================

Notes
-----
Data Set Characteristics:
    :Number of Instances: 150 (50 in each of three classes)
    :Number of Attributes: 4 numeric, predictive att
...

キーDESCRの値は,データセットの簡単な説明である.(description)ここでは,説明の最初だけ見る.

Iris Plants Database

Notes

Data Set Characteristics:特性 :Number of Instances: 150 (50 in each of three classes)インスタンスの数:150(3クラスにそれぞれ50ずつ) :Number of Attributes: 4 numeric, predictive att属性の数:4つの数値属性,予測に利用可能 ...

キーtarget_namesに対応する値は文字列の配列で, 予測しようとしている花の種類が格納されている

print("Target names:{}".format(iris_dataset['target_names']))
Target names:['setosa' 'versicolor' 'virginica']

キーfeature_namesに対応する値は文字列のリストで.それぞれの特徴量の説明が格納されている.

print("Feature names: \n{}".format(iris_dataset['feature_names']))
Feature names: 
['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)']

Feature names: 特徴量の名前 ガクの長さ 幅 花弁の長さ ['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)']花弁の幅

ダータ本題は,targetとdataフィールドに格納されている.dataには,ガクの長さ,ガクの幅などが,NumPy配列とし格納されている.

print("Type of data:{}".format(type(iris_dataset['data'])))
Type of data:<class 'numpy.ndarray'>

配列dataの行は個々の花に対応し,列は個々の花に対して行われた4つの測定に対応する.

print("Shape of data: {}".format(iris_dataset['data'].shape))
Shape of data: (150, 4)

配列には170の花の測定結果が格納されている.機械学習では個々のアイテムをサンプルといい,その特徴を特徴量と呼ぶことを思い出そう.data配列のshapeはサンプルの個数かける特徴量の数である.これはscikit-learnの慣習的表現である.

print("First five columns of data:\n{}".format(iris_dataset['data'][:5]))
First five columns of data:
[[ 5.1  3.5  1.4  0.2]
 [ 4.9  3.   1.4  0.2]
 [ 4.7  3.2  1.3  0.2]
 [ 4.6  3.1  1.5  0.2]
 [ 5.   3.6  1.4  0.2]]

このデータから,最初の5つの花は花弁の幅が全て0.2cmで,5つの中では最初の花が最も長い5.1cmのガクを持っていることがわかる.

配列targetには,測定された個々の花の種類が,やはりNumPy配列として格納されている.

print("Type of target: {}".format(type(iris_dataset['target'])))
Type of target: <class 'numpy.ndarray'>

targetは1次元の配列で,個々の花に1つのエントリが対応する.

print("Shape of target: {}".format(iris_dataset['target'].shape))
Shape of target: (150,)

種類は0から2までの整数としてエンコードされている.

print("Target:\n{}".format(iris_dataset['target']))
Target:
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2
 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
 2 2]

これらの数値の意味は,配列iris['target_names']で与えられる.0はsetosaを1はversicolorを2はvirginicaを意味する.

データセットを並べ替えて,分割するtrain_test_splitという関数がscikit-learnには用意されている.データのテストに用いる.

1.7.2

成功度合いの測定:訓練データとテストデータ

過学習した状態を換言すると,モデルがうまく汎化(generalize)できていない状態である.

scikit-learnでは,データを大文字のXで,ラベルを小文字のyで表すのが,一般的である.これは数式のf(x)=yに習っている.さらに数学での寒冷に従い,2次元配列(行列)であるデータには,大文字のXを,1次元配列(べクトル)であるラベルには,小文字yを用いる.

https://github.com/amueller/introduction_to_ml_with_python/raw/cccbbca86d00f9d5ffb643c740de4489de80436d/images/iris_petal_sepal.png

train_test_split関数を呼び出し結果を,この命名規則に従った変数に代入しよう.

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(
    iris_dataset['data'],iris_dataset['target'],random_state=0)

train_test_split関数は,分割を行う前に擬似乱数を用いてデータをシャッフルする.データポイントはラベルでソートされているので,単純に最後に25%をデータセットにすると,すべてのデータポイントがラベル2になってしまう.3クラスの内,1つしか含まれていないようなデータセットでは,モデルの汎化がうまく行っているか判断できない.だから,先にデータをシャッフルし,テストデータに全てのクラスが含まれるようにする.

同じ関数を何度か呼び出した際に,確実に同じ結果が得られるよう,ramdom_stateパラメータを用いて,擬似乱数生成器に同じシードを渡している.これによって出力が決定的になり,常に同じ結果が得られるようになる.本書では,乱数を用いる際には,常にこのようにrandom_stateパラメータを固定し用いる.

関数train_test_splitの出力は,X_train,X_test,y_test,y_train,y_testとなる.これらは全てNumpy配列で,X_trainにはデータセットの75%の行が,X_testには残りの25%が含まれている.

print("X_train shape:{}".format(X_train.shape))
print("y_train shape:{}".format(y_train.shape))
X_train shape:(112, 4)
y_train shape:(112,)
print("X_train shape: {}".format(X_train.shape))
print("y_train shape: {}".format(y_train.shape))
X_train shape: (112, 4)
y_train shape: (112,)

1.7.3 最初にすべきこと:データを良く観察する

データは精査すべきである.

データを検査する最良の方法は可視化である. その1つは,散布図である. 2次元の画面で散布図を効果的にプロットするのがペアプロットである. 全ての組合せ可能な特徴量の組み合わせをプロットするものだ. 特徴量の数が少ない場合にはうまくいく.しかし相関を同時にみることができないため,この方法で可視化してもデータの興味深い側面を見ることができない場合がある.

以下のグラフを作成するには,まずnumpy配列をpandasのdataframeに変換する.pandasはscatter_matrixと呼ばれるペアプロットを作成する関数を持つ.グラフマトリックスの対角部分にはここの特徴量のヒストグラムが描画される.

# X_trainのデータからDataFrameを作る.
# iris_dataset.feature_namesの文字列を使ってカラムに名前をつける
iris_datagrame = pd.DataFrame(X_train, columns=iris_dataset.feature_names)
# データフレームからscatter matrixを作成し,y_trainに従って色をつける
pd.plotting.scatter_matrix(iris_dataframe, c=y_train, figsize=(15, 15), marker='o',
                           hist_kwds={'bins': 20}, s=60, alpha=.8, cmap=mglearn.cm3)
----------------------------------------------------------------------

NameError                            Traceback (most recent call last)

<ipython-input-31-f2a828888675> in <module>()
      3 iris_datagrame = pd.DataFrame(X_train, columns=iris_dataset.feature_names)
      4 # データフレームからscatter matrixを作成し,y_trainに従って色をつける
----> 5 pd.plotting.scatter_matrix(iris_dataframe, c=y_train, figsize=(15, 15), marker='o',
      6                            hist_kwds={'bins': 20}, s=60, alpha=.8, cmap=mglearn.cm3)


NameError: name 'iris_dataframe' is not defined
# create dataframe from data in X_train
# label the columns using the strings in iris_dataset.feature_names
iris_dataframe = pd.DataFrame(X_train, columns=iris_dataset.feature_names)
# create a scatter matrix from the dataframe, color by y_train
grr = pd.plotting.scatter_matrix(iris_dataframe, c=y_train, figsize=(15, 15), marker='o',
                           hist_kwds={'bins': 20}, s=60, alpha=.8, cmap=mglearn.cm3)
----------------------------------------------------------------------

NameError                            Traceback (most recent call last)

<ipython-input-32-5c8cdb97d244> in <module>()
      4 # create a scatter matrix from the dataframe, color by y_train
      5 grr = pd.plotting.scatter_matrix(iris_dataframe, c=y_train, figsize=(15, 15), marker='o',
----> 6                            hist_kwds={'bins': 20}, s=60, alpha=.8, cmap=mglearn.cm3)


NameError: name 'mglearn' is not defined
# create dataframe from data in X_train
# label the columns using the strings in iris_dataset.feature_names
iris_dataframe = pd.DataFrame(X_train, columns=iris_dataset.feature_names)
# create a scatter matrix from the dataframe, color by y_train
pd.plotting.scatter_matrix(iris_dataframe, c=y_train, figsize=(15, 15), marker='o',
                           hist_kwds={'bins': 20}, s=60, alpha=.8, cmap=mglearn.cm3)
----------------------------------------------------------------------

NameError                            Traceback (most recent call last)

<ipython-input-33-0c4391853194> in <module>()
      4 # create a scatter matrix from the dataframe, color by y_train
      5 pd.plotting.scatter_matrix(iris_dataframe, c=y_train, figsize=(15, 15), marker='o',
----> 6                            hist_kwds={'bins': 20}, s=60, alpha=.8, cmap=mglearn.cm3)


NameError: name 'mglearn' is not defined
# create dataframe from data in X_train
# label the columns using the strings in iris_dataset.feature_names
iris_dataframe = pd.DataFrame(X_train, columns=iris_dataset.feature_names)
# create a scatter matrix from the dataframe, color by y_train
pd.plotting.scatter_matrix(iris_dataframe, c=y_train, figsize=(15, 15), marker='o',
                           hist_kwds={'bins': 20}, s=60, alpha=.8, cmap=mglearn.cm3)
----------------------------------------------------------------------

NameError                            Traceback (most recent call last)

<ipython-input-34-0c4391853194> in <module>()
      4 # create a scatter matrix from the dataframe, color by y_train
      5 pd.plotting.scatter_matrix(iris_dataframe, c=y_train, figsize=(15, 15), marker='o',
----> 6                            hist_kwds={'bins': 20}, s=60, alpha=.8, cmap=mglearn.cm3)


NameError: name 'mglearn' is not defined

1.7.4 最初のモデル:k-最近傍法

scikit-learnにはさまざまなクラス分類アルゴリズムが用意されている.ここでは,わかりやすい,k-最近傍法(k-Nearest Neighbors)によるクラス分類を用いる.このモデルを構築するには,単に訓練セットを格納するだけでよい.

このアルゴリズムは,新しいデータポイントに対して予測する際に,新しい点に最も近い点を訓練セットから探し,新しい点に最も近かった点のラベルを新しいデータポイントに与える.

k-最近傍法のkは,新しい点に最も近い1点だけを用いたりするのではなく,訓練セットの中の固定されたk個の近傍点(例えば3,5)を用いることができることを意味する.予測には,これらの近傍店の中の多数を占めるクラスを採用する.今回は,1つの近傍点しか使わない.

すべてのscikit-learnの機械学習モデルには,Estimatorと総称される個別のクラスに実装される.k-最近傍法クラス分類アルゴリズムはneighborsモジュールのKNeiborsClassifierクラスに実装されている.モデルを使う前に,クラスのインスタンスを生成してオブジェクトを作らなけれあならない.この際にパラメータを渡すことができる.KNeighborsClassfierの最も重要なパラメータは近傍点の数だが,ここでは1つとする.

from sklearn.neighbors import KNeighborsClassifier
knn = KNeighborsClassifier(n_neighbors=1)

knnオブジェクトは,訓練データからモデルを構築する際に用いられるアルゴリズムと新しいデータポイントに対して予測するためのアルゴリズムとをカプセル化している.さらに,訓練データからアルゴリズムがが抽出した情報も保持する.KNeighborsClassifierの場合には,単純に訓練データその斧を保持している.

訓練セットからモデルを構築するにはknnオブのfitメソッドを呼び出す.このメソッドはは,訓練データを含むNumpy配列X_trainとそれに対応する訓練ラベルを含むNumpu配列y_trainを引数に取る.

knn.fit(X_train, y_train)
KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski',
           metric_params=None, n_jobs=1, n_neighbors=1, p=2,
           weights='uniform')

このfitメソッドはknnオブエクとそのものを返す(同時にknnを書き換える)ので,出力にこのクラス分類オブジェクトの文字列表現が表示されている.この文字列表現から,モデルを構築する際に用いられたパラメータが分かる.ほとんどのパラメータがデフォルトだが,n_neighbors=1だけは,我々が与えたものだ.scigit-learnのモデルの多くは多数のパラメータがあるが,ほとんどは速度の最適化のためや,まれにしか使われないものだ.

1.7.5 予測を行う.

さて,ラベルが分かっていない新しいデータに対して予測をしてみよう.野生のアイリスを見つけたとして,ガク長5cm,ガク幅2.9,花弁の長さが1cm花弁の幅が0.2cm,である.このアイリスの品種はなんだろうか.このデータをnumpy配列に格納し,さの形を計算してみる

X_new = np.array([[5,2.9,1,0.2]])
print("X_new.shape:{}".format(X_new.shape))
X_new.shape:(1, 4)

ここで,1つの花の測定結果を2次元のnumpy配列にしていることに注意しよう.これは,scikit-learnが常に入力が2次元numpy配列であることを前提にするためだ. 予測を行うにはknnオブジェクトのpredickメソッドを呼ぶ.

prediction = knn.predict(X_new)
print("Prediction: {}".format(prediction))
print("Predicted target name: {}".format(
       iris_dataset['target_names'][prediction]))
Prediction: [0]
Predicted target name: ['setosa']

我々のモデルは,新しいアイリスをクラス0すなわち,setosaだと判断した.

しかし,これは正しいかどうかわからない.

1.7.6 モデルの評価

ここで先程作ったテストセットを用いる.モデルがどれくらい上手く機能していかどうか.精度(accuracy)を計算して測定できる. 精度は正しく品種を予測できたアイリスの割合である.

y_pred = knn.predict(X_test)
print("Test set predictions:\n {}".format(y_pred))
Test set predictions:
 [2 1 0 2 0 2 0 1 1 1 2 1 1 1 1 0 1 1 0 0 2 1 0 0 2 0 0 1 1 0 2 1 0 2 2 1 0
 2]
print("Test set score: {:.2f}".format(np.mean(y_pred == y_test)))
Test set score: 0.97

knnオブジェクトのscoreメソッドを用いても良い.このメソッドはテストセットに対応する精度を計算してくれる.

print("Test set score: {:.2f}".format(knn.score(X_test,y_test)))
Test set score: 0.97

このモデルでは,97%の精度で予測できた.

1.8 まとめと今後の展望

クラス分類問題では,分類結果となる品種はクラスと呼ばれ,個々のアイリスの品種はラベルと呼ばれる.

今回はk-最近傍法クラス分類アルゴリズムを用いた.これは,新しいデータポイントのラベルをそれと最も近い訓練データによって予測するアルゴリズムだ.このクラスはKNeighborsClassifierッックラスに実装される.

以下必要最小限の実装

X_train, X_test, y_train, y_test = train_test_split(
    iris_dataset['data'], iris_dataset['target'], random_state=0)

knn = KNeighborsClassifier(n_neighbors=1)
knn.fit(X_train, y_train)

print("Test set score: {:.2f}".format(knn.score(X_test, y_test)))
Test set score: 0.97

fit,predict,scoreメソッドは教師あり学習モデルに共通のインターフェースである.