3

I'm working with a numpy array called "C_ClfGtLabels" in which 374 artist/creator names are stored. I want to append a 375th artist class with a string "other artists". I thought I could just do that as follows:

C_ClfGtLabels.append('other artists')

However, this results in the following error:

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

I saw found this problem a few times on stackoverflow, to which the answer in one case was to use concatenate instead of append. When I tried that I got the following error:

TypeError: don't know how to convert scalar number to int

It seems to be a problem that the datatype does not match the datatype that I, trying to append/concatenate, which would be of type string. However, I don't know what I should do to make them match. The data inside the Clabels array is as follows:

    [u"admiral, jan l'" u'aldegrever, heinrich' u'allard, abraham'
 u'allard, carel' u'almeloveen, jan van' u'altdorfer, albrecht'
 u'andriessen, jurriaan' u'anthonisz., cornelis' u'asser, eduard isaac' ..]

Any advice on how I can setup the "other artists" string so that I can append it to C_ClfGtLabels?

5
  • what is the shape and the dtype of your C_ClfGtLabels numpy array? what do you mean by C_ClfGtLabels.np.append('other artists')? Commented May 20, 2015 at 13:40
  • The shape of the array is (374,), the dtype seems to be unsigned int (.dtype returns <u58). Oops, I meant C_ClfGtLabels.append instead of C_ClfGtLabels.np.append. I just want to append the string "other artists" to the array. Commented May 20, 2015 at 13:56
  • what is the output of C_ClfGtLabels.np? Commented May 20, 2015 at 13:57
  • you could probably work on a list instead: lst = list(C_ClfGtLabels) if you really need, you can always convert it back later np.asarray(lst) Commented May 20, 2015 at 14:06
  • The .np was a mistake, ignore that (I just removed it from my question). The output of the C_ClfGtlabels is as displayed in the last block of the question. Still, to answer your question, the output of C_ClfGtLabels.np is " 'numpy.ndarray' object has no attribute 'np' " Commented May 20, 2015 at 14:09

1 Answer 1

3

A quick workaround is to convert your C_ClfGtLabels into a list first, append, and convert it back into an ndarray

lst = list(C_ClfGtLabels)
lst.append('other artists')

C_ClfGtLabels = np.asarray(lst)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.