Files
Sklearn/rent/calc_by_lr.py
2019-05-07 16:21:04 +08:00

40 lines
1.4 KiB
Python

import os
import sys
import numpy as np
import pandas as pd
from sklearn import preprocessing
from sklearn import svm
from sklearn.model_selection import GridSearchCV, train_test_split
from sklearn.pipeline import Pipeline
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=.2)
c_range = np.logspace(-5, 15, 11, base=2)
gamma_range = np.logspace(-9, 3, 13, base=2)
param_grid = [{'svr__kernel': ['rbf', 'linear'], 'svr__C': c_range, 'svr__gamma': gamma_range}]
svr = svm.SVR()
pipeline = Pipeline([('standard', preprocessing.StandardScaler()),
('minMax', preprocessing.MinMaxScaler()),
('svr', svr)])
grid = GridSearchCV(pipeline, param_grid, cv=3, n_jobs=20)
clf = grid.fit(x_train, y_train.values.ravel())
test_score = grid.score(x_test, y_test)
print('测试得分是%s' % test_score)
print('训练得分是%s' % grid.score(x_train, y_train))