Task4:建模与预测

建模调参

一、基础知识

1. 线性回归模型

线性回归对于特征的要求

特征符合线性行和可加性。假设因变量为Y,自变量为X1,X2,则回归分析的默认假设为Y=b+a1X1+a2X2+ε。 线性性:X1每变动一个单位,Y相应变动a1个单位,与X1的绝对数值大小无关。可加性:X1对Y的影响是独立于其他自变量(如X2)的。
特征之间应相互独立。
处理长尾分布

理解线性回归模型

线性回归五大假设:
1)线性性 & 可加性,
2)误差项(ε)之间应相互独立,
3)自变量(X1,X2)之间应相互独立,
4)误差项(ε)的方差应为常数,
5)误差项(ε)应呈正态分布

2.模型性能验证

评价函数与目标函数

目标函数是最终需要优化的函数(obj=loss+Ω),其中包括经验损失和结构损失。经验损失(loss)就是损失函数/代价函数。结构损失(Ω)就是正则项之类的来控制模型复杂程度的函数。
评价函数即损失函数/代价函数,用于评估预测值和真实值差异。常用有:平均绝对误差(Mean Absolute Error,MAE),均方误差(Mean Squared Error,MSE),平均绝对百分误差(Mean Absolute Percentage Error,MAPE),均方根误差(Root Mean Squared Error), R2(R-Square)。

3.模型调参
贪心调参方法

所谓贪心算法是指,在对问题求解时,总是做出在当前看来是最好的选择。也就是说,不从整体最优上加以考虑,它所做出的仅仅是在某种意义上的局部最优解。适用的前提是:局部最优策略能导致产生全局最优解。
基本思路

  • 建立数学模型来描述问题

  • 把求解的问题分成若干个子问题

  • 对每个子问题求解,得到子问题的局部最优解

  • 把子问题的解局部最优解合成原来问题的一个解

代码:

import pandas as pd
import numpy as np
import warnings
warnings.filterwarnings('ignore')
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_feature = reduce_mem_usage(pd.read_csv('data_for_tree.csv'))
Memory usage of dataframe is 60507328.00 MB
Memory usage after optimization is: 15724107.00 MB
Decreased by 74.0%
continuous_feature_names = [x for x in sample_feature.columns if x not in ['price','brand','model','brand']]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值