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

What must be done when image data cannot be converted to float?

Asked by Darsh K Feb 15, 2023 1.9K views 1 answer
Share

About this question

I am trying to convert files stored in a CSV file dataset to grayscale. I run the code which worked on my previous dataset but I now get this error:


TypeError: Image data of  object cannot be converted to float

The code I produced is below:
import pandas as pd 
import numpy as np 
from sklearn.datasets import load_digits
import matplotlib.pyplot as plt
data_path = "pe_section_headers.csv"
data1 = pd.read_csv(data_path);
data = data1
#data = data.drop("Malware", axis=1)
#data = data.drop("Name", axis=1)
data = data.values
data = data.reshape(data.shape[0], data.shape[1], 1)
data = np.tile(data, (1, data.shape[1]))
for i in range(data.shape[0]):
    plt.tick_params(left=False, bottom=False, labelleft=False, labelbottom=False)
    plt.imshow(data[i], cmap="gray")
    plt.savefig(f"output_image_{data1.iloc[i,0]}.png")
    plt.close()
#print(data[0].shape)
And the error message I receive is:

---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)
in
     21 for i in range(data.shape[0]):
     22     plt.tick_params(left=False, bottom=False, labelleft=False, labelbottom=False)
---> 23     plt.imshow(data[i], cmap="gray")
     24     plt.savefig(f"output_image_{data1.iloc[i,0]}.png")
     25     plt.close()
~/opt/anaconda3/envs/jacksprojectnew/lib/python3.6/site-packages/matplotlib/pyplot.py in imshow(X, cmap, norm, aspect, interpolation, alpha, vmin, vmax, origin, extent, filternorm, filterrad, resample, url, data, **kwargs)
   2728         filternorm=filternorm, filterrad=filterrad, resample=resample,
   2729         url=url, **({"data": data} if data is not None else {}),
-> 2730         **kwargs)
   2731     sci(__ret)
   2732     return __ret
~/opt/anaconda3/envs/jacksprojectnew/lib/python3.6/site-packages/matplotlib/__init__.py in inner(ax, data, *args, **kwargs)
   1445     def inner(ax, *args, data=None, **kwargs):
   1446         if data is None:
-> 1447             return func(ax, *map(sanitize_sequence, args), **kwargs)
   1448 
   1449         bound = new_sig.bind(ax, *args, **kwargs)
~/opt/anaconda3/envs/jacksprojectnew/lib/python3.6/site-packages/matplotlib/axes/_axes.py in imshow(self, X, cmap, norm, aspect, interpolation, alpha, vmin, vmax, origin, extent, filternorm, filterrad, resample, url, **kwargs)
   5521                               resample=resample, **kwargs)
   5522 
-> 5523         im.set_data(X)
   5524         im.set_alpha(alpha)
   5525         if im.get_clip_path() is None:
~/opt/anaconda3/envs/jacksprojectnew/lib/python3.6/site-packages/matplotlib/image.py in set_data(self, A)
    701                 not np.can_cast(self._A.dtype, float, "same_kind")):
    702             raise TypeError("Image data of dtype {} cannot be converted to "
--> 703                             "float".format(self._A.dtype))
    704 
    705         if self._A.ndim == 3 and self._A.shape[-1] == 1:
TypeError: Image data of dtype object cannot be converted to float
Could you suggest what this means?

Your answer

1 Answer

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.