gpt4 book ai didi

python - 'one-hot-encoded' 的 'feature Importance' 变量的显示名称

转载 作者:行者123 更新时间:2023-12-01 02:37:57 24 4
gpt4 key购买 nike

完成算法的训练和验证后,如何正确显示“one-hot-encoded”特征的名称?我想整齐地显示每个功能的名称及其重要性。以下是我尝试过的:

显示功能重要性:

grid_search.best_estimator_.feature_importances_
array([ 7.67359589e-02, 7.20731884e-02, 4.38667330e-02,
1.69222269e-02, 1.51816327e-02, 1.66947835e-02,
1.56858183e-02, 3.43347923e-01, 5.95555727e-02,
7.65422356e-02, 1.11224727e-01, 1.02677088e-02,
1.32720377e-01, 1.06447326e-04, 4.45207929e-03,
4.62258699e-03])

获取热门类别名称:

cat_one_hot_attribs = list(encoder.classes_)
print(cat_one_hot_attribs)
['<1H OCEAN', 'INLAND', 'ISLAND', 'NEAR BAY', 'NEAR OCEAN']

获取其余名称(其他类别):

num_attribs = list(X_train)

['longitude',
'latitude',
'housing_median_age',
'total_rooms',
'total_bedrooms',
'population',
'households',
'median_income',
'rooms_per_household',
'bedrooms_per_household',
'population_per_household',
0,
1,
2,
3,
4]

现在我执行以下操作:

attributes = num_attribs + cat_one_hot_attribs

print(pd.DataFrame(sorted(zip(feature_importance, attributes), reverse=True)))

但我得到以下信息:

         0                         1
0 0.343348 median_income
1 0.132720 1
2 0.111225 population_per_household
3 0.076736 longitude
4 0.076542 bedrooms_per_household
5 0.072073 latitude
6 0.059556 rooms_per_household
7 0.043867 housing_median_age
8 0.016922 total_rooms
9 0.016695 population
10 0.015686 households
11 0.015182 total_bedrooms
12 0.010268 0
13 0.004623 4
14 0.004452 3
15 0.000106 2

我也尝试过其他方法,但都失败了。

有人可以建议一种方法来使其正确显示吗?谢谢。

编辑:

根据@cᴏʟᴅsᴘᴇᴇᴅ的回答,我尝试了以下方法:

feature_importance = grid_search.best_estimator_.feature_importances_

cat_one_hot_attribs = list(encoder.classes_)

num_attribs = list(X_train)
attributes = num_attribs + cat_one_hot_attribs

vals = sorted(zip(feature_importance, attributes), key=lambda x: x[0], reverse=True)
df = pd.DataFrame(vals)
print(df)

仍然得到如上所述的输出。

最佳答案

分解它。首先按键排序。确保仅考虑feature_importance

设置:

import pandas as pd
import numpy as np

feature_importance = np.array([ 7.67359589e-02, 7.20731884e-02, 4.38667330e-02,
1.69222269e-02, 1.51816327e-02, 1.66947835e-02,
1.56858183e-02, 3.43347923e-01, 5.95555727e-02,
7.65422356e-02, 1.11224727e-01, 1.02677088e-02,
1.32720377e-01, 1.06447326e-04, 4.45207929e-03,
4.62258699e-03])

cat_one_hot_attribs = ['<1H OCEAN', 'INLAND', 'ISLAND', 'NEAR BAY', 'NEAR OCEAN']

num_attribs = ['longitude',
'latitude',
'housing_median_age',
'total_rooms',
'total_bedrooms',
'population',
'households',
'median_income',
'rooms_per_household',
'bedrooms_per_household',
'population_per_household',
0,
1,
2,
3,
4]

attributes = num_attribs
<小时/>

获取按feature_importance排序的vals列表。

vals = sorted(zip(feature_importance, attributes), key=lambda x: x[0], reverse=True)
df = pd.DataFrame(vals)

然后,使用.replace将编码替换为cat_one_hot_attribs中的值。

df.iloc[:, -1] = df.iloc[:, -1].replace({i : k for i, k in enumerate(cat_one_hot_attribs)})
df

0 1
0 0.343348 median_income
1 0.132720 INLAND
2 0.111225 population_per_household
3 0.076736 longitude
4 0.076542 bedrooms_per_household
5 0.072073 latitude
6 0.059556 rooms_per_household
7 0.043867 housing_median_age
8 0.016922 total_rooms
9 0.016695 population
10 0.015686 households
11 0.015182 total_bedrooms
12 0.010268 <1H OCEAN
13 0.004623 NEAR OCEAN
14 0.004452 NEAR BAY
15 0.000106 ISLAND

关于python - 'one-hot-encoded' 的 'feature Importance' 变量的显示名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46025147/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com