LoginSignup
5
2

More than 5 years have passed since last update.

scikit-learnで機械学習の入口に立ってみた。

Posted at

概要

  • クラスチェンジするためには、下記を実行せよとお告げがあった。

    • 現在の知識(pandas,numpy,matplotlib)の知識を深めよ。
    • scikit-learnを学べ。
    • コミュニケーション能力は必要だ。
  • 実行しますか。
    -> はい
    いいえ

scikit-learnのtutorial

import

import

import numpy as np
import pandas as pd
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
iris_dataset = load_iris()
import seaborn as sns

%matplotlib inline

データの内容

  • irisのデータセットでは、下記のようにデータが含まれています。中でも、dataはfeature_namesのように4つのパーツのデータが入っています。
  • irisがどの種別に属するかは、targetで確認することができます。(target_namesで名称も確認できます。)
iris_dataset.keys()
>> dict_keys(['data', 'target', 'target_names', 'DESCR', 'feature_names', 'filename'])

iris_dataset.feature_names
>> ['sepal length (cm)',
 'sepal width (cm)',
 'petal length (cm)',
 'petal width (cm)']

iris_dataset.target
>>array([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_dataset.target_names
>> array(['setosa', 'versicolor', 'virginica'], dtype='<U10')

テストデータと訓練用データ

  • 下記でテストデータと訓練用データを分割します。デフォルトで全データの25%がテストデータとなります。
  • random_state=0としているのは、再現性を持たせるために使用しています。
train_test_split
X_train,X_test,y_train,y_test = train_test_split(iris_dataset.data,
                                                 iris_dataset.target,
                                                 random_state=0)

データの確認(可視化)

  • データフレームにして、可視化します。
  • 書籍では、pd.scatter_matrixを使ってた気がしますが、warningが出た気がしたので、下記のようにしました。
    • warningは、pd.plotting.scatter_matrixで回避できますがseabornのほうが楽だったので手を抜きました簡潔に実装できます。
  • こうしてみると、setosa:0は他の2種と明確に別れており、versicolor:1とvirginica:2もなんとか分離できそうな感じがします。
dataframe
df_iris_train = pd.DataFrame(X_train,columns=iris_dataset.feature_names)
df_iris_train = pd.concat([df_iris_train,pd.DataFrame(y_train,columns=['target'])],axis=1)

sns.pairplot(df_iris_train,hue='target')

googleColaboratry.png

k近傍法

  • n_neighborsはデフォルトで5ですが、1にしました。
    • n_neighborsが大きいとノイズに強いそうですが、精度が落ちそうなため1にしました。(前段の可視化でノイズが少なそうだと判断したため)
KNeighborsClassifier
knn = KNeighborsClassifier(n_neighbors=1)
knn.fit(X_train,y_train)

結果

  • 精度は97%となり、かなり高精度で予測できていることがわかります。
    • 元々きれいなデータなのでこれくらいは当然なのでしょうか?
score
knn.score(X_test,y_test)
>> 0.9736842105263158

まとめ

  • matplotlib使うの忘れてる・・・
  • pairplot便利すぎわろた
  • 機械学習の入口に立てた気はします。
  • 理論を理解してしないと、変数をいじれないのがもどかしいので勉強します。
  • 次のキャリアパスに向けて、より高みを目指す。
5
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
2