Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: only length-1 arrays can be converted to Python scalars while trying to exponentially fit data

f=np.loadtxt('Single Small Angle 1.txt',unpack=True,skiprows=2) g=np.loadtxt('Single Small Angle 5.txt',unpack=True,skiprows=2)  x = f-g[:,:11944] t=range(len(x)) m=math.log10(abs(x))  np.polyfit(t,m)  plt.plot(t,abs(x)) plt.show() 

I'm just not sure on how to fix my issue. It keeps saying:

m=math.log10(abs(x)) TypeError: only length-1 arrays can be converted to Python scalars 
like image 505
user3291404 Avatar asked Feb 10 '14 20:02

user3291404


1 Answers

Non-numpy functions like math.abs() or math.log10() don't play nicely with numpy arrays. Just replace the line raising an error with:

m = np.log10(np.abs(x)) 

Apart from that the np.polyfit() call will not work because it is missing a parameter (and you are not assigning the result for further use anyway).

like image 181
Tom Pohl Avatar answered Oct 03 '22 21:10

Tom Pohl