Saving the predictions to CSV

I am doing something wrong trying to append the results from the models prediction.

My attempt is to append the predictions to a new variable and the convert that variable with the over 40,000 outcomes to array to save into CSV.

My attempt is as follows:

totalpreds = []
import seaborn as sns
nb_classes = 2

confusion_matrix = torch.zeros(nb_classes, nb_classes)
with torch.no_grad():
    for i, (inputs, classes) in enumerate(testloader):
        inputs = inputs
        classes = classes
        outputs = clf(inputs)
        _, preds = torch.max(outputs, 1)
        # Saving predictions
        arr = preds.cpu().numpy()
# write CSV
np.savetxt('output.csv', totalpreds)

It’s not working… I am sure it’s something wrong in my thinking here. Any help would be appreciated!

Consider working with Pandas when dealing with CSVs:

import pandas as pd

n_preds = 4
output_dim = 3

# random data in your format
preds = [np.random.rand(output_dim) for _ in range(n_preds)]

# convert data into dataframe
df = pd.DataFrame(preds)

# save data to csv