numpy loglog linear regression

1.2k Views Asked by At

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: enter image description here

1

There are 1 best solutions below

0
On BEST ANSWER

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')