人工手动筛选特征
创始人
2024-11-15 02:04:05
0

一、相关性分析

计算特征之间的相关性,并移除高度相关的特征(相关系数大于 0.95)

import pandas as pd   import numpy as np    # 创建一个示例数据框   data = {       'A': [1, 2, 3, 4, 5],       'B': [5, 4, 3, 2, 1],       'C': [1, 1, 2, 2, 3],       'D': [5, 3, 2, 4, 5],       'E': [2, 4, 6, 8, 10]   }    X = pd.DataFrame(data)   print("原始数据框:")   print(X)    # 计算相关矩阵   corr_matrix = X.corr().abs()   print("\n相关矩阵:")   print(corr_matrix)    # 获取上三角矩阵   upper = corr_matrix.where(np.triu(np.ones(corr_matrix.shape), k=1).astype(bool))   print("\n上三角矩阵:")   print(upper)    # 找出任何相关系数大于 0.95 的列   to_drop = [column for column in upper.columns if any(upper[column] > 0.95)]   print("\n要移除的特征:")   print(to_drop)    # 移除高相关特征   X_reduced = X.drop(to_drop, axis=1)   print("\n去除高度相关后的数据框:")   print(X_reduced)

二、重要性分析

提取和展示模型中各特征的重要性,尤其是查看特征重要性为零的特征以及非零特征。

import pandas as pd   import lightgbm as lgb   from sklearn.datasets import load_breast_cancer   from sklearn.model_selection import train_test_split    # 加载数据集   data = load_breast_cancer()   X = pd.DataFrame(data.data, columns=data.feature_names)   y = pd.Series(data.target)    # 拆分训练和测试数据   X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)    # 创建 LightGBM 数据集   d_train = lgb.Dataset(X_train, label=y_train)    # 训练 LightGBM 模型   model = lgb.train(params={'objective': 'binary'}, train_set=d_train, num_boost_round=100)    # 获取特征重要性   fea_iptc = pd.DataFrame({       'feature_name': X.columns,       'importance': model.feature_importance()   })    # 显示特征重要性数据框   print(fea_iptc)    # 找到特征重要性为0的特征   fea_importance_0 = fea_iptc['feature_name'].loc[fea_iptc['importance'] == 0].tolist()   print('fea_importance_0: ', len(fea_importance_0), fea_importance_0)    # 找到特征重要性不为0的特征   fea_importance_not_0 = fea_iptc['feature_name'].loc[fea_iptc['importance'] != 0].tolist()   print('fea_importance_not_0: ', len(fea_importance_not_0), fea_importance_not_0)

三、iv和psi分析

使用信息值(IV)和群体稳定指数(PSI)筛选特征

import pandas as pd    # 示例数据:特征名称与它们的 IV 和 PSI 值   data = {       'name': ['feature1', 'feature2', 'feature3', 'feature4', 'feature5'],       'iv': [0.02, 0.15, 0.03, 0.01, 0.5],  # IV 值       'psi': [0.04, 0.02, 0.06, 0.07, 0.03]  # PSI 值   }    # 创建 DataFrame   iv_df = pd.DataFrame(data)    # 定义筛选特征的函数   def select_features_by_iv_psi(feature_stats, iv_threshold=0.01, psi_threshold=0.05):       """根据IV和PSI值筛选特征"""       high_iv_features = feature_stats[feature_stats['iv'] >= iv_threshold]       selected_features = high_iv_features[high_iv_features['psi'] < psi_threshold]       return selected_features['name'].tolist()    # 可选的特征列表   selected_features_list = ['feature1', 'feature2', 'feature3']    # 过滤特征   filtered_iv_df = iv_df[iv_df['name'].isin(selected_features_list)]    # 使用筛选函数   selected_features = select_features_by_iv_psi(filtered_iv_df)    # 输出筛选结果   print("Selected features:", len(selected_features), selected_features)

相关内容

热门资讯

推荐一款!大凉山生活号跑得快有... 推荐一款!大凉山生活号跑得快有挂吗(透明挂)果然是有挂(2022已更新)(哔哩哔哩);1、用户打开应...
9分钟辅助挂!中至吉安系统发牌... 9分钟辅助挂!中至吉安系统发牌,wEpoKe本来是真的有挂,必赢方法(有挂教程);中至吉安系统发牌是...
我来向大家传授!吉祥游戏填大坑... 我来向大家传授!吉祥游戏填大坑外挂(透视辅助)透明挂透视辅助器(2023已更新)(哔哩哔哩)1、全新...
2024教程!蛮籽麻将有没有套... 2024教程!蛮籽麻将有没有套路(辅助挂)确实是有挂(2024已更新)(哔哩哔哩)1、蛮籽麻将有没有...
二分钟辅助挂!博雅红河辅助,来... 二分钟辅助挂!博雅红河辅助,来玩德州app原来存在有挂,插件教程(有挂普及);1、博雅红河辅助系统规...
分享实测!决战十三水ios版辅... 分享实测!决战十三水ios版辅助器(辅助挂)透视脚本辅助神器(2020已更新)(哔哩哔哩);1、决战...
一分钟了解!微信版福建旺旺麻将... 一分钟了解!微信版福建旺旺麻将有挂吗(辅助)切实真的有挂(2021已更新)(哔哩哔哩);一分钟了解!...
3分钟了解!雀神小程序辅助,w... 3分钟了解!雀神小程序辅助,wepoke其实真的有挂,解密教程(有挂解说)1、雀神小程序辅助ai辅助...
玩家必看!边锋保皇辅助工具(透... 玩家必看!边锋保皇辅助工具(透明挂)外挂透视辅助神器(2024已更新)(哔哩哔哩);1、进入游戏-大...
每日必看!福建兄弟十三水是否有... 每日必看!福建兄弟十三水是否有外 挂(透视辅助)本来存在有挂(2022已更新)(哔哩哔哩);一、福建...