-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfinal_code.py
More file actions
299 lines (218 loc) · 5.52 KB
/
Copy pathfinal_code.py
File metadata and controls
299 lines (218 loc) · 5.52 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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# -*- coding: utf-8 -*-
"""DWDM.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1r0qDCHbCeRTM-j1Vl8AKNYsxDsYdII0i
"""
!echo "deb http://downloads.skewed.de/apt/bionic bionic universe" >> /etc/apt/sources.list
!apt-key adv --keyserver pgp.skewed.de --recv-key 612DEFB798507F25
!apt-get update
!apt-get install python3-graph-tool
import csv
import networkx as nximport
import collections
import numpy as np
import matplotlib.pyplot as plt
import math
import networkx as nx
#Taking input from a csv file
def read_csv_file(file_name):
with open(file_name, newline='') as csvfile:
data = list(csv.reader(csvfile))
data = sorted(data,key = lambda x: int(x[0]))
return data
# Compressing all nodes from string to int
def compressed_dictionaty(data,upto_tim):
dit = {}
compress = 0
for x in data:
if(int(x[0])>upto_tim):
break
# Trying compressing one end of edge
st = x[1]
if st in dit:
pass
else:
dit[st] = compress
compress = compress + 1
# Trying compressing other end of edge
st = x[2]
if st in dit:
continue
else:
dit[st] = compress
compress = compress + 1
return dit
def plot_graph(G):
nx.draw(G, with_labels=True, font_weight='bold')
plt.show()
#Upto how much time stamp
upto_tim = 100
# Initialising a Graph
G = nx.DiGraph()
# Initialising a deque
de = collections.deque([0,0])
# Setting a minimum checking value for the
mini_check = 0.05
# Reading CSV File
data = read_csv_file('input.csv')
# Compressing string to int
dit = compressed_dictionaty(data,upto_tim)
def getting_warning_list(p=0.5,q=0.33,k=3):
graph_no_list = []
Gk = [0]*k
#creating dequeue
de = [0]*k
#Setting number of previous dependents
prev_dependents = 3
for i in range(k):
temp_lis = [0]*prev_dependents
de[i] = collections.deque(temp_lis)
# Adding all nodes to the graph Plotting
for x in dit:
G.add_node(dit[x])
n = len(G)
# Chosing source node list and destination list and creating K random graph
sizes = [0]*k
for i in range(k):
Gk[i] = []
source = [0]*n
dest = [0]*n
for x in G:
y = np.random.binomial(1, p)
if(y):
source[x] = 1
y = np.random.binomial(1, q)
if(y):
dest[x] = 1
Gk[i]=[source, dest]
for nodes in G:
if(source[nodes] or dest[nodes]):
sizes[i] = sizes[i] + 1
prev = 0
sum = 0
sz = len(dit)
def mean_sq_error(de):
mean_sq = 0
for itr in de:
mean_sq = mean_sq + (rat - itr)*(rat - itr)
mean_sq = math.sqrt(mean_sq)
if(mean_sq >= mini_check*sizes[i]):
return 1
return 0
def mean_ab_error(de):
mean_ab = 0
for itr in de:
mean_sq = mean_ab + abs(rat - itr)
if(mean_sq >= mini_check*sizes[i]):
return 1
return 0
X = []
Y = []
for x in data:
if(int(x[0])>upto_tim):
break
# Taking 2 points connected by an edge
u = dit[x[1]]
v = dit[x[2]]
sum = sum + 1
G.add_weighted_edges_from([(u,v,1)])
# Adding all nodes of same timestamp
if(int(x[0]) == prev):
continue
prev = int(x[0])
flag = 0
error_list = [0]*k
XX = []
for i in range(k):
if(sizes[i] == 0):
continue
source = Gk[i][0]
dest = Gk[i][1]
score = 0
# Making new subgraph
g = nx.DiGraph()
# Adding nodes in the graph
for nodes in G:
if(source[nodes] or dest[nodes]):
g.add_node(nodes)
# Creating subgraphs
for e in G.edges():
uu = e[0]
vv = e[1]
if(source[uu] and dest[vv]):
g.add_weighted_edges_from([(uu,vv,1)])
score += 1
XX += [score]
rat = score
if(mean_sq_error(de[i])):
flag = 1
error_list[i] = rat
X.append(XX)
Y.append(flag)
# plot_graph(G)
if(flag):
# print("Warning")
graph_no_list.append(int(x[0]))
# print(error_list)
else:
for i in range(k):
de[i].append(error_list[i])
de[i].popleft()
# print(de)
G.clear()
for x in dit:
G.add_node(dit[x])
return X, Y, graph_no_list
X, Y, original_error_list = getting_warning_list(1,1,1)
print(original_error_list)
X, Y, get_other_list1 = getting_warning_list(0.5,0.33,1)
print(get_other_list1)
X, Y, get_other_list2 = getting_warning_list(0.5,0.33,20)
print(get_other_list2)
error1 = 0
for i in original_error_list:
if(get_other_list1.count(i) == 0):
error1 = error1 + 1
error2 = 0
for i in original_error_list:
if(get_other_list2.count(i) == 0):
error2 = error2 + 1
error1 = error1/len(original_error_list) * 100.00
error2 = error2/len(original_error_list) * 100.00
print(error1,error2)
label = ['First','Second']
index = np.arange(len(label))
plt.bar([0,1], [error1,error2])
plt.xlabel('Type')
plt.ylabel('Error Percentage')
plt.xticks(index, label)
plt.title('Comparision')
plt.show()
# Applying Regression Model to predict the error
from sklearn.linear_model import LogisticRegression
from sklearn.datasets.samples_generator import make_blobs
from sklearn import metrics
# Training data for Regression Model
# 80% for training and 20% for testing
train_x = X[:80]
train_y = Y[:80]
# Testing data
test_x = X[80:]
test_y = Y[80:]
# # pick some random from given
# import random
# for i in range(10):
# j = random.randint(0,len(X))
# test_x.append(X[j])
# test_y.append(Y[j])
# fit final model
model = LogisticRegression()
model.fit(train_x, train_y)
# make predictions
predicted = model.predict(test_x)
# show the inputs and predicted outputs
for i in range(len(test_x)):
print("X=%s, Predicted=%s, Actual=%s" % (test_x[i], predicted[i], test_y[i]))
print("Accuracy:",metrics.accuracy_score(test_y, predicted))
print("Precision:",metrics.precision_score(test_y, predicted))