我正在學習如何解釋用Python建立的指數函數的線性回歸模型。我建立了一個模型,首先通過取自然對數将指數Y資料轉換成直線。然後我建立一個線性模型,并記錄坡度和截距。最後,我嘗試使用斜率和截距來計算樣本值。具體地說,當X=1.1時,我試圖計算Y。Y應該是~2.14,但是我的模型解釋得到的Y值是3.78。在
問題1:我在解釋模型時做錯了什麼。在
問題2:我必須改變X數組的形狀,否則在重新裝配. 為什麼我要改變X數組的形狀。在
代碼如下:import matplotlib.pyplot as plt
import numpy as np
from sklearn import datasets, linear_model
# create some exponential data
X = np.arange(1, 10, 0.1)
print(X)
Y = np.power(2, X)
print(Y)
# transform the exponential Y data to make it a straight line
ln_Y = np.log(Y)
# show the exponential plot
plt.scatter(X, Y)
plt.show()
# Create linear regression object
regr = linear_model.LinearRegression()
# reshape the X to avoid regr.fit errors
X = np.reshape(X, (X.size, 1))
# Train the model using the training sets
regr.fit(X,ln_Y)
# The coefficients
print('Slope: \n', regr.coef_)
print('Intercept: \n', regr.intercept_)
# predict Y when X = 1.1 (should be approximately 2.14354693)
# equation = e^(0.00632309*1.1) + 2.7772517886
print("predicted val = ", np.exp(0.00632309*1.1) + 2.7772517886)