-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathevaluate_model.py
70 lines (52 loc) · 1.71 KB
/
evaluate_model.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
# Compare Algorithms
import pandas as pd
import matplotlib.pyplot as plt
from sklearn import model_selection
from sklearn.preprocessing import LabelEncoder
from sklearn import ensemble
from sklearn.ensemble import RandomForestRegressor
from xgboost import XGBRegressor
# load dataset
path = 'Dataset/train.csv'
#loading dataset
df = pd.read_csv(path, sep = ",", engine = "python")
#taking care of null values
df = df.fillna('0')
#label encoding
lb_make = LabelEncoder()
heads = df.columns
for i in range(len(df.columns)):
if df[heads[i]].dtypes == 'O':
df[heads[i]] = lb_make.fit_transform(df[heads[i]].astype(str))
#extracting input and output features
X = df.iloc[:,:-1].values
Y = df.iloc[:,-1].values
# prepare configuration for cross validation test harness
seed = 7
# prepare models
#model evaluation
models = []
models.append(('XGBoost',XGBRegressor()))
models.append(('GBR', ensemble.GradientBoostingRegressor(loss='quantile', alpha=0.1,
n_estimators=250, max_depth=3,
learning_rate=.1, min_samples_leaf=9,
min_samples_split=9)))
models.append(('RFR', RandomForestRegressor()))
# evaluate each model in turn
results = []
names = []
scoring = 'r2'
for name, model in models:
kfold = model_selection.KFold(n_splits=10, random_state=seed)
cv_results = model_selection.cross_val_score(model, X, Y, cv=kfold, scoring=scoring)
results.append(cv_results)
names.append(name)
msg = "%s: %f (%f)" % (name, cv_results.mean(), cv_results.std())
print(msg)
# boxplot algorithm comparison
fig = plt.figure()
fig.suptitle('Algorithm Comparison')
ax = fig.add_subplot(111)
plt.boxplot(results)
ax.set_xticklabels(names)
plt.show()