基于Python爬虫+机器学习技术的杭州租房价格预测建模研究

20 篇文章 18 订阅
5 篇文章 61 订阅

 3f6a7ab0347a4af1a75e6ebadee63fc1.gif

🤵‍♂️ 个人主页:@艾派森的个人主页

✍🏻作者简介:Python学习者
🐋 希望大家多多支持,我们一起进步!😄
如果文章对你有帮助的话,
欢迎评论 💬点赞👍🏻 收藏 📂加关注+


目录

1.项目背景

2.数据获取

3.技术工具

4.实验过程

4.1导入数据

4.2数据预处理

4.3数据可视化

4.4特征工程

4.5构建模型

4.6特征重要性

4.7模型预测

源代码 


 

1.项目背景

        随着互联网技术的快速发展,网络已经成为人们获取信息、交流互动的重要平台。在房地产领域,尤其是在租房市场中,大量的租房信息涌现在各大租房网站和平台上。然而,这些海量的数据往往分散、不规范,难以直接用于分析和预测。因此,如何有效地从这些数据中提取有价值的信息,进而对租房价格进行预测和优化,成为了一个亟待解决的问题。

        杭州作为中国的经济发达城市,人口流动性大,租房市场活跃。然而,租房价格的波动往往受到多种因素的影响,如房屋的位置、面积、装修情况、配套设施等。这些因素之间相互作用,使得租房价格的预测变得复杂而困难。传统的预测方法往往基于经验或者简单的统计分析,难以准确捕捉价格变动的规律。

        Python作为一种强大的编程语言,具有简洁易读、功能强大等特点,在数据处理、爬虫、机器学习等领域有着广泛的应用。通过Python爬虫技术,我们可以从租房网站上抓取大量的租房信息,包括房屋的描述、价格、位置等。而机器学习技术则可以从这些数据中学习出价格变动的规律,进而构建出预测模型。

        因此,本研究旨在结合Python爬虫和机器学习技术,对杭州租房价格进行预测建模与优化研究。通过抓取租房网站上的数据,提取出影响租房价格的关键因素,并利用机器学习算法构建预测模型。通过对模型的优化和验证,我们可以更加准确地预测租房价格,为租房者和房东提供有价值的参考信息,同时也为房地产市场的研究和决策提供数据支持。

2.数据获取

本次实验的数据集来源于房天下中杭州的租房数据

1f145c1e48584145bc03270268cfe53a.png

通过分析网页的结构,我们发现列表页中的数据并不满足需求,所以我们需要进入详情页,然后使用xpath进行数据提取。故我们写爬虫代码的思路就是先用requests库发送列表页请求,使用xpath获取列表页中各详情页的url,然后再次发送详情页请求,使用xpath提取返回内容中的数据,最后再加个多页爬取并将数据保存再本地即可。

定义爬虫主函数,获取详情页url 

def main(page,f):
    url = f'https://{city_en}.zu.fang.com/house/i3{page}/?rfss=1-9988c4a227ce113113-a6'
    resp = requests.get(url,headers=headers)
    tree = etree.HTML(resp.text)
    dl_list = tree.xpath('//div[@class="houseList"]/dl')
    print(len(dl_list))
    if len(dl_list) == 0:
        print('IP异常!验证码警告!请返回官网刷新验证码!')
        raise
    else:
        for dl in dl_list:
            href = dl.xpath('./dt/a/@href')[0]
            href = f'https://{city_en}.zu.fang.com' + href
            try:
                get_details(href)
                f.flush()
                time.sleep(random.random())
            except Exception as e:
                print(e)
                pass

定义一个获取详情页的函数

def get_details(url):
    headers['referer'] = 'referer: http://search.fang.com/'
    resp = requests.get(url,headers=headers)
    tree = etree.HTML(resp.text)
    # 城市
    city_ = city
    # 房屋租金
    try:
        house_price = tree.xpath('/html/body/div[5]/div[1]/div[5]/div[1]/div/i/text()')[0] + '元/月'
    except:
        house_price = '暂无数据'
    # 交付方式
    try:
        pay_type = tree.xpath('/html/body/div[5]/div[1]/div[5]/div[1]/div/a/text()')[0]
    except:
        pay_type = '暂无数据'
    # 出租方式
    try:
        hire_style = tree.xpath('/html/body/div[5]/div[1]/div[5]/div[2]/div[1]/div[1]/a/text()')[0]
    except:
        hire_style = '暂无数据'
    # 房屋户型
    try:
        house_type = tree.xpath('/html/body/div[5]/div[1]/div[5]/div[2]/div[2]/div[1]/text()')[0]
    except:
        house_type = '暂无数据'
    # 房屋面积
    try:
        house_area = tree.xpath('/html/body/div[5]/div[1]/div[5]/div[2]/div[3]/div[1]/text()')[0]
    except:
        house_area = '暂无数据'
    # 房屋朝向
    try:
        house_direct = tree.xpath('/html/body/div[5]/div[1]/div[5]/div[3]/div[1]/div[1]/text()')[0]
    except:
        house_direct = '暂无数据'
    # 楼层
    try:
        floor = tree.xpath('/html/body/div[5]/div[1]/div[5]/div[3]/div[2]/div[1]/a/text()')[0]
    except:
        floor = '暂无数据'
    # 房屋装修
    try:
        house_dec = tree.xpath('/html/body/div[5]/div[1]/div[5]/div[3]/div[3]/div[1]/a/text()')[0]
    except:
        house_dec = '暂无数据'
    # 小区
    try:
        xiaoqu = tree.xpath('//*[@id="agantzfxq_C02_07"]/text()')[0]
    except:
        xiaoqu = '暂无数据'
    # 距地铁距离
    try:
        subway_meter = tree.xpath('/html/body/div[5]/div[1]/div[5]/div[4]/div[2]/div/a/text()')[0]
    except:
        subway_meter = '暂无数据'
    # 地址
    try:
        place = tree.xpath('/html/body/div[5]/div[1]/div[5]/div[4]/div[3]/div[2]/a/text()')[0]
    except:
        place = '暂无数据'
    # 配套设施
    try:
        other_fac_list = tree.xpath('/html/body/div[5]/div[2]/div[1]/div[2]/div[2]/ul/li/text()')
        other_fac = ' '.join(other_fac_list)
    except:
        other_fac = '暂无数据 '
    # 房源亮点
    try:
        house_light = tree.xpath('/html/body/div[5]/div[2]/div[1]/div[1]/div[2]/div/div/ul/li[1]/div[2]/text()')[0]
    except:
        house_light = '暂无数据'

代码运行结果如下:

9c414634c53c42b9b336d0a298e14d00.png

最后保存的数据文件如下:

f4609a384b3c4fb0aa025ae7226af974.png

注:完整爬取代码请关注文末公主号后加入QQ粉丝群领取!

3.技术工具

Python版本:3.9

代码编辑器:jupyter notebook

4.实验过程

4.1导入数据

首先导入本次实验用到的第三方库并加载数据集

import matplotlib.pylab as plt
import numpy as np
import seaborn as sns
import pandas as pd
plt.rcParams['font.sans-serif'] = ['SimHei'] #解决中文显示
plt.rcParams['axes.unicode_minus'] = False   #解决符号无法显示
sns.set(font='SimHei')
import warnings
warnings.filterwarnings('ignore')

df = pd.read_csv('杭州租房数据.csv')
df.head()

7d6c8ab5d8fe4da2af04129640556e7a.png

查看数据大小

e2dc597e54914de8bf56b44fd1f494c3.png

可以看出数据是共有6000条,14个变量

778e5429a9ba447d8ece5f84c24b468a.png

查看数据描述性统计

6ded7f58523c4337bc34ab7024beb60c.png

4.2数据预处理

统计缺失值情况

4d824e9791df4b03a1b0aba00cfc62f2.png

可以发现配套设施变量中有31个缺失值

统计重复值情况

350c96d02cfb4b1a92c2a4f89c957976.png

可以发现数据集中共有2023条重复数据。

删除缺失值和重复值

36b69b7a4a77439cbc5980701eccafc3.png

经过清洗之后还剩3957条有效数据。

处理租金数据的格式

df['房屋租金'] = df['房屋租金'].apply(lambda x:int(x.split('元')[0]))
df['房屋租金']

12ac6ec1407e4161b39cfae09d2d83a5.png

处理房屋面积的格式

df['房屋面积'] = df['房屋面积'].apply(lambda x:int(x[:-2]))
df['房屋面积']

0644c7cde7df4233a74800c9d0867744.png

处理距离地铁距离,提取出数字

def subway_distance(x):
    try:
        result = x.split('约')[1].split('米')[0]
    except:
        result = 0
    return int(result)

df['距地铁距离'] = df['距地铁距离'].apply(subway_distance)
df['距地铁距离']

 0c08c17bd8be461ba3f7da268e6c879f.png

4.3数据可视化

sns.boxplot(data=df,x='房屋租金')
plt.show()

f79b26f98e9a4ba187c17db3e1888584.png

sns.histplot(data=df,x='房屋租金',kde=True)
plt.show()

72a376770a8143e5b3d3131a1a6f18e7.png

sns.histplot(data=df,x='房屋面积',kde=True)
plt.show()

a96520349e124edfa92b6de6928ca926.png

plt.scatter(x=df['房屋面积'],y=df['房屋租金'])
plt.show()

3fbd1ca574214b2e9d6a2fbbf3efc388.png

sns.countplot(data=df,x='交付方式')
plt.show()

e353eeee6b024a36b8d8e5e84b0f17c1.png

df['出租方式'].value_counts().plot(kind='pie',autopct='%.2f%%')
plt.show()

029953a03b1b4fe7b50e8ec59e9a80e2.png

sns.boxplot(data=df,y='房屋租金',x='交付方式')
plt.show()

cbe64c5e6c194e1f96064bb977161c69.png

sns.boxplot(data=df,y='房屋租金',x='出租方式')
plt.show()

4d7317037bfb4d6c893c42961a6b3fc6.png

sns.barplot(data=df,x='房屋朝向',y='房屋租金')
plt.show()

f97467db19bb4c34bb95960730b247bb.png

df['楼层'].value_counts().plot(kind='pie',autopct='%.2f%%')
plt.show()

4704a7d63c264d25acd77219f63c5799.png

sns.barplot(data=df,x='楼层',y='房屋租金')
plt.show()

4383cb8e42404dd2a9e301eeeca059d1.png

# 相关性分析
sns.heatmap(df.corr(),vmax=1,annot=True,linewidths=0.5,cbar=False,cmap='YlGnBu',annot_kws={'fontsize':18})
plt.xticks(fontsize=20)
plt.yticks(fontsize=20)
plt.title('各个因素之间的相关系数',fontsize=20)
plt.show()

 6dfe29dd5f18442292de1b257c28ee17.png

绘制房源亮点词云图

import jieba
import collections
import re
import stylecloud
from PIL import Image

def draw_WorldCloud(df,pic_name,color='black'):
    data =  ''.join([item for item in df])
    # 文本预处理 :去除一些无用的字符只提取出中文出来
    new_data = re.findall('[\u4e00-\u9fa5]+', data, re.S)
    new_data = "".join(new_data)
    # 文本分词
    seg_list_exact = jieba.cut(new_data, cut_all=True)
    result_list = []
    with open('停用词库.txt', encoding='utf-8') as f: #可根据需要打开停用词库,然后加上不想显示的词语
        con = f.readlines()
        stop_words = set()
        for i in con:
            i = i.replace("\n", "")   # 去掉读取每一行数据的\n
            stop_words.add(i)

    for word in seg_list_exact:
        if word not in stop_words and len(word) > 1:
            result_list.append(word)
    word_counts = collections.Counter(result_list)

    # 词频统计:获取前100最高频的词
    word_counts_top = word_counts.most_common(100)
    print(word_counts_top)

    # 绘制词云图
    stylecloud.gen_stylecloud(text=' '.join(result_list[:500]), # 提取500个词进行绘图
                            collocations=False, # 是否包括两个单词的搭配(二字组)
                            font_path=r'C:\Windows\Fonts\msyh.ttc', #设置字体,参考位置为  C:\Windows\Fonts\ ,根据里面的字体编号来设置
                            size=800, # stylecloud 的大小
                            palette='cartocolors.qualitative.Bold_7', # 调色板,调色网址: https://jiffyclub.github.io/palettable/
                            background_color=color, # 背景颜色
                            icon_name='fas fa-circle', # 形状的图标名称 蒙版网址:https://fontawesome.com/icons?d=gallery&p=2&c=chat,shopping,travel&m=free
                            gradient='horizontal', # 梯度方向
                            max_words=2000, # stylecloud 可包含的最大单词数
                            max_font_size=150, # stylecloud 中的最大字号
                            stopwords=True, # 布尔值,用于筛除常见禁用词
                            output_name=f'{pic_name}.png') # 输出图片
    # 打开图片展示
    img=Image.open(f'{pic_name}.png')
    img.show()
draw_WorldCloud(df['房源亮点'],'房源亮点词云图') # 词云图可视化

c14248a765914c9ba5337f4adb733275.png

4.4特征工程

筛选特征

# 特征筛选
new_df = df[['房屋租金', '交付方式', '出租方式', '房屋户型', '房屋面积', '房屋朝向', '楼层', '房屋装修','距地铁距离']]
new_df

2765a7de55d9477ea7ec4812c7a5b4cf.png

编码处理

# 编码处理
from sklearn.preprocessing import LabelEncoder
for col in new_df.describe(include='O').columns.to_list():
    new_df[col] = LabelEncoder().fit_transform(new_df[col])
new_df

1c97e594d7854b4eb1933ab6cd15f3ed.png

 准备建模数据,并拆分数据集为训练集和测试集

from sklearn.model_selection import train_test_split
# 准备数据
X = new_df.drop('房屋租金',axis=1)
y = new_df['房屋租金']
# 划分数据集
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.2,random_state=42)
print('训练集大小:',X_train.shape[0])
print('测试集大小:',X_test.shape[0])

417ebc71485a4a3481f848ba74a3779a.png

4.5构建模型

先定义一个训练并输出模型指标的函数

from sklearn.metrics import r2_score,mean_absolute_error,mean_squared_error
# 定义一个训练模型并输出模型的评估指标
def train_model(ml_model):
    print("Model is: ", ml_model)
    model = ml_model.fit(X_train, y_train)
    print("Training score: ", model.score(X_train,y_train))
    predictions = model.predict(X_test)
    r2score = r2_score(y_test, predictions)
    print("r2 score is: ", r2score)
    print('MAE:', mean_absolute_error(y_test,predictions))
    print('MSE:', mean_squared_error(y_test,predictions))
    print('RMSE:', np.sqrt(mean_squared_error(y_test,predictions)))
    # 真实值和预测值的差值
    sns.distplot(y_test - predictions)

 构建多元回归模型

# 构建多元线性回归
from sklearn.linear_model import LinearRegression
lg = LinearRegression()
train_model(lg)

 248fbdd64bca4afa8652c20dc86f45d9.png

96aa794a3b8d4aad9385721c365c5168.png

构建决策树回归模型

# 构建决策树回归
from sklearn.tree import DecisionTreeRegressor
tree = DecisionTreeRegressor()
train_model(tree)

aaa97f9b8e1e4f669b80a07909fcefbe.png

3c1b1fe8a7b8455fbdbd246474ffdd08.png

构建XGBoost回归模型

# 构建xgboost回归模型
from xgboost import XGBRegressor
xgb = XGBRegressor()
train_model(xgb)

96f433a06f8441b8b58a152f0df34c73.png

147a6fc58ae6492e95bb9766583fcfca.png

通过对比三个模型的指标,我们发现xgboost模型拟合效果最好,故我们最终选择其作为最终模型。

4.6特征重要性

# 特征重要性评分
feat_labels = X_train.columns[0:]
importances = xgb.feature_importances_
indices = np.argsort(importances)[::-1]
index_list = []
value_list = []
for f,j in zip(range(X_train.shape[1]),indices):
    index_list.append(feat_labels[j])
    value_list.append(importances[j])
    print(f + 1, feat_labels[j], importances[j])
plt.figure(figsize=(10,6))
plt.barh(index_list[::-1],value_list[::-1])
plt.yticks(fontsize=12)
plt.title('各特征重要程度排序',fontsize=14)
plt.show()

2738a0663bfd446b93a4caf82aa9fd21.png

 5e54196b3a40480fa0cbf7f1d8815863.png

4.7模型预测

# 模型预测
y_pred = xgb.predict(X_test)
result_df = pd.DataFrame()
result_df['真实值'] = y_test
result_df['预测值'] = y_pred
result_df.head(10)

0310ed11094a43f1aa0e8e1133ea73fb.png

预测结果可视化

# 模型预测可视化
plt.figure(figsize=(10,6))  
plt.plot(range(len(y_test))[:200],y_pred[:200],'b',label='predict')
plt.plot(range(len(y_test))[:200],y_test[:200],'r',label='test')
plt.legend(loc='upper right',fontsize=15)
plt.xlabel('the number of house',fontdict={'weight': 'normal', 'size': 15})
plt.ylabel('value of Price',fontdict={'weight': 'normal', 'size': 15})
plt.show()

ffddf3f6f5a544baaddc8a8e5ef7d0de.png

源代码 

import matplotlib.pylab as plt
import numpy as np
import seaborn as sns
import pandas as pd
plt.rcParams['font.sans-serif'] = ['SimHei'] #解决中文显示
plt.rcParams['axes.unicode_minus'] = False   #解决符号无法显示
sns.set(font='SimHei')
import warnings
warnings.filterwarnings('ignore')

df = pd.read_csv('杭州租房数据.csv')
df.head()
df.shape
df.info()
df.describe().T
df.isnull().sum() # 统计缺失值情况
df.duplicated().sum()  # 统计重复数据情况
df.dropna(inplace=True) # 删除缺失值
df.drop_duplicates(inplace=True) # 删除重复值
df.shape
df['房屋租金'] = df['房屋租金'].apply(lambda x:int(x.split('元')[0]))
df['房屋租金']
df['房屋面积'] = df['房屋面积'].apply(lambda x:int(x[:-2]))
df['房屋面积']
sns.boxplot(data=df,x='房屋租金')
plt.show()
sns.histplot(data=df,x='房屋租金',kde=True)
plt.show()
sns.boxplot(data=df,y='房屋面积')
plt.show()
sns.histplot(data=df,x='房屋面积',kde=True)
plt.show()
plt.scatter(x=df['房屋面积'],y=df['房屋租金'])
plt.show()
df['交付方式'].value_counts()
sns.countplot(data=df,x='交付方式')
plt.show()
df['出租方式'].value_counts().plot(kind='pie',autopct='%.2f%%')
plt.show()
sns.boxplot(data=df,y='房屋租金',x='交付方式')
plt.show()
sns.boxplot(data=df,y='房屋租金',x='出租方式')
plt.show()
df['房屋朝向'].value_counts().plot(kind='pie',autopct='%.2f%%')
plt.show()
sns.barplot(data=df,x='房屋朝向',y='房屋租金')
plt.show()
df.head(2)
def subway_distance(x):
    try:
        result = x.split('约')[1].split('米')[0]
    except:
        result = 0
    return int(result)

df['距地铁距离'] = df['距地铁距离'].apply(subway_distance)
df['距地铁距离']
plt.scatter(x=df['距地铁距离'],y=df['房屋租金'])
plt.show()
df['楼层'].value_counts().plot(kind='pie',autopct='%.2f%%')
plt.show()
sns.barplot(data=df,x='楼层',y='房屋租金')
plt.show()
df['房屋装修'].value_counts()
df['房屋装修'].value_counts().plot(kind='pie',autopct='%.2f%%')
plt.show()
sns.barplot(data=df,x='房屋装修',y='房屋租金')
plt.show()
# 相关性分析
sns.heatmap(df.corr(),vmax=1,annot=True,linewidths=0.5,cbar=False,cmap='YlGnBu',annot_kws={'fontsize':18})
plt.xticks(fontsize=20)
plt.yticks(fontsize=20)
plt.title('各个因素之间的相关系数',fontsize=20)
plt.show()
import jieba
import collections
import re
import stylecloud
from PIL import Image

def draw_WorldCloud(df,pic_name,color='black'):
    data =  ''.join([item for item in df])
    # 文本预处理 :去除一些无用的字符只提取出中文出来
    new_data = re.findall('[\u4e00-\u9fa5]+', data, re.S)
    new_data = "".join(new_data)
    # 文本分词
    seg_list_exact = jieba.cut(new_data, cut_all=True)
    result_list = []
    with open('停用词库.txt', encoding='utf-8') as f: #可根据需要打开停用词库,然后加上不想显示的词语
        con = f.readlines()
        stop_words = set()
        for i in con:
            i = i.replace("\n", "")   # 去掉读取每一行数据的\n
            stop_words.add(i)

    for word in seg_list_exact:
        if word not in stop_words and len(word) > 1:
            result_list.append(word)
    word_counts = collections.Counter(result_list)

    # 词频统计:获取前100最高频的词
    word_counts_top = word_counts.most_common(100)
    print(word_counts_top)

    # 绘制词云图
    stylecloud.gen_stylecloud(text=' '.join(result_list[:500]), # 提取500个词进行绘图
                            collocations=False, # 是否包括两个单词的搭配(二字组)
                            font_path=r'C:\Windows\Fonts\msyh.ttc', #设置字体,参考位置为  C:\Windows\Fonts\ ,根据里面的字体编号来设置
                            size=800, # stylecloud 的大小
                            palette='cartocolors.qualitative.Bold_7', # 调色板,调色网址: https://jiffyclub.github.io/palettable/
                            background_color=color, # 背景颜色
                            icon_name='fas fa-circle', # 形状的图标名称 蒙版网址:https://fontawesome.com/icons?d=gallery&p=2&c=chat,shopping,travel&m=free
                            gradient='horizontal', # 梯度方向
                            max_words=2000, # stylecloud 可包含的最大单词数
                            max_font_size=150, # stylecloud 中的最大字号
                            stopwords=True, # 布尔值,用于筛除常见禁用词
                            output_name=f'{pic_name}.png') # 输出图片
    # 打开图片展示
    img=Image.open(f'{pic_name}.png')
    img.show()
draw_WorldCloud(df['房源亮点'],'房源亮点词云图') # 词云图可视化
draw_WorldCloud(df['配套设施'],'配套设施词云图') # 词云图可视化
# 特征筛选
new_df = df[['房屋租金', '交付方式', '出租方式', '房屋户型', '房屋面积', '房屋朝向', '楼层', '房屋装修','距地铁距离']]
new_df
# 编码处理
from sklearn.preprocessing import LabelEncoder
for col in new_df.describe(include='O').columns.to_list():
    new_df[col] = LabelEncoder().fit_transform(new_df[col])
new_df
from sklearn.model_selection import train_test_split
# 准备数据
X = new_df.drop('房屋租金',axis=1)
y = new_df['房屋租金']
# 划分数据集
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.2,random_state=42)
print('训练集大小:',X_train.shape[0])
print('测试集大小:',X_test.shape[0])
from sklearn.metrics import r2_score,mean_absolute_error,mean_squared_error
# 定义一个训练模型并输出模型的评估指标
def train_model(ml_model):
    print("Model is: ", ml_model)
    model = ml_model.fit(X_train, y_train)
    print("Training score: ", model.score(X_train,y_train))
    predictions = model.predict(X_test)
    r2score = r2_score(y_test, predictions)
    print("r2 score is: ", r2score)
    print('MAE:', mean_absolute_error(y_test,predictions))
    print('MSE:', mean_squared_error(y_test,predictions))
    print('RMSE:', np.sqrt(mean_squared_error(y_test,predictions)))
    # 真实值和预测值的差值
    sns.distplot(y_test - predictions)
# 构建多元线性回归
from sklearn.linear_model import LinearRegression
lg = LinearRegression()
train_model(lg)
# 构建knn回归
from sklearn.neighbors import KNeighborsRegressor
knn = KNeighborsRegressor()
train_model(knn)
# 构建决策树回归
from sklearn.tree import DecisionTreeRegressor
tree = DecisionTreeRegressor()
train_model(tree)
# 构建随机森林回归
from sklearn.ensemble import RandomForestRegressor
forest = RandomForestRegressor()
train_model(forest)
# GBDT回归
from sklearn.ensemble import GradientBoostingRegressor
gbdt = GradientBoostingRegressor()
train_model(gbdt)
# 构建xgboost回归模型
from xgboost import XGBRegressor
xgb = XGBRegressor()
train_model(xgb)
# 特征重要性评分
feat_labels = X_train.columns[0:]
importances = xgb.feature_importances_
indices = np.argsort(importances)[::-1]
index_list = []
value_list = []
for f,j in zip(range(X_train.shape[1]),indices):
    index_list.append(feat_labels[j])
    value_list.append(importances[j])
    print(f + 1, feat_labels[j], importances[j])
plt.figure(figsize=(10,6))
plt.barh(index_list[::-1],value_list[::-1])
plt.yticks(fontsize=12)
plt.title('各特征重要程度排序',fontsize=14)
plt.show()
# 模型预测
y_pred = xgb.predict(X_test)
result_df = pd.DataFrame()
result_df['真实值'] = y_test
result_df['预测值'] = y_pred
result_df.head(10)
# 模型预测可视化
plt.figure(figsize=(10,6))  
plt.plot(range(len(y_test))[:200],y_pred[:200],'b',label='predict')
plt.plot(range(len(y_test))[:200],y_test[:200],'r',label='test')
plt.legend(loc='upper right',fontsize=15)
plt.xlabel('the number of house',fontdict={'weight': 'normal', 'size': 15})
plt.ylabel('value of Price',fontdict={'weight': 'normal', 'size': 15})
plt.show()

资料获取,更多粉丝福利,关注下方公众号获取

a74f7d5d03234f7c8a635562034442a0.gif#pic_center

 

 

  • 100
    点赞
  • 123
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 116
    评论
基于python的山东省二手车价格分析与预测研究内容主要包括以下几个方面: 1. 数据收集:首先需要从不同渠道获取山东省二手车的相关数据,包括车型、品牌、车龄、里程数、上牌时间、上牌地点等信息。可以通过网络爬虫技术从二手车交易网站、车行、个人销售信息中获取数据,并存储在数据库中。 2. 数据清洗:对收集到的数据进行清洗和预处理,包括去除重复值、处理缺失值、异常值和错误值,并进行数据格式转换和标准化,以确保数据的质量和完整性。 3. 数据分析:利用python中的数据分析库(如pandas、numpy)对清洗后的数据进行统计分析,通过可视化手段探索二手车价格与各项参数之间的关系,包括车龄、里程、车型等对价格的影响。 4. 模型建立:基于python中的机器学习库(如scikit-learn、tensorflow)构建二手车价格预测模型,可以采用线性回归、决策树、随机森林等算法进行建模,并使用交叉验证等技术来评估模型的性能。 5. 模型评估和优化:通过对模型进行评估和优化,不断调整模型参数、特征选择等方法,提高模型的准确度和鲁棒性。 6. 预测应用:最后利用模型对未来山东省二手车价格进行预测,为二手车交易市场提供价格参考和决策支持。 总之,基于python的山东省二手车价格分析与预测研究内容涵盖了数据收集、清洗、分析,模型建立、评估和应用的全过程,旨在为二手车交易市场提供科学的价格分析和预测支持。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 116
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

艾派森

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值