Python Forum

Full Version: list problem
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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)