
[code]

    import pandas as pd
    from sklearn.model_selection import cross_val_score, train_test_split
    from sklearn.metrics import accuracy_score, confusion_matrix
    from sklearn.tree import DecisionTreeClassifier
    from sklearn.metrics import roc_curve, auc
    import matplotlib.pyplot as plt
    from sklearn.ensemble import RandomForestClassifier
    from sklearn.model_selection import train_test_split, GridSearchCV
    from sklearn.metrics import accuracy_score
    
    
    #データの読み込み
    data = pd.read_csv('BioAsseT pt3 PRRS.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.2, random_state=42)
    
    
    param_grid = {
        'n_estimators': [300],
        'max_depth': [10],
        'min_samples_split': [3],
        'min_samples_leaf': [2],
    }
    
    # ランダムフォレストモデルの作成
    model = RandomForestClassifier(random_state=42)
    
    # GridSearchCVを用いてパラメータのチューニング
    grid_search = GridSearchCV(estimator=model, param_grid=param_grid, cv=10)
    grid_search.fit(X_train, Y_train)
    
    # チューニングされたモデルの取得
    best_model = grid_search.best_estimator_
    
    # テストデータを用いた予測
    y_pred = best_model.predict(X_test)
    
    # 精度の評価
    accuracy = accuracy_score(Y_test, y_pred)
    print("Accuracy:", accuracy) 
    
    import numpy as np
    
    feature_gini_coefficients = []
    for i in range(len(data.columns)-1):
        # 特徴量を除いた残りの特徴量でデータを作成
        X = data.drop(columns=[data.columns[i]])
        # モデルを学習
        best_model.fit(X, Y)
        # 特徴量の重要度を取得
        feature_importances = best_model.feature_importances_
        # ジニ係数を計算
        gini_index = 1 - np.sum(np.square(feature_importances))
        # 結果をリストに追加
        feature_gini_coefficients.append(gini_index)
    
    # 結果を表示
    for i in range(len(data.columns)-1):
        print("Feature: {}, Gini coefficient: {:.2f}".format(data.columns[i], feature_gini_coefficients[i]))
    
[/code]

結果が以下の通りです

[code]

    Feature: external, Gini coefficient: 0.79
    Feature: es1, Gini coefficient: 0.79
    Feature: es2, Gini coefficient: 0.78
    Feature: es3, Gini coefficient: 0.79
    Feature: es4, Gini coefficient: 0.79
    Feature: es5, Gini coefficient: 0.79
    Feature: es6, Gini coefficient: 0.79
    Feature: es7, Gini coefficient: 0.78
    Feature: es8, Gini coefficient: 0.78
    Feature: es9, Gini coefficient: 0.78
    Feature: internal, Gini coefficient: 0.79
    Feature: is1, Gini coefficient: 0.78
    Feature: is2, Gini coefficient: 0.78
    Feature: is3, Gini coefficient: 0.78
    Feature: is4, Gini coefficient: 0.78
    Feature: is5, Gini coefficient: 0.78
    Feature: is6, Gini coefficient: 0.78
    Feature: is7, Gini coefficient: 0.77
    Feature: diagnostic, Gini coefficient: 0.78
    Feature: ds1, Gini coefficient: 0.77
    Feature: ds2, Gini coefficient: 0.78
    Feature: ds3, Gini coefficient: 0.78
    Feature: ds4, Gini coefficient: 0.77 ```
    
[/code]


CC BY-SA 4.0
2023-06-15T06:47:00.403
95240
2023-06-15T06:47:00.403
56639
question
python 機械学習
モデルの正解率が96％にもかかわらず、各特徴量のジニ係数の結果があまりにもばらつきがないため過学習もしくはチューニング手法を間違っていると考えましたがどこでミスが起きているのかわからず助言をいただきたいです