-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
86 lines (65 loc) · 4.04 KB
/
test.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
from flask import Flask, render_template, request
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.ensemble import GradientBoostingClassifier
def sensorpred():
# status sensor only record ON/OFF status
status_sensors = [
'D001', 'D002', 'D003', 'D004', 'D005', 'D006', 'D007', 'D008', 'D009', 'D010', 'D011', 'D012', 'D013', 'D014', 'D015', 'D016', 'D017', 'D018',
'E002', 'E003', 'F001', 'F002',
'I001', 'I002', 'I006', 'I010', 'I011', 'I012',
'L001', 'L002', 'L003', 'L004', 'L005', 'L006', 'L007', 'L008', 'L009', 'L010', 'L011',
'M001', 'M002', 'M003', 'M004', 'M005', 'M006', 'M007', 'M008', 'M009', 'M010', 'M011', 'M012', 'M013', 'M014', 'M015', 'M016', 'M017', 'M018', 'M019', 'M020', 'M021', 'M022', 'M023', 'M024', 'M025', 'M026', 'M027', 'M028', 'M029', 'M030', 'M031', 'M032', 'M033', 'M034', 'M035', 'M036', 'M037', 'M038', 'M039', 'M040', 'M041', 'M042', 'M043', 'M044', 'M045', 'M046', 'M047', 'M048', 'M049', 'M050', 'M051', 'M052',
'MA201', 'MA202', 'MA203', 'MA204', 'MA205', 'MA207',
'SS001', 'SS002', 'SS003', 'SS004', 'SS005', 'SS006', 'SS007', 'SS008', 'SS009', 'SS010', 'SS011', 'SS012', 'SS015', 'SS016', 'SS017', 'SS018', 'SS019', 'SS020', 'SS021',
]
# digit sensor record the sensor value of that moment
digit_sensors = [
'LL002', 'LL003', 'LL004', 'LL005', 'LL006', 'LL007', 'LL008', 'LL009', 'LL010', 'LL011',
'LS001', 'LS002', 'LS003', 'LS004', 'LS005', 'LS006', 'LS007', 'LS008', 'LS009', 'LS010', 'LS011', 'LS012', 'LS013', 'LS014', 'LS015', 'LS016', 'LS017', 'LS018', 'LS019', 'LS020', 'LS021', 'LS022', 'LS023', 'LS024', 'LS025', 'LS026', 'LS027', 'LS028', 'LS029', 'LS030', 'LS031', 'LS032', 'LS033', 'LS034', 'LS035', 'LS036', 'LS037', 'LS038', 'LS039', 'LS042', 'LS043', 'LS044', 'LS045', 'LS046', 'LS047', 'LS048', 'LS049', 'LS050', 'LS051', 'LS201', 'LS202', 'LS203', 'LS204', 'LS205', 'LS206', 'LS207',
'P001',
'T001', 'T002', 'T003', 'T004', 'T005', 'T101', 'T102', 'T103', 'T104', 'T105', 'T106', 'T107', 'T108', 'T109', 'T110', 'T111'
]
#---------Train Model--------
df = pd.read_csv("standar.csv")
y = df['diagnosis']
X = df.drop(['diagnosis'], axis = 1)
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
s_model = scaler.fit(X)
X_scaled = s_model.transform(X)
output_map = {3: 0, 4: 0, 5: 0, 7: 0, 8: 0, 9: 0, 1: 1, 2: 1, 6: 1, 10: 0}
y_relabel = y.map(output_map)
X_resampled, X_test, y_resampled, y_test = train_test_split(X_scaled, y_relabel, test_size=0.2)
# X_resampled, y_resampled = SMOTE().fit_resample(X_train, y_train)
y_resampled.value_counts()
clf = GradientBoostingClassifier(n_estimators = 200, max_depth = 200)
clf.fit(X_resampled, y_resampled)
#-----------------
test_data = pd.DataFrame(columns = status_sensors + digit_sensors)
read_test = pd.read_csv('test.txt', sep=' ', header=None, names=["time", "sensor", "reads", "col04"])
# get how many sensor appeared in the file
test_dict = read_test['sensor'].value_counts().to_dict()
for k, v in test_dict.items():
if k in digit_sensors:
# count the sum of the data value
total_value = read_test.loc[read_test["sensor"] == k, "reads"].astype(float).sum()
# calculate average value of sensor
test_dict[k] = round(total_value/v, 4)
new_row = {}
for key in status_sensors + digit_sensors:
# if the sensor appeared in the file, get the number of it appears or the avg
# otherwise set it to zero
if key not in test_dict.keys():
new_row[key] = 0.0
else:
new_row[key] = float(test_dict[key])
# add the data to the dataframe
test_data = test_data.append(new_row, ignore_index=True)
test_scaled = s_model.transform(test_data)
pred = clf.predict(test_scaled)
print(pred[0])
if __name__ == '__main__':
# sensorpred()
import sklearn
print(sklearn.__version__)