-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandom_forest.py
More file actions
79 lines (64 loc) · 2.67 KB
/
random_forest.py
File metadata and controls
79 lines (64 loc) · 2.67 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
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
72
73
74
75
76
77
78
79
"""
Author: Joshua De los Santos
Modified: 1:24PM - 8/11/2018
Description:
Methods to build, update and save a random forest classifier.
"""
import pandas as pd
from sklearn.externals import joblib
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
RF_model_file = 'RF_model.joblib'
emotion_training_set = 'emotion_training_set.csv'
def update_Random_Forest(): # pragma: no cover
"""Build a random forest classifier for azure data
:return: Random Forest Model
"""
rfdf = pd.read_csv(emotion_training_set)
target_column = rfdf['satisfaction']
del rfdf['satisfaction']
xTrain, xTest, yTrain, yTest = train_test_split(rfdf,
target_column,
test_size=0.5,
stratify=target_column,
random_state=736251)
irs_rf = RandomForestClassifier(n_estimators=100,
oob_score=True,
random_state=487368)
irs_rf.fit(xTrain, yTrain)
accuracy_score(yTest, irs_rf.predict(xTest))
return irs_rf
def build_New_Forest(partition, random_int, estimators, criteria, random): # pragma: no cover
"""Build a random forest classifier for azure data
:return: Random Forest Model
"""
rfdf = pd.read_csv(emotion_training_set)
target_column = rfdf['satisfaction']
del rfdf['satisfaction']
xTrain, xTest, yTrain, yTest = train_test_split(rfdf,
target_column,
test_size=partition,
stratify=target_column,
random_state=random_int)
irs_rf = RandomForestClassifier(n_estimators=estimators,
criterion=criteria,
oob_score=True,
random_state=random)
irs_rf.fit(xTrain, yTrain)
print("Validation Accuracy: ", accuracy_score(yTest, irs_rf.predict(xTest)))
return irs_rf
def test_model_accuracy(model):
return
def update_To_File(model): # pragma: no cover
"""Saves model to a .joblib file
:param model: sklearn classifier object
"""
joblib.dump(model, RF_model_file)
def RF_To_File(model, file_path):
joblib.dump(model, file_path)
def load_RF_File():
"""Load .joblib model file
:return: classifier object
"""
return joblib.load(RF_model_file)