[バイオインフォマティクス] HSIC LASSO法を用いた特徴選択

目次

1. 実験の紹介

2. 実験環境

1. 仮想環境を構成する

2. ライブラリバージョンの紹介

3.IDE

3. 実験内容

0. 必要なツールをインポートする

1. データの読み取り

2. トレーニング セットとテスト セットを分割する

3. HSIC LASSO 機能の選択を実行します。

4. 特徴抽出

5. ランダムフォレストによる分類(全特徴量使用)

6. ランダム フォレストを使用した分類 (HSIC を使用して選択された特徴):

7. コードの統合


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でエラーが発生しており、解決策はまだ見つかっていません...)

win11 インストール Anaconda (2022.10) + pycharm (2022.3/2023.1.4) + 仮想環境の構成_QomolangmaH のブログ - CSDN ブログ https://blog.csdn.net/m0_63834988/article/details/128693741 icon-default.png?t=N7T8https://blog.csdn .net/ m0_63834988/記事/詳細/128693741

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_trainsum y_train) とテストセット ( X_testsum 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_hsicHSIC LASSO モデルに入力することによって実行されます。
    • hsic.input入力データと機能名の設定に使用されます
    • hsic.classification特徴選択のために HSIC LASSO アルゴリズムを実行するために使用されます
      • 選択したフィーチャは に保存されますgenes
      • 対応する特徴スコアは に保存されますscore
    • genes、scoreDataFrame に保存され ますres

4. 特徴抽出

hsic_x_train = X_train[res[0]]
hsic_x_test = X_test[res[0]]

        HSIC LASSO によって選択された特徴インデックスに従って、対応する特徴サブセットが元のトレーニング セットX_trainテスト セットから抽出され、それぞれとに格納されますX_testhsic_x_trainhsic_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))

おすすめ

転載: blog.csdn.net/m0_63834988/article/details/133443975