Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Data Science
  3. Question
Data Science

Exception: Data must be 1-dimensional

Asked by Daniel Baker Sep 26, 2024 1.9K views 3 answers
Share

About this question

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

Your answer

3 Answers

Natalie Latest answer

Answered on Dec 19, 2024

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

Was this helpful?

Fabianhintz

Answered on Nov 20, 2024

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

Was this helpful?

More Data Science discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Data Science Blogs

Guides, tips and career advice on Data Science from JanBask experts.