python-3.x 如何在列表中添加多变量值?

hc2pp10m  于 2022-12-24  发布在  Python
关注(0)|答案(3)|浏览(125)
Accuracy_Data = list()

Accuracy = KNN(features_train, features_test, label_train, label_test)
print("KNN algorithm:", str(Accuracy * 100),"%")

Accuracy = DecisionTree(features_train, features_test, label_train, label_test)
print("Decision Tree:", str(Accuracy * 100,"%")

Accuracy = SVM(features_train, features_test, label_train, label_test)
print("The accuracy of SVM algorithm is", str(Accuracy * 100,"%"), "%")

Accuracy = GNB(features_train, features_test, label_train, label_test)
print("The accuracy of Gaussian Naive Bayes is", str(Accuracy * 100,"%"), "%")

Accuracy = RFC(features_train, features_test, label_train, label_test)
print("The accuracy of Random Forest is", str(Accuracy * 100,"%"), "%")

Accuracy = ADC(features_train, features_test, label_train, label_test)
print("The accuracy of Ada Boost Classifier is", str(Accuracy * 100,"%"), "%")

for x in Accuracy:
    Accuracy_Data.append(x)

我的精度值是从不同的函数返回的,我希望列表(Accuracy_Data)收集每个精度值,而不需要更改每个精度值的变量名,然后将它们添加到列表中。

s3fp2yjn

s3fp2yjn1#

据我所知,你可以试试这样的方法:

Accuracy_knn = KNN(features_train, features_test, label_train, label_test)
print("KNN algorithm:", str(Accuracy * 100),"%")

Accuracy_Data.append(Accuracy_knn) 

Accuracy_svm = SVM(features_train, features_test, label_train, label_test)
print("The accuracy of SVM algorithm is", str(Accuracy * 100,"%"), "%")

Accuracy_Data.append(Accuracy_svm)

等等。

3okqufwl

3okqufwl2#

你可以在调用函数Accuracy_Data之后,将Accuracy附加到Accuracy_Data中。

Accuracy_Data = list()

Accuracy = KNN(features_train, features_test, label_train, label_test)
print("KNN algorithm:", str(Accuracy * 100),"%")
Accuracy_Data.append(Accuracy)

Accuracy = DecisionTree(features_train, features_test, label_train, label_test)
print("Decision Tree:", str(Accuracy * 100,"%")
Accuracy_Data.append(Accuracy)

Accuracy = SVM(features_train, features_test, label_train, label_test)
print("The accuracy of SVM algorithm is", str(Accuracy * 100,"%"), "%")
Accuracy_Data.append(Accuracy)

Accuracy = GNB(features_train, features_test, label_train, label_test)
print("The accuracy of Gaussian Naive Bayes is", str(Accuracy * 100,"%"), "%")
Accuracy_Data.append(Accuracy)

Accuracy = RFC(features_train, features_test, label_train, label_test)
print("The accuracy of Random Forest is", str(Accuracy * 100,"%"), "%")
Accuracy_Data.append(Accuracy)

Accuracy = ADC(features_train, features_test, label_train, label_test)
print("The accuracy of Ada Boost Classifier is", str(Accuracy * 100,"%"), "%")
Accuracy_Data.append(Accuracy)
mlnl4t2r

mlnl4t2r3#

你知道装饰图案吗?
我认为你应该试试类似的例子:

temp = list()

def foobar(fn):
    def _inner(*args, **kwargs):
        temp.append(fn(*args, **kwargs))
return _inner

@foobar
def func1(a,b,c):
    return f"{a} {b} {c}"

@foobar
def func2(a,b,c):
    return f"{a}-{b}-{c}"

@foobar
def func3(a,b,c):
    return f"{a}={b}={c}"

if __name__ == '__main__':
    func1(1, 2, 3)
    func2(1, 2, 3)
    func3(1, 2, 3)
    print(temp)

返回:
["1 2 3"、"1 - 2 - 3"、"1 = 2 = 3"]

相关问题