机器学习项目实战(五) 住房价格预测

机器学习项目实战系列   住房价格预测

目录

机器学习项目实战系列   住房价格预测

一、概述

二、分析数据

1.数据导入

2.基础统计运算

3.特征观察

4.建立模型

5.分析模型表现

(1)学习曲线

(2)复杂度曲线

6.拟合模型

7.预测销售价格



一、概述

数据集包含波士顿剩余区域的房价,房子的费用根据犯罪率,房间数量等各种因素而变化。

房屋价格预测数据集 https://www.cs.toronto.edu/~delve/data/boston/bostonDetail.html

CRIM “numeric” 人均犯罪率
ZN “numeric” 超过2W5平方英尺的住宅用地所占比例
INDUS “numeric” 城市非零售业的商业用地比例
CHAS “integer” Charles河是否流经
NOX “numeric” 一氧化碳浓度
RM “numeric” 每栋住宅的平均房间数
AGE “numeric” 1940年以前建成的自住房比例
DIS “numeric” 到波士顿五个中心区域的加权平均距离
RAD “integer” 到达高速公路的便利指数
TAX “numeric” 每1W美元的全值财产税率
PIRATIO “numeric” 师生比
B “numeric” BK是黑人比例,越接近0.63越小,B=1000*(BK-0.63)^2
LSTAT “numeric” 低收入人口比例
MEDV “numeric” 自住房屋房价的平均房价单位为(1W美元)

机器学习项目实战(五) 住房价格预测_第1张图片


二、分析数据

1.数据导入

机器学习项目实战(五) 住房价格预测_第2张图片

机器学习项目实战(五) 住房价格预测_第3张图片

2.基础统计运算

机器学习项目实战(五) 住房价格预测_第4张图片

3.特征观察

  • RM 是该地区中每个房屋的平均房间数量
  • LSTAT 是指该地区有多少百分比的业主属于是低收入阶层(有工作但收入微薄)
  • PTRATIO 是该地区的中学和小学里,学生和老师的数目比(学生/老师)

机器学习项目实战(五) 住房价格预测_第5张图片

4.建立模型

机器学习项目实战(五) 住房价格预测_第6张图片

5.分析模型表现

(1)学习曲线

vs.ModelLearning(features, prices)

机器学习项目实战(五) 住房价格预测_第7张图片

(2)复杂度曲线

机器学习项目实战(五) 住房价格预测_第8张图片

6.拟合模型

from sklearn.tree import DecisionTreeRegressor
from sklearn.metrics import make_scorer
from sklearn.model_selection import GridSearchCV

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]. """
    
    # Create cross-validation sets from the training data
    # sklearn version 0.18: ShuffleSplit(n_splits=10, test_size=0.1, train_size=None, random_state=None)
    # sklearn versiin 0.17: ShuffleSplit(n, n_iter=10, test_size=0.1, train_size=None, random_state=None)
    cv_sets = ShuffleSplit(n_splits=10, test_size=0.20, random_state=42)
    
    # 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
    params = {'max_depth': range(1,11)}

    # TODO: Transform 'performance_metric' into a scoring function using 'make_scorer' 
    scoring_fnc = make_scorer(performance_metric)

    # TODO: Create the grid search cv object --> GridSearchCV()
    # Make sure to include the right parameters in the object:
    # (estimator, param_grid, scoring, cv) which have values 'regressor', 'params', 'scoring_fnc', and 'cv_sets' respectively.
    grid = GridSearchCV(regressor, params, scoring=scoring_fnc, cv=cv_sets)

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

拟合模型的最优深度是多少

7.预测销售价格

机器学习项目实战(五) 住房价格预测_第9张图片

 

 

你可能感兴趣的:(机器学习,机器学习,房屋价格预测,实战)