美文网首页数据分析数据分析
天池数据挖掘比赛 - 二手车交易价格预测

天池数据挖掘比赛 - 二手车交易价格预测

作者: dataTONG | 来源:发表于2020-04-03 23:34 被阅读0次

    疑问:#use make_scorer to convert a metric to a scorer.

    连续:'kilometer', 'power', 'brand_amount', 'brand_price_average', 'brand_price_max',
    'brand_price_median', 'brand_price_min', 'brand_price_std',
    'brand_price_sum';used_time
    'v_0', 'v_1', 'v_10', 'v_11', 'v_12', 'v_13', 'v_14','v_2', 'v_3', 'v_4', 'v_5', 'v_6', 'v_7', 'v_8', 'v_9',

    分类:'power_bin'、'model', 'brand', 'bodyType', 'fuelType','gearbox', 'power_bin','notRepairedDamage', 【city、'name','offerType','seller','kilometer'】
    【以下两个类别特征严重倾斜,故这边先删掉】"seller"、"offerType"

    数据挖掘流程
    【赛题理解】如果用简短的话来说,并且在比赛的角度或者做工程的角度,就是该赛题符合的问题是什么问题,大概要去用哪些指标,哪些指标是否会做到线上线下的一致性(不太理解??),是否有效的利于我们进一步的探索更高线上分数的线下验证方法,在业务上,你是否对很多原始特征有很深刻的了解,并且可以通过EDA来寻求他们直接的关系,最后构造出满意的特征。
    【赛题理解后能做什么】这题的难点可能在哪里,关键点可能在哪里,哪些地方可以挖掘更好的特征,用什么样得线下验证方式更为稳定,出现了过拟合或者其他问题,估摸可以用什么方法去解决这些问题,哪些数据是可靠的,哪些数据是需要精密的处理的,哪部分数据应该是关键数据(背景的业务逻辑下,比如CTR的题,一个寻常顾客大体会有怎么样的购买行为逻辑规律,或者风电那种题,如果机组比较邻近,相关一些风速,转速特征是否会很近似)。这时是在一个宏观的大体下分析的,有助于摸清整个题的思路脉络,以及后续的分析方向。

    EDA(作图与量化)

    探索性数据分析.PNG
    一、载入数据:
    载入训练集和测试集;
    简略观察数据(head()+shape);
    
    二、数据总览:
    通过describe()来熟悉数据的相关统计量
    通过info()来熟悉数据类型
    
    三、【数据清洗】判断数据缺失和异常
    查看每列的存在nan情况
    异常值检测
    
    删除特征严重倾斜的列:
    del Test_data["offerType"]
    
    四、了解预测值的分布
    总体分布概况(无界约翰逊分布等)
    查看skewness and kurtosis【正态分布的偏度和峰度都看做零;偏度——左偏右偏,峰度——高顶平顶】
    查看预测值的具体频数
    
    五、特征分为类别特征和数字特征,并对类别特征查看【类别特征:特征nunique分布 】
    - 数字特征分析
    相关性分析
    查看几个特征得偏度和峰值
    每个数字特征得分布可视化
    数字特征相互之间的关系可视化
    多变量互相回归关系可视化
    - 类型特征分析
    unique分布
    类别特征箱形图可视化
    类别特征的小提琴图可视化
    类别特征的柱形图可视化类别
    特征的每个类别频数可视化(count_plot)
    
    六、用pandas_profiling生成数据报告
    

    1、作图分析

    小提琴图VS箱型图

    2、量化分析(统计连续变量 / 数值特征??的相关性)

    • 相关性分析(当资料不服从双变量正态分布或总体分布未知,或原始数据用等级表示时,宜用 spearman或kendall相关)
      皮尔森(pearson)相关系数:要求服从正态分布。连续性变量??
      斯皮尔曼(spearman)相关系数:定序变量或不满足正态分布假设的等间隔数据
      肯德尔(kendall)相关系数:定序变量或不满足正态分布假设的等间隔数据。

      相关性分析(定序变量的差值无意义;import scipy eta???)
      数据分类:定性数据(定类、定序)、定量数据(定距、定比)
    • 独立性分析


      独立性分析(他推荐一种MVtest的独立性分析方法,可在其github下载python包,后续数据竞赛直接拿来用???)
    • 偏度与峰度
      偏度Skew(【数据的不对称程度】;左偏右偏:指的是长尾巴拖在哪边;较大的正值表明该分布具有右侧较长尾部。较大的负值表明有左侧较长尾部。)
      峰度Kurt(【数据分布顶的尖锐程度】;高顶平顶;>3的峰度系数说明观察量更集中,有比正态分布更短的尾部;<3的峰度系数说明观测量不那么集中,有比正态分布更长的尾部,类似于矩形的均匀分布。)
      正态分布的偏度为0,峰度为3
      数据面试题:正态分布、偏态分布及峰态分布

      偏度与峰度(根据具体数值,脑补图像:正态分布的偏度为0,峰度为3)

    特征工程

    零基础入门数据挖掘系列之「特征工程」

    主要目的:在于将数据转换为能更好地表示潜在问题的特征,从而提高机器学习的性能。包含:数据理解、数据清洗、特征构造、特征选择、类别不平衡。

    • 异常值处理:是为了去除噪声;
    • 缺失值填补:可以加入先验知识(构造统计量特征也可以增加先验知识【分类特征是否都可以处理成统计量?按实际业务进行】);按类别补充缺失值。


      数据清洗(类别特征的分布编码?distribution encoder)

      特征构造的时候需要考虑:数值特征(提取统计量特征作为先验知识),类别特征(一般会尝试之间的交叉组合,embedding【高维稀疏特征不适用onehot编码,反而高维稀疏的特征需要embedding???】也是一种思路),时间特征(本项目还需要对汽车的使用时间used_time列进行分箱,使用了3年以下,3-7年,7-10年和10年以上,分为四个等级, 10年之后就是报废车了,应该会影响价格)。

    【特征交叉】交叉主要获得的是后者的总数、方差、最大值、最小值、平均数、众数、峰度等等。使用分类特征“brand”、“model”、“kilometer”、“bodyType”、“fuelType”
    与“price”、“days”、“power”进行特征交叉。

    特征构造
    分箱
    特征选择

    创新:大类变为小类、变成多分类;小类视为异常点,用异常检测来建模


    类别不平衡(创新:大类变为小类、变成多分类;小类视为异常点,用异常检测来建模)

    【本项目用于树模型的数据集的预处理】有异常值(LR或者树模型都要处理异常值)、分桶(连续特征离散化:LR或者树模型都要);此项目并没有处理缺失值(树模型可以不处理缺失值???)。

    匿名特征【特征统计、变换、四则运算、多项式组合】:有些比赛的特征是匿名特征,这导致我们并不清楚特征相互直接的关联性,这时我们就只有单纯基于特征进行处理,比如装箱,groupby,agg 等这样一些操作进行一些特征统计,此外还可以对特征进行进一步的 log,exp 等变换,或者对多个特征进行四则运算(如上面我们算出的使用时长),多项式组合等然后进行筛选。由于特性的匿名性其实限制了很多对于特征的处理,当然有些时候用 NN 去提取一些特征也会达到意想不到的良好效果。
    非匿名特征【结合背景的特征构建】:对于知道特征含义(非匿名)的特征工程,特别是在工业类型比赛中,会基于信号处理,频域提取,峰度,偏度等构建更为有实际意义的特征,这就是结合背景的特征构建;在推荐系统中也是这样的,各种类型点击率统计,各时段统计,加用户属性的统计等等,这样一种特征构建往往要深入分析背后的业务逻辑或者说物理原理,从而才能更好的找到 magic。
    当然特征工程其实是和模型结合在一起,这就是为什么要为 LR NN 做分桶和特征归一化的原因,而对于特征的处理效果和特征重要性等往往要通过模型来验证。

    高基类别特征:如银行卡号、ip地址、店铺ID等类别很多的类别型特征。这种特征由于类别很多,做onehot处理会导致维度很高并且很稀疏(很多空值),无论对于树模型或者线性模型,稀疏都不是一个好的性质。特征一旦稀疏,就会失去统计的显著性??,降低模型的预测精度。

    【长尾分布 / 幂律分布】针对逻辑回归LR模型(LR模型与神经网络一致??;而非决策树DT,因为DT效果不明显)模型,一般服从长尾分布(或者长尾分布的镜像??),可以取log之后再做归一化。很多模型都假设数据误差项符合正态分布,而长尾分布的数据违背了这一假设。

    对幂律分布,可以采用公式
    概率分布细谈:厚尾、长尾、幂律、指数
    什么是「长尾效应」 ?

    【Xgboost(xgb不要求标准化)处理分类特征不做one-hot编码,因为树模型不推荐对离散特征进行one-hot】具体原因:(1)产生样本切分不平衡;(2)决策树依赖的是数据的统计信息
    本质原因:特征的预测能力被人为的拆分成多分,每一份与其他特征竞争最优划分节点时都会失败,所以特征的重要性比实际低。
    分类特征,构造统计量;针对类别特征brand,构造统计量特征price,来增加先验知识】为了增加先验知识,告诉模型品牌过去的历史信息。

    建模调参

    验证方法

    特征工程>模型融合>参数调优;贪心:先全力做好特征工程,再在此基础上,模型调参。


    模型选择(验证集)与参数调优(精力:特征工程>模型融合>参数调优;)

    模型融合

    最佳:不同模型的预测差异大,得分近似(找不同模型)。


    模型融合
    模型融合2

    Datawhale 零基础入门数据挖掘+Baseline Task1 赛题理解
    二手车交易价格预测——数据挖掘学习问题答疑汇总
    赛题理解 - 《二手车价格影响因素》《二手车常用估价方法 》《汽车行业统计数据》《二手车市场分析报告》
    pythonlibs下载whl网址
    用Python进行时间处理合集
    数据的偏度和峰度——df.skew()、df.kurt()
    回归分析的五个基本假设

    技巧

    取出字典dict中对应value最小key值。——min(best_obj.items(), key=lambda x:x[1])[0]
    嵌入式特征选择:Lasso回归、Ridge回归、决策树。
    sklearn里有rubust函数,可以去方差????训练集的异常值不需要删除???


    object

    1、【特征稀疏 / 严重倾斜(name和regioncode特征太稀疏)VS特征全同???】特征一旦稀疏,容易失去统计的显著性????????????
    不具有统计意义/显著性的特征可删除:

    不具有统计意义的特征可删除
    2、本案例的baseline里面先特征筛选、再分训练集和测试街,最后-1填补缺失值。
    3、测试集含异常值的处理方法:规则方法?手动改?
    4、power最大值过大(与中位数差异大):有可能异常值,或者不是正太分布。
    5、特征间的相关性系数接近1,可能带来复共线性 / 多重共线性(精确相关关系或高度相关关系)的问题,故需要剔除某个特征。
    6、
    【回归问题标签列的统计特征】
    def Sta_inf(data):
        print('_min',np.min(data))
        print('_max:',np.max(data))
        print('_mean',np.mean(data))
        print('_ptp(极差?)',np.ptp(data))
        print('_std',np.std(data))
        print('_var',np.var(data))
    

    【df的索引取值:可用df.iloc[index集合],而非df.iloc[index集合,:]】
    【分层StratifiedKFold抽样取序号index】

    sk = StratifiedKFold(n_splits=5,shuffle=True,random_state=0)
    for train_ind,val_ind in sk.split(X_data,Y_data):  #train_ind,val_ind是索引??
        print('train:%s |test:%s'%(train_ind,val_ind))
        
        train_x=X_data.iloc[train_ind,:].values
        train_y=Y_data.iloc[train_ind,:]
        val_x=X_data.iloc[val_ind,:].values
        val_y=Y_data.iloc[val_ind,:]
    

    【权重表达】

     (1-MAE_lgb/(MAE_xgb+MAE_lgb))*val_lgb + (1-MAE_xgb/(MAE_xgb+MAE_lgb))*val_xgb
    

    【实例化sub = pd.DataFrame()后填数】

    sub = pd.DataFrame()##实例化一个pd或者df,然后往里面填数据;先实例化,然后填数据
    sub['SaleID'] = TestA_data.SaleID
    sub['price'] = sub_Weighted
    sub.to_csv('./sub_Weighted.csv',index=False)
    

    【热力图显示 VS (连续/数值特征)相关系数的直方图降序排列】

    plt.figure(figsize=(15,15))
    sns.heatmap(Train_data[numeric_features].corr(),  linewidths=.5, square=True, annot=True, fmt='.2f', linecolor='white')
    
    Train_data[numeric_features].corr()['price'].sort_values(ascending = False).plot(kind='bar')
    

    【偏度与峰度的图形显示】

    【各个特征偏度的直方图】
    Train_data.skew().sort_values(ascending=False)[1:-1].plot(kind='bar')
    
    【各个特征峰度的直方图】
    Train_data.kurt().sort_values(ascending=False)[2:].plot(kind='bar')
    plt.axhline(3)  ##正态分布的偏度为0,峰度为3
    
    【数值特征的偏度与峰度】
    for col in numeric_features:
        print('特征%s'%col,'\t',
             'Skewness: %.2f'%Train_data[col].skew(),'\t',
             'Kurtosis: {:6.2f}'.format(Train_data[col].kurt()))
    

    【可视化:每个特征(类别、数量)、两特征之间(类别、数量)?? 的可视化】

    【两数字特征之间的关系可视化】
    sns.set()
    columns =numeric_features # ['price', 'v_12', 'v_8' , 'v_0', 'power', 'v_5',  'v_2', 'v_6', 'v_1', 'v_14']
    sns.pairplot(Train_data[columns],size = 2 ,kind ='scatter',diag_kind='kde')
    plt.show()
    
    【每个数值特征的分布可视化】
    f = pd.melt(Train_data, value_vars=numeric_features)  # len(f)/len(numeric_features) ==Train_data.shape[0]
    g = sns.FacetGrid(f, col="variable",  col_wrap=4, sharex=False, sharey=False)
    g = g.map(sns.distplot , "value")  ### plt.hist,'value'
    

    【多变量之间的关系可视化】
    多变量之间的关系可视化

    【类别特征:特征nunique分布 和 箱形图可视化】

    for cat_fea in categorical_features:
        print('特征{}\t取值个数:{}'.format(cat_fea,Train_data[cat_fea].nunique()))
    
    【类别特征的小提琴图可视化】
    for cat in categorical_features:
        sns.violinplot(x=cat,y='price',data=Train_data)
        plt.show()
    

    【异常值处理的代码】

    # 这里我包装了一个异常值处理的代码,可以随便调用。
    def outliers_proc(data, col_name, scale=3):
        """
        用于清洗异常值,默认用 box_plot(scale=3)进行清洗
        :param data: 接收 pandas 数据格式
        :param col_name: pandas 列名
        :param scale: 尺度
        :return:
        """
        def box_plot_outliers(data_ser, box_scale):
            """
            利用箱线图去除异常值
            :param data_ser: 接收 pandas.Series 数据格式
            :param box_scale: 箱线图尺度,
            :return:
            """
            iqr = box_scale * (data_ser.quantile(0.75) - data_ser.quantile(0.25))
            val_low = data_ser.quantile(0.25) - iqr
            val_up = data_ser.quantile(0.75) + iqr
            rule_low = (data_ser < val_low)
            rule_up = (data_ser > val_up)
            return (rule_low, rule_up), (val_low, val_up)
    
        data_n = data.copy()
        data_series = data_n[col_name]
        rule, value = box_plot_outliers(data_series, box_scale=scale)
        #print('结果:',rule,value)
        
        index = np.arange(data_series.shape[0])[rule[0] | rule[1]]  #(&,|)和(and,or)
        print("Delete number is: {}".format(len(index)))
        
        data_n = data_n.drop(index)  #如何删除行:df.drop(index)
        data_n.reset_index(drop=True, inplace=True)  #drop=True意思是删除旧索引??
        print("Now column number is: {}".format(data_n.shape[0]))
        
        index_low = np.arange(data_series.shape[0])[rule[0]]
        outliers = data_series.iloc[index_low]  #series取值:series.iloc[index_low]
        print("Description of data less than the lower bound is:")
        print(type(outliers))
        print(pd.Series(outliers).describe())
        
        index_up = np.arange(data_series.shape[0])[rule[1]]
        outliers = data_series.iloc[index_up]
        print("Description of data larger than the upper bound is:")
        print(pd.Series(outliers).describe())
        
        print(len(index_up)+len(index_low))
        
        fig, ax = plt.subplots(1, 2, figsize=(10, 7))
        sns.boxplot(y=data[col_name], data=data, palette="Set1", ax=ax[0])
        sns.boxplot(y=data_n[col_name], data=data_n, palette="Set1", ax=ax[1])
        return data_n
    

    【时间特征提取(errors='coerce')】

    data['used_time'] =(pd.to_datetime(data['creatDate'], format='%Y%m%d', errors='coerce') - 
                        pd.to_datetime(data['regDate'], format='%Y%m%d', errors='coerce')).dt.days
    

    【构造brand的price统计量特征:train训练集groupby("brand")构造,merge到测试集;字典中的字典可用来构造df

    # 计算某品牌的销售统计量,同学们还可以计算其他特征的统计量
    # 这里要以 train 的数据计算统计量
    Train_gb = Train_data.groupby("brand")
    all_info = {}
    for kind, kind_data in Train_gb:
        info = {}
        kind_data = kind_data[kind_data['price'] > 0]
        info['brand_amount'] = len(kind_data)
        info['brand_price_max'] = kind_data.price.max()
        info['brand_price_median'] = kind_data.price.median()
        info['brand_price_min'] = kind_data.price.min()
        info['brand_price_sum'] = kind_data.price.sum()
        info['brand_price_std'] = kind_data.price.std()
        info['brand_price_average'] = round(kind_data.price.sum() / (len(kind_data) + 1), 2)
        all_info[kind] = info
    brand_fe = pd.DataFrame(all_info).T.reset_index().rename(columns={"index": "brand"})
    data = data.merge(brand_fe, how='left', on='brand')
    

    【数据分桶(以 power 为例):缺失值也进桶】

    bin = [i*10 for i in range(31)]
    data['power_bin'] = pd.cut(data['power'], bin, labels=False)
    data[['power_bin', 'power']].head()
    

    【删除列 / 特征】

    df.drop(['creatDate', 'regDate', 'regionCode'], axis=1,inplace=True)
    
    del df['creatDate']  ??????
    

    【特征符合长尾分布的log_min_max处理方式(给LR用)】

    #特征符合长尾分布的log_min_max处理方式
    def log_min_max(df,df_train,col):
        #data = pd.read_csv('D:/data_for_tree.csv')  可能要重新读取
        import scipy.stats as st
        import numpy as np
        import seaborn as sns
        log_train_n = np.log(df_train[col]+1)   
        
        plt.figure(1)  #作图看特征是否符合长尾分布
        df[col].plot(kind='hist',bins=100)  #此处可处理缺失值;而改用的sns不能处理缺失值:sns.distplot(data['used_time'], kde=True, fit=st.norm)
    
        df[col] = np.log(df[col]+1)  #训练集与测试集一起的有log无归一化,再作图
        plt.figure(2)
        sns.distplot(df[col], kde=True, fit=st.norm)
    
        df[col]=(df[col] - np.min(log_train_n))/(np.max(log_train_n)-np.min(log_train_n))  #训练集minmax,再一起log,再作图
        plt.figure(3)
        sns.distplot(df[col], kde=True, fit=st.norm)
        
        #return df[col]  ???
    

    【高势集特征model,也就是类别中取值个数非常多的, 一般可以使用聚类的方式,然后独热】

    from scipy.cluster.hierarchy import linkage, dendrogram
    #from sklearn.cluster import AgglomerativeClustering
    from sklearn.cluster import KMeans
    
    ac = KMeans(n_clusters=3)
    ac.fit(model_price_data)
    model_fea = ac.predict(model_price_data)
    plt.scatter(model_price_data[:,0], model_price_data[:,1], c=model_fea)
    cat_data_hot['model_fea'] = model_fea
    cat_data_hot = pd.get_dummies(cat_data_hot, columns=['model_fea'])
    #但是发现KMeans聚类不好,可以尝试层次聚类试试,并且这个聚类数量啥的应该也会有影响,
    #这里只是提供一个思路,我觉得这个特征做的并不是太好,还需改进。
    

    【降低内存代码:通过精度转化降低内存损耗】reduce_mem_usage 函数通过调整数据类型,帮助我们减少数据在内存中占用的空间

    def reduce_mem_usage(df):
        """ iterate through all the columns of a dataframe and modify the data type
            to reduce memory usage.        
        """
        start_mem = df.memory_usage().sum() 
        print('Memory usage of dataframe is {:.2f} MB'.format(start_mem))
        
        for col in df.columns:
            col_type = df[col].dtype
            
            if col_type != object:
                c_min = df[col].min()
                c_max = df[col].max()
                if str(col_type)[:3] == 'int':
                    if c_min > np.iinfo(np.int8).min and c_max < np.iinfo(np.int8).max:
                        df[col] = df[col].astype(np.int8)
                    elif c_min > np.iinfo(np.int16).min and c_max < np.iinfo(np.int16).max:
                        df[col] = df[col].astype(np.int16)
                    elif c_min > np.iinfo(np.int32).min and c_max < np.iinfo(np.int32).max:
                        df[col] = df[col].astype(np.int32)
                    elif c_min > np.iinfo(np.int64).min and c_max < np.iinfo(np.int64).max:
                        df[col] = df[col].astype(np.int64)  
                else:
                    if c_min > np.finfo(np.float16).min and c_max < np.finfo(np.float16).max:
                        df[col] = df[col].astype(np.float16)
                    elif c_min > np.finfo(np.float32).min and c_max < np.finfo(np.float32).max:
                        df[col] = df[col].astype(np.float32)
                    else:
                        df[col] = df[col].astype(np.float64)
            else:
                df[col] = df[col].astype('category')
    
        end_mem = df.memory_usage().sum() 
        print('Memory usage after optimization is: {:.2f} MB'.format(end_mem))
        print('Decreased by {:.1f}%'.format(100 * (start_mem - end_mem) / start_mem))
        return df
    
    【调用方法】
    sample_feature2 = reduce_mem_usage(pd.read_csv('D:/data_for_lr.csv'))
    

    【多个子图的作图plt.subplot(1,3,1):原始VS删除VS截断】

    import seaborn as sns
    import scipy.stats as st
    
    print('It is clear to see the price shows a typical exponential distribution')
    
    plt.figure(figsize=(15,5))
    plt.subplot(1,3,1)
    sns.distplot(train_y,fit=st.norm)
    plt.subplot(1,3,2)
    sns.distplot(train_y[train_y<train_y.quantile(0.9)],fit=st.norm)  ##此处相当于删除,而非截断
    
    a= train_y.copy()
    a[a>train_y.quantile(0.9)] = train_y.quantile(0.9)
    plt.subplot(1,3,3)
    sns.distplot(a,fit=st.norm)  ##此处相当于截断
    

    【五折交叉验证(五折交叉验证得出验证集);函数的装饰器;标签的log处理与未处理】
    简单建模(线性回归;删测试集,不分验证集) & 五折交叉验证(分验证集) & 模拟真实业务情况(结合时间序列,更好滴分验证集); 绘制学习率曲线与验证曲线

    #五折交叉验证(五折交叉验证得出验证集)【20200501】
    
    from sklearn.model_selection import cross_val_score
    from sklearn.metrics import mean_absolute_error,make_scorer
    
    #函数的装饰器
    def log_transfer(func):
        def wrapper(y,yhat):
            result = func(np.log(y),np.nan_to_num(np.log(yhat)))
            return result
        return wrapper
    
    model_cvs = LinearRegression(normalize = True)
    scores = cross_val_score(model_cvs,X=train_X, y=train_y, verbose=0, cv = 5,
                             scoring=make_scorer(log_transfer(mean_absolute_error)))
    
    print('AVG:', np.mean(scores))
    scores
    
    #使用线性回归模型,对未处理标签的特征数据进行五折交叉验证(Error 1.36)
    
    #五折交叉验证【20200501】
    model_cvs2 = LinearRegression(normalize = True)
    scores = cross_val_score(model_cvs2, X=train_X, y=train_y_ln, verbose=1, cv = 5, 
                             scoring=make_scorer(mean_absolute_error))
    #use `make_scorer` to convert a metric to a scorer.
    print('AVG:', np.mean(scores))
    scores
    
    #使用线性回归模型,对处理过标签的特征数据进行五折交叉验证(Error 0.19)
    

    【绘制学习率曲线与验证曲线】learning_curve返回了3个数:train_sizes, train_scores, test_scores = learning_curve???

    from sklearn.model_selection import learning_curve,validation_curve
    
    def plot_learning_curve(estimator, title, X, y, ylim=None, 
                            cv=None,n_jobs=1, train_size=np.linspace(.1, 1.0, 5 )): 
        plt.figure()
        plt.title(title)
        if ylim is not None:
            plt.ylim(*ylim)
        plt.xlabel('Training example')
        plt.ylabel('score')
        
        train_sizes, train_scores, test_scores = learning_curve(
            estimator, X, y, cv=cv, 
            n_jobs=n_jobs, train_sizes=train_size, scoring = make_scorer(mean_absolute_error))  
        train_scores_mean = np.mean(train_scores, axis=1)  
        train_scores_std = np.std(train_scores, axis=1)  
        test_scores_mean = np.mean(test_scores, axis=1)  
        test_scores_std = np.std(test_scores, axis=1)  
        
        plt.grid()#区域  ##网格线
        plt.fill_between(train_sizes, train_scores_mean - train_scores_std,  
                         train_scores_mean + train_scores_std, alpha=0.1,  
                         color="r")  
        plt.fill_between(train_sizes, test_scores_mean - test_scores_std,  
                         test_scores_mean + test_scores_std, alpha=0.1,  
                         color="g")  
        plt.plot(train_sizes, train_scores_mean, 'o-', color='r',  
                 label="Training score")  
        plt.plot(train_sizes, test_scores_mean,'o-',color="g",  
                 label="Cross-validation score")  
        plt.legend(loc="best")  
        return plt
    
    调用
    plot_learning_curve(LinearRegression(), 'Liner_model', train_X[:1000], 
                        train_y_ln[:1000], ylim=(0.0, 0.9), cv=5, n_jobs=1)  
    

    【贪心调参】
    GridSearchCV网格搜索【同时满足三种功能:fit、score和交叉验证】:同时搜索多个参数,本质是枚举。GS.best_params_最佳参数取值组合和GS.best_score_模型评判标准

    best_obj = dict()
    for obj in objective:
        model = LGBMRegressor(objective=obj)
        score = np.mean(cross_val_score(model, X=train_X, y=train_y_ln, verbose=0, cv = 5, scoring=make_scorer(mean_absolute_error)))
        best_obj[obj] = score
        
    best_leaves = dict()
    for leaves in num_leaves:
        model = LGBMRegressor(objective=min(best_obj.items(), key=lambda x:x[1])[0], num_leaves=leaves)
        score = np.mean(cross_val_score(model, X=train_X, y=train_y_ln, verbose=0, cv = 5, scoring=make_scorer(mean_absolute_error)))
        best_leaves[leaves] = score
        
    best_depth = dict()
    for depth in max_depth:
        model = LGBMRegressor(objective=min(best_obj.items(), key=lambda x:x[1])[0],
                              num_leaves=min(best_leaves.items(), key=lambda x:x[1])[0],
                              max_depth=depth)
        score = np.mean(cross_val_score(model, X=train_X, y=train_y_ln, verbose=0, cv = 5, scoring=make_scorer(mean_absolute_error)))
        best_depth[depth] = score
    
    【作图】
    sns.lineplot(x=['0_initial','1_turning_obj','2_turning_leaves','3_turning_depth'], y=[0.143 ,min(best_obj.values()), min(best_leaves.values()), min(best_depth.values())])
    

    【贝叶斯调参】from bayes_opt import BayesianOptimization

    bayes_opt流程

    • 定义优化函数
    • 定义优化参数
    • 开始优化
    • 显示结果

    【特征的分布怎么判断不一样】卡方检验??;特征的分布图像和sns的kde拟合图画在一起来对比??

    Windows下,pip安装时ReadTimeoutError解决办法
    延长等待时间完美解决问题——
    windows下在cmd中,linux在终端下输入如下命令:pip --default-timeout=100 install -U pip

    np.set_printoptions(suppress = True) ##浮点数转化后的显示方法

    相关文章

      网友评论

        本文标题:天池数据挖掘比赛 - 二手车交易价格预测

        本文链接:https://www.haomeiwen.com/subject/cehuuhtx.html