Skip to content
Advertisement

‘numpy.ndarray’ object has no attribute ‘find’ while trying to generate boxplot?

I am trying to generate a box plot. Here is my code, data below:

JavaScript

The data is float which is verified by the print command as its output is <type 'float'>. On running the code, I am getting the following error (full stacktrace below)

AttributeError: 'numpy.ndarray' object has no attribute 'find'

My data (e.g. in dataset1) looks like this

JavaScript

output of data.shape = (756,)

Data file format:

JavaScript

Stacktrace

JavaScript

Does anybody have any idea, how to resolve it?

Advertisement

Answer

The immediate cause of your problem is that dataset1 and dataset2 are ndarray type, with dtype == object.

Although your values are read in as float type, when you access the column of the values array you return (at the line dataset1 = data1[:,ithattr1]), the dtype is changed to object (as you are actually pulling the data row by row, then extracting a column and numpy has both floats and strings in the row, so has to coerce to the most specific common data type – object).

You can get around this several ways. One is simply to make the arrays into lists, at which point Python coerces what looks like a float to be a float, i.e. change

JavaScript

to

JavaScript

Another is to add lines explicitly setting the type:

JavaScript

This is a gotcha when you access pandas dataframes or numpy arrays containing different data types in columns by index. It’s pretty hard to debug (took me a while to get it for this question and I’ve seen it before – see the edit history)


However, the way you’re handling your data via numerical indices also means you end up having to reorder your columns etc for convenience in your loadData function. A better way would be to let pandas do all the heavy lifting on types etc…

As an example – I’ve put your code into what (I think) is a more conventional pandas / python writing. It’s a bit shorter and doesn’t require the hack to change the data to a list that I give you above. Code is below and output plot after that (using the input data snippet from your question)

JavaScript

Output

boxplot of provided data

Advertisement