-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
35 lines (27 loc) · 984 Bytes
/
train.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
"""
This module allows to encode the label, to normalize the features, to train the random forest model and to evaluate the model
"""
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import StandardScaler
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score, classification_report
# label encoder
def encoding(label):
le= LabelEncoder()
label =le.fit_transform(label)
return label
# Normalization/Standardisation
def normalize(features):
features = StandardScaler().fit_transform(features)
return features
# Training
def training(X_train, y_train, X_test):
rfc = RandomForestClassifier(n_estimators=100, random_state=24)
rfc.fit(X_train, y_train)
y_pred = rfc.predict(X_test)
return y_pred
# Evaluate model
def evaluate_model(y_test, y_pred):
accuracy = accuracy_score(y_test, y_pred)
report = classification_report(y_test, y_pred)
return accuracy, report