import numpy as np
import pandas as pd
from Udacity.model_check.boston_house_price import visuals as vs # Supplementary code
from sklearn.model_selection import ShuffleSplit # Pretty display for notebooks
# 让结果在notebook中显示 # Load the Boston housing dataset
# 载入波士顿房屋的数据集
data = pd.read_csv('housing.csv')
prices = data['MEDV']
features = data.drop('MEDV', axis=1)
# print(data.describe())
# Success
# 完成
print("Boston housing dataset has {} data points with {} variables each.".format(*data.shape))
# 目标:计算价值的最小值
minimum_price = np.min(data['MEDV']) # 目标:计算价值的最大值
maximum_price = np.max(data['MEDV']) # 目标:计算价值的平均值
mean_price = np.mean(data['MEDV']) # 目标:计算价值的中值
median_price = np.median(data['MEDV']) # 目标:计算价值的标准差
std_price = np.std(data['MEDV']) # 目标:输出计算的结果
print("Statistics for Boston housing dataset:\n")
print("Minimum price: ${:,.2f}".format(minimum_price))
print("Maximum price: ${:,.2f}".format(maximum_price))
print("Mean price: ${:,.2f}".format(mean_price))
print("Median price ${:,.2f}".format(median_price))
print("Standard deviation of prices: ${:,.2f}".format(std_price))
# RM,LSTAT,PTRATIO,MEDV
"""
初步分析结果是
1.RM越大MEDV越大
2.LSTATA越大MEDV越小
3.PTRATIO越大MEDV越小
""" # TODO: Import 'r2_score'
def performance_metric(y_true, y_predict):
""" Calculates and returns the performance score between
true and predicted values based on the metric chosen. """
from sklearn.metrics import r2_score
# TODO: Calculate the performance score between 'y_true' and 'y_predict' score = r2_score(y_true,y_predict) # Return the score
return score # score = performance_metric([3, -0.5, 2, 7, 4.2], [2.5, 0.0, 2.1, 7.8, 5.3])
# print ("Model has a coefficient of determination, R^2, of {:.3f}.".format(score)) from sklearn.model_selection import train_test_split X_train, X_test, y_train, y_test = train_test_split(features, prices, test_size=0.80, random_state=1) # Success
print ("Training and testing split was successful.")
# vs.ModelLearning(features, prices) def fit_model(X, y):
""" Performs grid search over the 'max_depth' parameter for a
decision tree regressor trained on the input data [X, y]. """
from sklearn.tree import DecisionTreeRegressor from sklearn.model_selection import KFold
# Create cross-validation sets from the training data
cross_validator = KFold(10)
# cv_sets = ShuffleSplit(X.shape[0], test_size=0.20, random_state=0) # TODO: Create a decision tree regressor object
regressor = DecisionTreeRegressor() # TODO: Create a dictionary for the parameter 'max_depth' with a range from 1 to 10
max_depth = [1,2,3,4,5,6,7,8,9,10]
params = {"max_depth":max_depth}
from sklearn.metrics import make_scorer
# TODO: Transform 'performance_metric' into a scoring function using 'make_scorer'
scoring_fnc = make_scorer(performance_metric)
from sklearn.model_selection import GridSearchCV
# TODO: Create the grid search object
grid = GridSearchCV(regressor,params,scoring_fnc,cv=cross_validator) # Fit the grid search object to the data to compute the optimal model
grid = grid.fit(X, y) # Return the optimal model after fitting the data
return grid.best_estimator_ reg = fit_model(X_train, y_train) # Produce the value for 'max_depth'
print ("Parameter 'max_depth' is {} for the optimal model.".format(reg.get_params()['max_depth'])) client_data = [[5, 17, 15], # Client 1
[4, 32, 22], # Client 2
[8, 3, 12]] # Client 3 # Show predictions
for i, price in enumerate(reg.predict(client_data)):
print ("Predicted selling price for Client {}'s home: ${:,.2f}".format(i+1, price))

【udacity】机器学习-波士顿房价预测的更多相关文章

  1. 【udacity】机器学习-波士顿房价预测小结

    Evernote Export 机器学习的运行步骤 1.导入数据 没什么注意的,成功导入数据集就可以了,打印看下数据的标准格式就行 用个info和describe 2.分析数据 这里要详细分析数据的内 ...

  2. 波士顿房价预测 - 最简单入门机器学习 - Jupyter

    机器学习入门项目分享 - 波士顿房价预测 该分享源于Udacity机器学习进阶中的一个mini作业项目,用于入门非常合适,刨除了繁琐的部分,保留了最关键.基本的步骤,能够对机器学习基本流程有一个最清晰 ...

  3. 机器学习实战二:波士顿房价预测 Boston Housing

    波士顿房价预测 Boston housing 这是一个波士顿房价预测的一个实战,上一次的Titantic是生存预测,其实本质上是一个分类问题,就是根据数据分为1或为0,这次的波士顿房价预测更像是预测一 ...

  4. Python之机器学习-波斯顿房价预测

    目录 波士顿房价预测 导入模块 获取数据 打印数据 特征选择 散点图矩阵 关联矩阵 训练模型 可视化 波士顿房价预测 导入模块 import pandas as pd import numpy as ...

  5. Tensorflow之多元线性回归问题(以波士顿房价预测为例)

    一.根据波士顿房价信息进行预测,多元线性回归+特征数据归一化 #读取数据 %matplotlib notebook import tensorflow as tf import matplotlib. ...

  6. 《用Python玩转数据》项目—线性回归分析入门之波士顿房价预测(二)

    接上一部分,此篇将用tensorflow建立神经网络,对波士顿房价数据进行简单建模预测. 二.使用tensorflow拟合boston房价datasets 1.数据处理依然利用sklearn来分训练集 ...

  7. chapter02 回归模型在''美国波士顿房价预测''问题中实践

    #coding=utf8 # 从sklearn.datasets导入波士顿房价数据读取器. from sklearn.datasets import load_boston # 从sklearn.mo ...

  8. 基于sklearn的波士顿房价预测_线性回归学习笔记

    > 以下内容是我在学习https://blog.csdn.net/mingxiaod/article/details/85938251 教程时遇到不懂的问题自己查询并理解的笔记,由于sklear ...

  9. 02-11 RANSAC算法线性回归(波斯顿房价预测)

    目录 RANSAC算法线性回归(波斯顿房价预测) 一.RANSAC算法流程 二.导入模块 三.获取数据 四.训练模型 五.可视化 更新.更全的<机器学习>的更新网站,更有python.go ...

随机推荐

  1. [bzoj2259][Oibh]新型计算机_Dijkstra

    新型计算机 bzoj-2259 Oibh 题目大意:给定一个n个数的数列,第i个数为a[i],更改第i个数至x的代价为|x-a[i]|.求最小代价,使得:读入一个数s1后,向后连着读s1个数,然后如s ...

  2. Spring深入理解(一)

    Spring 框架的设计理念与设计模式分析 Spring核心组件 Spring 框架中的核心组件只有三个:Core.Context 和 Beans Spring 的设计理念 前面介绍了 Spring ...

  3. 万众创业,互联网+,WTO

    WTO的保护期,发展的非常繁荣.但大部分的资源都配置在了房地产这个支柱产业, 而被保护的行业小日子过得不错, 研发再投入?那是傻子才做的事情,别墅.豪车.美女.这才是生活. 但突然有一天,发现保护期要 ...

  4. CefSharp 设置cookie

    设置cookie var cookieManager = CefSharp.Cef.GetGlobalCookieManager(); await cookieManager.SetCookieAsy ...

  5. @RequestParam,@PathVariable等注解区别

    一.@RequestParam和@PathVariable的区别 1.@RequestParam是从uri中request后面的参数串来取得参数的 2.@PathVariable是从uri模板中取得参 ...

  6. luogu2518 [HAOI2010] 计数

    题目大意 给出一个数字$n$,求满足下列条件的数$x$的个数: $x<n$ 对于来自于$x$十进制各个数位上的非零数字,它们的种类与个数都与$n$的相同. 思路 入手点 设$n$有$t$位数字, ...

  7. Linux - vim的基本使用

    通过which指令来查看文件位置! [root@local ~]# which vim /usr/bin/vim [root@local ~]# which vi /usr/bin/vi [root@ ...

  8. ELK+kafka日志收集

    一.服务器信息   版本 部署服务器 用途 备注 JDK jdk1.8.0_102 使用ELK5的服务器 Logstash 5.1.1 安装Tomcat的服务器 发送日志 Kafka降插件版本 Log ...

  9. VisoStudio 允许局域网联机调试网站

    第一步:修改配置文件 添加IP访问配置 找到vs访问网站的端口后,添加一行新的配置 第二步:使用CMD命令进行网络配置 netsh http / user=everyone 删除网络配置的命令(注意最 ...

  10. golang二维码

    package main import ( "github.com/boombuler/barcode" "github.com/boombuler/barcode/qr ...