forked from XuhaoWan/Pc_CO2RR_ML
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGBR.py
41 lines (33 loc) · 1.13 KB
/
GBR.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/env python
# -*- coding:utf-8 -*-
#author: xhwan
import numpy as np
from sklearn.ensemble import GradientBoostingRegressor as GBR
from sklearn.metrics import mean_squared_error as mse
from sklearn.metrics import r2_score
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler
data_train = np.loadtxt('data_train.csv', delimiter=",", dtype="float")
x = data_train[..., 0:20 ]
ss = MinMaxScaler()
x = ss.fit_transform(x)
y = data_train[...,20 ]
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=16)
params = {'n_estimators': 500,
'max_depth': 5,
'min_samples_split': 5,
'learning_rate': 0.005,
'loss': 'huber'}
model = GBR(**params)
model.fit(x_train,y_train)
rmse = np.sqrt(mse(y_train,model.predict(x_train)))
r2 = r2_score(y_train,model.predict(x_train))
rmset = np.sqrt(mse(y_test,model.predict(x_test)))
r2t = r2_score(y_test,model.predict(x_test))
print('pre:', model.predict(x_test))
print(y_test)
print(rmse)
print(r2)
print(rmset)
print(r2t)
print(model.feature_importances_)