Sklearn研究ノート:一般的な章
前に書く
Sklearnの公式ドキュメントの内容は非常に詳細ですが、初心者が学ぶのはあまり友好的ではないようです。
この一連の「学習ノート」は、Sklearnの公式文書を参照して編集されています。構造は基本的に変更されておらず、内容が削除されることはめったにありません(詳細すぎて「偏っている」)ので、後で参照して確認できます。
0.はじめに
Scikit-learnは、教師あり学習と教師なし学習をサポートするオープンソースの機械学習ライブラリです。また、モデルのフィッティング、データの前処理、モデルの選択と評価、およびその他の多くのツールのためのさまざまなツールも提供します。
0.1フィッティングと予測:推定量の基礎(出力)
(フィッティングと予測:推定量の基本)
- Scikit-learnで提供される多数の組み込みの機械学習アルゴリズムとモデルは推定量と呼ばれます。
- 各推定量は、fit()メソッドを使用してデータを近似できます。
- fit(X)→y、fit()関数は2つの入力を受け取ります
- Xの形式:(n_samples、n_features)、つまり、サンプル行と特徴行。
- yの値は、実数(回帰タスク)または整数(分類タスク)です。
- Xとyはどちらも、numpy配列または配列のようなデータ型である必要があります。
フィッティング:
>>> from sklearn.ensemble import RandomForestClassifier
>>> clf = RandomForestClassifier(random_state=0)
>>> X = [[ 1, 2, 3], # 2 samples, 3 features
... [11, 12, 13]]
>>> y = [0, 1] # classes of each sample
>>> clf.fit(X, y)
RandomForestClassifier(random_state=0)
予測:
>>> clf.predict(X) # predict classes of the training data
array([0, 1])
>>> clf.predict([[4, 5, 6], [14, 15, 16]]) # predict classes of new data
array([0, 1])
0.2コンバーターとプリプロセッサー(入力)
(トランスフォーマーとプリプロセッサー)
一般的なパイプライン(パイプライン)には、次の2つの部分があります。①
入力としてのプリプロセッサ:
データの変換または「代入?」
②出力としての予測子:ターゲットの予測値予測子(予測子)
>>> from sklearn.preprocessing import StandardScaler
>>> X = [[0, 15],
... [1, -10]]
>>> StandardScaler().fit(X).transform(X)
array([[-1., 1.],
[ 1., -1.]])
0.3パイプライン:リンクされたプリプロセッサと評価者
コンバーターとエバリュエーター/プレディクターは、統合された全体(オブジェクト)に統合されます:パイプライン、すなわち:
トランスフォーマー+推定量(予測子)=パイプライン
注:Sklearn-learnの推定量と予測量は直接同等である必要があります(推定量(予測量))
例:
>>> from sklearn.preprocessing import StandardScaler
>>> from sklearn.linear_model import LogisticRegression
>>> from sklearn.pipeline import make_pipeline
>>> from sklearn.datasets import load_iris
>>> from sklearn.model_selection import train_test_split
>>> from sklearn.metrics import accuracy_score
...
>>> # create a pipeline object
>>> pipe = make_pipeline(
... StandardScaler(),
... LogisticRegression(random_state=0)
... )
...
>>> # load the iris dataset and split it into train and test sets
>>> X, y = load_iris(return_X_y=True)
>>> X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
...
>>> # fit the whole pipeline
>>> pipe.fit(X_train, y_train)
Pipeline(steps=[('standardscaler', StandardScaler()),
('logisticregression', LogisticRegression(random_state=0))])
>>> # we can now use it like any other estimator
>>> accuracy_score(pipe.predict(X_test), y_test)
0.97...
0.4モデル評価
(モデル評価)
モデルのトレーニングが完了したら、目に見えない「新しいデータ」を直接予測するのではなく、モデルをすぐに評価する必要があります。
上で使用したのは、データセットをトレーニングセットとテストセットに分割するtrain_test_split()メソッドですが、scikit-learnには、モデル検証、特に相互検証のための他の多くのツールがあります。
例として5分割交差検定を取り上げます。
>>> from sklearn.datasets import make_regression
>>> from sklearn.linear_model import LinearRegression
>>> from sklearn.model_selection import cross_validate
...
>>> X, y = make_regression(n_samples=1000, random_state=0)
>>> lr = LinearRegression()
...
>>> result = cross_validate(lr, X, y) # defaults to 5-fold CV
>>> result['test_score'] # r_squared score is high because dataset is easy
array([1., 1., 1., 1., 1.])
0.5自動パラメータ検索
すべてのエバリュエーターにはパラメーター(またはハイパーパラメーター)があり、デバッグできます。通常の状況では、パラメータ値は手元のデータによって決定されるため、パラメータ値の選択方法がわかりません。
Sklearnは、(相互検証を通じて)最適なパラメーターの組み合わせを自動的に見つけることができるいくつかのツールを提供します。例としてRandomizedSearchCVオブジェクトを取り上げましょう。検索が終了すると、RandomizedSearchCVはRandomForestRegressorと同様の役割になり、最適なパラメーターの組み合わせによってトレーニングされます。
>>> from sklearn.datasets import fetch_california_housing
>>> from sklearn.ensemble import RandomForestRegressor
>>> from sklearn.model_selection import RandomizedSearchCV
>>> from sklearn.model_selection import train_test_split
>>> from scipy.stats import randint
...
>>> X, y = fetch_california_housing(return_X_y=True)
>>> X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
...
>>> # define the parameter space that will be searched over
>>> param_distributions = {
'n_estimators': randint(1, 5),
... 'max_depth': randint(5, 10)}
...
>>> # now create a searchCV object and fit it to the data
>>> search = RandomizedSearchCV(estimator=RandomForestRegressor(random_state=0),
... n_iter=5,
... param_distributions=param_distributions,
... random_state=0)
>>> search.fit(X_train, y_train)
RandomizedSearchCV(estimator=RandomForestRegressor(random_state=0), n_iter=5,
param_distributions={
'max_depth': ...,
'n_estimators': ...},
random_state=0)
>>> search.best_params_
{
'max_depth': 9, 'n_estimators': 4}
>>> # the search object now acts like a normal random forest estimator
>>> # with max_depth=9 and n_estimators=4
>>> search.score(X_test, y_test)
0.73...
注:個別のエバリュエーターではなく、パイプラインを検索してください。
注:
実際には、ほとんどの場合、単一の推定量ではなく、パイプラインを検索する必要があります。主な理由の1つは、パイプラインを使用せずにデータセット全体に前処理ステップを適用してから、あらゆる種類の相互検証を実行すると、トレーニングデータとテストデータの間の独立性の基本的な仮定を破ることになるためです。実際、データセット全体を使用してデータを前処理したため、テストセットに関するいくつかの情報をトレインセットで利用できます。これは、推定量の一般化力を過大評価することにつながります(このkaggleの投稿で詳細を読むことができます)。
相互検証と検索にパイプラインを使用すると、この一般的な落とし穴を大幅に防ぐことができます。
1.前処理
2.モデルの選択
3.アルゴリズム
3.1分類
3.2回帰
3.3クラスタリング
3.4次元削減
API
調査するデータマイニング
にはsklearnを使用してください〜
最後に書く
今後も、シリーズのまとめとまとめとして「LearningSummary」シリーズを更新していきます。
参照: