项目名称: 北京地区短租数据集分析及价格建模预测

项目概述: 本项目主要是对短租数据集进行数据探索性分析,通过特征工程提取相关特征,在11个回归模型中对数据集进行建模训练,实现房价预测。

       最后经过对比分析,选取其中表现较好的模型进一步调参优化,得到最优模型。

项目背景: 共享,通过让渡闲置资源的使用权,在有限增加边际成本的前提下,提高了资源利用效率。随着信息的透明化,越来越多的共享

       发生在陌生人之间。短租,共享空间的一种模式,不论是否体验过入住陌生人的家中,你都可以从短租的数据里挖掘有趣的信息。

       活动采用了短租房源相关的公开数据,包括了结构化的表格数据、非结构化的文本和地图数据。

数据源   : 链接:https://pan.baidu.com/s/1YUHjTViaTm1Rrg_kzmFEBw  密码:6we5

项目流程:

      1. 数据采集

      2. 数据查看及预处理

      3. EDA

      4. 特征工程

      5. 建模预测

      6. 模型评估与优化

 

导包             

  1. 1 import numpy as np
  2. 2 import pandas as pd
  3. 3 import seaborn as sns
  4. 4 import matplotlib.pyplot as plt
  5. 5 %matplotlib inline
  6. 6 import scipy.stats as ss
  7. 7 import missingno as miss
  8. 8 import jieba
  1. 9 from sklearn.preprocessing import Normalizer
  2. 10 from sklearn.preprocessing import StandardScaler,LabelEncoder
  3. 11 from sklearn.preprocessing import OneHotEncoder,MinMaxScaler
  4. 12 from sklearn.neighbors import KNeighborsRegressor
  5. 13 from sklearn.linear_model import LinearRegression,Lasso,Ridge,LogisticRegression
  6. 14 from sklearn.tree import DecisionTreeRegressor,ExtraTreeRegressor
  7. 15 from sklearn.ensemble import RandomForestRegressor,ExtraTreesRegressor
  8. 16 from sklearn.ensemble import AdaBoostRegressor,GradientBoostingRegressor
  9. 17 from sklearn.metrics import mean_squared_error
  10. 18 from sklearn.svm import SVR
  11. 19 from xgboost import XGBRegressor
  12. 20 from sklearn.decomposition import PCA
  13. 21
  14. 22 from sklearn.model_selection import train_test_split
  15. 23 from sklearn.model_selection import KFold,StratifiedKFold
  16. 24 from sklearn.model_selection import GridSearchCV
  17. 25
  18. 26 import warnings
  19. 27 warnings.filterwarnings(\'ignore\')
  20. 28
  21. 29 # 设置全局字体
  22. 30 plt.rcParams[\'font.sans-serif\'] = \'SimHei\'
  23. 31 # 解决保存图像是负号\'-\'显示为方块的问题
  24. 32 plt.rcParams[\'axes.unicode_minus\'] = False

 

 

1. 数据加载           

  1. 1 data = pd.read_csv(\'./listings.csv\')
  2. 2 data1 = data.copy() # 数据备份,防止后续的处理错误,及时挽回损失

 

 

2.查看数据概况      

  主要包括:查看数据类型、缺失值、异常值、数据分布情况等基本信息

  1. 1 display(data1.head(),data1.columns)

 

数据情况如上图所示,主要是有关可租房源的信息,字段含义与英文含义基本一致。

 

字段含义:

  id:样本ID

  name:房屋名称

  host_id:房东ID

  host_name:房东姓名

  neighbourhood_group:所属行政区

  neighbourhood:所属行政区

  latitude:纬度

  longitude:经度

  room_type:房间类型

  price:价格

  minimum_nights:最小可租天数

  number_of_reviews:评论数量

  last_review:最后一次评论时间

  reviews_per_month:每月评论占比

  calculated_host_listings_count:可出租房屋

  availability_365:每年可出租时长

  1. 1 miss.matrix(data1) # 查看数据缺失情况

 

通过上图可以清楚的看到数据缺失情况。

 

  1. 1 data1.info() # 通过info()方法可以快速获取数据集的简单描述,特别是总行 数、每个属性的类型和非空值的数量

  1. 1 data.describe()

 

 

总结如下:  

  6. price值:

    最小值为0:说明可能存在异常值

    25%分位值:235

    中位数: 389

    75%分位值:577

    最大值: 68983,可能存在极值干扰

 

绘制各特征的直方图(可以看到数据的分布情况)

  1. 1 data1.hist(bins=50,figsize=(20,15))

 

3. 特征工程(含EDA)      

  1. 1 # 创建数据表,分析数据缺失
  2. 2 def draw_missing_data_table(data):
  3. 3 total = data.isnull().sum().sort_values(ascending=False)
  4. 4 percent = (data.isnull().sum()/data.isnull().count()).sort_values(ascending=False)
  5. 5 missing_data = pd.concat([total,percent],axis=1,keys=[\'Total\',\'Percent\'])
  6. 6
  7. 7 return missing_data
  1. 1 null_ret = draw_missing_data_table(data1)
  2. 2 null_ret

 

由以上信息总结如下:

  1. 1. neighbourhood_group列全部为空 --- (删除此列)
  2. 2. last_reviewreviews_per_month两列存在比较大的数据缺失 --- (填值)
  3. 3. name列只有一个空值,含义是房屋名称,无法进行填充 --- (删除)

  1. 1 # 删除neighbourhood_group列
  2. 2 data1.drop(labels=\'neighbourhood_group\',axis=1,inplace=True)
  1. 1 # name列
  2. 2 cond = data1[\'name\'].isnull()
  3. 3 data1[cond] # 查看到name列只有一个空值,含义是房屋名称,无法进行填充,因此选择删除此条样本

  1. 1 # 删除name列空数据
  2. 2 cond = data1[\'name\'].notnull()
  3. 3 data1 = data1[cond]
  1. 1 # last_review列空值
  2. 2 cond = data1[\'last_review\'].isnull()
  3. 3 data1[cond] # 共有11157个空值
  1. 1 # 插值 0
  2. 2 data1[\'last_review\'].fillna(0,inplace=True)
  1. 1 # reviews_per_month列空值
  2. 2 cond = data1[\'reviews_per_month\'].isnull()
  3. 3 data1[cond] # 共11157个空值
  1. 1 mean = data1[\'reviews_per_month\'].mean()
  2. 2 mean = round(mean,2) # 保留两位小数
  3. 3 mean
  4. 4
  5. 5 # 插值 mean
  6. 6 data1[\'reviews_per_month\'].fillna(value=mean,inplace=True)
  1. 1 data1.info() # 查看已经不存在空值

  1. 1 def neighbourhood_str(data):
  2. 2 neighbourhoods = []
  3. 3 list = data[\'neighbourhood\'].str.findall(\'\w+\').tolist()
  4. 4 for i in list:
  5. 5 neighbourhoods.append(i[0])
  6. 6 return neighbourhoods
  7. 7
  8. 8
  9. 9 data1[\'neighbourhood\'] = neighbourhood_str(data1)
  10. 10 data1.head()

 

  1. 1 # 取出其中要分析的特征,单独观察各特征的数据分布情况
  2. 2 subsets = [\'price\',\'minimum_nights\',\'number_of_reviews\',\'reviews_per_month\',
  3. 3 \'calculated_host_listings_count\',\'availability_365\']
  4. 4
  5. 5
  6. 6
  7. 7 # 画出箱型图,观察数据集中的异常值
  8. 8
  9. 9 fig,ax = plt.subplots(len(subsets),1,figsize=(20,10))
  10. 10
  11. 11 plt.subplots_adjust(hspace=1) # 设置子图间的高度距离为100%
  12. 12 for i,subset in enumerate(subsets):
  13. 13 sns.boxplot(data1[subset],ax=ax[i],whis=2,orient=\'h\')
  14. 14

  1. 1 # 自定义函数,展示观察数据集的各统计计量指标
  2. 2
  3. 3 \'\'\'
  4. 4 变异系数:标准差与平均数的比值,记为C·V
  5. 5
  6. 6 1. 变异系数是衡量资料中各观测值变异程度的另一个统计计量。它可以消除单位和平均数不同对两个或对多个资料变异程度比较的影响。
  7. 7
  8. 8 2. 当进行两个或多个资料变异程度的比较时,如果度量单位与平均数相同,可以直接利用标准差来比较。如果度量单位和平均数不同时,
  9. 9 比较其变异程度就不能采用标准差,而需采用标准差与平均数的比值来比较。
  10. 10
  11. 11 3. 计算公式:C·V =( 标准偏差 SD / 平均值Mean )× 100%
  12. 12
  13. 13 4. 在进行数据统计分析时,如果变异系数大于15%,则要考虑该数据可能不正常,应该删除。
  14. 14 \'\'\'
  15. 15
  16. 16 def EDA(df):
  17. 17 data = {}
  18. 18 for i in subsets:
  19. 19 data.setdefault(i,[]) # setdefault(key,default=None)如果键不存在于字典中,将会添加键并将值设为默认值
  20. 20 data[i].append(df[i].skew()) # 数据偏度
  21. 21 data[i].append(df[i].kurt()) # 数据峰度
  22. 22 data[i].append(df[i].mean()) # 数据均值
  23. 23 data[i].append(df[i].std()) # 数据方差
  24. 24 data[i].append(df[i].std()/df[i].mean()) # 变异系数
  25. 25 data[i].append(df[i].max()-df[i].min()) # 极值
  26. 26 data[i].append(df[i].quantile(0.25)) # 第一分位数 Q1
  27. 27 data[i].append(df[i].quantile(0.75)) # 第四分位数 Q3
  28. 28 data[i].append(df[i].quantile(0.75)-df[i].quantile(0.25)) # 四分位数间距IQR=Q3-Q1
  29. 29 data[i].append(df[i].median()) # 中位数
  30. 30
  31. 31 data_df = pd.DataFrame(data,index=[\'偏度\',\'峰度\',\'均值\',\'标准差\',\'变异系数\',\'极差\',\'第一分位数\',\'第四分位数\',
  32. 32 \'四分位距\',\'中位数\'],columns=subsets)
  33. 33 return data_df.T
  34. 34
  1. 1 EDA(data1)

 

  1.   1. 因为是短租场景,价格price中存在上万的租金可能就是异常值;
  2.   2. 最少租住天数也存在很多异常值。

 

  1. 1 # 删除price特征中的异常值(删除价格为0的数据)
  2. 2 cond = data1[\'price\'] > 0
  3. 3 data1 = data1[cond]
  4. 4 data1.shape

 

 

特征选择1        

  1. 1 # 剔除与分析目标不相关的属性 latitude,longitude
  2. 2 # 对于短租来说,在一个地区里,价格差距不会太大,一般会和房子类型等其他因素有关
  3. 3
  4. 4 data1.drop(labels=[\'latitude\',\'longitude\'],axis=1,inplace=True)
  1. 1 # 使用 sns的 violinplot()函数绘制小提琴图,查看价格集中在哪个阶段
  2. 2
  3. 3 plt.figure(figsize=(15,6))
  4. 4 plt.subplot(1,2,1)
  5. 5 sns.violinplot(data1[\'price\'])
  6. 6
  7. 7 plt.subplot(1,2,2)
  8. 8 sns.violinplot(np.log(data1[\'price\'])) # 使用log函数对特征进行平滑处理

 

  1.   1. 数据比较集中,只存在极少部分高价格数据;
  2.   2. 使用log函数对特征进行平滑处理后,将价格之间的差距缩小,得到了一个更合理的分布图。

 

特征选择2        

  1. 1 # 对价格进行单独分析,观察价格的具体分布
  2. 2
  3. 3 sns.distplot(data1[data1[\'price\'] < 3000][\'price\'])

 

  1.   [0,1000]区间内的数据分布接近正态分布,在预测价格时取此区间数据进行建模即可。
  1. 1 # 取出价格在[0,1000]区间的数据
  2. 2
  3. 3 data2 = data1[data1[\'price\'] <= 1000]
  4. 4 data2.shape # (25977, 13)

 

 

查看房源总体分布情况   

  1. 1 # 查看房源总体分布情况
  2. 2
  3. 3 # data2[\'neighbourhood\'].value_countsounts() # AttributeError: \'Series\' object has no attribute \'value_countsounts\'
  4. 4 listing = data2.neighbourhood.value_counts() # 这种写法就不会报错
  5. 5 listing = pd.DataFrame(listing)
  6. 6 listing[\'percent\'] = (listing[\'neighbourhood\'].values/np.sum(listing.values)).round(3)
  7. 7 listing

 

  1. 1 # 绘制饼图,查看总体分布情况
  2. 2
  3. 3 listing = data2.neighbourhood.value_counts()
  4. 4 labels = listing.index
  5. 5
  6. 6 sns.set(font_scale=1.5)
  7. 7 plt.figure(figsize=(15,15))
  8. 8 plt.title(\'各区listing分布占比\',fontdict={\'fontsize\':18})
  9. 9
  10. 10 # 朝阳区、海淀区、东城区3个占比较高的区使用 explode突出显示
  11. 11 plt.pie(listing,
  12. 12 labels=labels,
  13. 13 autopct=\'%0.2f%%\',
  14. 14 explode=[0.07 if i in [\'东城区\',\'朝阳区\',\'海淀区\'] else 0 for i in labels],
  15. 15 startangle=90,
  16. 16 counterclock=False,
  17. 17 textprops={\'fontsize\':10,\'color\':\'black\'},
  18. 18 colors=sns.color_palette(\'summer_r\',n_colors=18))
  19. 19
  20. 20 plt.legend(loc=\'best\',shadow=True,fontsize=11)

 

 

 

  1. 1 # 对各区房源价格均值进行对比(透视表)
  2. 2
  3. 3 price_pair = pd.pivot_table(data2,index=\'neighbourhood\',columns=\'room_type\',
  4. 4 values=\'price\',aggfunc=np.mean)
  5. 5
  6. 6 price_pair

  1. 1 # 将最终结果进行可视化展示(热图)
  2. 2
  3. 3 plt.figure(figsize=(12,12))
  4. 4 sns.heatmap(price_pair,cmap=sns.color_palette(\'PiYG\',n_colors=32),annot=True,fmt=\'.0f\')

 

  1.   1. Entire home/apt类型:东城、密云、平谷、延庆、怀柔区价格较高;
  2.   2. Private room类型:延庆、怀柔、门头沟区价格较高;
  3.   3. Shared room类型:延庆、怀柔区价格较高。
  4.   总体延庆县和怀柔区价格较高。

 

绘制词云图    

 

  1. 1 # 分析受关注程度高的房源与受关注程度低的房源之间关于名字属性有什么区别
  2. 2
  3. 3 \'\'\'获取各区评论数量top或者bottom的数量,根据 num 参数获取排名靠前的数据以及排名靠后的数据\'\'\'
  4. 4 def get_review_tb(df,num):
  5. 5 result = []
  6. 6 groups = df.groupby(\'neighbourhood\')
  7. 7 for x,group in groups:
  8. 8 if num>0:
  9. 9 result.append(group.sort_values(by=\'number_of_reviews\',ascending=False)[:num])
  10. 10 if num<0:
  11. 11 result.append(group.sort_values(by=\'number_of_reviews\',ascending=False)[num:])
  12. 12 result = pd.concat(result)
  13. 13
  14. 14 return result
  15. 15

 

  1. 1 reviews_top10 = get_review_tb(data2,10)
  2. 2 reviews_bottom10 = get_review_tb(data2,-10)
  3. 3
  4. 4 display(reviews_top10.head(),reviews_bottom10.head())

 

  1. 1 # 打印停用词词汇表
  2. 2
  3. 3 with open(\'./stopwords.txt\',\'rt\',encoding=\'utf8\') as f:
  4. 4 result = f.read().split()
  5. 5
  6. 6 print(result)

 

  1. 1 # 导入相关分析包
  2. 2
  3. 3 import jieba
  4. 4 from wordcloud import WordCloud
  5. 5 from imageio import imread

 

  1. 1 \'\'\'获取房源名字中的关键字\'\'\'
  2. 2
  3. 3 def get_words(df):
  4. 4 s = []
  5. 5 words_dic = {}
  6. 6 with open(\'./stopwords.txt\',\'rt\',encoding=\'utf8\') as f: # 根据停用词过滤词汇
  7. 7 result = f.read().split()
  8. 8 for i in df:
  9. 9 words = jieba.lcut(i) # 利用jieba对房屋名称进行拆词分析,获取高频词汇
  10. 10 word = [x for x in words if x not in result]
  11. 11 s.extend(word)
  12. 12 for word in s:
  13. 13 if word != \' \': # 去掉字符串为空格的数据
  14. 14 words_dic.setdefault(word,0) # 为该字典设置默认值,如果不存在则添加,如果该键存在,则跳过并忽略
  15. 15 words_dic[word] += 1
  16. 16
  17. 17 return words_dic,s
  18. 18
  19. 19
  20. 20
  21. 21 \'\'\'删除无关字符\'\'\'
  22. 22
  23. 23 def select_word(dic):
  24. 24 new_dic = {}
  25. 25 for key,val in dic.items():
  26. 26 if key not in [\'\',\'\',\'\',\'\']:
  27. 27 if val > 6:
  28. 28 new_dic[key] = val
  29. 29
  30. 30 return new_dic
  1. top_words,s1 = get_words(reviews_top10.name.astype(\'str\')) # 强制转化为字符串格式
  2. bottom_words,s2 = get_words(reviews_bottom10.name.astype(\'str\')) # 强制转化为字符串格式
  3.  
  4. # 转换成Series,方便绘图可视化
  5. top_words_s = pd.Series(select_word(top_words)).sort_values(ascending=False)
  6. bottom_words_s = pd.Series(select_word(bottom_words)).sort_values(ascending=False)
  7. print(top_words_s)
  8. print(\'*\'*100)
  9. print(bottom_words_s)

  1. 1 # 绘图进行比较,受关注度较小和受关注度较大的房源名称中词的分布情况
  2. 2
  3. 3 fig,ax = plt.subplots(2,1,figsize=(10,5))
  4. 4 ax[0].set_title(\'受关注较大房源信息里name中词的出现次数分布图\')
  5. 5 ax[1].set_title(\'受关注较小房源信息里name中词的出现次数分布图\')
  6. 6
  7. 7 # pd.Series(top_words_s)
  8. 8 top_words_s.plot(kind=\'bar\',ylim=[0,40],color=\'r\')
  9. 9 bottom_words_s.plot(kind=\'bar\',ylim=[0,40],color=\'r\')

 

  1.   1. 无论是受关注小还是受关注大的房源,大部分词汇都集中在地铁、公寓、北京、温馨等词汇中;
  2.   2. 总体来看,受关注程度与房屋描述相关性不大。

 

  1. 1 \'\'\'绘制词云图\'\'\'
  2. 2
  3. 3 def cloud_s(datas):
  4. 4 new_data = []
  5. 5 for data in datas:
  6. 6 if data != \' \':
  7. 7 if data not in [\'\',\'\',\'\',\'\']:
  8. 8 new_data.append(data)
  9. 9
  10. 10 return new_data
  1. 1 s1 = cloud_s(s1)
  2. 2 s1 = \' \'.join(s1)
  3. 3 s1

  1. 1 s2 = cloud_s(s2)
  2. 2 s2 = \' \'.join(s2)
  3. 3 s2

  1. 1 mask= imread(\'./pic.jpg\') # 此处为使用遮罩的情况,即生成的词云形状
  2. 2
  3. 3 def draw_cloude(mask, s, num):
  4. 4 wordcloud = WordCloud(background_color = \'#FFFFFF\', # 词云图片的背景颜色
  5. 5 width = 800, # 词云图片的宽度,默认400像素
  6. 6 height = 800, # 词云图片的高度,默认200像素
  7. 7 font_path = \'/opt/anaconda3/lib/python3.8/site-packages/wordcloud/DroidSansMono.ttf\', # 词云指定字体文件的完整路径
  8. 8 # max_words = 200, #词云图中最大词数,默认200
  9. 9 # max_font_size = 80, # 词云图中最大的字体字号,默认None,根据高度自动调节
  10. 10 # min_font_size = 20, # 词云图中最小的字体字号,默认4号
  11. 11 font_step = 1, # 词云图中字号步进间隔,默认1
  12. 12 mask = mask, # 词云形状,默认None,即方形图
  13. 13 ).generate(s) # 由txt文本生成词云
  14. 14 #返回对象
  15. 15 image_produce = wordcloud.to_image()
  16. 16 #显示图像
  17. 17 # image_produce.show()
  18. 18 # 将词云图保存为名为sample的文件
  19. 19 wordcloud.to_file("sample%d.png" % num)
  20. 20
  21. 21
  22. 22 draw_cloude(mask, s1, 1)
  23. 23 draw_cloude(mask, s2, 2)
  1. 1 sample1 = plt.imread(\'./sample1.png\')
  2. 2 plt.imshow(sample1)
  3. 3
  4. 4
  5. 5 sample2 = plt.imread(\'./sample2.png\')
  6. 6 plt.imshow(sample2)

 

效果如图:

 

  name、host_name、neighbourhood、room_type、last_review几个特征数据类型为String类型。其中:

  1.   1. neighbourhoodroom_type是对price(价格)有较大影响的特征,选择量化处理;
  2.   2. last_review是时间数据,选择转换为时间类型或删除

 

  1. 1 # LabelEncoder
  2. 2 le = LabelEncoder()
  3. 3
  4. 4 labels = [\'neighbourhood\',\'room_type\']
  5. 5 for col in labels:
  6. 6 data2[col] = le.fit_transform(data2[col])
  7. 7
  8. 8 data2.head()

 

  1. # 剔除 last_review、reviews_per_month 两个特征
  2. drop_labels = [\'last_review\',\'reviews_per_month\']
  3. data2.drop(labels=drop_labels,axis=1,inplace=True)

 

 

特征选择3        

  1. 1 # 查看各特征之间相关性系数
  2. 2 # 相关性系数越靠近 1 或 -1 ,则代表特征之间越有相关性
  3. 3 pair_cols = [\'price\', \'neighbourhood\', \'room_type\', \'minimum_nights\', \'number_of_reviews\',
  4. 4 \'calculated_host_listings_count\', \'availability_365\']
  5. 5
  6. 6 correlation = data2[pair_cols].corr()
  7. 7
  8. 8 correlation

  1. 1 # 可视化
  2. 2
  3. 3 plt.figure(figsize=(12,12))
  4. 4 sns.pairplot(data2[pair_cols])

  1. 1 other_feature = data2[[\'id\',\'name\',\'host_id\',\'host_name\']] # ID等属性先取出来,方便后面合并表格使用
  2. 2
  3. 3 # 删除[\'id\',\'name\',\'host_id\',\'host_name\']特征
  4. 4 data3 = data2.drop([\'id\',\'name\',\'host_id\',\'host_name\'],axis=1)
  1. 1 data3.head()

 

 

    x_train, x_test, y_train, y_test = train_test_split(x_dum, y, test_size = 0.25, random_state = 1)

    y_log = np.log(y)

    x_train, x_test, y_train_log, y_test_log = train_test_split(x_dum,y_log,test_size = 0.25,random_state = 1)

 

  1. 1 # 数据
  2. 2 X = data3.iloc[:,[0,1,3,4,5,6]]
  3. 3
  4. 4 # 目标值
  5. 5 y = data3.iloc[:,2]
  1. 1 # 哑编码
  2. 2 one = OneHotEncoder()
  3. 3
  4. 4 X = one.fit_transform(X)

 

 

  1. 1 # 数据分割
  2. 2 X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.2)
  1. 1 # 创建模型对象
  2. 2 models=[LinearRegression(),KNeighborsRegressor(),SVR(),Ridge(),Lasso(),DecisionTreeRegressor(),
  3. 3 ExtraTreeRegressor(),XGBRegressor(),RandomForestRegressor(),AdaBoostRegressor(),GradientBoostingRegressor()]
  4. 4
  5. 5 models_str=[\'LinearRegression\',\'KNNRegressor\',\'SVR\',\'Ridge\',\'Lasso\',\'DecisionTree\',
  6. 6 \'ExtraTree\',\'XGBoost\',\'RandomForest\',\'AdaBoost\',\'GradientBoost\']
  1. 1 # 循环出每一个模型,进行模型的构建
  2. 2
  3. 3 big_score = []
  4. 4 big_mean_score = []
  5. 5
  6. 6 for name,model in zip(models_str,models):
  7. 7 score_ = []
  8. 8 mean_score = []
  9. 9 print(\'开始训练模型:\' + name)
  10. 10 model = model #建立模型
  11. 11 model.fit(X_train, y_train)
  12. 12 y_pred = model.predict(X_test)
  13. 13 score = model.score(X_test, y_test)
  14. 14 score_.append(str(score)[:5])
  15. 15 mean_score.append(mean_squared_error(y_test, y_pred))
  16. 16
  17. 17 big_score.append(score_)
  18. 18 big_mean_score.append(mean_score)
  19. 19 print(name +\' 得分:\'+str(score))
  20. 20 print(\'均方误差:\',mean_squared_error(y_test, y_pred))

  1. 1 df = pd.DataFrame(np.array(big_score).reshape(-1,1),index=models_str, columns=[\'score\'])
  2. 2 df[\'mean_score\'] = np.array(big_mean_score).reshape(-1,1)
  3. 3 df

 

  1.   1. DecisionTreeExtraTreeAdaBoost得分最低,SVR得分也不高,原因是SVR训练数据需要标准化处理;
  2.   2. XGBoostRandomForestGradientBoost得分较高,这几个都是集成算法,后续对其进行参数调优,效果应该更好。

 

  1. 1 # 平滑处理
  2. 2 y_log = np.log(y)
  3. 3
  4. 4 # 数据分割
  5. 5 X_train,X_test,y_train_log,y_test_log = train_test_split(X,y_log,test_size=0.2)
  1. 1 # 循环出每一个模型,进行模型的构建
  2. 2
  3. 3 big_score = []
  4. 4 big_mean_score = []
  5. 5
  6. 6 for name,model in zip(models_str,models):
  7. 7 score_ = []
  8. 8 mean_score = []
  9. 9 print(\'开始训练模型:\' + name)
  10. 10 model = model #建立模型
  11. 11 model.fit(X_train, y_train_log)
  12. 12 y_pred = model.predict(X_test)
  13. 13 score = model.score(X_test, y_test_log)
  14. 14 score_.append(str(score)[:5])
  15. 15 mean_score.append(mean_squared_error(y_test_log, y_pred))
  16. 16
  17. 17 big_score.append(score_)
  18. 18 big_mean_score.append(mean_score)
  19. 19 print(name +\' 得分:\'+str(score))
  20. 20 print(\'均方误差:\',mean_squared_error(y_test_log, y_pred))

  1. 1 df_log = pd.DataFrame(np.array(big_score).reshape(-1,1),index=models_str, columns=[\'score_log\'])
  2. 2 df_log[\'mean_score_log\'] = np.array(big_mean_score).reshape(-1,1)
  3. 3 df_log

 

  1.   1. 部分模型预测效果得到了很好的提升;
  2.   2. XGBoost有很明显的提升。

 

  1. 1 # 对数据进行标准化处理后,再进行训练
  2. 2
  3. 3 scale_X = StandardScaler(with_mean=False)
  4. 4 X1 = scale_X.fit_transform(X)
  5. 5 scale_y = StandardScaler()
  6. 6 y = np.array(y).reshape(-1,1)
  7. 7 y1 = scale_y.fit_transform(y)
  8. 8 y1 = y1.ravel() # 扁平化数据
  9. 9 X_train1, X_test1, y_train1, y_test1 = train_test_split(X1, y1, test_size =0.2)
  1. 1 # 循环出每一个模型,进行模型的构建
  2. 2
  3. 3 big_score_reg = []
  4. 4 big_mean_score_reg = []
  5. 5
  6. 6 for name,model in zip(models_str,models):
  7. 7 score_ = []
  8. 8 mean_score = []
  9. 9 print(\'开始训练模型:\' + name)
  10. 10 model = model #建立模型
  11. 11 model.fit(X_train1, y_train1)
  12. 12 y_pred = model.predict(X_test1)
  13. 13 score = model.score(X_test1, y_test1)
  14. 14 score_.append(str(score)[:5])
  15. 15 mean_score.append(mean_squared_error(y_test1, y_pred))
  16. 16
  17. 17 big_score_reg.append(score_)
  18. 18 big_mean_score_reg.append(mean_score)
  19. 19 print(name +\' 得分:\'+str(score))
  20. 20 print(\'均方误差:\',mean_squared_error(y_test1, y_pred))

  1. 1 df_reg = pd.DataFrame(np.array(big_score_reg).reshape(-1,1),index=models_str, columns=[\'score_reg\'])
  2. 2 df_reg[\'mean_score_reg\'] = np.array(big_mean_score_reg).reshape(-1,1)
  3. 3 df_reg

 

  1.   1. 标准化后的数据对Lasso模型不太友好。

 

  1. 1 # 将所有数据得分进行对比分析
  2. 2
  3. 3 df1 = df.join(df_log)
  4. 4 df2 = df1.join(df_reg)
  5. 5 df2

  1.   选择: XGBoost 模型

 

  1. 1 # 平滑处理
  2. 2 y_log = np.log(y)
  3. 3
  4. 4 # 数据分割
  5. 5 X_train,X_test,y_train_log,y_test_log = train_test_split(X,y_log,test_size=0.2)
  1. 1 # 先对n_estimators参数进行调试
  2. 2
  3. 3 cv_params = {\'n_estimators\':[i for i in range(250,300)]}
  4. 4 other_params = {\'learning_rate\': 0.1, \'n_estimators\': 500, \'max_depth\': 5, \'min_child_weight\': 1, \'seed\': 0,
  5. 5 \'subsample\': 0.8, \'colsample_bytree\': 0.8, \'gamma\': 0, \'reg_alpha\': 0, \'reg_lambda\': 1}
  6. 6
  7. 7 xgb = XGBRegressor(**other_params)
  8. 8 clf = GridSearchCV(estimator=xgb, param_grid=cv_params, scoring=\'r2\', cv=5, verbose=1, n_jobs=4)
  9. 9 clf.fit(X_train,y_train_log)
  10. 10 evalute_result = clf.cv_results_
  11. 11
  12. 12 print(\'每轮迭代运行结果:{0}\'.format(evalute_result))
  13. 13 print(\'参数的最佳取值:{0}\'.format(clf.best_params_))
  14. 14 print(\'最佳模型得分:{0}\'.format(clf.best_score_))

 

  1.   n_estimators 最好参数为 273,更新参数,继续调参

 

  1. 1 # 优化max_depth和min_child_weight参数
  2. 2
  3. 3 cv_params = {\'max_depth\': [3, 4, 5, 6, 7, 8, 9, 10], \'min_child_weight\': [1, 2, 3, 4, 5, 6]}
  4. 4
  5. 5 other_params = {\'learning_rate\': 0.1, \'n_estimators\': 273, \'max_depth\': 5, \'min_child_weight\': 1, \'seed\': 0,
  6. 6 \'subsample\': 0.8, \'colsample_bytree\': 0.8, \'gamma\': 0, \'reg_alpha\': 0, \'reg_lambda\': 1}
  7. 7
  8. 8 xgb = XGBRegressor(**other_params)
  9. 9 clf = GridSearchCV(estimator=xgb, param_grid=cv_params, scoring=\'r2\', cv=5, verbose=1, n_jobs=4)
  10. 10 clf.fit(X_train,y_train_log)
  11. 11 evalute_result = clf.cv_results_
  12. 12
  13. 13 print(\'每轮迭代运行结果:{0}\'.format(evalute_result))
  14. 14 print(\'参数的最佳取值:{0}\'.format(clf.best_params_))
  15. 15 print(\'最佳模型得分:{0}\'.format(clf.best_score_))

 

  1.   max_depth 最好参数为 9min_child_weight 最好参数为 2,更新参数,继续调参

 

  1. 1 # 进一步优化gamma
  2. 2
  3. 3 cv_params = {\'gamma\': [0.1, 0.2, 0.3, 0.4, 0.5, 0.6]}
  4. 4
  5. 5 other_params = {\'learning_rate\': 0.1, \'n_estimators\': 273, \'max_depth\': 9, \'min_child_weight\': 2, \'seed\': 0,
  6. 6 \'subsample\': 0.8, \'colsample_bytree\': 0.8, \'gamma\': 0, \'reg_alpha\': 0, \'reg_lambda\': 1}
  7. 7
  8. 8 xgb = XGBRegressor(**other_params)
  9. 9 clf = GridSearchCV(estimator=xgb, param_grid=cv_params, scoring=\'r2\', cv=5, verbose=1, n_jobs=5)
  10. 10 clf.fit(X_train, y_train_log)
  11. 11 evalute_result = clf.cv_results_
  12. 12 print(\'每轮迭代运行结果:{0}\'.format(evalute_result))
  13. 13 print(\'参数的最佳取值:{0}\'.format(clf.best_params_))
  14. 14 print(\'最佳模型得分:{0}\'.format(clf.best_score_))

 

  1.   gamma 最好参数为 0.1,更新参数,继续调参

 

  1. 1 # 优化subsample 和 colsample_bytree
  2. 2
  3. 3 cv_params = {\'subsample\': [0.6, 0.7, 0.8, 0.9], \'colsample_bytree\': [0.6, 0.7, 0.8, 0.9]}
  4. 4
  5. 5 other_params = {\'learning_rate\': 0.1, \'n_estimators\': 273, \'max_depth\': 9, \'min_child_weight\': 2, \'seed\': 0,
  6. 6 \'subsample\': 0.8, \'colsample_bytree\': 0.8, \'gamma\': 0.1, \'reg_alpha\': 0, \'reg_lambda\': 1}
  7. 7
  8. 8 xgb = XGBRegressor(**other_params)
  9. 9 clf = GridSearchCV(estimator=xgb, param_grid=cv_params, scoring=\'r2\', cv=5, verbose=1, n_jobs=5)
  10. 10 clf.fit(X_train, y_train_log)
  11. 11 evalute_result = clf.cv_results_
  12. 12 print(\'每轮迭代运行结果:{0}\'.format(evalute_result))
  13. 13 print(\'参数的最佳取值:{0}\'.format(clf.best_params_))
  14. 14 print(\'最佳模型得分:{0}\'.format(clf.best_score_))

 

  1.   colsample_bytree 最好参数为 0.8subsample 最好参数为 0.9,更新参数,继续调参

 

  1. 1 # 优化 reg_alpha 和 reg_lambda
  2. 2
  3. 3 cv_params = {\'reg_alpha\': [0.05, 0.1, 0.2, 0.5, 1], \'reg_lambda\': [0.01, 0.05, 0.1, 1, 2, 3]}
  4. 4
  5. 5 other_params = {\'learning_rate\': 0.1, \'n_estimators\': 273, \'max_depth\': 9, \'min_child_weight\': 2, \'seed\': 0,
  6. 6 \'subsample\': 0.9, \'colsample_bytree\': 0.8, \'gamma\': 0.1, \'reg_alpha\': 0, \'reg_lambda\': 1}
  7. 7
  8. 8 xgb = XGBRegressor(**other_params)
  9. 9 clf = GridSearchCV(estimator=xgb, param_grid=cv_params, scoring=\'r2\', cv=5, verbose=1, n_jobs=5)
  10. 10 clf.fit(X_train, y_train_log)
  11. 11 evalute_result = clf.cv_results_
  12. 12 print(\'每轮迭代运行结果:{0}\'.format(evalute_result))
  13. 13 print(\'参数的最佳取值:{0}\'.format(clf.best_params_))
  14. 14 print(\'最佳模型得分:{0}\'.format(clf.best_score_))

 

 

  1.   reg_alpha 最好参数为 1reg_lambda 最好参数为 0.05,更新参数,继续调参

 

  1. 1 # 优化 learning_rate
  2. 2
  3. 3 cv_params = {\'learning_rate\': [0.01, 0.05, 0.07, 0.1, 0.2]}
  4. 4
  5. 5 other_params = {\'learning_rate\': 0.1, \'n_estimators\': 273, \'max_depth\': 9, \'min_child_weight\': 2, \'seed\': 0,
  6. 6 \'subsample\': 0.9, \'colsample_bytree\': 0.8, \'gamma\': 0.1, \'reg_alpha\': 1, \'reg_lambda\': 0.05}
  7. 7
  8. 8 xgb = XGBRegressor(**other_params)
  9. 9 clf = GridSearchCV(estimator=xgb, param_grid=cv_params, scoring=\'r2\', cv=5, verbose=1, n_jobs=5)
  10. 10 clf.fit(X_train, y_train_log)
  11. 11 evalute_result = clf.cv_results_
  12. 12 print(\'每轮迭代运行结果:{0}\'.format(evalute_result))
  13. 13 print(\'参数的最佳取值:{0}\'.format(clf.best_params_))
  14. 14 print(\'最佳模型得分:{0}\'.format(clf.best_score_))

 

  1.   learning_rate 最好参数为 0.1

 

  1. 1 # 整合所有的参数,进行训练
  2. 2
  3. 3 other_params = {\'learning_rate\': 0.1, \'n_estimators\': 273, \'max_depth\': 9, \'min_child_weight\': 2, \'seed\': 0,
  4. 4 \'subsample\': 0.9, \'colsample_bytree\': 0.8, \'gamma\': 0.1, \'reg_alpha\': 1, \'reg_lambda\': 0.05}
  5. 5
  6. 6 print(\'开始训练模型\')
  7. 7 # 建模
  8. 8 xgb = XGBRegressor(learning_rate=0.1, n_estimators=273, max_depth=9, min_child_weight=2, seed=0,
  9. 9 subsample=0.9, colsample_bytree=0.8, gamma=0.1, reg_alpha= 1, reg_lambda=0.05)
  10. 10 xgb.fit(X_train,y_train_log)
  11. 11 y_pre = xgb.predict(X_test)
  12. 12 score = xgb.score(X_test,y_test_log)
  13. 13
  14. 14 print(\'得分:\' + str(score))
  15. 15 print(\'均方误差:\',mean_squared_error(y_test_log,y_pre))

 

最终得到的结果虽然有提升,但得分并不算高,分析原因有:

  1.   1. 数据集特征太少;
  2.   2. 数据集特征与价格特征之间的相关性比较小。

 

版权声明:本文为Rms-Blog原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/Rms-Blog/p/13678271.html