-
Notifications
You must be signed in to change notification settings - Fork 0
/
standardize.py
55 lines (47 loc) · 1.73 KB
/
standardize.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
# 极差法数据标准化
import json
def std(myList):
resPath = myList[0]
outPath = myList[1]
# 该指标是否为正向指标,即指标数值越大对结果是越好还是越坏,越好则为正向指标,isPositive为True,否则False
isPositive = myList[2]
f = open(resPath, encoding='utf-8')
outFile = open(outPath, "w", encoding='utf-8')
res = f.read()
data = json.loads(res)
myMin = 1
minCase = ""
myMax = 0
maxCase = ""
valueList = []
for case in data:
valueList.append((case, data[case]))
minTuple = min(valueList, key=lambda x: x[1])
maxTuple = max(valueList, key=lambda x: x[1])
minCase = minTuple[0]
myMin = minTuple[1]
maxCase = maxTuple[0]
myMax = maxTuple[1]
print("min:", myMin, minCase)
print("max:", myMax, maxCase)
standard_dict = {}
# 正向指标,标准化结果为 (xi-min)/(max-min)
if isPositive:
for case in data:
ori_value = data[case]
standard_value = (ori_value - myMin) / (myMax - myMin)
standard_dict.setdefault(case, standard_value)
# 负向指标,标准化结果为(max-xi)/(max-min)
else:
for case in data:
ori_value = data[case]
standard_value = (myMax - ori_value) / (myMax - myMin)
standard_dict.setdefault(case, standard_value)
json.dump(standard_dict, outFile, sort_keys=True)
f.close()
outFile.close()
if __name__ == '__main__':
valuesList = [['markLevel.json', 'stdMarkLevel.json', False], ['pyPercent.json', 'stdPyPercent.json', False],
['countLine.json', 'stdCountLine.json', True]]
for value in valuesList:
std(value)