1

I received error while appending element to an array.

array([[ 5,  89,  342,  282,    3,  644, 1254,  634,    4,  711,   58,
        1554,  23,  613,  565,    2,  787,  968,  640,    7,  676,  65,
         346]])

I want to append mean of this array 3 times to this array so if the mean is 10, "10, 10, 10" will be added to the array.

mean = array.mean()
array= array.append(mean, mean, mean)

error message

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

Where did I do wrong ? thank you

6
  • append as you are using it, is meant for lists. Perhaps you want numpy.append? Commented Nov 1, 2018 at 22:03
  • sharing the full code would also be of huge help Commented Nov 1, 2018 at 22:03
  • @DavidG thanks. if i used np.append I got another error message 'append() missing 1 required positional argument: 'values'' Commented Nov 1, 2018 at 22:05
  • @JanNeduchal This is the full code already Commented Nov 1, 2018 at 22:06
  • it obviously isnt if the interpreter is saying there was a problem with a numpy object (where is the import?) Commented Nov 1, 2018 at 22:08

1 Answer 1

1

you should do something like this.

import numpy as np

array = np.array([[ 5,  89,  342,  282,    3,  644, 1254,  634,    4,  711,   58,
        1554,  23,  613,  565,    2,  787,  968,  640,    7,  676,  65,
         346]])

mean = array.mean()

new_array = np.append(array,np.array([mean,mean,mean]))
Sign up to request clarification or add additional context in comments.

5 Comments

works well on testing. Out of curiousity - do you know why this works using numpy?
I'm not sure what you are meaning. I know each object has its own methods. 'numpy' is an object 'array' is another object too. Append is a method for 'numpy' not for 'array'.
Because array is a mutable object ?
I'm confusing append on a list with append on an array - just getting up to scratch on numpy. That helps some - thanks!
@takkyi83 I think this is true. if you refer to this address you will find some function are "(numpy.ndarray method)". these function can be used as method of 'array' object. but some of them like 'append' are just "(in module numpy)". so the second one can be available for 'numpy' objects.

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.