机器学习之挖掘melb_data.csv数据

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()  #数据描述
RoomsBathroomLandsizeLattitudeLongtitude
count6196.0000006196.0000006196.0000006196.0000006196.000000
mean2.9314071.576340471.006940-37.807904144.990201
std0.9710790.711362897.4498810.0758500.099165
min1.0000001.0000000.000000-38.164920144.542370
25%2.0000001.000000152.000000-37.855438144.926198
50%3.0000001.000000373.000000-37.802250144.995800
75%4.0000002.000000628.000000-37.758200145.052700
max8.0000008.00000037000.000000-37.457090145.526350
x.head()  #head函数默认取数据集前5行,观察数据集有时能发现惊喜
RoomsBathroomLandsizeLattitudeLongtitude
121.0156.0-37.8079144.9934
232.0134.0-37.8093144.9944
441.0120.0-37.8072144.9941
632.0245.0-37.8024144.9993
721.0256.0-37.8060144.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_error

predicted_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 

转载于:https://www.cnblogs.com/roygood/p/10136054.html

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值