import pickle
def compare_pred_with_true_values(weights):
for vect in weights:
if vect[1] <= 0.5:
vect[1] = 0
else:
vect[1] = 1
return weights
def counter(weights):
count_0_knowing_0 = 0
count_1_knowing_1 = 0
count_0_knowing_1 = 0
count_1_knowing_0 = 0
for vect in weights:
if int(vect[0])==0 and vect[0]==vect[1]:
count_0_knowing_0 += 1
elif int(vect[0])==1 and vect[0]==vect[1]:
count_1_knowing_1 += 1
elif int(vect[0])==1 and vect[0]!=vect[1]:
count_0_knowing_1 += 1
else:
count_1_knowing_0 +=1
json = {"count_0_knowing_0": count_0_knowing_0,
"count_1_knowing_1": count_1_knowing_1,
"count_0_knowing_1": count_0_knowing_1,
"count_1_knowing_0": count_1_knowing_0}
return json
if __name__ == "__main__":
weights = pickle.load(open("weights_extension.pkl", "rb"))
weights = [[vect[0], vect[1]] for vect in weights]
weights_copy = compare_pred_with_true_values(weights)
json = counter(weights_copy)
print(json)
weights
只是形式为[[0, 0.0013], [1, 0.578], ..., [0, 0.0012]]
的列表,输出为{"count_0_knowing_0": 4283, "count_1_knowing_1": 39717, "count_0_knowing_1": 1283, "count_1_knowing_0": 320}
。该代码用于查看“相对于真实值的预测有多好?” >
该代码最初是用于测试的,但是现在我需要将其插入我的主代码中,但这远非最佳。我不知道我们是否可以找到可以完成相同工作的python库。使用Scikit学习还是scipy?
我们如何扩展该代码,使其可以与多种类型的标签一起使用?此处适用于标签0和1,但是我们可以扩展它使其适用于标签{-n, .., -2, -1, 0, 1, 2, 3, 4, ..., m}
吗?
答案 0 :(得分:0)
counter()
函数可以这样编写:
首先,像这样创建json
变量:
json = {'count_{!s}_knowing_{!s}'.format(a, b): 0
for a in range(2) for b in range(2)}
然后,引用这样的变量:
count_0_knowing_0
成为...
json['count_0_knowing_0']
然后最后return json
,而无需再次创建json变量。