2023年12月2日发(作者:13年索八二手车报价)

回归分析:二手车价格预测项目说明:数据来源阿里天池的一个挖掘比赛:预测二手车交易价格import pandas as pdimport as pltimport numpy as npfrom _selection import train_test_splitfrom s import mean_absolute_errorfrom _model import LassoCV,RidgeCV# from le import GradientBoostingRegressor #

集成方法回归库# from _selection import GridSearchCVfilename = r\'C:UsersliuhaoDesktop新建文件夹二手车used_car_train_\'train = _csv(filename, sep=\' \')iqr = train[\'price\'].quantile(0.75) + ((train[\'price\'].quantile(0.75) - train[\'price\'].quantile(0.25))*1.5)(train[\'price\'][train[\'price\'] > iqr].index,inplace=True)train[\'price\'] = 1p(train[\'price\'])bra_p = train[\'price\'].groupby(train[\'brand\']).mean()train[\'b_p\'] = train[\'brand\'].apply(lambda x:bra_[x])train[\'used_months\'] = ((_datetime(train[\'creatDate\'], format=\'%Y%m%d\', errors=\'coerce\') -

_datetime(train[\'regDate\'], format=\'%Y%m%d\', errors=\'coerce\')).)/30train[\'used_months\'].fillna(train[\'used_months\'].mean(),inplace=True)def fill_missing(df): df[\'fuelType\'] = df[\'fuelType\'].fillna(train[\'fuelType\'].value_counts().index[0]) df[\'gearbox\'] = df[\'gearbox\'].fillna(train[\'gearbox\'].value_counts().index[0]) df[\'bodyType\'] = df[\'bodyType\'].fillna(train[\'bodyType\'].value_counts().index[0]) df[\'model\'] = df[\'model\'].fillna(train[\'model\'].value_counts().index[0]) df[\'brand\'] = df[\'brand\'].fillna(train[\'brand\'].value_counts().index[0]) return dfndata = fill_missing(train)ndata[\'notRepairedDamage\'].replace(\'-\',ndata[\'notRepairedDamage\'].value_counts().index[0],inplace=True)ndata[\'power\'] = ndata[\'power\'].map(lambda x: 600 if x>600 else x)all_features = ([\'SaleID\', \'name\', \'regDate\', \'model\', \'seller\', \'offerType\', \'creatDate\',\'regionCode\',], axis=1)def data_astype(df): df[\'brand\'] = df[\'brand\'].astype(str) df[\'bodyType\'] = df[\'bodyType\'].astype(str) df[\'fuelType\'] = df[\'fuelType\'].astype(str) df[\'gearbox\'] = df[\'gearbox\'].astype(str) df[\'notRepairedDamage\'] = df[\'notRepairedDamage\'].astype(str) return dfcolumns = [\'b_p\',\'used_months\',\'power\', \'kilometer\',\'v_0\', \'v_1\', \'v_2\', \'v_3\', \'v_4\', \'v_5\', \'v_6\', \'v_7\', \'v_8\', \'v_9\', \'v_10\', \'v_11\', \'v_12\', \'v_13\', \'v_14\']for i in columns: all_features[i] = (all_features[i] - all_features[i].mean())/all_features[i].std()all_features = data_astype(all_features)all_features = _dummies(all_features).reset_index(drop=True)X = all_features[all_features[\'price\'].notnull()].drop([\'price\'], axis=1)y = all_features[all_features[\'price\'].notnull()][\'price\']y = all_features[all_features[\'price\'].notnull()][\'price\']X_train, X_valid, y_train, y_valid = train_test_split(X, y, random_state=15, shuffle=True)model = RidgeCV(cv=5)# model_gbr = GradientBoostingRegressor() #

建立GradientBoostingRegressor回归对象# parameters = {\'loss\': [\'ls\', \'lad\', \'huber\', \'quantile\'],# \'learning_rate\': [0.05, 0.1, 0.15],# \'max_depth\': [2, 3, 4],# \'min_samples_split\': [2, 3],# \'min_samples_leaf\': [1, 2, 4]} #

定义要优化的参数信息# model = GridSearchCV(estimator=model_gbr, param_grid=parameters, cv=5, n_jobs=-1)

(X_train, y_train)mae_train = mean_absolute_error(1(y_train), 1(t(X_train)))mae_valid = mean_absolute_error(1(y_valid), 1(t(X_valid)))print(\'训练集MAE: {}\'.format(mae_train))print(\'验证集MAE: {}\'.format(mae_valid))c = dict(zip(,_))for k,v in (): if v != 0: print(k,v)训练集MAE: 716.66验证集MAE: 718.82power 0.387714kilometer -0.20526v_0 0.382288v_1 4.558832354583367v_2 0.26327v_3 -0.5552v_4 0.1152475v_5 -2.1387v_6 -9.292v_7 -1.7258869116610935v_8 -1.2923321301431097v_9 -0.1815429v_10 -4.497229173748067v_11 1.1832v_12 1.227659570720378v_13 -0.2645348897326188v_14 0.11129b_p 0.593905used_months 0.453932brand_0 0.82881brand_1 0.54303brand_10 0.2188brand_11 0.968252brand_12 0.29203brand_13 -0.05547brand_14 0.676693brand_15 -0.234297brand_16 -0.15248brand_17 -0.9151895brand_18 0.17484brand_19 0.84757brand_2 0.37241brand_20 -0.492022brand_21 -0.839328brand_22 -0.75515brand_23 0.16288brand_24 -0.117689brand_24 -0.117689brand_25 -0.87798brand_26 0.52352brand_27 0.737275brand_28 -0.35312brand_29 -0.242997brand_3 -0.499956brand_30 -0.4301brand_31 -0.496774brand_32 0.858006brand_33 0.985093brand_34 -0.52528brand_35 0.62051brand_36 0.06041brand_37 0.899794brand_38 -0.240178brand_39 -0.465794brand_4 0.52512brand_5 -0.98612brand_6 -0.029205brand_7 0.415147brand_8 0.164806brand_9 0.814356bodyType_0.0 -0.348785bodyType_1.0 -0.321329bodyType_2.0 -0.686644bodyType_3.0 0.562396bodyType_4.0 0.74143bodyType_5.0 -0.457995bodyType_6.0 -0.39483764bodyType_7.0 0.337573fuelType_0.0 0.50734fuelType_1.0 0.63767fuelType_2.0 0.22088fuelType_3.0 -0.628988fuelType_4.0 -0.926156fuelType_5.0 0.499934fuelType_6.0 -0.15783gearbox_0.0 -0.687038gearbox_1.0 0.786226notRepairedDamage_0.0 0.861217notRepairedDamage_1.0 -0.215818初始MAE是1000多,最终最好的训练结果 MAE 是岭回归700多。没有上传预测结果,因为排行榜榜首只有300多,入榜也才400多。GridSearchCV数据量太大,电脑好久都跑不出来结果手动设定GBR参数,最终结果比岭回归强一点model = GradientBoostingRegressor(learning_rate=0.15,max_depth=4,loss=\"ls\")训练集MAE: 561.9验证集MAE: 579.8975947651696

更多推荐

二手车,预测,回归