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. react hooks沉思录

    将UI组件抽象为状态处理机.分为普通状态和副作用状态. 一.综述 useState:处理函数只改变引用的状态本身:副作用状态:会对引用状态以外的状态和变量进行修改:useReducer:用解藕化的机制 ...

  2. [教程]Ubuntu16.04安装texstudio

    [教程]Ubuntu16.04安装texstudio step 1 首先要下载texlive. 不会的戳这里 然后直接终端命令 sudo apt-get install texstudio step ...

  3. CLR Exception---E0434F4D

    什么是CLR Exception---E0434F4D 就是公共语言运行时(CLR)异常,异常代码为0xE0434F4D.因此任何托管异常,如NullReferenceException.invali ...

  4. golang 几个好用的cli package

    cli 开发在golang 的软件开发中占用很大,同时开源的比较好用的cli也很多,以下是整理的几个cli github.com/spf13/cobra 这个比较有名了, 好多框架都使用了这个 以下是 ...

  5. nginx之系统参数优化

    系统参数优化 默认的Linux内核参数考虑的是最通用场景,不符合用于支持高并发访问的Web服务器的定义,根据业务特点来进行调整,当Nginx作为静态web内容服务器.反向代理或者提供压缩服务器的服务器 ...

  6. linux高性能服务器编程 (三) --TCP协议详解

    第三章 IP协议详解 TCP协议是TCP/IP协议族中的另外一个重要的协议,与IP协议相比,TCP协议更高进应用层.一些重要的socket选项都和TCP协议相关.这一章主要从如下方面学习: 1)TCP ...

  7. shell 文件的包含

    使用.或者source api.sh function intadd() { let data=$+$ echo $data } test.sh #!/bin/bash . api.sh read d ...

  8. 【Spring Boot】内嵌容器

    Spring Boot内嵌容器支持Tomcat.Jetty.Undertow. tomcat容器 spring boot 的web应用开发必须使用spring-boot-starter-web,其默认 ...

  9. Linux搭建简单的http文件服务器111

    http://192.168.31.69:8090/file/http://47.92.90.25:21888/file/在Ubuntu中通过apt-get install apache2 安装apa ...

  10. Appium入门脚本

    没有用框架的代码实现登录功能: import time from selenium import webdriver # 创建字典 desired_caps = {} desired_caps['pl ...