-2

I have made predictions, however the predictions tend to vary so I would like to perform many in a loop, save the prediction within a list and then find the average.

I am stuck at the list making step, here is what I have tried.

EmptyList =[]

for i in range(3):
    model = DecisionTreeClassifier()
    model.fit(X_train_selected, y_train)
    predictions = model.predict(X_validation_selected)
    Acc = accuracy_score(y_validation, predictions)
    EmptyList[i] + Acc
    print(EmptyList)

Can anyone suggest how to perform n number of predictions and then store these in a list?

desertnaut
  • 52,940
  • 19
  • 125
  • 157
Krutik
  • 423
  • 4
  • 9
  • Google _python how to append to a list_ - find [what-is-the-difference-between-pythons-list-methods-append-and-extend](https://stackoverflow.com/questions/252703/what-is-the-difference-between-pythons-list-methods-append-and-extend) and read it. – Patrick Artner Feb 05 '21 at 10:13

1 Answers1

2

Use the .append method:

EmptyList =[]

for i in range(3):
    model = DecisionTreeClassifier()
    model.fit(X_train_selected, y_train)
    predictions = model.predict(X_validation_selected)
    Acc = accuracy_score(y_validation, predictions)
    EmptyList.append(Acc)
    print(EmptyList)
Jakub Szlaur
  • 1,344
  • 7
  • 23