厦门市房价预测系统

房产估值模型训练及预测结果2

2018-06-27  本文已影响114人  潇洒坤

1.源数据文件下载

用于进行回归预测的源数据文件下载链接: https://pan.baidu.com/s/16-JGI-JnksC9I7I_ghvrug 密码: ey46

2.编写代码并运行

1.第1次代码修改并运行

下面一段代码与之前预测评分只有0.83的文章相比,数据源多了房屋高度的分类和房屋建筑年代的分类。根据运行结果来看,这是一个有效的分类,提高了预测准确率。

from sklearn import preprocessing
from sklearn.neural_network import MLPRegressor
from sklearn.ensemble import GradientBoostingRegressor
import pandas as pd
import math
from sklearn.model_selection import KFold

def cleanOutlier(data,column,mul=3):
    data = data[data[:,column].argsort()] #得到排序后的ndarray
    l = len(data)
    low = int(l/4)
    high = int(l/4*3)
    lowValue = data[low,column]
    highValue = data[high,column]
    print("下四分位数为{}  上四分位数{}".format(lowValue,highValue))
    if lowValue - mul * (highValue - lowValue) < data[0,column] :
        delLowValue = data[0,column]
    else:
        delLowValue = lowValue - mul * (highValue - lowValue)
    if highValue + mul * (highValue - lowValue) > data[-1,column]:
        delHighValue = data[-1,column]
    else:
        delHighValue = highValue + mul * (highValue - lowValue)
    print("删除第{}列中数值小于{}或者大于{}的部分".format(column,\
          delLowValue,delHighValue))
    for i in range(low):
        if data[i,column] >= delLowValue:
            recordLow = i 
            break
    for i in range(len(data)-1,high,-1):
        if data[i,column] <= delHighValue:
            recordHigh = i
            break
    #打印处理异常值的相关信息
    print("原矩阵共有{}行".format(len(data)),end=',')
    print("保留{}到{}行".format(recordLow,recordHigh),end=',')
    data = data[recordLow:recordHigh+1]
    print("删除第{}列中的异常值后剩余{}行".format(column,\
          recordHigh+1-recordLow))
    return data

df = pd.read_excel("厦门房价数据处理结果.xlsx")
data = df.values.astype('float')
data = cleanOutlier(data,0)
x = data[:,1:]
y = data[:,0]
for i in range(len(y)):
    y[i] = math.log(y[i])

kf = KFold(n_splits=5,shuffle=True)

for train_index,test_index in kf.split(x):
    train_x = x[train_index]    
    test_x = x[test_index]
    train_y = y[train_index]
    test_y = y[test_index]

    model_mlp = MLPRegressor(solver='lbfgs',hidden_layer_sizes=(20,20,20),random_state=1)
    model_mlp.fit(train_x,train_y.ravel())
    mlp_score = model_mlp.score(test_x,test_y.ravel())
    print("sklearn多层感知器-回归模型得分",mlp_score)
    
    model_gbr = GradientBoostingRegressor(learning_rate=0.1)
    model_gbr.fit(train_x,train_y.ravel())
    gbr_score = model_gbr.score(test_x,test_y.ravel())
    print("sklearn集成-回归模型得分",gbr_score)

上面一段代码的运行结果为:

sklearn多层感知器-回归模型得分 0.8372352880455567
sklearn集成-回归模型得分 0.9103113202098574
sklearn多层感知器-回归模型得分 0.8776823262293032
sklearn集成-回归模型得分 0.915195456505515
sklearn多层感知器-回归模型得分 0.8767527980991213
sklearn集成-回归模型得分 0.9155513360324288
sklearn多层感知器-回归模型得分 0.865894829352436
sklearn集成-回归模型得分 0.907927739780212
sklearn多层感知器-回归模型得分 0.8576044250407024
sklearn集成-回归模型得分 0.9156697685167987

从上面的结果看出,此次模型训练集成-回归模型优于多层感知器-回归模型

2.第2次代码修改并运行

第2次代码与第1次代码主要的不同之处是对x即输入变量做了标准化。根据运行结果来看,标准化提高了回归模型的预测准确率。

from sklearn import preprocessing
from sklearn.neural_network import MLPRegressor
from sklearn.ensemble import GradientBoostingRegressor
import pandas as pd
import math
from sklearn.model_selection import KFold

def cleanOutlier(data,column,mul=3):
    data = data[data[:,column].argsort()] #得到排序后的ndarray
    l = len(data)
    low = int(l/4)
    high = int(l/4*3)
    lowValue = data[low,column]
    highValue = data[high,column]
    print("下四分位数为{}  上四分位数{}".format(lowValue,highValue))
    if lowValue - mul * (highValue - lowValue) < data[0,column] :
        delLowValue = data[0,column]
    else:
        delLowValue = lowValue - mul * (highValue - lowValue)
    if highValue + mul * (highValue - lowValue) > data[-1,column]:
        delHighValue = data[-1,column]
    else:
        delHighValue = highValue + mul * (highValue - lowValue)
    print("删除第{}列中数值小于{}或者大于{}的部分".format(column,\
          delLowValue,delHighValue))
    for i in range(low):
        if data[i,column] >= delLowValue:
            recordLow = i 
            break
    for i in range(len(data)-1,high,-1):
        if data[i,column] <= delHighValue:
            recordHigh = i
            break
    #打印处理异常值的相关信息
    print("原矩阵共有{}行".format(len(data)),end=',')
    print("保留{}到{}行".format(recordLow,recordHigh),end=',')
    data = data[recordLow:recordHigh+1]
    print("删除第{}列中的异常值后剩余{}行".format(column,\
          recordHigh+1-recordLow))
    return data

df = pd.read_excel("厦门房价数据处理结果.xlsx")
data = df.values.astype('float')
data = cleanOutlier(data,0)
x = data[:,1:]
y = data[:,0]
for i in range(len(y)):
    y[i] = math.log(y[i])

kf = KFold(n_splits=5,shuffle=True)

for train_index,test_index in kf.split(x):
    train_x = x[train_index]    
    test_x = x[test_index]
    train_y = y[train_index]
    test_y = y[test_index]

    ss_x = preprocessing.StandardScaler()
    train_x = ss_x.fit_transform(train_x)
    test_x = ss_x.transform(test_x)

    model_mlp = MLPRegressor(solver='lbfgs',hidden_layer_sizes=(20,20,20),random_state=1)
    model_mlp.fit(train_x,train_y.ravel())
    mlp_score = model_mlp.score(test_x,test_y.ravel())
    print("sklearn多层感知器-回归模型得分",mlp_score)
    
    model_gbr = GradientBoostingRegressor(learning_rate=0.1)
    model_gbr.fit(train_x,train_y.ravel())
    gbr_score = model_gbr.score(test_x,test_y.ravel())
    print("sklearn集成-回归模型得分",gbr_score)

上面一段代码的运行结果为:

sklearn多层感知器-回归模型得分 0.9420052610363624
sklearn集成-回归模型得分 0.9119298712798816
sklearn多层感知器-回归模型得分 0.9408811404890329
sklearn集成-回归模型得分 0.9119071943013952
sklearn多层感知器-回归模型得分 0.9453408533881785
sklearn集成-回归模型得分 0.9183101348039411
sklearn多层感知器-回归模型得分 0.9420925829808715
sklearn集成-回归模型得分 0.909328953608675
sklearn多层感知器-回归模型得分 0.9427244328757453
sklearn集成-回归模型得分 0.9106290975464613

从上面的结果看出,输入变量x进行标准化之后提高了多层感知器-回归模型的得分,这次训练结果多层感知器-回归模型优于集成-回归模型

3.第3次代码修改并运行

第3次代码与第2次代码主要的不同之处调整了回归模型的参数。从运行结果看出,调参使集成-回归模型得分从0.90提升到0.95,0.05的提高,是一次成功的调参。

from sklearn import preprocessing
from sklearn.neural_network import MLPRegressor
from sklearn.ensemble import GradientBoostingRegressor
import pandas as pd
import math
from sklearn.model_selection import KFold

def cleanOutlier(data,column,mul=3):
    data = data[data[:,column].argsort()] #得到排序后的ndarray
    l = len(data)
    low = int(l/4)
    high = int(l/4*3)
    lowValue = data[low,column]
    highValue = data[high,column]
    print("下四分位数为{}  上四分位数{}".format(lowValue,highValue))
    if lowValue - mul * (highValue - lowValue) < data[0,column] :
        delLowValue = data[0,column]
    else:
        delLowValue = lowValue - mul * (highValue - lowValue)
    if highValue + mul * (highValue - lowValue) > data[-1,column]:
        delHighValue = data[-1,column]
    else:
        delHighValue = highValue + mul * (highValue - lowValue)
    print("删除第{}列中数值小于{}或者大于{}的部分".format(column,\
          delLowValue,delHighValue))
    for i in range(low):
        if data[i,column] >= delLowValue:
            recordLow = i 
            break
    for i in range(len(data)-1,high,-1):
        if data[i,column] <= delHighValue:
            recordHigh = i
            break
    #打印处理异常值的相关信息
    print("原矩阵共有{}行".format(len(data)),end=',')
    print("保留{}到{}行".format(recordLow,recordHigh),end=',')
    data = data[recordLow:recordHigh+1]
    print("删除第{}列中的异常值后剩余{}行".format(column,\
          recordHigh+1-recordLow))
    return data

df = pd.read_excel("厦门房价数据处理结果.xlsx")
data = df.values.astype('float')
data = cleanOutlier(data,0)
x = data[:,1:]
y = data[:,0]
for i in range(len(y)):
    y[i] = math.log(y[i])

kf = KFold(n_splits=5,shuffle=True)

for train_index,test_index in kf.split(x):
    train_x = x[train_index]    
    test_x = x[test_index]
    train_y = y[train_index]
    test_y = y[test_index]

    ss_x = preprocessing.StandardScaler()
    train_x = ss_x.fit_transform(train_x)
    test_x = ss_x.transform(test_x)

    model_mlp = MLPRegressor(solver='lbfgs',hidden_layer_sizes=(30,30,30),random_state=1)
    model_mlp.fit(train_x,train_y.ravel())
    mlp_score = model_mlp.score(test_x,test_y.ravel())
    print("sklearn多层感知器-回归模型得分",mlp_score)
    
    model_gbr = GradientBoostingRegressor(n_estimators = 1000,learning_rate=0.3)
    model_gbr.fit(train_x,train_y.ravel())
    gbr_score = model_gbr.score(test_x,test_y.ravel())
    print("sklearn集成-回归模型得分",gbr_score)

上面一段代码的运行结果为:

sklearn多层感知器-回归模型得分 0.9409698368795202
sklearn集成-回归模型得分 0.9572998845800237
sklearn多层感知器-回归模型得分 0.9415776157615223
sklearn集成-回归模型得分 0.9550918318968675
sklearn多层感知器-回归模型得分 0.9477305164540656
sklearn集成-回归模型得分 0.9601481639665502
sklearn多层感知器-回归模型得分 0.9476815842932507
sklearn集成-回归模型得分 0.9588259351144909
sklearn多层感知器-回归模型得分 0.9399513836020602
sklearn集成-回归模型得分 0.9578897231281281

两个模型的评分到0.95左右,可以算是比较准确的模型,模型训练就到此告一段落。

上一篇下一篇

猜你喜欢

热点阅读