import pandas as pd
import numpy as np
from sklearn import linear_model
import matplotlib.pyplot as plt
from pylab import mpl
mpl.rcParams['font.sans-serif']=['SimHei']
mpl.rcParams['axes.unicode_minus']=False
# 最小二乘法OLS
data1=pd.read_excel('./数据.xlsx',sheet_name='Sheet1')
x=np.array(data1['全社会固定资产投资(X)'].values.reshape(-1,1))
y=np.array(data1['工业增加值(Y)'])
reg=linear_model.LinearRegression()
reg.fit(x,y)
print('最小二乘法OLS:y=%f+%f*x'%(reg.intercept_,reg.coef_))
plt.style.use('ggplot')
plt.subplot(1,3,1)
plt.plot(x,reg.predict(x),'k')
plt.plot(x,y,'.r')
plt.text(120000,200000,'y=%.2f+%.2f*x'%(reg.intercept_,reg.coef_),fontsize=12)
plt.title('最小二乘法OLS')
plt.xlabel('全社会固定资产投资(X)')
plt.ylabel('工业增加值(Y)')
# 岭回归Ridge
reg2=linear_model.Ridge(alpha=1)
reg2.fit(x,y)
print('岭回归Ridge:y=%f+%f*x'%(reg2.intercept_,reg2.coef_))
plt.style.use('ggplot')
plt.subplot(1,3,2)
plt.plot(x,reg2.predict(x),'k')
plt.plot(x,y,'.r')
plt.text(120000,200000,'y=%.2f+%.2f*x'%(reg2.intercept_,reg2.coef_),fontsize=12)
plt.title('岭回归Ridge(a=1)')
plt.xlabel('全社会固定资产投资(X)')
plt.ylabel('工业增加值(Y)')
# 拉索回归Lasso
reg3=linear_model.Ridge(alpha=1)
reg3.fit(x,y)
print('拉索回归Lasso:y=%f+%f*x'%(reg3.intercept_,reg3.coef_))
plt.style.use('ggplot')
plt.subplot(1,3,3)
plt.plot(x,reg3.predict(x),'k')
plt.plot(x,y,'.r')
plt.text(120000,200000,'y=%.2f+%.2f*x'%(reg3.intercept_,reg3.coef_),fontsize=12)
plt.title('拉索回归Lasso(a=1)')
plt.xlabel('全社会固定资产投资(X)')
plt.ylabel('工业增加值(Y)')
plt.show()
Ⅲ.效果1
最小二乘法OLS:y=13347.367949+0.529279*x
岭回归Ridge:y=13347.367950+0.529279*x
拉索回归Lasso:y=13347.367950+0.529279*x
Ⅳ.原始数据2
Ⅴ.python程序2
import pandas as pd
import numpy as np
from sklearn import linear_model
data2=pd.read_excel('数据.xlsx',sheet_name='Sheet2')
x=np.array(data2.iloc[:,1:7])
y=np.array(data2.iloc[:,7])
# 最小二乘法OLS
reg_1=linear_model.LinearRegression()
reg_1.fit(x,y)
a0=reg_1.intercept_
a1,a2,a3,a4,a5,a6=reg_1.coef_
print('最小二乘法OLS:y=%f+(%f*x1)+(%f*x2)+(%f*x3)+(%f*x4)+(%f*x5)+(%f*x6)'%(a0,a1,a2,a3,a4,a5,a6))
# 岭回归Ridge
reg_2=linear_model.Ridge(alpha=1)
reg_2.fit(x,y)
b0=reg_2.intercept_
b1,b2,b3,b4,b5,b6=reg_2.coef_
print('岭回归Ridge:y=%f+(%f*x1)+(%f*x2)+(%f*x3)+(%f*x4)+(%f*x5)+(%f*x6)'%(b0,b1,b2,b3,b4,b5,b6))
# 拉索回归Lasso
reg_3=linear_model.Lasso(alpha=1)
reg_3.fit(x,y)
c0=reg_3.intercept_
c1,c2,c3,c4,c5,c6=reg_3.coef_
print('最小二乘法OLS:y=%f+(%f*x1)+(%f*x2)+(%f*x3)+(%f*x4)+(%f*x5)+(%f*x6)'%(c0,c1,c2,c3,c4,c5,c6))
Ⅵ.效果2
最小二乘法OLS:y=-115.574092+(0.432860*x1)+(-0.006699*x2)+(1.520125*x3)+(0.862782*x4)+(-0.042709*x5)+(0.234687*x6)
岭回归Ridge:y=-115.573539+(0.432860*x1)+(-0.006698*x2)+(1.520107*x3)+(0.862777*x4)+(-0.042707*x5)+(0.234687*x6)
最小二乘法OLS:y=-115.560303+(0.432871*x1)+(-0.006680*x2)+(1.519698*x3)+(0.862634*x4)+(-0.042665*x5)+(0.234663*x6)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)