Home > front end >  How to add multi-variable values in a list?
How to add multi-variable values in a list?

Time:12-21

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)

My accuracy values are returned from different functions and I want the list (Accuracy_Data) to collect each accuracy without changing the variable name for each one then adding them to a list. How can I do this?

CodePudding user response:

As per my understanding, you can try something like this:

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)

and so on.

CodePudding user response:

You could just append Accuracy to Accuracy_Data after calling the fuction Something like this:

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)

CodePudding user response:

Do you know about decorator pattern?

I think you should try something like sample:

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)

return:

['1 2 3', '1-2-3', '1=2=3']

  • Related