Python Forum
list problem - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: list problem (/thread-7510.html)



list problem - python_newbie09 - Jan-13-2018

how can i improve the code below, the problem lies with how y_pred is stored which i cant add to total. the idea is to calculate accuracy over 150 training examples and mean error rate.

if __name__ == "__main__":
    dataset = "car.data"
    attributes = ['buying', 'maint', 'doors', 'persons', 'lug_boot', 'safety']
    target = 'acceptability'
    samples = 150
    total = 0.0
    k=0
    while (k<6):
        k = input("Enter the number of neighbours: ")
        k = int(k)
        for i in range(samples):
            y_pred = []
            temp = KNN(X_train,y_train,X_test,y_pred,k)
            y_pred = np.asarray(y_pred)
        acc = accuracy_score(y_test, y_pred)
        total = total + temp
        acc = total/samples
        error_rate = 100 - avg_acc
        cm = confusion_matrix(y_test,y_pred)
        cr = classification_report(y_test,y_pred)
        print(acc)total = total + temp
        print("Accuracy:", +acc)
        print("Avg Accuracy:" +avg_acc)
        print(cm)
        print(cr)