我使用scipy.stats
中的linregress
编写了一个线性回归代码,并想将其与我在网上找到的使用sklearn.linear_model
中的LinearRegression
的另一个代码进行比较。
使用scipy
时,真值和预测值似乎很容易提取(如果我做得正确的话),但在sklearn
代码中,当我尝试计算MSE和RMSE时,收到了一个错误。
对于使用sklearn
进行线性回归的MSE和RMSE计算,我应该选择哪些作为预测值和真值?
scipy
代码:
from scipy.stats import linregressimport mathfrom sklearn.metrics import mean_squared_errorimport pandas as pdimport statisticsimport numpy as npdata_y = [76.6,118.6,200.8,362.3,648.9]data_x = [10,20,40,80,160]s_data_y = pd.Series(data_y)s_data_x = pd.Series(data_x)slope, intercept, r_value, p_value, std_err = linregress(s_data_x,s_data_y)linregress(s_data_x,s_data_y)true_val = s_data_ypredicted_val = intercept + slope * s_data_xmse = mean_squared_error(true_val, predicted_val)rmse = math.sqrt(mse)plt.scatter(s_data_x,s_data_y)plt.plot(s_data_x, predicted_val, 'r', label='fitted line')plt.xlabel("X",fontweight='bold')plt.ylabel("Y",fontweight='bold')plt.grid(True)plt.show()print(dev_contact_resistance_params_all)print(f'{slope=}\n{intercept=}\n{r_value=}\n{p_value=}\n{std_err=}\n')print('Mean square error:',mse)print('Root mean square error:',rmse)
sklearn
代码(来自网络):
from sklearn import linear_modelimport matplotlib.pyplot as pltimport numpy as npimport random#----------------------------------------------------------------------------------------## Step 1: training dataY = [76.6,118.6,200.8,362.3,648.9]X = [10,20,40,80,160]X = np.asarray(X)Y = np.asarray(Y)X = X[:,np.newaxis]Y = Y[:,np.newaxis]plt.scatter(X,Y)#----------------------------------------------------------------------------------------## Step 2: define and train a modelmodel = linear_model.LinearRegression()model.fit(X, Y)print(model.coef_, model.intercept_)#----------------------------------------------------------------------------------------## Step 3: predictionx_new_min = 0.0x_new_max = 200.0X_NEW = np.linspace(x_new_min, x_new_max, 100)X_NEW = X_NEW[:,np.newaxis]Y_NEW = model.predict(X_NEW)plt.plot(X_NEW, Y_NEW, color='coral', linewidth=3)plt.grid()plt.xlim(x_new_min,x_new_max)plt.ylim(0,1000)plt.title("Simple Linear Regression using scikit-learn and python 3",fontsize=10)plt.xlabel('x')plt.ylabel('y')plt.savefig("simple_linear_regression.png", bbox_inches='tight')plt.show()true_val = Ypredicted_val = Y_NEWmse = mean_squared_error(true_val, predicted_val)rmse = math.sqrt(mse)print('Mean square error:',mse)print('Root mean square error:',rmse)
来自sklearn
代码的错误:
Traceback (most recent call last): File "C:\Users\test.py", line 21, in <module> mse = mean_squared_error(r_value, p_value) File "C:\lib\site-packages\sklearn\metrics\_regression.py", line 423, in mean_squared_error y_type, y_true, y_pred, multioutput = _check_reg_targets( File "C:\Users\lib\site-packages\sklearn\metrics\_regression.py", line 89, in _check_reg_targets check_consistent_length(y_true, y_pred) File "C:\Users\lib\site-packages\sklearn\utils\validation.py", line 328, in check_consistent_length lengths = [_num_samples(X) for X in arrays if X is not None] File "C:\Users\lib\site-packages\sklearn\utils\validation.py", line 328, in <listcomp> lengths = [_num_samples(X) for X in arrays if X is not None] File "C:\Users\lib\site-packages\sklearn\utils\validation.py", line 268, in _num_samples raise TypeError(ValueError: Found input variables with inconsistent numbers of samples: [5, 100]
回答:
您可以按照以下方式使用sklearn.linear_model.LinearRegression
复制scipy.stats.linregress
的结果:
import numpy as npfrom scipy.stats import linregressfrom sklearn.linear_model import LinearRegressionfrom sklearn.metrics import mean_squared_error# input datay = np.array([76.6, 118.6, 200.8, 362.3, 648.9])x = np.array([10, 20, 40, 80, 160])# scipy linear regressionslope, intercept, r_value, p_value, std_err = linregress(x, y)y_pred = intercept + slope * xmse = mean_squared_error(y_true=y, y_pred=y_pred, squared=True)rmse = mean_squared_error(y_true=y, y_pred=y_pred, squared=False)print('scipy intercept: {:.6f}'.format(intercept))print('scipy slope: {:.6f}'.format(slope))print('scipy MSE: {:.6f}'.format(mse))print('scipy RMSE: {:.6f}'.format(rmse))# scipy intercept: 45.058333# scipy slope: 3.812608# scipy MSE: 49.793366# scipy RMSE: 7.056441# sklearn linear regressionreg = LinearRegression().fit(x.reshape(- 1, 1), y)y_pred = reg.predict(x.reshape(- 1, 1))mse = mean_squared_error(y_true=y, y_pred=y_pred, squared=True)rmse = mean_squared_error(y_true=y, y_pred=y_pred, squared=False)print('sklearn intercept: {:.6f}'.format(reg.intercept_))print('sklearn slope: {:.6f}'.format(reg.coef_[0]))print('sklearn MSE: {:.6f}'.format(mse))print('sklearn RMSE: {:.6f}'.format(rmse))# sklearn intercept: 45.058333# sklearn slope: 3.812608# sklearn MSE: 49.793366# sklearn RMSE: 7.056441