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_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

机器学习之挖掘melb_data.csv数据的更多相关文章

  1. python机器学习-sklearn挖掘乳腺癌细胞(五)

    python机器学习-sklearn挖掘乳腺癌细胞( 博主亲自录制) 网易云观看地址 https://study.163.com/course/introduction.htm?courseId=10 ...

  2. python机器学习-sklearn挖掘乳腺癌细胞(四)

    python机器学习-sklearn挖掘乳腺癌细胞( 博主亲自录制) 网易云观看地址 https://study.163.com/course/introduction.htm?courseId=10 ...

  3. python机器学习-sklearn挖掘乳腺癌细胞(三)

    python机器学习-sklearn挖掘乳腺癌细胞( 博主亲自录制) 网易云观看地址 https://study.163.com/course/introduction.htm?courseId=10 ...

  4. python机器学习-sklearn挖掘乳腺癌细胞(二)

    python机器学习-sklearn挖掘乳腺癌细胞( 博主亲自录制) 网易云观看地址 https://study.163.com/course/introduction.htm?courseId=10 ...

  5. python机器学习-sklearn挖掘乳腺癌细胞(一)

    python机器学习-sklearn挖掘乳腺癌细胞( 博主亲自录制) 网易云观看地址 https://study.163.com/course/introduction.htm?courseId=10 ...

  6. SQL Server 2016五大优势挖掘企业用户数据价值

    SQL Server 2016五大优势挖掘企业用户数据价值 转载自:http://soft.zdnet.com.cn/software_zone/2016/0318/3074442.shtml 3月1 ...

  7. [moka同学摘录]Yii2 csv数据导出扩展

    yii2-thecsv(Yii2框架csv数据导出扩展) github: https://github.com/13552277443/yii2-thecsv 1.安装 运行 php composer ...

  8. mysql导出csv/excel文件的几种方法,mysql的load导入csv数据

    方法一 php教程用mysql的命令和shell select * into outfile './bestlovesky.xls' from bestlovesky where 1 order by ...

  9. python_如何读写csv数据

    案例: 通过股票网站,我们获取了中国股市数据集,它以csv数据格式存储 Data,Open,High,Low,Close,Volume,Adj Close 2016-06-28,8.63,8.47,8 ...

随机推荐

  1. Helm 安装部署Kubernetes的dashboard

    Kubernetes Dashboard 是 k8s集群的一个 WEB UI管理工具,代码托管在 github 上,地址:https://github.com/kubernetes/dashboard ...

  2. swift函数式编程之compose

    func a(en:String) -> String { return en + "a"; } func b(en:String) -> String { retur ...

  3. Tomcat不能访问ln -s软连接文件夹的前因后果

    为了部署方便,把webapps下的大文件(图片等资源)放到工程外,通过软连接的方式设置 命令最常用的参数是-s,具体用法是:ln -s 源文件 目标文件. ln -s /usr/local/pic/i ...

  4. apollo报:系统出错,请重试或联系系统负责人

    说明:基于 docker 搭建的 apollo,创建项目后一直报系统出错,请重试或联系系统负责人错误. 项目人员列表一直空白: 经排查是数据库配置参数不匹配,由于自己的虚拟机 ip 为 192.168 ...

  5. 计蒜客 41391.query-二维偏序+树状数组(预处理出来满足情况的gcd) (The Preliminary Contest for ICPC Asia Xuzhou 2019 I.) 2019年徐州网络赛)

    query Given a permutation pp of length nn, you are asked to answer mm queries, each query can be rep ...

  6. ffmpeg结合SDL编写播放器(三)

    接下来是解析影片的帧 /*** project.c ***/ #include<stdio.h> #include<libavcodec/avcodec.h> #include ...

  7. 查看Linux机器的外网IP

    curl icanhazip.comcurl ifconfig.mecurl curlmyip.comcurl ip.appspot.comcurl ipinfo.io/ipcurl ipecho.n ...

  8. svn无法还原 、svn无法更新

    报错:  Previous operation has not finished; run 'cleanup' if it was interrupted 上一个操作尚未完成:如果中断,请运行“清理”

  9. 市值TOP10,人类进化及中美坐标

    题记:观察人类进化,以及各国.各民族在这个进化中所起的作用.所处的位置,市值 TOP 10 的变迁,会是一个再好不过的指标! 2008年,经历了全球金融危机后,原油期货一路飙升,创出了147.27美元 ...

  10. docker删除名称为<none>的镜像

    docker rmi $(docker images | awk '/^<none>/ { print $3 }')