-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomForest.py
More file actions
39 lines (34 loc) · 1.28 KB
/
RandomForest.py
File metadata and controls
39 lines (34 loc) · 1.28 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
'''
__author__ = "Darpit Patel"
__version__ = "1.0.1"
__maintainer__ = "Darpit Patel"
__GitHub__ = https://github.com/DarpitPatel/
'''
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
import pickle
from sklearn import tree
#read csv using pandas
data=pd.read_csv('input.csv')
#Select all row and second and third column as input of model
df_x=data.iloc[:,[1,2]]
df_x=df_x.astype('int')
#Select all row and first column as output of model
df_y=data.iloc[:,0]
df_y=df_y.astype('int')
x_train, x_test, y_train, y_test = train_test_split(df_x, df_y, test_size=0.7, random_state=4)
clf = RandomForestClassifier()
trained_model = clf.fit(x_train, y_train)
predictions = trained_model.predict(x_test)
#print accuracy of model
print("Train Accuracy : ", accuracy_score(y_train, trained_model.predict(x_train)))
print("Test Accuracy : ", accuracy_score(y_test, predictions))
#Save the model for persistence
filename="pickel_model"
with open(filename, 'wb') as fid:
pickle.dump(clf, fid)
#Make point prediction for a single input
print("Point Prediction : ", trained_model.predict([[78,2]]) )