forked from taherfattahi/dnn-distance-line-protection-zone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDistanceLineProtectionZone_randomforest.py
104 lines (78 loc) · 2.48 KB
/
DistanceLineProtectionZone_randomforest.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
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
# -*- coding: utf-8 -*-
"""DifferentialProtectionCurve-RandomForest.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/19afitvPEO5yfDkP6h_Wp3kridGV9Hy2e
"""
#Import scikit-learn dataset library
from sklearn import datasets
import pandas as pd
import numpy as np
#Import scikit-learn metrics module for accuracy calculation
from sklearn import metrics
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import RandomForestClassifier
from google.colab import files
uploaded = files.upload()
#Load dataset
datasets = pd.read_csv('DistanceDataset.csv', sep=',')
X = datasets.iloc[:, [0,1]].values
Y = datasets.iloc[:, 2].values
datasets.head()
# Splitting the dataset into the Training set and Test set
X_Train, X_Test, Y_Train, Y_Test = train_test_split(X, Y, test_size = 0.25, random_state = 0)
scaler = StandardScaler()
scaler.fit(X_Train)
X_train1 = scaler.transform(X_Train)
X_train1 = X_train1.astype(np.float32)
X_test1 = scaler.transform(X_Test)
X_test1 = X_test1.astype(np.float32)
n = 0
for item in Y_Train:
if item == 1.03:
Y_Train[n] = 1
elif item == 0:
Y_Train[n] = 0
elif item == 0.23:
Y_Train[n] = 2
elif item == 0.43:
Y_Train[n] = 3
else:
Y_Train[n] = 4
n += 1
n = 0
for item in Y_Test:
if item == 1.03:
Y_Test[n] = 1
elif item == 0:
Y_Test[n] = 0
elif item == 0.23:
Y_Test[n] = 2
elif item == 0.43:
Y_Test[n] = 3
else:
Y_Test[n] = 4
n += 1
#Create a Gaussian Classifier
model_RandomForestClassifier=RandomForestClassifier(n_estimators=100)
#Train the model using the training sets y_pred=clf.predict(X_test)
model_RandomForestClassifier.fit(X_train1,Y_Train)
# prediction on test set
y_pred=model_RandomForestClassifier.predict(X_test1)
# Model Accuracy, how often is the classifier correct?
print("Accuracy:",metrics.accuracy_score(Y_Test, y_pred))
def find_nearest(array, value):
array = np.asarray(array)
idx = (np.abs(array - value)).argmin()
return array[idx], idx
x_sample = scaler.transform([[-12, -1]])
x_sample = scaler.transform([[14, -1]])
x_sample = scaler.transform([[10, -5]])
x_sample = scaler.transform([[6, -2.5]])
x_sample = x_sample.astype(np.float32)
predict = model_RandomForestClassifier.predict(x_sample)
predict_prob = model_RandomForestClassifier.predict_proba(x_sample)
print(predict)
print(predict_prob)