零基础入门数据挖掘——二手车交易价格预测(03建模调参)

本文从零基础入门数据挖掘,通过二手车交易价格预测为例,讲解了线性回归模型、模型验证方法、特征选择和模型调参。内容包括线性回归、五折交叉验证、Lasso与Ridge回归、模型对比以及贪心、网格和贝叶斯调参策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

赛题地址
3.1 学习目标

了解常用的机器学习模型,并掌握机器学习模型的建模与调参流程 完成相应学习打卡任务

3.2 内容介绍

  1. 线性回归模型:线性回归对于特征的要求;处理长尾分布;理解线性回归模型;
  2. 模型性能验证:评价函数与目标函数;交叉验证方法;留一验证方法;针对时间序列问题的验证;绘制学习率曲线;绘制验证曲线;
  3. 嵌入式特征选择:Lasso回归;Ridge回归;决策树;
  4. 模型对比:常用线性模型;常用非线性模型;
  5. 模型调参:贪心调参方法;网格调参方法;贝叶斯调参方法

3.3代码示例
3.3.1读取数据

import pandas as pd
import numpy as np
import warnings
warnings.filterwarnings('ignore')
sample_feature = reduce_mem_usage(pd.read_csv('data_for_tree.csv'))

读取上次处理好的数据文件

continuous_feature_names = [x for x in sample_feature.columns if x not in ['price','brand','model','brand']]

3.3.2 线性回归 & 五折交叉验证 & 模拟真实业务情况

sample_feature = sample_feature.dropna().replace('-', 0).reset_index(drop=True)
sample_feature['notRepairedDamage'] = sample_feature['notRepairedDamage'].astype(np.float32)
train = sample_feature[continuous_feature_names + ['price']]
train_X = train[continuous_feature_names]
train_y = train['price']

3.3.2 - 1 简单建模

from sklearn.linear_model import LinearRegression
model = LinearRegression(normalize=True)
model = model.fit(train_X, train_y)
'intercept:'+ str(model.intercept_)
#查看训练的线性回归模型的截距(intercept)与权重(coef)
sorted(dict(zip(continuous_feature_names, model.coef_)).items(), key=lambda x:x[1], reverse=True)
from matplotlib import pyplot as plt
subsample_index = np.random.randint(low=0, high=len(train_y), size=50)
#绘制特征v_9的值与标签的散点图,图片发现模型的预测结果(蓝色点)与真实标签(黑色点)的分布差异较大,且部分预测值出现了小于0的情况,说明我们的模型存在一些问题
plt.scatter(train_X['v_9'][subsample_index], train_y[subsample_index], color='black')
plt.scatter(train_X['v_9'][subsample_index], model.predict(train_X.loc[subsample_index]), color='blu
plt.xlabel('v_9')
plt.ylabel('price')
plt.legend(['True Price','Predicted Price'],loc='upper right')
print('The predicted price is obvious different from true price')
plt.show()

在这里插入图片描述
通过作图我们发现数据的标签(price)呈现长尾分布,不利于我们的建模预测。原因是很多模型都假设数据误差项符合正态分布,而长尾分布的数据违背了这一假设

import seaborn as sns
print('It is clear to see the price shows a typical exponential distribution')
plt.figure(figsize=(15,5))
plt.subplot(1,2,1)
sns.distplot(train_y)
plt.subplot(1,2,2)
sns.distplot(train_y[train_y 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值