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

最新文章

  1. 完全删除TFS2013上的项目
  2. zabbix安装unixODBC配置完之后报错
  3. 原生JS制作贪吃蛇小游戏
  4. gtp转换mbr
  5. listview指定某item的点击效果
  6. [转]Could not load file or assembly 'System.Core, Version=2.0.5.0 和autofac冲突的问题
  7. NPOI 添加行
  8. 又是一个小正则replace
  9. 重叠I/O之事件通知
  10. 格而知之16:我所理解的Block(2)
  11. C#生成ACCESS文件几点注意事项
  12. 写你自己struts1框架
  13. MongoDB:利用官方驱动改装为EF代码风格的MongoDB.Repository框架 五 --- 为List<MongoDBRef>增加扩展方法
  14. meta 是什么??
  15. BZOJ 3143: [Hnoi2013]游走 [概率DP 高斯消元]
  16. [SDOI2014]数表
  17. react 的进阶
  18. Luogu P3346 [ZJOI2015]诸神眷顾的幻想乡 广义SAM 后缀自动机
  19. django 中自带的加密方法
  20. Java基础知识➣集合整理(三)

热门文章

  1. django QuerySet对象转换成字典对象
  2. Asp.Net使用Yahoo.Yui.Compressor.dll压缩Js|Css
  3. 0622通过插件的方式来热安装sphinx
  4. Mycat分表分库
  5. hdu5355 思维+爆搜
  6. Hardware/Firmware/Software的区别
  7. 关于使用chrome插件改动全部的站点的响应responseHeaders头的注意
  8. poj1179 区间dp(记忆化搜索写法)有巨坑!
  9. Test Doubles - Fakes, Mocks and Stubs.
  10. PHP检测输入数据是否合法常用的类(转)