Confusion matrix


(evo) #1

Hello, I did FNN for 4 class classifications.
How is it possible to calculate confusion matrix?


#2

You could use the scikit-learn implementation. Just get your predictions and targets using .numpy().

Alternatively you could of course calculate the conf matrix manually, if you don’t want to install/use scikit-learn.


(evo) #3

I applied scikit-learn implementation, but output from FNN tend to 0. As a result my confusion matrix looks weird.


for epoch in range (num_epochs):
    out = model(input_train).to(device)
    _, pred = out.max(1)
    total += target_train.size(0)
    correct += (pred == target_train).sum().item()
    print(input_train)
    print(pred)
    loss = loss_func(out,target_train)
    counter +=1
    print('loss train', "Epoch N", counter,loss.data[0])
    model.zero_grad()
    loss.backward()
    opt.step()
print('Accuracy of the network on train dataset: {} %'.format(100 * correct / total))

conf_matrix = metrics.confusion_matrix(pred, y)

[[530783 0 0 0]
[ 8097 0 0 0]
[ 20079 0 0 0]
[ 16682 0 0 0]]

Where can be an error?


#4

It looks like your model does not learn anything useful.
Since your classes are imbalanced, you could try to use a weighted loss function or the WeightedRandomSampler.