【udacity】机器学习-波士顿房价预测
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】机器学习-波士顿房价预测的更多相关文章
- 【udacity】机器学习-波士顿房价预测小结
Evernote Export 机器学习的运行步骤 1.导入数据 没什么注意的,成功导入数据集就可以了,打印看下数据的标准格式就行 用个info和describe 2.分析数据 这里要详细分析数据的内 ...
- 波士顿房价预测 - 最简单入门机器学习 - Jupyter
机器学习入门项目分享 - 波士顿房价预测 该分享源于Udacity机器学习进阶中的一个mini作业项目,用于入门非常合适,刨除了繁琐的部分,保留了最关键.基本的步骤,能够对机器学习基本流程有一个最清晰 ...
- 机器学习实战二:波士顿房价预测 Boston Housing
波士顿房价预测 Boston housing 这是一个波士顿房价预测的一个实战,上一次的Titantic是生存预测,其实本质上是一个分类问题,就是根据数据分为1或为0,这次的波士顿房价预测更像是预测一 ...
- Python之机器学习-波斯顿房价预测
目录 波士顿房价预测 导入模块 获取数据 打印数据 特征选择 散点图矩阵 关联矩阵 训练模型 可视化 波士顿房价预测 导入模块 import pandas as pd import numpy as ...
- Tensorflow之多元线性回归问题(以波士顿房价预测为例)
一.根据波士顿房价信息进行预测,多元线性回归+特征数据归一化 #读取数据 %matplotlib notebook import tensorflow as tf import matplotlib. ...
- 《用Python玩转数据》项目—线性回归分析入门之波士顿房价预测(二)
接上一部分,此篇将用tensorflow建立神经网络,对波士顿房价数据进行简单建模预测. 二.使用tensorflow拟合boston房价datasets 1.数据处理依然利用sklearn来分训练集 ...
- chapter02 回归模型在''美国波士顿房价预测''问题中实践
#coding=utf8 # 从sklearn.datasets导入波士顿房价数据读取器. from sklearn.datasets import load_boston # 从sklearn.mo ...
- 基于sklearn的波士顿房价预测_线性回归学习笔记
> 以下内容是我在学习https://blog.csdn.net/mingxiaod/article/details/85938251 教程时遇到不懂的问题自己查询并理解的笔记,由于sklear ...
- 02-11 RANSAC算法线性回归(波斯顿房价预测)
目录 RANSAC算法线性回归(波斯顿房价预测) 一.RANSAC算法流程 二.导入模块 三.获取数据 四.训练模型 五.可视化 更新.更全的<机器学习>的更新网站,更有python.go ...
随机推荐
- 【例题 4-5 uva 512】Spreadsheet Tracking
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 每个操作对与一个点来说变化是固定的. 因此可以不用对整个数组进行操作. 对于每个询问,遍历所有的操作.对输入的(x,y)进行相应的变 ...
- leetcode || 50、Pow(x, n)
problem: Implement pow(x, n). Hide Tags Math Binary Search 题意:求x的n次幂 thinking: (1)最简单想到的是直观上的数学幂函数求法 ...
- arcgis server10.2.2公布地图基础服务的详细步骤
1.直接打开制作好的.mxd文档,比方这里: 2.打开mxd文档之后.打开菜单:file-share as -services 弹出地图公布服务的界面: 点击publish之后,耐心的等待一段时间,地 ...
- Tomcat容器 web.xml具体解释
<init-param> <param-name>debug</param-name> <param-value>0</param-value&g ...
- oc1
// zs.h #ifndef __day11__zs__ #define __day11__zs__ #include <stdio.h> int sum(int v1, int v2) ...
- tflearn anaconda 安装过程记录
准备工作:gcc升级为4.8.2glibc升级为2.18 /opt/xxx/xxx/components/ficlient/bigdata_env 里加入:export LD_LIBRARY_PATH ...
- 【NOIP 2004】 虫食算
[题目链接] https://www.luogu.org/problemnew/show/P1092 [算法] 搜索 + 剪枝 直接搜索显然会超时,考虑剪枝 1 : 优化搜索顺序 2 : 假设我们已经 ...
- Serializable-源码分析
package java.io; public interface Serializable { } 代码很简单,功能也很简单,对象通过这个接口来实现序列化和反序列的.下面来看看小例子. import ...
- JS轮播图动态渲染四种方法
一. 获取轮播图数据 ajax 二.根据数据动态渲染 (根据当前设备 屏幕宽度判断) 1. 准备数据 2. 把数据转换成html格式的字符串 动态创建元素 字符串拼接 模板引擎 框架方法 2.把字符 ...
- html5左右滑动页面效果实现
The Demo of h5 slider achiev by Myself 主要思路: 设置一个容器container,然后里面有几个page,获取到屏幕的宽度并将其赋值给page,然后contai ...