首页 技术 正文
技术 2022年11月16日
0 收藏 720 点赞 3,135 浏览 3763 个字

mel_data.csv是关于melb地区房屋的数据##


mel_data.csv

import pandas as pd
melbourne_file_path = "E:\data\Melbourne Housing Snapshot\melb_data.csv"
melbourne_data = pd.read_csv(melbourne_file_path) #读数据
melbourne_data.columns  #读取属性名
Index(['Suburb', 'Address', 'Rooms', 'Type', 'Price', 'Method', 'SellerG',
'Date', 'Distance', 'Postcode', 'Bedroom2', 'Bathroom', 'Car',
'Landsize', 'BuildingArea', 'YearBuilt', 'CouncilArea', 'Lattitude',
'Longtitude', 'Regionname', 'Propertycount'],
dtype='object')
#这里忽略缺失值
melbourne_data = melbourne_data.dropna(axis=0)
#可以使用点符号来提取变量。这一列存储在一个Series中,它大致类似于只有一列数据的DataFrame。
#我们将使用点符号来选择我们想要预测的列,这称为预测目标。按照惯例,预测目标称为y。
y = melbourne_data.Price #选取预测目标属性
#筛选变量属性
melbourne_features = ['Rooms', 'Bathroom', 'Landsize', 'Lattitude', 'Longtitude']
x = melbourne_data[melbourne_features]  #简单清洗后的数据
x.describe()  #数据描述

.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}

.dataframe tbody tr th {
vertical-align: top;
}.dataframe thead th {
text-align: right;
}
Rooms Bathroom Landsize Lattitude Longtitude
count 6196.000000 6196.000000 6196.000000 6196.000000 6196.000000
mean 2.931407 1.576340 471.006940 -37.807904 144.990201
std 0.971079 0.711362 897.449881 0.075850 0.099165
min 1.000000 1.000000 0.000000 -38.164920 144.542370
25% 2.000000 1.000000 152.000000 -37.855438 144.926198
50% 3.000000 1.000000 373.000000 -37.802250 144.995800
75% 4.000000 2.000000 628.000000 -37.758200 145.052700
max 8.000000 8.000000 37000.000000 -37.457090 145.526350
x.head()  #head函数默认取数据集前5行,观察数据集有时能发现惊喜

.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}

.dataframe tbody tr th {
vertical-align: top;
}.dataframe thead th {
text-align: right;
}
Rooms Bathroom Landsize Lattitude Longtitude
1 2 1.0 156.0 -37.8079 144.9934
2 3 2.0 134.0 -37.8093 144.9944
4 4 1.0 120.0 -37.8072 144.9941
6 3 2.0 245.0 -37.8024 144.9993
7 2 1.0 256.0 -37.8060 144.9954
#导入sklearn,选择决策树
from sklearn.tree import DecisionTreeRegressor
#定义模型的随机参数以确保每次运行结果相同
melbourne_model = DecisionTreeRegressor(random_state=1)
#创建模型
melbourne_model.fit(x,y)
DecisionTreeRegressor(criterion='mse', max_depth=None, max_features=None,
max_leaf_nodes=None, min_impurity_decrease=0.0,
min_impurity_split=None, min_samples_leaf=1,
min_samples_split=2, min_weight_fraction_leaf=0.0,
presort=False, random_state=1, splitter='best')
#对训练数据的前几行进行预测(这样做基本没意义,后面我们会看到)
print('对前5行数据做预测')
print(x.head())
print('预测结果是:')
print(melbourne_model.predict(x.head()))
对前5行数据做预测
Rooms Bathroom Landsize Lattitude Longtitude
1 2 1.0 156.0 -37.8079 144.9934
2 3 2.0 134.0 -37.8093 144.9944
4 4 1.0 120.0 -37.8072 144.9941
6 3 2.0 245.0 -37.8024 144.9993
7 2 1.0 256.0 -37.8060 144.9954
预测结果是:
[ 1035000. 1465000. 1600000. 1876000. 1636000.]
#上面我们的预测结果出来了,那如何评价我们的模型的预测能力?
#我们很自然的想到将预测结果越接近现实越好,能掐会算、料事如神那更好
#我们可以将预测值与实际值比较,得出一个误差,那这个误差越小就代表预测越准
#我们有很多行数据,我们将这些误差求和做平均就得到一个模型的平均误差
#这个平均误差被称为mean absolute error 平均绝对误差MAE,它是一个简单的评价指标
from sklearn.metrics import mean_absolute_errorpredicted_home_prices = melbourne_model.predict(x)
mean_absolute_error(y, predicted_home_prices)
1115.7467183128902
#我们来看看,MAE是1115.7,相比百万级的房价误差很小了,我们的预测很精准?
#是,看起来还不错,但这里预测的是模型已知的数据,这是事后诸葛亮
#把考试原题研究了一波,再去考试那结果能不好吗?
#我们创建模型的目标是什么?我们想要从这些数据中挖掘规律,去预测我们想知道但不知道的东西
#这里我们想知道的东西是房价,所以我们评价模型的预测能力要使用模型未使用过的数据
#就像严格的考试很少出现原题,只有这样才能考出真实水平
#我们的模型评价也要用未使用过的数据才能得出真实的预测能力
#这个未使用过的数据我们称为validation data验证数据,用于验证模型能力
from sklearn.model_selection import train_test_split# split data into training and validation data, for both features and target
# The split is based on a random number generator. Supplying a numeric value to
# the random_state argument guarantees we get the same split every time we
# run this script.
train_x, val_x, train_y, val_y = train_test_split(x, y, random_state = 0)
# Define model
melbourne_model = DecisionTreeRegressor()
# Fit model
melbourne_model.fit(train_x, train_y)# get predicted prices on validation data
val_predictions = melbourne_model.predict(val_x)
print(mean_absolute_error(val_y, val_predictions))
274669.096837
#上面我们将数据分为训练数据和测试数据,用训练数据得到的模型去预测测试数据
#wow,MAE已经达到十万级,与百万级的房价相比这个误差是惊人的
#我们看到这个模型考试做原题表现不错,但不考原题就表现极其差
#所以我们测试模型时一定不能、不能、不能用训练数据,我们要用validation data
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,989
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,504
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,348
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,131
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,765
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,842