import sys import numpy as np import pandas as pd from sklearn.model_selection import train_test_split from sklearn.tree import DecisionTreeRegressor from sklearn.pipeline import Pipeline from sklearn import preprocessing sys.path.append("/home/wenyan/Sklearn") import common base_year = common.parse_date("1900-01-01") def years(now): year = common.parse_date(str(now), '%Y') return int((year - base_year).days/365) train_data = pd.read_csv(open('../data/小区租金样本.csv')) train_data["build_year"] = train_data["build_year"].apply(lambda param: years(param)) x = train_data[["lat", "lng", "build_year", "room", "avg_price"]] y = train_data[["rent_price"]] x = x.astype(np.float64) y = y.astype(np.float64) x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=.3) for depth in range(2, 100): pipeline = Pipeline([('standard', preprocessing.StandardScaler()), ('minMax', preprocessing.MinMaxScaler()), ('svr', DecisionTreeRegressor(max_depth=depth, max_features =5))]) clf = pipeline.fit(x_train, y_train.values.ravel()) test_score = pipeline.score(x_test, y_test) print('depth:' + str(depth) + '\t测试得分是%s' % test_score) print('depth:' + str(depth) + '\t训练得分是%s' % pipeline.score(x_train, y_train))