python 线性回归 技术方案亮点_Python Sklearn多元线性回归显示R平方

I calculated my multiple linear regression equation and I want to see the adjusted R-squared. I know that the score function allows me to see r-squared, but it is not adjusted.

import pandas as pd #import the pandas module

import numpy as np

df = pd.read_csv ('/Users/jeangelj/Documents/training/linexdata.csv', sep=',')

df

AverageNumberofTickets NumberofEmployees ValueofContract Industry

0 1 51 25750 Retail

1 9 68 25000 Services

2 20 67 40000 Services

3 1 124 35000 Retail

4 8 124 25000 Manufacturing

5 30 134 50000 Services

6 20 157 48000 Retail

7 8 190 32000 Retail

8 20 205 70000 Retail

9 50 230 75000 Manufacturing

10 35 265 50000 Manufacturing

11 65 296 75000 Services

12 35 336 50000 Manufacturing

13 60 359 75000 Manufacturing

14 85 403 81000 Services

15 40 418 60000 Retail

16 75 437 53000 Services

17 85 451 90000 Services

18 65 465 70000 Retail

19 95 491 100000 Services

from sklearn.linear_model import LinearRegression

model = LinearRegression()

X, y = df[['NumberofEmployees','ValueofContract']], df.AverageNumberofTickets

model.fit(X, y)

model.score(X, y)

>>0.87764337132340009

I checked it manually and 0.87764 is R-squared; whereas 0.863248 is the adjusted R-squared.

解决方案

There are many different ways to compute R^2 and the adjusted R^2, the following are few of them (computed with the data you provided):

from sklearn.linear_model import LinearRegression

model = LinearRegression()

X, y = df[['NumberofEmployees','ValueofContract']], df.AverageNumberofTickets

model.fit(X, y)

SST = SSR + SSE (ref definitions)

# compute with formulas from the theory

yhat = model.predict(X)

SS_Residual = sum((y-yhat)**2)

SS_Total = sum((y-np.mean(y))**2)

r_squared = 1 - (float(SS_Residual))/SS_Total

adjusted_r_squared = 1 - (1-r_squared)*(len(y)-1)/(len(y)-X.shape[1]-1)

print r_squared, adjusted_r_squared

# 0.877643371323 0.863248473832

# compute with sklearn linear_model, although could not find any function to compute adjusted-r-square directly from documentation

print model.score(X, y), 1 - (1-model.score(X, y))*(len(y)-1)/(len(y)-X.shape[1]-1)

# 0.877643371323 0.863248473832

Another way:

# compute with statsmodels, by adding intercept manually

import statsmodels.api as sm

X1 = sm.add_constant(X)

result = sm.OLS(y, X1).fit()

#print dir(result)

print result.rsquared, result.rsquared_adj

# 0.877643371323 0.863248473832

Yet another way:

# compute with statsmodels, another way, using formula

import statsmodels.formula.api as sm

result = sm.ols(formula="AverageNumberofTickets ~ NumberofEmployees + ValueofContract", data=df).fit()

#print result.summary()

print result.rsquared, result.rsquared_adj

# 0.877643371323 0.863248473832

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是关于Python中使用sklearn库实现多元线性回归模型的建立、评估和调优的详细介绍。 1. 多元线性回归模型简介 多元线性回归模型是一种用于建立因变量和多个自变量之间关系的统计模型。它假设因变量与自变量之间存在线性关系,并且能够通过最小二乘法等方法进行求解。 多元线性回归模型的一般形式为: y = β0 + β1x1 + β2x2 + … + βpxp + ε 其中,y 表示因变量,x1, x2, …, xp 表示自变量,β0, β1, β2, …, βp 表示回归系数,ε 表示误差项。多元线性回归的目标是求出最优的回归系数,使得模型的预测误差最小。 2. sklearn库实现多元线性回归模型的建立 sklearn库提供了LinearRegression类来实现多元线性回归模型的建立。下面是一个简单的示例代码: ```python from sklearn.linear_model import LinearRegression # 创建线性回归模型对象 model = LinearRegression() # 使用数据拟合模型 model.fit(X, y) # 打印回归系数 print(model.coef_) ``` 其中,X表示自变量的数据集,y表示因变量的数据集。model.fit(X, y)用于拟合模型,model.coef_用于获取回归系数。 3. 多元线性回归模型的评估 在建立多元线性回归模型后,需要对模型进行评估以确定其预测能力的好坏。常用的评估指标包括均方误差(MSE)、平均绝对误差(MAE)和决定系数(R2)等。 下面是一个使用sklearn库计算MSE和R2的示例代码: ```python from sklearn.metrics import mean_squared_error, r2_score # 使用模型进行预测 y_pred = model.predict(X) # 计算MSE和R2 mse = mean_squared_error(y, y_pred) r2 = r2_score(y, y_pred) # 打印评估结果 print("MSE:", mse) print("R2:", r2) ``` 4. 多元线性回归模型的调优 为了提高模型的预测能力,需要对模型进行调优。常用的调优方法包括特征选择和正则化等。 特征选择是指选择对因变量具有最强预测能力的自变量。sklearn库提供了SelectKBest、RFE和SelectFromModel等特征选择方法。下面是一个使用SelectKBest方法进行特征选择的示例代码: ```python from sklearn.feature_selection import SelectKBest, f_regression # 使用SelectKBest方法选择前两个最具预测能力的特征 selector = SelectKBest(f_regression, k=2) X_new = selector.fit_transform(X, y) # 使用新的特征集拟合模型 model.fit(X_new, y) ``` 正则化是指通过加入惩罚项来抑制模型过拟合的方法。sklearn库提供了Ridge、Lasso和ElasticNet等正则化方法。下面是一个使用Ridge方法进行正则化的示例代码: ```python from sklearn.linear_model import Ridge # 创建Ridge模型对象 model = Ridge(alpha=0.1) # 使用数据拟合模型 model.fit(X, y) ``` 其中,alpha是正则化强度的超参数,取值范围为[0, ∞]。alpha越大,惩罚力度越大,模型越不容易过拟合。 好的,以上就是关于Python中使用sklearn库实现多元线性回归模型的建立、评估和调优的介绍。希望对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值