Machine-learning-with-python-data-feature-selection

提供:Dev Guides
移動先:案内検索

機械学習-データ機能の選択

前の章では、機械学習のためにデータを前処理して準備する方法について詳しく見てきました。 この章では、データの特徴選択とそれに関連するさまざまな側面について詳しく説明します。

データ機能選択の重要性

機械学習モデルのパフォーマンスは、トレーニングに使用されるデータ機能に直接比例します。 提供されるデータ機能が無関係な場合、MLモデルのパフォーマンスは悪影響を受けます。 一方、関連するデータ機能を使用すると、MLモデル、特に線形およびロジスティック回帰の精度を高めることができます。

ここで、自動機能選択とは何かという疑問が生じます。 これは、出力または予測変数に最も関連性のあるデータ内の特徴を選択する助けとなるプロセスとして定義される場合があります。 属性選択とも呼ばれます。

以下は、データをモデル化する前の自動機能選択の利点の一部です-

  • データモデリングの前に特徴選択を実行すると、過剰適合が減少します。
  • データモデリングの前にフィーチャ選択を実行すると、MLモデルの精度が向上します。
  • データモデリングの前に機能選択を実行すると、トレーニング時間が短縮されます。

機能選択テクニック

以下は、PythonでMLデータをモデル化するために使用できる自動機能選択技術です-

単変量選択

この特徴選択手法は、統計的テストの助けを借りて、予測変数と最も強い関係を持つこれらの特徴を選択するのに非常に役立ちます。 scikit-learn PythonライブラリのSelectKBest0classの助けを借りて、単変量特徴選択手法を実装できます。

この例では、Pima Indians Diabetesデータセットを使用して、カイ2乗統計検定を使用して、最適な機能を備えた4つの属性を選択します。

from pandas import read_csv
from numpy import set_printoptions
from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import chi2
path = r'C:\pima-indians-diabetes.csv'
names = ['preg', 'plas', 'pres', 'skin', 'test', 'mass', 'pedi', 'age', 'class']
dataframe = read_csv(path, names=names)
array = dataframe.values

次に、配列を入力コンポーネントと出力コンポーネントに分けます-

X = array[:,0:8]
Y = array[:,8]

次のコード行は、データセットから最適な機能を選択します-

test = SelectKBest(score_func=chi2, k=4)
fit = test.fit(X,Y)

また、選択に応じて出力用のデータを要約することもできます。 ここでは、精度を2に設定し、各属性の最高のスコアとともに最高の機能を備えた4つのデータ属性を示しています-

set_printoptions(precision=2)
print(fit.scores_)
featured_data = fit.transform(X)
print ("\nFeatured data:\n", featured_data[0:4])

出力

[ 111.52 1411.89 17.61 53.11 2175.57 127.67 5.39 181.3 ]
Featured data:
[[Recursive Feature Elimination

As the name suggests, RFE (Recursive feature elimination) feature selection technique removes the attributes recursively and builds the model with remaining attributes. We can implement RFE feature selection technique with the help of _RFE_ class of _scikit-learn_ Python library.

==== Example

In this example, we will use RFE with logistic regression algorithm to select the best 3 attributes having the best features from Pima Indians Diabetes dataset to.

[source,prettyprint,notranslate]

パンダからread_csvをsklearn.feature_selectionからインポートRFEをsklearn.linear_modelからインポートLogisticRegression path = r’C:\ pima-indians-diabetes.csv 'names = [' preg '、' plas '、' pres '、' skin '、 'test'、 'mass'、 'pedi'、 'age'、 'class'] dataframe = read_csv(path、names = names)array = dataframe.values

Next, we will separate the array into its input and output components −

[source,result,notranslate]

X = array [:、0:8] Y = array [:、8]

The following lines of code will select the best features from a dataset −

[source,prettyprint,notranslate]

model = LogisticRegression()rfe = RFE(model、3)fit = rfe.fit(X、Y)print( "機能の数:%d")print( "選択された機能:%s")print( "機能ランキング: %s ")

==== Output

[source,result,notranslate]

機能の数:3選択された機能:[True False False False False True True True False]機能ランキング:[1 2 3 5 6 1 1 4]

We can see in above output, RFE choose preg, mass and pedi as the first 3 best features. They are marked as 1 in the output.

=== Principal Component Analysis (PCA)

PCA, generally called data reduction technique, is very useful feature selection technique as it uses linear algebra to transform the dataset into a compressed form. We can implement PCA feature selection technique with the help of PCA class of scikit-learn Python library. We can select number of principal components in the output.

==== Example

In this example, we will use PCA to select best 3 Principal components from Pima Indians Diabetes dataset.

[source,prettyprint,notranslate]

パンダからread_csvをsklearn.decompositionからインポートimport PCA path = r’C:\ pima-indians-diabetes.csv 'names = [' preg '、' plas '、' pres '、' skin '、' test '、' mass '、' pedi '、' age '、' class '] dataframe = read_csv(path、names = names)array = dataframe.values

Next, we will separate array into input and output components −

[source,result,notranslate]

X = array [:、0:8] Y = array [:、8]

The following lines of code will extract features from dataset −

[source,prettyprint,notranslate]

pca = PCA(n_components = 3)fit = pca.fit(X)print( "説明された分散:%s")%fit.explained_variance_ratio_ print(fit.components_)

==== Output

[source,result,notranslate]

差異の説明:[0.88854663 0.06159078 0.02579012] [[e-03 9.78115765e-02 1.60930503e-02 6.07566861e-02 9.93110844e-01 1.40108085e-02 5.37167919e-04 -3.56474430e-03] [2.26488861e-02 9.72210040e -01 1.41909330e-01 -5.78614699e-02 -9.46266913e-02 4.69729766e-02 8.16804621e-04 1.40168181e-01] [-2.24649003e-02 1.43428710e-01 -9.22467192e-01 -3.07013055e-01 2.09773019 e-02 -1.32444542e-01 -6.39983017e-04 -1.25454310e-01]]

We can observe from the above output that 3 Principal Components bear little resemblance to the source data.

=== Feature Importance

As the name suggests, feature importance technique is used to choose the importance features. It basically uses a trained supervised classifier to select features. We can implement this feature selection technique with the help of ExtraTreeClassifier class of scikit-learn Python library.

==== Example

In this example, we will use ExtraTreeClassifier to select features from Pima Indians Diabetes dataset.

[source,prettyprint,notranslate]

pandasからread_csvをsklearn.ensembleからインポートExtraTreesClassifier path = r’C:\ Desktop \ pima-indians-diabetes.csv 'names = [' preg '、' plas '、' pres '、' skin '、' test '、 'mass'、 'pedi'、 'age'、 'class'] dataframe = read_csv(data、names = names)array = dataframe.values

Next, we will separate array into input and output components −

[source,result,notranslate]

X = array [:、0:8] Y = array [:、8]

The following lines of code will extract features from dataset −

[source,result,notranslate]

モデル= ExtraTreesClassifier()model.fit(X、Y)print(model.feature_importances_)

==== Output

[source,result,notranslate]

[ 0.11070069 0.2213717 0.08824115 0.08068703 0.07281761 0.14548537 0.12654214 0.15415431]

From the output, we can observe that there are scores for each attribute. The higher the score, higher is the importance of that attribute.