TypeError: 'numpy.float64' object is not iterable in calculation

Question:

my code is start when I do my generators, as :

train_data_dir= "/Users/awabe/Desktop/Project/PapilaDB/ExpertsSegmentations/ImagesWithContours train"

train_datagen = ImageDataGenerator(rescale=1./255,
    shear_range=0.2,
    zoom_range=0.2,
    horizontal_flip=True,
    validation_split=0.2) # set validation split

train_generator = train_datagen.flow_from_directory(
    train_data_dir,
    target_size=(2576, 2000),
    batch_size=24,
    class_mode='binary',
    subset='training') # set as training data

validation_generator = train_datagen.flow_from_directory(
    train_data_dir, # same directory as training data
    target_size=(2576, 2000),
    batch_size=24,
    class_mode='binary',
    subset='validation') # set as validation data

and by the code:

plt.xticks(rotation=90)
plt.bar(x=labels, height=np.mean(train_generator.labels, axis=0))
plt.title("Frequency of Each Class")
plt.show()

it gives me 1.5 as a frequency for each class.

then I use:

freq_pos, freq_neg = compute_class_freqs(train_generator.labels)
freq_pos

to get the frequencies as seen.

and when I run:

data = pd.DataFrame({"Class": labels, "Label": "Positive", "Value": freq_pos})
data = data.append([{"Class": labels[l], "Label": "Negative", "Value": v} for l,v in enumerate(freq_neg)], ignore_index=True)
plt.xticks(rotation=90)
f = sns.barplot(x="Class", y="Value", hue="Label" ,data=data)

then I get the error when I try to visualize these two contribution ratios next to each other:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[173], line 2
      1 data = pd.DataFrame({"Class": labels, "Label": "Positive", "Value": freq_pos})
----> 2 data = data.append([{"Class": labels[l], "Label": "Negative", "Value": v} for l,v in enumerate(freq_neg)], ignore_index=True)
      3 plt.xticks(rotation=90)
      4 f = sns.barplot(x="Class", y="Value", hue="Label" ,data=data)

TypeError: 'numpy.float64' object is not iterable
Asked By: Awab Elkhair

||

Answers:

It means that you’re trying to loop through an object that loops don’t work on.

A potential fix is to surround freq_neg with range, which in turn would mean the enumerate is redundant, considering both l and v has the same value.

for l,v in enumerate(range(freq_neg))]
Answered By: tim the fiend