栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

一种可以根据相关性进行特征筛选的函数

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

一种可以根据相关性进行特征筛选的函数

#去掉相关性太高的特征
def identify_collinear(corr_matrix,correlation_threshold,X):
    #相关性矩阵
    upper = corr_matrix.where(np.triu(np.ones(corr_matrix.shape), k = 1).astype(np.bool))
    #相关性大小和阈值进行对比
    to_drop = [column for column in upper.columns if any(upper[column].abs() > correlation_threshold)]
    #生成空的dataframe
    record_collinear = pd.Dataframe(columns = ['corr_feature1', 'corr_feature2', 'corr_value'])
    #遍历阈值判断出的每一列名
    for column in to_drop:
        #再次进行阈值判断得到列名和相关系数
        corr_features = list(upper.index[upper[column].abs() > correlation_threshold])
        corr_values = list(upper[column][upper[column].abs() > correlation_threshold])
        drop_features = [column for _ in range(len(corr_features))]    
        #存入dataframe
        temp_df = pd.Dataframe.from_dict({'corr_feature1': drop_features,
                                         'corr_feature2': corr_features,
                                         'corr_value': corr_values})
        #合并到大表格
        record_collinear = record_collinear.append(temp_df, ignore_index = True)
    drops=[]## 下面是删除规则
    #没有dataframe生成说明不需要删除特征
    if record_collinear.shape[0]==0:
        return 'nothing!','nothing!'
    #得到需要删除特征的列名
    for i,j in zip(record_collinear.corr_feature1.values.tolist(),record_collinear.corr_feature2.values.tolist()):
        if X[i].nunique()>X[j].nunique():
            drops.append(j)
        else:
            drops.append(i)
    drops=list(set(drops))
    return record_collinear,drops

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/588368.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号