1
$\begingroup$

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 dtype 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)
<ipython-input-8-9f9e3cc13a98> in <module>
     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 anyone suggest what this means? Thanks

$\endgroup$
0
$\begingroup$

The data you are trying to plot (data[i]) contains data of type object (i.e. a string) which of course cannot be plotted as they are not numbers. This is caused by the fact that one of the columns in your dataset is a string which you are trying to plot, likely the Name column. Try removing the Name column from the dataset and see if there are other column which contains strings.

$\endgroup$
2
  • $\begingroup$ To add to above ans, it may also happen even if only one row of the columns you are using contains string instead of number, this will cause numpy to treat entire column as column of strings. Happens all the time with uncleaned data where missing value maybe encoded via '.' or 'na' or ' '. $\endgroup$
    – Deep
    Mar 14 at 6:19
  • $\begingroup$ Thank you both :) $\endgroup$
    – Jack
    Mar 15 at 14:15

Not the answer you're looking for? Browse other questions tagged or ask your own question.