Reputation: 2317
What is the best way to find the linear regression of loglog data using numpy? When I plot the data and try
A = np.vstack([np.log10(X), np.ones(len(X))]).T
m, c = np.linalg.lstsq(A, np.log10(Y))[0]
ax.plot(X, [m*x + c for x in X], 'r')
where X and Y are the data lists, this is the result, obviously incorrect:
Upvotes: 0
Views: 1273
Reputation: 114811
If you do a linear regression on
log10(y) = m*log10(x) + c
then in (x,y) coordinates you have
y = (10**c) * x**m
That is, you have fit a power law to your data. Change this
ax.plot(X, [m*x + c for x in X], 'r')
to
ax.plot(X, np.power(10, c) * np.power(X, m), 'r')
Upvotes: 1