人工手动筛选特征
创始人
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)

相关内容

热门资讯

终于清楚!手机脚本辅助软件(普... 终于清楚!手机脚本辅助软件(普及辅助插件)本来有透视手筋1、手机脚本辅助软件辅助软件下载优化,手机脚...
指南辅助!hhpoker有没有... 指南辅助!hhpoker有没有辅助(辅助)总是真的是有辅助工具(有挂方针)1、游戏颠覆性的策略玩法,...
玩家必备科普!微乐小程序免费黑... 您好,微乐小程序免费黑科技这款游戏可以开挂的,确实是有挂的,需要了解加微【485275054】很多玩...
玩家必看科普!欢乐联盟游戏辅助... 玩家必看科普!欢乐联盟游戏辅助(分享辅助app)切实有透视指南1、首先打开欢乐联盟游戏辅助辅助器下载...
要领辅助!wepoker透视脚... 要领辅助!wepoker透视脚本免费app(辅助)都是存在有辅助方法(今日头条)wepoker透视脚...
重大推荐!微乐小程序免费黑科技... 重大推荐!微乐小程序免费黑科技,微乐河北麻将辅助助手,微扑克教程(果真有挂);最新版2026是一款经...
玩家亲测!雀友会广东潮汕苹果(... 玩家亲测!雀友会广东潮汕苹果(详情辅助脚本)确实有透视指南1、完成雀友会广东潮汕苹果有辅助插件,帮助...
诀窍辅助!hhpoker可以控... 诀窍辅助!hhpoker可以控制吗(辅助)本来有辅助挂(果真有挂)1、hhpoker可以控制吗辅助软...
推荐一款!微乐小程序免费黑科技... 推荐一款!微乐小程序免费黑科技,微乐广西麻将辅助工具,必赢方法(有挂解惑);是一款可以让一直输的玩家...
玩家必看秘籍!悟空大厅微信辅助... 玩家必看秘籍!悟空大厅微信辅助(分享辅助神器)真是有透视要领悟空大厅微信辅助是不是有人用挂微扑克wp...