forked from chennnnnyize-zz/Neural-Decoding-for-OPF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Nearest_neighbor.py
152 lines (90 loc) · 3.69 KB
/
Nearest_neighbor.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#Nearest Neighbor predictor of active set of constraints
import numpy as np
import matplotlib.pyplot as plt
#from util import *
import random
import csv
random.seed(32)
from sklearn.metrics import mean_squared_error
import time
from keras.models import Sequential
from sklearn.neighbors import KNeighborsClassifier
#Train two classifiers: load to generators' sets; load to lines' sets
num_of_buses=14
num_of_gen=5
num_of_lines=20
def seq_to_label(unique_set, current_seq):
label=np.zeros((current_seq.shape[0],1), dtype=int)
for j in range(current_seq.shape[0]):
for i in range(unique_set.shape[0]):
if np.array_equal(unique_set[i], current_seq[j])==True:
label[j,0]=i
return label
def label_to_seq(unique_set, current_seq):
seq=np.zeros((current_seq.shape[0], unique_set.shape[1]), dtype=float)
for i in range(current_seq.shape[0]):
seq[i,:]=unique_set[current_seq[i],:]
return seq
#Check data_all_s2_new
with open('data_all14_s3_new.csv', 'r') as csvfile:
reader = csv.reader(csvfile)
rows = [row for row in reader]
data_all = np.array(rows, dtype=float)
with open('gen_s3_schedule_true.csv', 'r') as csvfile:
reader = csv.reader(csvfile)
rows = [row for row in reader]
Y_all = np.array(rows, dtype=float)
unique_gen_scheudle = np.unique(Y_all, axis=0)
print(unique_gen_scheudle)
label_gen=seq_to_label(unique_gen_scheudle, Y_all)
#s=label_to_seq(unique_gen_scheudle, label_gen)
print(np.shape(label_gen))
with open('line_s3_schedule_true.csv', 'r') as csvfile:
reader = csv.reader(csvfile)
rows2 = [row for row in reader]
Line_all = np.array(rows2, dtype=float)
unique_line_scheudle= np.unique(Line_all, axis=0)
print(unique_line_scheudle)
label_line=seq_to_label(unique_line_scheudle, Line_all)
#label_line=keras.utils.to_categorical(label_line, dtype='float32')
print("data line shape", np.shape(data_all))
X = np.copy(data_all[:data_all.shape[0], :num_of_buses])
print("The last data sample instance", X[-1])
max_valuex = np.max(X, axis=0)
min_valuex = np.min(X, axis=0)
train_X = (np.copy(X) - min_valuex) / (max_valuex - min_valuex)
print("Training x maximum", max_valuex)
print("Training x minimum", min_valuex)
train_Y = np.copy(label_gen) #(np.copy(Y1) - min_valuey1) / (max_valuey1 - min_valuey1)
num_samples = train_X.shape[0]
index = np.arange(num_samples)
#index = random.sample(range(num_samples), num_samples)
X_train = np.copy(train_X[index[:int(0.8*num_samples)]])
Y_train = np.copy(train_Y[index[:int(0.8*num_samples)]])
test_X = np.copy(train_X[index[int(0.8*num_samples):]])
test_Y = np.copy(train_Y[index[int(0.8*num_samples):]])
print("###################Begin working on generator classification#######################")
neigh = KNeighborsClassifier(n_neighbors=3)
neigh.fit(X_train, Y_train)
print("Fitting completed")
#print(neigh.predict(data_all[8000:]))
error=0
pred_label=neigh.predict(test_X)
with open('nieghbor14_gen_s3.csv', 'wb') as f:
writer = csv.writer(f)
writer.writerows(pred_label)
print("###################Begin working on line classification#######################")
train_Y =np.copy(label_line) #(np.copy(Y1) - min_valuey1) / (max_valuey1 - min_valuey1)
num_samples = train_X.shape[0]
index = np.arange(num_samples)
X_train = np.copy(train_X[index[:int(0.8*num_samples)]])
Y_train = np.copy(train_Y[index[:int(0.8*num_samples)]])
test_X = np.copy(train_X[index[int(0.8*num_samples):]])
test_Y = np.copy(train_Y[index[int(0.8*num_samples):]])
neigh = KNeighborsClassifier(n_neighbors=3)
neigh.fit(X_train, Y_train)
error=0
pred_label=neigh.predict(test_X)
with open('nieghbor14_line_s3.csv', 'wb') as f:
writer = csv.writer(f)
writer.writerows(pred_label)