• 探索性数据分析EDA


    一、数据总体了解

    基本汇总信息

    head()         # 查看前5行数据(括号缺省值是5)

    tail()            # 查看后5行数据(括号缺省值是5)

    info()           #查看数据介绍(每个特征的基本信息)

    describe()   #查看统计量(总数,平均值,方差)

    shape         #样本个数和特征维度train.shape

    columns     #查看特征名

    当查看的数据太长了,中间的数据被省略了可以使用

    特征太长,中间省略时加转置(.T):

    如:head().T  

    显示所有行/列以及设置value显示的长度

    1. #设置value的显示长度为200,默认为50
    2. pd.set_option('max_colwidth',200)
    3. #显示所有列,把行显示设置成最大
    4. pd.set_option('display.max_columns', None)
    5. #显示所有行,把列显示设置成最大
    6. pd.set_option('display.max_rows', None)

     

    二、数据类型了解

    特征一般分为类别特征和数值特征组成,数值型又分为连续型和离散型。

    数值型特征本是可以直接入模的,但往往风控人员要对其做分箱,转化为WOE编码进而做标准评分卡等操作。从模型效果上来看,特征分箱主要是为了降低变量的复杂性,减少变量噪音对模型的影响,提高自变量和因变量的相关度。从而使模型更加稳定。
     

    数值型特征

    1. # 数值类型
    2. numerical_feature = list(train.select_dtypes(exclude=['object']).columns)
    3. numerical_feature

    由于数值类型又可以分为连续变量、离散型变量和单值变量

    1. # 连续型变量
    2. serial_feature = []
    3. # 离散型变量
    4. discrete_feature = []
    5. # 单值变量
    6. unique_feature = []
    7. for fea in numerical_feature:
    8. temp = train[fea].nunique()# 返回的是唯一值的个数
    9. if temp == 1:
    10. unique_feature.append(fea)
    11. # 自定义变量的值的取值个数小于10就为离散型变量
    12. elif temp <= 10:
    13. discrete_feature.append(fea)
    14. else:
    15. serial_feature.append(fea)

    连续型变量

    查看某一个数值型变量的分布,查看变量是否符合正态分布,如果不符合正太分布的变量可以log化后再观察下是否符合正态分布。

    正态化的原因:一些情况下正态非正态可以让模型更快的收敛,一些模型要求数据正态(eg. GMM、KNN),保证数据不要过偏态即可,过于偏态可能会影响模型预测结果。

    可视化查看是否正态性。

    1. #每个数字特征得分布可视化
    2. f = pd.melt(train, value_vars=serial_feature)
    3. g = sns.FacetGrid(f, col="variable", col_wrap=3, sharex=False, sharey=False)
    4. g = g.map(sns.distplot, "value")

    查看连续型变量的分布情况

    1. import seaborn as sns
    2. plt.figure(1 , figsize = (8 , 5))
    3. sns.distplot(train.特征,bins=40)
    4. plt.xlabel('特征')

     查看离散型变量的分布情况:

    1. # 查看label的
    2. import seaborn as sns
    3. sns.kdeplot(train.loanAmnt[label[label==1].index], label='1', shade=True)
    4. sns.kdeplot(train.loanAmnt[label[label==0].index], label='0', shade=True)
    5. plt.xlabel('loanAmnt')
    6. plt.ylabel('Density');

     查看annualIncome的分布情况:

    1. plt.figure(1 , figsize = (8 , 5))
    2. sns.distplot(train['annualIncome'])
    3. plt.xlabel('annualIncome')

     离散型变量

    离散型变量的类型数情况

    1. for f in discrete_feature:
    2. print(f, '类型数:', train[f].nunique())

     可视化

    1. import seaborn as sns
    2. import matplotlib.pyplot as plt
    3. df_ = train[discrete_feature]# 离散型变量
    4. sns.set_style("whitegrid") # 使用whitegrid主题
    5. fig,axes=plt.subplots(nrows=1,ncols=1,figsize=(8,10))# nrows=4,ncols=2,括号加参数4x2个图
    6. for i, item in enumerate(df_):
    7. plt.subplot(4,2,(i+1))
    8. ax=sns.countplot(item,data = df_,palette="Pastel1")
    9. plt.xlabel(str(item),fontsize=14)
    10. plt.ylabel('Count',fontsize=14)
    11. plt.xticks(fontsize=13)
    12. plt.yticks(fontsize=13)
    13. #plt.title("Churn by "+ str(item))
    14. i=i+1
    15. plt.tight_layout()
    16. plt.show()

    单值变量

    单值变量表示该特征只有一种类别,对于数值全部都一样的特征,可以考虑直接删除

    分类型特征

    1. # 分类型特征
    2. category_feature = list(filter(lambda x: x not in numerical_feature,list(train.columns)))
    3. category_feature

    分类型特征可视化呈现

    1. df_category = train[['label']]
    2. sns.set_style("whitegrid") # 使用whitegrid主题
    3. color = sns.color_palette()
    4. fig,axes=plt.subplots(nrows=1,ncols=1,figsize=(10,10))
    5. for i, item in enumerate(df_category):
    6. plt.subplot(2,1,(i+1))
    7. #ax=df[item].value_counts().plot(kind = 'bar')
    8. ax=sns.countplot(item,data = df_category)
    9. plt.xlabel(str(item),fontsize=14)
    10. plt.ylabel('Count',fontsize=14)
    11. plt.xticks(fontsize=13)
    12. plt.yticks(fontsize=13)
    13. #plt.title("Churn by "+ str(item))
    14. i=i+1
    15. plt.tight_layout()
    16. plt.show()

    统计一下每个类别的数量

    1. for i in train[['label']]:
    2. print(train[i].value_counts())
    3. print()

    三、标签的分布

    查看标签是否平衡

    若分问题中各类别样本数量差距太大,则会造成样本不均衡的问题。样本不均衡不利于建立与训练出正确的模型,且不能做出合理的评估。

    1. label=train.isDefault
    2. label.value_counts()/len(label)

    可视化

    sns.countplot(label)

    如果类别的比例差别很大,样本较不平衡,对于这种情况,考虑后续将进行采样等操作

    标签和分类类别之间的分布关系

    1. train_loan_fr = train.loc[train['isDefault'] == 1]
    2. train_loan_nofr = train.loc[train['isDefault'] == 0]
    3. fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, figsize=(15, 8))
    4. # 目标变量为1时候grade的分布
    5. train_loan_fr.groupby("grade").size().plot.bar(ax=ax1)
    6. # 目标变量为0时候grade的分布
    7. train_loan_nofr.groupby("grade")["grade"].count().plot.bar(ax=ax2)
    8. # 目标变量为1时候employmentLength的分布
    9. train_loan_fr.groupby("employmentLength").size().plot.bar(ax=ax3)
    10. # 目标变量为0时候employmentLength的分布
    11. train_loan_nofr.groupby("employmentLength")["employmentLength"].count().plot.bar(ax=ax4)
    12. plt.xticks(rotation=90);

    查看正负样本的数据差异

    把数据集按正负样本分成两份,查看变量的分布差异

    1. train_positve = train[train['isDefault'] == 1]
    2. train_negative = train[train['isDefault'] != 1]
    3. f, ax = plt.subplots(len(numerical_feature),2,figsize = (10,80))
    4. for i,col in enumerate(numerical_feature):
    5. sns.distplot(train_positve[col],ax = ax[i,0],color = "blue")
    6. ax[i,0].set_title("positive")
    7. sns.distplot(train_negative[col],ax = ax[i,1],color = 'red')
    8. ax[i,1].set_title("negative")
    9. plt.subplots_adjust(hspace = 1)

    四、缺失值查看

    如果缺失值过多会对整体的模型结果产生一定的影响,因此每次在建模之前都需要对数据的缺失值情况就行查看,若有缺失情况,需要在后续特征工程中进行填补

    1. # 去掉标签
    2. X_missing = train.drop(['isDefault'],axis=1)
    3. # 查看缺失情况
    4. missing = X_missing.isna().sum()
    5. missing = pd.DataFrame(data={'特征': missing.index,'缺失值个数':missing.values})
    6. #通过~取反,选取不包含数字0的行
    7. missing = missing[~missing['缺失值个数'].isin([0])]
    8. # 缺失比例
    9. missing['缺失比例'] = missing['缺失值个数']/X_missing.shape[0]
    10. missing

    可视化

    1. # 可视化
    2. (train.isnull().sum()/len(train)).plot.bar(figsize = (20,6),color=['#d6ecf0','#a3d900','#88ada6','#ffb3a7','#cca4e3','#a1afc9'])

     

    五、异常值查看

    在统计学中,如果一个数据分布近似正态,那么大约 68% 的数据值会在均值的一个标准差范围内,大约 95% 会在两个标准差范围内,大约 99.7% 会在三个标准差范围内。

    1. def find_outliers_by_3segama(data,fea):
    2. data_std = np.std(data[fea])
    3. data_mean = np.mean(data[fea])
    4. outliers_cut_off = data_std * 3
    5. lower_rule = data_mean - outliers_cut_off
    6. upper_rule = data_mean + outliers_cut_off
    7. data[fea+'_outliers'] = data[fea].apply(lambda x:str('异常值') if x > upper_rule or x < lower_rule else '正常值')
    8. return data
    9. data_train = train.copy()
    10. for fea in numerical_feature:
    11. data_train = find_outliers_by_3segama(data_train,fea)
    12. print(data_train[fea+'_outliers'].value_counts())
    13. print(data_train.groupby(fea+'_outliers')['isDefault'].sum())
    14. print('*'*10)

    可视化

    1. import matplotlib.pyplot as pl
    2. plt.boxplot(train)

    六、数据相关关系

    查看各个特征与目标的相关系数.

    train.corr()["isDefault"].sort_values()
    

    可视化

    1. f, ax = plt.subplots(1,1, figsize = (20,20))
    2. cor = train[numerical_feature].corr()
    3. sns.heatmap(cor, annot = True, linewidth = 0.2, linecolor = "white", ax = ax, fmt =".1g" )

    特征间高相关

    两两特征之间高于0.6的过滤

    1. # 显示相关性高于0.6的变量
    2. def getHighRelatedFeatureDf(corr_matrix, corr_threshold):
    3. highRelatedFeatureDf = pd.DataFrame(corr_matrix[corr_matrix>corr_threshold].stack().reset_index())
    4. highRelatedFeatureDf.rename({'level_0':'feature_x', 'level_1':'feature_y', 0:'corr'}, axis=1, inplace=True)
    5. highRelatedFeatureDf = highRelatedFeatureDf[highRelatedFeatureDf.feature_x != highRelatedFeatureDf.feature_y]
    6. highRelatedFeatureDf['feature_pair_key'] = highRelatedFeatureDf.loc[:,['feature_x', 'feature_y']].apply(lambda r:'#'.join(np.sort(r.values)), axis=1)
    7. highRelatedFeatureDf.drop_duplicates(subset=['feature_pair_key'],inplace=True)
    8. highRelatedFeatureDf.drop(['feature_pair_key'], axis=1, inplace=True)
    9. return highRelatedFeatureDf
    10. getHighRelatedFeatureDf(train.corr(),0.6)
  • 相关阅读:
    TensorRT8.2.1.8基于Docker容器快速安装
    全网最全Django面试题整理(一)
    ffmpeg 支持用h265编码的rtmp
    typescript87-react中状态和事件
    虹科分享 | 集中管理的安全USB驱动器的好处
    git学习笔记
    iwebsec靶场 SQL注入漏洞通关笔记5- updatexml注入(报错型盲注)
    OneNote 教程,如何在 OneNote 中创建更多空间?
    全国工企专利匹配数据(1998-2014)
    3、CSS布局
  • 原文地址:https://blog.csdn.net/qq_21402983/article/details/126071556