Skip to content
Advertisement

Getting error while calculating AUC ROC for keras model predictions

I have a patient data named dat and labels (0 = No Disease, 1 = Disease) named labl both in the form of array. I predicted my model and stored the predictions named pre which is also an array, and I want to calculate and plot the AUC ROC. But I am getting this error while doing so.

TypeError: Singleton array array(0., dtype=float32) cannot be considered a valid collection.

This is just a single patient record. But when I predict my model on more patients, I can easily calculate the AUC ROC. But I want to find that for one patient only.

JavaScript

Using the below code I calculated the mortality as per time. But failed to calculate the AUC ROC.

JavaScript

The plot

plot

This is where I got the error:

JavaScript

————————————————————————— TypeError Traceback (most recent call last) /tmp/ipykernel_129/3666067037.py in 8 9 # compute ROC curve for predictions —> 10 rnn_roc = roc_curve(label,prediction) 11 12 # compute the area under the curve of prediction ROC

~/.conda/envs/default/lib/python3.9/site-packages/sklearn/metrics/_ranking.py in roc_curve(y_true, y_score, pos_label, sample_weight, drop_intermediate) 960 961 “”” –> 962 fps, tps, thresholds = _binary_clf_curve( 963 y_true, y_score, pos_label=pos_label, sample_weight=sample_weight 964 )

~/.conda/envs/default/lib/python3.9/site-packages/sklearn/metrics/_ranking.py in _binary_clf_curve(y_true, y_score, pos_label, sample_weight) 731 raise ValueError(“{0} format is not supported”.format(y_type)) 732 –> 733 check_consistent_length(y_true, y_score, sample_weight) 734 y_true = column_or_1d(y_true) 735 y_score = column_or_1d(y_score)

~/.conda/envs/default/lib/python3.9/site-packages/sklearn/utils/validation.py in check_consistent_length(*arrays) 327 “”” 328 –> 329 lengths = [_num_samples(X) for X in arrays if X is not None] 330 uniques = np.unique(lengths) 331 if len(uniques) > 1:

~/.conda/envs/default/lib/python3.9/site-packages/sklearn/utils/validation.py in (.0) 327 “”” 328 –> 329 lengths = [_num_samples(X) for X in arrays if X is not None] 330 uniques = np.unique(lengths) 331 if len(uniques) > 1:

~/.conda/envs/default/lib/python3.9/site-packages/sklearn/utils/validation.py in _num_samples(x) 267 if hasattr(x, “shape”) and x.shape is not None: 268 if len(x.shape) == 0: –> 269 raise TypeError( 270 “Singleton array %r cannot be considered a valid collection.” % x 271 )

TypeError: Singleton array array(0., dtype=float32) cannot be considered a valid collection.

JavaScript

Advertisement

Answer

The issue lies in your squeeze. You don’t need to specify the index when using squeeze. squeeze flattens the array into 1D. If you pick [:,0,:], it’s only 1 entry and hence the error.

Simply do

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement