I am struggling with a program I am making on the part where I have to store values that I get from my loop in an array.
What I tried to do is to make an empty array called M. Then for every new value "a" calculated in a loop I use the command M.append(a) to add the value a to the array M.
The thing is, python says this error : 'numpy.ndarray' object has no attribute 'append'
and I don't know how to fix it.
Here is my code :
import numpy as np
from matplotlib import pyplot as plt
with open('expFcn0.txt') as f:
M = np.array([])
print(M)
lines = f.readlines()
x = [float(line.split()[0]) for line in lines]
y = [float(line.split()[1]) for line in lines]
for i in range(0,181):
a=np.log(y[i])/x[i]
print(a)
i=i+1
M.append(a)
print(M)
plt.plot(x, y, 'r--')
plt.xlabel('Time')
plt.ylabel('Biomass')
plt.title('Exponential Function')
plt.show()
Thank you very much!
numpy.appendmethod, but appending to a numpy array is not good practice. You are probably better off creating a python list and appending to that. If you want a numpy array of the results, you can create a numpy array from that list.M = []. A list has an append, an array does not.i=i+1, doesn't work and should be avoided inside for loop.