Python作迴歸分析
假設上述是以輸入-輸出對的形式觀察到的數據。請使用指數函數家族,y=αe^βx,其中β<0,進行迴歸分析,並找出最佳的係數α和β。
程式碼:
import numpy as np
import pandas as pd
from scipy.optimize import curve_fit
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
# The data is assumed to be the observed input-output pairs.
data = {
'x': range(11),
'y': [9735, 4597, 2176, 1024, 483, 229, 108, 52, 24, 11, 6]
}
df = pd.DataFrame(data)
# Defining the exponential function y = α * e^(β * x).
def exponential_func(x, a, b):
return a * np.exp(b * x)
# Use non-linear least squares to fit the exponential function to data.
params, covariance = curve_fit(exponential_func, df['x'], df['y'])
# Extracting the optimum coefficients.
alpha, beta = params
# Now let's perform a linear regression on the log-transformed data for comparison.
# Linear regression requires a reshape of x to 2D array for sklearn
X_linear = df['x'].values.reshape(-1, 1)
y_linear = np.log(df['y']) # Transform the y values to a linear space
linear_regressor = LinearRegression()
linear_regressor.fit(X_linear, y_linear)
# Predict the values using both models
y_pred_exponential = exponential_func(df['x'], alpha, beta)
y_pred_linear = np.exp(linear_regressor.predict(X_linear)) # Transform predictions back to the original space
# Calculate the mean squared error for both models
mse_exponential = mean_squared_error(df['y'], y_pred_exponential)
mse_linear = mean_squared_error(df['y'], y_pred_linear)
# Output the parameters and the comparison of errors
print(f"Optimum α: {alpha}")
print(f"Optimum β: {beta}")
print(f"MSE for Exponential Regression: {mse_exponential}")
print(f"MSE for Linear Regression: {mse_linear}")
print(f"Difference in MSE: {mse_linear - mse_exponential}")
留言
張貼留言