机器学习基础-数据分析:房价预测

news/2024/7/5 2:03:32
  1. mac设置中文字体
#要设置下面两行才能显示中文 Arial Unicode MS 为字体
plt.rcParams['font.sans-serif'] = ['Arial Unicode MS']
#设置图片大小
plt.figure(figsize=(20, 11), dpi=200)
  1. pie官方文档

  2. 总体代码

```python
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

# 导入链家二手房数据
lianjia_df = pd.read_csv('./lianjia.csv')
# 删除没用的列
drop =['Id','Direction','Elevator','Renovation']
lianjia_df_clean = lianjia_df.drop(axis=1,columns=drop)
# 重新排列列位置
columns=['Region','District','Garden','Layout','Floor','Year','Size','Price']
lianjia_df_clean = pd.DataFrame(lianjia_df_clean,columns=columns)
lianjia_total_num = lianjia_df_clean['Region'].count()
# 导入安居客二手房数据
anjuke_df = pd.read_csv('./anjuke.csv')
# 数据清洗,重新摆放列位置
anjuke_df['District']=anjuke_df['Region'].str.extract(r'.+?-(.+?)-.+?',expand=False)
anjuke_df['Region']=anjuke_df['Region'].str.extract(r'(.+?)-.+?-.+?',expand=False)
columns=['Region','District','Garden','Layout','Floor','Year','Size','Price']
anjuke_df = pd.DataFrame(anjuke_df,columns=columns)
# 将两个数据集合并
# 增加一列,每平方的价格
df = pd.merge(lianjia_df_clean,anjuke_df,how='outer')
df['PriceMs']=df['Price']/df['Size']
# 对汇总数据进行清洗(Null,重复)
df.dropna(how='any')
df.drop_duplicates(keep='first',inplace=True)
# 删除价格大于25万一平
df = df.loc[df['PriceMs']<25]
anjuke_total_num = anjuke_df['Region'].count()
lianjia_total_num = lianjia_df_clean['Region'].count()
df_num = df['Floor'].count()
total_num = anjuke_total_num + lianjia_total_num
drop_num = total_num - df_num
print(total_num)
print(df_num)
print(drop_num)
26677
24281
2396
# 统计北京各区域二手房房价数量
df_house_count = df.groupby('Region')['Price'].count().sort_values(ascending=False)
print(df_house_count)
# 统计北京各区域二手房房价均值
df_house_mean = df.groupby('Region')['PriceMs'].mean().sort_values(ascending=False)
print(df_house_mean)
Region
朝阳       3147
海淀       2885
昌平       2878
丰台       2865
西城       2115
大兴       2106
通州       1600
房山       1575
东城       1517
顺义       1343
石景山       877
门头沟       500
亦庄开发区     457
北京周边      243
密云         89
平谷         51
怀柔         30
延庆          3
Name: Price, dtype: int64
Region
西城       10.710194
东城        9.897345
海淀        8.643937
朝阳        7.157441
丰台        5.781461
石景山       5.553180
亦庄开发区     4.721659
大兴        4.529565
通州        4.467039
顺义        4.316975
昌平        4.285696
门头沟       4.056528
怀柔        3.634485
房山        3.461693
平谷        2.553905
密云        2.518074
延庆        1.905722
北京周边      1.673941
Name: PriceMs, dtype: float64
def auto_x(bar,x_index):
    x = []
    for i in bar:
        print(i)
        x.append(i.get_x()+i.get_width()/2)
    x = tuple(x)
    plt.xticks(x,x_index)

# 设置一个在您的系统上可用的字体
plt.rcParams['font.sans-serif'] = ['Arial Unicode MS']
#设置图片大小
plt.figure(figsize=(20, 10))


# 创建一个子图
plt.subplot(211)

# 设置标题和标签
plt.title('各区域二手房平均价格的对比', fontsize=20)
plt.ylabel('二手房平均价格(万/平方米)', fontsize=15)
# 指定柱状图的 x 坐标和高度
bar1 = plt.bar(np.arange(len(df_house_mean.index)),df_house_mean.values,color='c')
auto_x(bar1,df_house_mean.index)
# 设置横坐标替换上面的代码
# bar1 = plt.bar(df_house_mean.index,df_house_mean,color='c')


plt.subplot(212)
plt.title('各区域二手房平均数量的对比', fontsize=20)
plt.ylabel('二手房数量', fontsize=15)
bar1 = plt.bar(np.arange(len(df_house_count.index)),df_house_count.values,color='c')
auto_x(bar1,df_house_count.index)
plt.show()
Rectangle(xy=(-0.4, 0), width=0.8, height=10.7102, angle=0)
Rectangle(xy=(0.6, 0), width=0.8, height=9.89735, angle=0)
Rectangle(xy=(1.6, 0), width=0.8, height=8.64394, angle=0)
Rectangle(xy=(2.6, 0), width=0.8, height=7.15744, angle=0)
Rectangle(xy=(3.6, 0), width=0.8, height=5.78146, angle=0)
Rectangle(xy=(4.6, 0), width=0.8, height=5.55318, angle=0)
Rectangle(xy=(5.6, 0), width=0.8, height=4.72166, angle=0)
Rectangle(xy=(6.6, 0), width=0.8, height=4.52956, angle=0)
Rectangle(xy=(7.6, 0), width=0.8, height=4.46704, angle=0)
Rectangle(xy=(8.6, 0), width=0.8, height=4.31697, angle=0)
Rectangle(xy=(9.6, 0), width=0.8, height=4.2857, angle=0)
Rectangle(xy=(10.6, 0), width=0.8, height=4.05653, angle=0)
Rectangle(xy=(11.6, 0), width=0.8, height=3.63449, angle=0)
Rectangle(xy=(12.6, 0), width=0.8, height=3.46169, angle=0)
Rectangle(xy=(13.6, 0), width=0.8, height=2.55391, angle=0)
Rectangle(xy=(14.6, 0), width=0.8, height=2.51807, angle=0)
Rectangle(xy=(15.6, 0), width=0.8, height=1.90572, angle=0)
Rectangle(xy=(16.6, 0), width=0.8, height=1.67394, angle=0)
Rectangle(xy=(-0.4, 0), width=0.8, height=3147, angle=0)
Rectangle(xy=(0.6, 0), width=0.8, height=2885, angle=0)
Rectangle(xy=(1.6, 0), width=0.8, height=2878, angle=0)
Rectangle(xy=(2.6, 0), width=0.8, height=2865, angle=0)
Rectangle(xy=(3.6, 0), width=0.8, height=2115, angle=0)
Rectangle(xy=(4.6, 0), width=0.8, height=2106, angle=0)
Rectangle(xy=(5.6, 0), width=0.8, height=1600, angle=0)
Rectangle(xy=(6.6, 0), width=0.8, height=1575, angle=0)
Rectangle(xy=(7.6, 0), width=0.8, height=1517, angle=0)
Rectangle(xy=(8.6, 0), width=0.8, height=1343, angle=0)
Rectangle(xy=(9.6, 0), width=0.8, height=877, angle=0)
Rectangle(xy=(10.6, 0), width=0.8, height=500, angle=0)
Rectangle(xy=(11.6, 0), width=0.8, height=457, angle=0)
Rectangle(xy=(12.6, 0), width=0.8, height=243, angle=0)
Rectangle(xy=(13.6, 0), width=0.8, height=89, angle=0)
Rectangle(xy=(14.6, 0), width=0.8, height=51, angle=0)
Rectangle(xy=(15.6, 0), width=0.8, height=30, angle=0)
Rectangle(xy=(16.6, 0), width=0.8, height=3, angle=0)

在这里插入图片描述

# 各区域二手房数量百分比
plt.figure(figsize=(10, 10))
plt.title('各区域二手房数量的百分比',fontsize=20)
ex = [0]*len(df_house_count)
ex[0] = 0.1
plt.pie(df_house_count,radius=1,autopct='%1.f%%',labels=df_house_count.index,explode=ex)
plt.show()


在这里插入图片描述

# 获取二手房总价的范围
def get_price_range(price, base=100):
    return '{0}-{1}'.format(int(price//base)*base, int(price//base)*base+base)

# 获取二手房面积的范围
def get_size_range(size, base=30):
    return '{0}-{1}'.format(int(size//base)*base, int(size//base)*base+base)

# 筛选房屋总价小于1000万的二手房信息进行统计 \d+表示一到多个数字
df['GroupPrice'] = df['Price'].apply(get_price_range)
df['GroupPriceSplit'] = df['GroupPrice'].str.extract('(\d+)-\d+', expand=False)
df['GroupPriceSplit'] = df['GroupPriceSplit'].astype('int')

sort_by_price_range = df.loc[df['GroupPriceSplit']<1000, ['GroupPrice','Price','GroupPriceSplit']] 
sort_by_price_range.set_index('GroupPrice', inplace=True) 
sort_by_price_range.sort_values(by='GroupPriceSplit', inplace=True) 

# 筛选房屋面积小于300万的二手房信息进行统计
df['GroupSize'] = df['Size'].apply(get_size_range)
df['GroupSizeSplit'] = df['GroupSize'].str.extract('(\d+)-\d+', expand=False)
df['GroupSizeSplit'] = df['GroupSizeSplit'].astype('int')
sort_by_size_range = df.loc[df['GroupSizeSplit']<300, ['GroupSize','Size','GroupSizeSplit']] 
sort_by_size_range.set_index('GroupSize', inplace=True)
sort_by_size_range.sort_values(by='GroupSizeSplit', inplace=True)
display(sort_by_size_range)


# 对房价和房屋面积分组
df_group_price = sort_by_price_range.groupby('GroupPrice')['Price'].count()
df_group_size = sort_by_size_range.groupby('GroupSizeSplit')['Size'].count()
    
# 房价范围 vs 房屋数量可视化分析
fig_group_pirce = plt.figure(figsize=(20,5))
plt.subplot(121)
plt.title(u'北京二手房房价/数量统计', fontsize=15)
plt.xlabel(u'二手房房价区间(单位:万)', fontsize=15)
plt.ylabel(u'二手房数量', fontsize=15)
rect_group_price = plt.bar(np.arange(len(df_group_price.index)), df_group_price.values)
auto_x(rect_group_price, df_group_price.index) 

plt.subplot(122)
plt.title(u'北京二手房面积/数量统计', fontsize=15)
plt.xlabel(u'二手房房屋面积区间', fontsize=15)
plt.ylabel(u'二手房数量', fontsize=15)
rect_group_size = plt.bar(np.arange(len(df_group_size.index)), df_group_size.values)
auto_x(rect_group_size, df_group_size.index) 

plt.show()
SizeGroupSizeSplit
GroupSize
0-3022.00
0-3020.00
0-3029.00
0-3015.00
0-3028.00
.........
270-300273.0270
270-300298.0270
270-300284.0270
270-300280.0270
270-300275.0270

23877 rows × 2 columns

Rectangle(xy=(-0.4, 0), width=0.8, height=129, angle=0)
Rectangle(xy=(0.6, 0), width=0.8, height=641, angle=0)
Rectangle(xy=(1.6, 0), width=0.8, height=2588, angle=0)
Rectangle(xy=(2.6, 0), width=0.8, height=4601, angle=0)
Rectangle(xy=(3.6, 0), width=0.8, height=4277, angle=0)
Rectangle(xy=(4.6, 0), width=0.8, height=3207, angle=0)
Rectangle(xy=(5.6, 0), width=0.8, height=2227, angle=0)
Rectangle(xy=(6.6, 0), width=0.8, height=1535, angle=0)
Rectangle(xy=(7.6, 0), width=0.8, height=1167, angle=0)
Rectangle(xy=(8.6, 0), width=0.8, height=864, angle=0)
Rectangle(xy=(-0.4, 0), width=0.8, height=56, angle=0)
Rectangle(xy=(0.6, 0), width=0.8, height=3997, angle=0)
Rectangle(xy=(1.6, 0), width=0.8, height=8441, angle=0)
Rectangle(xy=(2.6, 0), width=0.8, height=5608, angle=0)
Rectangle(xy=(3.6, 0), width=0.8, height=3046, angle=0)
Rectangle(xy=(4.6, 0), width=0.8, height=1334, angle=0)
Rectangle(xy=(5.6, 0), width=0.8, height=663, angle=0)
Rectangle(xy=(6.6, 0), width=0.8, height=371, angle=0)
Rectangle(xy=(7.6, 0), width=0.8, height=219, angle=0)
Rectangle(xy=(8.6, 0), width=0.8, height=142, angle=0)

在这里插入图片描述


http://lihuaxi.xjx100.cn/news/1595714.html

相关文章

QMC5883L-磁力计椭球拟合校准

1.概述 磁力计椭球拟合校准是一种将磁力计测量数据校准到真实磁场的技术。这种技术通常使用椭球模型来拟合磁力计的测量结果&#xff0c;然后通过最小二乘法来找到拟合参数的最优解。 2.总体思想 磁力计椭球拟合校准的思想包括以下几个步骤&#xff1a; 1.数据预处理&#x…

【STM32单片机】数学自动出题器设计

文章目录 一、功能简介二、软件设计三、实验现象联系作者 一、功能简介 本项目使用STM32F103C8T6单片机控制器&#xff0c;使用按键、IIC OLED模块等。 主要功能&#xff1a; 系统运行后&#xff0c;OLED液晶显示出题器开机界面&#xff0c;默认结果范围为100&#xff0c;可按…

用《斗破苍穹》的视角打开C#委托2 委托链 / 泛型委托 / GetInvocationList

委托链 经过不懈地努力&#xff0c;我终于成为了斗师&#xff0c;并成功掌握了两种斗技——八极崩和焰分噬浪尺。于是&#xff0c;我琢磨着&#xff0c;能不能搞一套连招&#xff0c;直接把对方带走。 using System; using System.Collections.Generic; using System.Linq; u…

Vue中...(扩展运算符)的作用

对数组和对象而言&#xff0c;就是将运算符后面的变量里东西每一项拆下来。 &#xff08;一&#xff09;操作数组 // 1.把数组中的元素孤立起来 let iArray [1, 2, 3]; console.log(...iArray); // 打印结果 1 2 3// 2.在数组中添加元素 let iArray [1, 2, 3]; console.log…

云资源上传的审核机制

云资源上传的审核机制可以通过以下几个步骤进行判断&#xff1a; 扫描检测&#xff1a;使用自动化工具对云资源进行扫描和检测&#xff0c;以识别潜在的违规内容或安全风险。这可以包括使用恶意软件检测工具、敏感信息检测工具、版权侵权检测工具等。 人工审核&#xff1a;对于…

网站安全维护:守护您的数字领土

在这个数字时代&#xff0c;网站已成为企业和个人展示自己的重要平台。然而&#xff0c;随着互联网的高速发展&#xff0c;网站安全问题也日益严峻。黑客和入侵软件等威胁不断涌现&#xff0c;因此&#xff0c;保护网站免受这些威胁的影响变得至关重要。本文将探讨网站安全维护…

GitLab平台安装中经典安装语句含义解析

yum -y install policycoreutils openssh-server openssh-clients postfix 这是一个Linux命令&#xff0c;用于使用YUM包管理器安装指定的软件包。下面是对这个命令各部分的解释&#xff1a; yum&#xff1a;这是一个Linux命令行工具&#xff0c;用于管理RPM&#xff08;Red …

Helm部署EMQX集群

端口说明&#xff1a; 端口说明集群内端口Nodeport备注mqtt188331274Port for MQTTmqttssl888331170Port for MQTT(SSL)mgmt8081无ws808330099Port for WebSocket/HTTPwss808432580Port for WSS/HTTPSdashboard1808331303Port for dashboard 一、Helm在线部署EMQX集群 1、安…