Reputation: 515
I want to plot the least-square regression line for the X and Y in the log-log scale plot and find coefficients. The line function is log(Y) = a.log(X) + b equivalently, Y = 10^b . X^a. What are the coefficients a and b? how can I use polyfit in NumPy?
I use code below using this code but I get this runtime error:
divide by zero encountered in log10 X_log = np.log10(X)
X_log = np.log10(X)
Y_log = np.log10(Y)
X_mean = np.mean(X_log)
Y_mean = np.mean(Y_log)
num = 0
den = 0
for i in range(len(X)):
num += (X_log[i] - X_mean)*(Y_log[i] - Y_mean)
den += (X_log[i] - X_mean)**2
m = num / den
c = Y_mean - m*X_mean
print (m, c)
Y_pred = m*X_log + c
plt.plot([min(X_log), max(X_log)], [min(Y_pred), max(Y_pred)], color='red') # predicted
plt.show()
Upvotes: 0
Views: 489
Reputation: 401
It seems like you have X-values that are too close to zero, can you show the values you send to log_x = np.log10(x)?
To use np.polyfit just write
coeff = np.polyfit(np.log10(x), np.log10(y), deg = 1)
coeff will now be a list [a,b] with your coefficients for a first-degree fit (hence deg = 1) to the data points (log(x), log(y)). If you want the variance in the coefficients use
coeff, cov = np.polyfit(np.log10(x), np.log10(y), deg = 1, cov = True)
cov is now your covariance matrix.
Upvotes: 1