目次
6. ランダム フォレストを使用した分類 (HSIC を使用して選択された特徴):
1. 実験の紹介
この実験では、特徴選択にHSIC LASSO (ヒルベルト・シュミット独立基準 LASSO) メソッドを実装し、ランダム フォレスト分類器を使用して選択された特徴サブセットを分類します。
特徴の選択は機械学習における重要なタスクの 1 つであり、モデルの有効性を向上させ、計算オーバーヘッドを削減し、データの主要な特徴を理解するのに役立ちます。
HSIC LASSO は、出力値に統計的に強く依存する非冗長な特徴を見つけるためのカーネル ベースの独立性測定方法です。
2. 実験環境
この一連の実験では PyTorch 深層学習フレームワークを使用し、関連する操作は次のとおりです (深層学習シリーズの記事の環境に基づく)。
1. 仮想環境を構成する
ディープラーニング連載記事の環境
conda create -n DL python=3.7
conda activate DL
pip install torch==1.8.1+cu102 torchvision==0.9.1+cu102 torchaudio==0.8.1 -f https://download.pytorch.org/whl/torch_stable.html
conda install matplotlib
conda install scikit-learn
新規追加
conda install pandas
conda install seaborn
conda install networkx
conda install statsmodels
pip install pyHSICLasso
注:私の実験環境では上記の順番で各種ライブラリをインストールしていますので、まとめてインストールしてみたい場合は(問題が起こるかどうかは神のみぞ知る)、
2. ライブラリバージョンの紹介
ソフトウェアパッケージ | 今回の実験版は | 現在の最新バージョン |
マットプロットライブラリ | 3.5.3 | 3.8.0 |
しこり | 1.21.6 | 1.26.0 |
パイソン | 3.7.16 | |
scikit-learn | 0.22.1 | 1.3.0 |
松明 | 1.8.1+cu102 | 2.0.1 |
トーショーディオ | 0.8.1 | 2.0.2 |
トーチビジョン | 0.9.1+cu102 | 0.15.2 |
新しい
ネットワークx | 2.6.3 | 3.1 |
パンダ | 1.2.3 | 2.1.1 |
pyHSICLase | 1.4.2 | 1.4.2 |
シーボーン | 0.12.2 | 0.13.0 |
状態モデル | 0.13.5 | 0.14.0 |
3.IDE
Pycharmの使用を推奨します(中でもpyHSICLassoライブラリはVScodeでエラーが発生しており、解決策はまだ見つかっていません...)
3. 実験内容
0. 必要なツールをインポートする
import random
import pandas as pd
from pyHSICLasso import HSICLasso
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix, classification_report
from sklearn.ensemble import RandomForestClassifier
1. データの読み取り
data = pd.read_csv("cancer_subtype.csv")
x = data.iloc[:, :-1]
y = data.iloc[:, -1]
2. トレーニング セットとテスト セットを分割する
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.3, random_state=10)
データセットをトレーニングセット ( X_train
sum y_train
) とテストセット ( X_test
sum y_test
) に分割します。そのうち、テスト セットはデータ全体の 30% を占めます。
3. HSIC LASSO 機能の選択を実行します。
random.seed(1)
le = LabelEncoder()
y_hsic = le.fit_transform(y_train)
x_hsic, fea_n = X_train.to_numpy(), X_train.columns.tolist()
hsic.input(x_hsic, y_hsic, featname=fea_n)
hsic.classification(200)
genes = hsic.get_features()
score = hsic.get_index_score()
res = pd.DataFrame([genes, score]).T
- ランダム シードを設定して、ランダム プロセスの再現性を確保します。
- ラベル エンコーディングを使用して
LabelEncoder
、ターゲット変数を数値形式に変換します。 - 特徴の選択は、トレーニング セット データ
X_train
とラベルをy_hsic
HSIC LASSO モデルに入力することによって実行されます。hsic.input
入力データと機能名の設定に使用されますhsic.classification
特徴選択のために HSIC LASSO アルゴリズムを実行するために使用されます- 選択したフィーチャは に保存されます
genes
。 - 対応する特徴スコアは に保存されます
score
。
- 選択したフィーチャは に保存されます
genes、score
DataFrame に保存され ますres
。
4. 特徴抽出
hsic_x_train = X_train[res[0]]
hsic_x_test = X_test[res[0]]
HSIC LASSO によって選択された特徴インデックスに従って、対応する特徴サブセットが元のトレーニング セットとX_train
テスト セットから抽出され、それぞれとに格納されます。X_test
hsic_x_train
hsic_x_test
5. ランダムフォレストによる分類(全特徴量使用)
rf_model = RandomForestClassifier(20)
rf_model.fit(X_train, y_train)
rf_pred = rf_model.predict(X_test)
print("RF all feature")
print(confusion_matrix(y_test, rf_pred))
print(classification_report(y_test, rf_pred, digits=5))
ランダム フォレスト分類子 (RandomForestClassifier) を使用して、すべての特徴を備えたトレーニング セットでトレーニングし、テスト セットで予測を行います。予測結果は に保存されrf_pred
、混同行列と分類レポートが出力されます。
6. ランダム フォレストを使用した分類 (HSIC を使用して選択された特徴):
rf_hsic_model = RandomForestClassifier(20)
rf_hsic_model.fit(hsic_x_train, y_train)
rf_hsic_pred = rf_hsic_model.predict(hsic_x_test)
print("RF HSIC feature")
print(confusion_matrix(y_test, rf_hsic_pred))
print(classification_report(y_test, rf_hsic_pred, digits=5))
ランダム フォレスト分類子は、HSIC LASSO を使用して選択された特徴サブセットでhsic_x_train
トレーニングされ、hsic_x_test
テスト セットの対応する特徴サブセットで予測が行われます。予測結果は に保存されrf_hsic_pred
、混同行列と分類レポートが出力されます。
7. コードの統合
# HSIC LASSO
# HSIC全称“Hilbert-Schmidt independence criterion”,“希尔伯特-施密特独立性指标”,跟互信息一样,它也可以用来衡量两个变量之间的独立性
# 核函数的特定选择,可以在基于核的独立性度量(如Hilbert-Schmidt独立性准则(HSIC))中找到对输出值具有很强统计依赖性的非冗余特征
# CIN 107 EBV 23 GS 50 MSI 47 normal 33
import random
import pandas as pd
from pyHSICLasso import HSICLasso
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix, classification_report
from sklearn.ensemble import RandomForestClassifier
data = pd.read_csv("cancer_subtype.csv")
x = data.iloc[:, :-1]
y = data.iloc[:, -1]
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.3, random_state=10)
random.seed(1)
le = LabelEncoder()
hsic = HSICLasso()
y_hsic = le.fit_transform(y_train)
x_hsic, fea_n = X_train.to_numpy(), X_train.columns.tolist()
hsic.input(x_hsic, y_hsic, featname=fea_n)
hsic.classification(200)
genes = hsic.get_features()
score = hsic.get_index_score()
res = pd.DataFrame([genes, score]).T
hsic_x_train = X_train[res[0]]
hsic_x_test = X_test[res[0]]
rf_model = RandomForestClassifier(20)
rf_model.fit(X_train, y_train)
rf_pred = rf_model.predict(X_test)
print("RF all feature")
print(confusion_matrix(y_test, rf_pred))
print(classification_report(y_test, rf_pred, digits=5))
rf_hsic_model = RandomForestClassifier(20)
rf_hsic_model.fit(hsic_x_train, y_train)
rf_hsic_pred = rf_hsic_model.predict(hsic_x_test)
print("RF HSIC feature")
print(confusion_matrix(y_test, rf_hsic_pred))
print(classification_report(y_test, rf_hsic_pred, digits=5))