-
Notifications
You must be signed in to change notification settings - Fork 3
/
model.py
53 lines (39 loc) · 1.73 KB
/
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
# model data
# from sklearn.linear_model import LinearRegression
from sklearn.ensemble import GradientBoostingRegressor
from sklearn.model_selection import train_test_split
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import StandardScaler, PolynomialFeatures
from sklearn.preprocessing import LabelEncoder, LabelBinarizer
from sklearn.pipeline import make_pipeline
from sklearn_pandas import DataFrameMapper, CategoricalImputer
import pandas as pd
import pickle
# import clean data
df = pd.read_csv('data_final.csv')
X = df.drop(['OrgName', 'orgSort','WaitTime_percent_within_target', 'ResultType', 'Target', 'PriorityDescription'], axis=1)
target = 'WaitTime_mean'
X = X.drop(target, axis=1)
df.sample(1)
df.iloc[0,1]
y = df[target]
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
mapper = DataFrameMapper([
# ('Key', [CategoricalImputer(), LabelBinarizer()]),
('day', [LabelBinarizer()]),
('Org_ID', [LabelBinarizer()]), #, PolynomialFeatures(include_bias=False)
# (['Org_ID'], [SimpleImputer(), StandardScaler()]), #, PolynomialFeatures(include_bias=False)
(['WaitTime_90percentile'], [SimpleImputer(), StandardScaler()]),
# PolynomialFeatures(include_bias=False),
(['case_per_day'], [SimpleImputer(), PolynomialFeatures(include_bias=False), StandardScaler()])
# (['case_per_month'], [SimpleImputer(), PolynomialFeatures(include_bias=False), StandardScaler()])
])
# score of 0.9837560259894104
est = GradientBoostingRegressor(n_estimators=100, max_depth=1)
pipe = make_pipeline(mapper, est)
pipe.fit(X_train, y_train)
pipe.score(X_test, y_test)
pickle.dump(pipe, open('model/pipe.pkl', 'wb'))
# # load from a model
# del pipe
# pipe = pickle.load(open('model/pipe.pkl', 'rb'))