Exception: Data must be 1-dimensional

267    Asked by DanielBAKER in Data Science , Asked on Sep 26, 2024

I am doing image classification and have built and evaluated my network. Now I want to apply my model to a blind test set with no labels. But I am facing an error “Data must be 1-dimensional”. I have mentioned the code below and tried updating something but it throws the same. Where I’m going wrong in this?


CODE:

blind_testSet = '/content/drive/My Drive/Colab Notebooks/submission'
test_datagen_blind = ImageDataGenerator(
    featurewise_center=True,
    featurewise_std_normalization=True,
    rescale = 1. / 255
   )


test_generator_blind = test_datagen.flow_from_directory(
    directory=blind_testSet,
    target_size=(256, 256),
    color_mode="rgb",
    batch_size=batch_size,
    class_mode="categorical",
    shuffle=False
)
preds =transfer_model.predict_generator(test_generator_blind,verbose=1,steps=val_steps)


import pandas as pd
import numpy as np
predicted_class_indices=np.argmax(preds,axis=1)
labels = (train_generator.class_indices)
labels = dict((v,k) for k,v in labels.items())
print(labels)
predictions = [labels[k] for k in predicted_class_indices]
filenames=test_generator_blind.filenames
results=pd.DataFrame({"image ID":filenames,
                  "Predictions":preds})
results.to_csv("submission.csv",index=False)


I have tried another piece of code but facing the same error:

    

preds=transfer_model.predict_generator(test_generator_blind,verbose=1,steps=val_steps).resape(-1,1)

Error:

    Exception Traceback (most recent call last)
in ()
      9 filenames=test_generator_blind.filenames
     10 results=pd.DataFrame({"image ID":filenames,
---> 11 "Predictions":preds})
     12 results.to_csv("submission.csv",index=False)
4 frames
/usr/local/lib/python3.6/dist-packages/pandas/core/internals/construction.py in 
sanitize_array(data, index, dtype, copy, raise_cast_failure)
    727     elif subarr.ndim > 1:
    728         if isinstance(data, np.ndarray):
--> 729             raise Exception("Data must be 1-dimensional")
    730         else:
    731             subarr = com.asarray_tuplesafe(data, dtype=dtype)
Exception: Data must be 1-dimensional
Answered by Hasegawa Saito

The error you’re facing is valueerror: per-column arrays must each be 1-dimensional, as Preds are not one-dimensional, which is why Python complains. A flat list of labels called predictions is what you should use in its place.


Instead of this snippet:

results=pd.DataFrame({"image ID":filenames,"Predictions":preds})
You can try this snippet:
results=pd.DataFrame({"image ID":filenames,"Predictions":predictions})

Your Answer

Answers (2)

Immerse yourself in PolyTrack with the extremely interesting racing world, it will help you relieve stress and fatigue when dealing with the above problems.

3 Days

I still do not understand Merge Fruit what your purpose is? Can you elaborate?

1 Month

Interviews

Parent Categories