-
Notifications
You must be signed in to change notification settings - Fork 0
/
lightgbm.py
75 lines (46 loc) · 1.53 KB
/
lightgbm.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env python
# coding: utf-8
# In[1]:
from sklearn.svm import SVR
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
import lightgbm as lgb
url = r'D:/lasso2.csv'
df_sc = pd.read_csv(url)
y = df_sc['租金']
X = df_sc.drop('租金', axis=1)
x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.25)
scaler=StandardScaler()
scaler=scaler.fit(x_train)
x_train_scaled=scaler.transform(x_train)
x_test_scaled=scaler.transform(x_test)
# In[4]:
my_model = lgb.LGBMRegressor(objective='regression', num_leaves=7, learning_rate=0.1, n_estimators=1500,
verbosity=2, max_depth=7)
my_model.fit(x_train_scaled, y_train, verbose=False)
predictions = my_model.predict(x_test_scaled)
msetest=mean_squared_error(predictions,y_test)
print(msetest)
# In[31]:
print(predictions)
# In[32]:
print(y_test)
# In[33]:
from sklearn.metrics import r2_score
rr=r2_score(predictions,y_test)
print(rr)
# In[5]:
from math import sqrt
from sklearn.metrics import mean_absolute_error
from sklearn.metrics import mean_squared_error
from sklearn.metrics import r2_score
print("lightgbm")
print("mean_absolute_error:", mean_absolute_error(predictions,y_test))
print("mean_squared_error:", mean_squared_error(predictions,y_test))
print("rmse:", sqrt(mean_squared_error(predictions,y_test)))
print("r2 score:", r2_score(predictions,y_test))
# In[ ]: