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

单变量分析代码-画图python

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

单变量分析代码-画图python

1.数值型变量

"""注:等宽分的时候有个问题没解决:table_feature_prob_label不包括个数为0的,但是count_list包括个数为0的"""
def num_feature_univariate_analy(feature,pred_prob,true_label,xlabel_name,y_label_name1,y_label_name2,y_label_name3,title_name):
    #等频分
    group_names = pd.qcut(feature,10,duplicates='drop').values.categories.left
    feature_bin = pd.qcut(feature,10,labels=group_names,duplicates='drop')
    #等宽分
    #group_names = pd.cut(feature,10).values.categories.left
    #feature_bin = pd.cut(feature,10,labels=group_names)
    
    #count_list = DataFrame(feature_bin).groupby(xlabel_name).size()
    count_list = list(pd.value_counts(feature_bin).sort_index())
    

    feature_bin = pd.DataFrame(np.array(feature_bin))
    #test_y = DataFrame(test_y.reset_index(drop=True))
    feature_prob_label = pd.concat([feature_bin,pd.Series(pred_prob),true_label.reset_index(drop=True)], axis=1)
    feature_prob_label.columns = [xlabel_name,y_label_name1,y_label_name2]
    table_feature_prob_label = pd.pivot_table(feature_prob_label, values=[y_label_name1,y_label_name2], index=[xlabel_name],aggfunc=np.mean)

    a=range(len(count_list))
    #a=range(len(table_feature_prob_label.index))
    
    fig=plt.figure(figsize=(6,4))
    ax1=fig.add_subplot(111)
    #ax1.plot(table_feature_prob_label.index,table_feature_prob_label[y_label_name1],'-',color='b',label=y_label_name1)
    ax1.plot(a,table_feature_prob_label[y_label_name1],'-',color='b',label=y_label_name1)
    
    
    ax2=fig.add_subplot(111) 
    ax2.plot(a,table_feature_prob_label[y_label_name2],'-',color='r',label=y_label_name2)

    ax1.set_xlabel(xlabel_name)
    ax1.set_ylabel(y_label_name1)
    ax2.set_ylabel(y_label_name2)
    
    
    
    
    #分布图
    ax3 = ax1.twinx()
    #feature_count_list = list(pd.value_counts(feature_bin).sort_index())
    
    ax3.bar(a,count_list,color='#99CC01',alpha=0.8,align='center',edgecolor='white',label=y_label_name3)
    ax3.set_ylabel(y_label_name3)
    #ax3.set_ylim([0,800])
    plt.xticks(a,group_names)
    

    fig.legend(loc=1,bbox_to_anchor=(0.96,1),bbox_transform=ax1.transAxes)

    plt.title(title_name)

    #设置数据分类名称
    #plt.xticks(table_feature_prob_label.index,list(pd.value_counts(feature).sort_index().index))
    #旋转x轴标题
    #plt.xticks(rotation=90)
    for tick in ax1.get_xticklabels():
        tick.set_rotation(90)

    plt.show()
    

2.分类型变量

#分类型变量的单变量分析
def num_feature_univariate_analy_categ(feature,pred_prob,true_label,xlabel_name,y_label_name1,y_label_name2,y_label_name3,title_name):
    feature=feature.fillna(-1)
    cut_bins=sorted(feature.unique().tolist())
    cut_bins.insert(0,cut_bins[0]-1)
    group_names = pd.cut(feature,cut_bins,duplicates='drop').values.categories.right
    feature_bin = pd.cut(feature,cut_bins,duplicates='drop')  
    count_list = list(pd.value_counts(feature_bin).sort_index())
    feature_bin = pd.DataFrame(np.array(feature_bin))
    feature_prob_label = pd.concat([feature_bin,pd.Series(pred_prob),true_label.reset_index(drop=True)], axis=1)
    feature_prob_label.columns = [xlabel_name,y_label_name1,y_label_name2]
    table_feature_prob_label = pd.pivot_table(feature_prob_label, values=[y_label_name1,y_label_name2], index=[xlabel_name],aggfunc=np.mean)

    a=range(len(count_list))

    fig=plt.figure(figsize=(6,4))
    ax1=fig.add_subplot(111)
    ax1.plot(a,table_feature_prob_label[y_label_name1],'-',color='b',label=y_label_name1)


    ax2=fig.add_subplot(111) 
    ax2.plot(a,table_feature_prob_label[y_label_name2],'-',color='r',label=y_label_name2)

    ax1.set_xlabel(xlabel_name)
    ax1.set_ylabel(y_label_name1)
    ax2.set_ylabel(y_label_name2)




    #分布图
    ax3 = ax1.twinx()
    ax3.bar(a,count_list,color='#99CC01',alpha=0.8,align='center',edgecolor='white',label=y_label_name3)
    ax3.set_ylabel(y_label_name3)
    plt.xticks(a,group_names)


    fig.legend(loc=1,bbox_to_anchor=(0.96,1),bbox_transform=ax1.transAxes)

    plt.title(title_name)
    for tick in ax1.get_xticklabels():
        tick.set_rotation(90)

    plt.show()

调用

categ_col=['unsign_num', 'apply_time','apply_workday', 'curr_day_orders','curr_month_orders',
 'curr_week_orders', 'paid_orders', 'm_city_level', 'is_wx_follow', 'insured_gender', 'gender', 'period',
 'previous_orders', 'relation', 'brand_rank', 'follow_term',
 'follow_time',
 'follow_workday',
 'have_medical',]
for col in categ_col:
    num_feature_univariate_analy_categ(testdata[col],preds_test,test_y,col,'preds positive rate','actual positive rate','sample', col)

# 数值型调用
import matplotlib.pyplot as plt
for col in feature_x_plt:
    num_feature_univariate_analy(testdata[col],preds_test,test_y,col,'preds positive rate','actual positive rate','sample',col)

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

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

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