Python 相对于真实值,预测有多好?

Python 相对于真实值,预测有多好?,python,machine-learning,Python,Machine Learning,权重只是一个表[[0,0.0013],[1,0.578],…,[0,0.0012]]的列表,输出是{count_0_知道_0:4283,count_1_知道_1:39717,count_0_知道_1:1283,count_1_知道_0:320}代码用于查看预测相对于真实值的好坏 这段代码一开始是用于测试的,但现在我需要在主代码中插入它,但它远不是最佳的。我不知道我们是否能找到一个python库来做同样的工作。使用Scikit学习还是使用scipy 我们如何扩展代码,使其能够处理多种类型的标签?这

权重只是一个表[[0,0.0013],[1,0.578],…,[0,0.0012]]的列表,输出是{count_0_知道_0:4283,count_1_知道_1:39717,count_0_知道_1:1283,count_1_知道_0:320}代码用于查看预测相对于真实值的好坏

这段代码一开始是用于测试的,但现在我需要在主代码中插入它,但它远不是最佳的。我不知道我们是否能找到一个python库来做同样的工作。使用Scikit学习还是使用scipy

我们如何扩展代码,使其能够处理多种类型的标签?这里使用的是标签0和1,但是我们是否可以扩展它,以便它可以使用标签{-n,…,-2,-1,0,1,2,3,4,…,m}?

计数器函数可以这样编写:

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)
json = {'count_{!s}_knowing_{!s}'.format(a, b): 0
        for a in range(2) for b in range(2)}
首先,创建json变量,如下所示:

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)
json = {'count_{!s}_knowing_{!s}'.format(a, b): 0
        for a in range(2) for b in range(2)}
然后,对变量的引用如下:

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)
json = {'count_{!s}_knowing_{!s}'.format(a, b): 0
        for a in range(2) for b in range(2)}
变成

count_0_knowing_0

最后,只需返回json,而无需再次创建json变量。

是的,有一种方法可以简化它。任何重复都可以被删除。你在哪里困惑*在这种情况下