Use numpy.concatenate(list1 , list2) or numpy.append()
Look into the thread at Concatenate a NumPy array to another NumPy array.
Answer from Arpita Biswas on Stack OverflowUse numpy.concatenate(list1 , list2) or numpy.append()
Look into the thread at Concatenate a NumPy array to another NumPy array.
Basically, numpy arrays don't have have access to append(). If we look into documentation, we need to use np.append(array), where array is the values to be appended.
import numpy as np
arr = np.array([1,2,3,47,1,0,2])
np.append(arr, 4)
AttributeError: 'numpy.ndarray' object has no attribute 'append'
Week 2 Exercise 6 "'numpy.ndarray' object has no attribute 'append'"
python - numpy ndarray object has no attribute append - Stack Overflow
python - Adding a row to a numpy array: 'numpy.ndarray' object has no attribute 'append' or 'vstack' - Stack Overflow
Videos
I need help. This is the code which gives error:
import numpy as np
num = 3
X_new = np.array([])
for i in range(1, num+1):
print('Enter the', i, '. data:')
n = float(input())
X_new.append(n)
print('\n', X_new)
And this is the error:
AttributeError: 'numpy.ndarray' object has no attribute 'append'
Numpy arrays don't have a method append(). You need to use np.append(array, values) as per the documentation, or for your case, np.append(M, a).
Other answers explain that numpy arrays do not have an .append() method and point to numpy.append. Using numpy.append, however, is bad practice because it creates a new array each time. A better solution is to create one numpy and fill it during the for loop (see end of answer).
An even better solution would make use of numpy's broadcasting. That's a core feature of numpy, and it's what helps make numpy fast.
import numpy as np
with open('expFcn0.txt') as f:
lines = f.readlines()
x = np.array([float(line.split()[0]) for line in lines])
y = np.array([float(line.split()[1]) for line in lines])
M = np.log(y) / x
You can also look into numpy.loadtxt to read the file into a numpy array directly.
How to fill a numpy array in a for loop:
import numpy as np
with open('expFcn0.txt') as f:
lines = f.readlines()
x = [float(line.split()[0]) for line in lines]
y = [float(line.split()[1]) for line in lines]
M = np.zeros(181)
for i in range(181):
a = np.log(y[i])/x[i]
print(a)
M[i] = a
>>> import numpy as np
>>> a = np.array([1, 2, 3])
>>> b = np.array([5, 6, 7])
>>> np.vstack(a, b)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: vstack() takes exactly 1 argument (2 given)
>>>
>>> np.vstack((a, b))
array([[1, 2, 3],
[5, 6, 7]])
vstack is a numpy function which takes only a single argument in the form of a tuple . You need to call it and assign the output to your variable. So instead of
g.vstack((temp_g,g))
use
g=np.vstack((temp_g,g))
Hi,
I'm trying to create a numpy v-stack and creating 3 np.array's for it, by filling them with a loop:
I get the error: 'AttributeError: 'numpy.ndarray' object has no attribute 'np' . I think I'm using the wrong notation to append to the empty arrays:
neighbor_id = [id_ for id_ in range(1, n_obs) if id_ != user_id]
neighbor_id_arr = np.array(neighbor_id)
similarity = np.array([])
num_interactions = np.array([])
# get similarity and num_interactions
for id_ in neighbor_id:
similarity.np.append(np.dot(user_item.loc[user_id],user_item.loc[id_])) #The issue is here, I think
num_interactions.np.append(user_interactions.loc[id_])
c = numpy.vstack((neighbor_id_arr, similarity,num_interactions))
Thanks!
James
You can add a NumPy array element by using the append() method of the NumPy module.
The syntax of append is as follows:
numpy.append(array, value, axis)
The values will be appended at the end of the array and a new ndarray will be returned with new and old values as shown above.
The axis is an optional integer along which define how the array is going to be displayed. If the axis is not specified, the array structure will be flattened
A quick look at the documentation shows that np.ndarray objects do not have a function append, it is a function of np itself:
class1 = np.append(class1, [i])