-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
38 lines (29 loc) · 1.22 KB
/
model.py
File metadata and controls
38 lines (29 loc) · 1.22 KB
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
import os
from sklearn.datasets import make_classification
from sklearn.externals import joblib
from sklearn.ensemble import RandomForestClassifier
from utils.logger import MainLogger
logger = MainLogger(__name__)
class Model(object):
model_file = "model.pkl"
def __init__(self):
if not os.path.exists(self.model_file):
self.train()
self.model = joblib.load(self.model_file)
logger.debug("Model loaded from {}".format(self.model_file))
def train(self):
X, y = make_classification(n_samples=5000,
n_features=300,
n_informative=100,
n_redundant=50,
n_repeated=0,
n_classes=10,
flip_y=0.01,
shuffle=True)
model = RandomForestClassifier(n_estimators=128, n_jobs=1)
model.fit(X, y)
joblib.dump(model, self.model_file)
logger.debug("Model saved to {}".format(self.model_file))
def predict(self, X):
predictions = self.model.predict_proba(X)
return predictions