-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomForest.py
More file actions
110 lines (77 loc) · 2.7 KB
/
RandomForest.py
File metadata and controls
110 lines (77 loc) · 2.7 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# %%
from sklearn.model_selection import cross_val_score
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn import metrics # for the check the error and accuracy of the model
from sklearn.ensemble import RandomForestClassifier
import time
from sklearn.metrics import confusion_matrix
import pylab as pl
from sklearn.metrics import precision_recall_fscore_support
# %%
data = pd.read_csv("pulsar_stars.csv",header=0)
data.head()
# diagnosis column is a object type so we can map it to integer value
data.head()
#data.to_csv(r'C:\Users\Deepak\Desktop\Masters Concordia\2019 Summer\INSE 6180\Project\Breast_Cancer_Sample.csv')
# %%
x = data.loc[:, data.columns != 'target_class'] # Independant variables
y = data['target_class'] # Dependant variables
# %%
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.25, random_state = 5 )
print(f'X Traib ----- > {len(x_train)}')
print(f'X TEST ----- > {len(x_test)}')
#x_train = x_train.values.tolist()
print(f'X Train type {type(x_train)}')
print(x_train.shape)
# %%
y_test.head
# %%
#To identify best n_estimators value
from sklearn.ensemble import RandomForestClassifier
error_rm = []
# Calculating error for K values between 1 and 100
for i in range(1, 50):
rm = RandomForestClassifier(n_estimators=i)
rm.fit(x_train,y_train)
pred_i = rm.predict(x_test)
error_rm.append(np.mean(pred_i != y_test))
#print(error_rm)
import matplotlib.pyplot as plt
%matplotlib inline
plt.figure(figsize=(12, 6))
plt.plot(range(1, 100), error_rm, color='red', linestyle='dashed', marker='o',
markerfacecolor='blue', markersize=10)
plt.title('Error Rate n_estimators Value')
plt.xlabel('n_estimators Value')
plt.ylabel('Mean Error')
# %%
#RandomForest classifier
start = time.time()
model=RandomForestClassifier(n_estimators=100, random_state=5)
model.fit(x_train,y_train)# now fit our model for training data
prediction=model.predict(x_test)# predict for the test data
end = time.time()
#prediction will contain the predicted value by our model predicted values of diagnosis column for test inputs
print(metrics.accuracy_score(prediction,y_test)) # to check the accuracy
# here we will use accuracy measurement between our predicted value and our test output values
print(end-start)
# %%
# Calculating confusion matrix
cm = confusion_matrix(y_test, prediction)
print(cm)
y_test.shape
#pl.matshow(cm)
#pl.title('Confusion matrix of the classifier')
#pl.colorbar()
#pl.show()
# %%
precision_recall_fscore_support(y_test, prediction, average='macro')
# %%
print(x_train.shape)
print(y_train.shape)
print(x_test.shape)
print(x_test.shape)