def predict(self, nu, gamma):
# classifier
classifier = svm.OneClassSVM(kernel="rbf", nu=nu, gamma=gamma)
# data for test
from_timestamp = self.min_timestamp + datetime.timedelta(days=365)
to_timestamp = self.max_timestamp
test_timestamps, test_values = self.load_monitor_data(from_timestamp, to_timestamp, "nan")
test_data = np.array(test_values)[:, 0:5]
# data for train
to_timestamp = self.min_timestamp + datetime.timedelta(days=365) + datetime.timedelta(hours=1)
train_timestamps, train_values = self.load_monitor_data(self.min_timestamp, to_timestamp, "1")
for i in range(len(test_timestamps)):
# predict
train_data = np.array(train_values)[:, 0:5]
classifier.fit(train_data)
label = classifier.predict(test_data[i])[0]
test_values[i][5] = int(label)
if label == 1:
test_values[i][6] = 0.0
train_values.append(test_values[i])
else:
test_values[i][6] = 1.0
print test_timestamps[i], label, test_values[i]
# write result into monitor file
fr = open(self.monitor_file, "r")
header = fr.readline()
lines = fr.readlines()
fr.close()
fw = open(self.monitor_file, "w") # update monitor file
fw.write(header)
for line in lines:
timestamp = datetime.datetime.strptime(line.strip().split(",")[0], "%Y-%m-%d %H:%M:%S")
if timestamp in test_timestamps:
idx = test_timestamps.index(timestamp)
value = test_values[idx]
timestamp = str(timestamp)
temperature = str(value[0])
ph = str(value[1])
conductivity = str(value[2])
orp = str(value[3])
do = str(value[4])
label = str(int(value[5]))
outlier_prob = str(value[6])
event_prob = str(value[7])
m = [timestamp, temperature, ph, conductivity, orp, do, label, outlier_prob, event_prob]
fw.write(",".join(m) + "\n")
else:
fw.write(line)
fw.close()
评论列表
文章目录