本文主要是介绍多元线性回归实战(二元),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一:算法具体实现
import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D data=np.genfromtxt("data1.csv",delimiter=",") x_data=data[:,:-1] y_data=data[:,-1] lr=0.0001 k1=k2=b=0 epoches=1000 def compute_error(k1,k2,b,x_data,y_data):totalError=0for i in range(0,len(x_data)):totalError+=(y_data[i]-(k1*x_data[i,0]+k2*x_data[i,1]+b))**2return totalError/float(len(x_data)) def function(x_data,y_data,k1,k2,b,lr,epoches):m = float(len(x_data))for i in range(epoches):k1_grad=k2_grad=b_grad=0for j in range(0,len(x_data)):b_grad+=(1/m)*((k1*x_data[j,0]+k2*x_data[j,1]+b)-y_data[j])k1_grad+=(1/m)*x_data[j,0]*((k1*x_data[j,0]+k2*x_data[j,1]+b)-y_data[j])k2_grad+=(1/m)*x_data[j,1]*((k1*x_data[j,0]+k2*x_data[j,1]+b)-y_data[j])k1-=(lr*k1_grad)k2-=(lr*k2_grad)b-=(lr*b_grad)return k1,k2,b compute_error(k1,k2,b,x_data,y_data) k1,k2,b=function(x_data,y_data,k1,k2,b,lr,epoches) ax=plt.figure().add_subplot(111,projection="3d") ax.scatter(x_data[:,0],x_data[:,1],y_data,c="r",marker="o",s=100) x0=x_data[:,0] x1=x_data[:,1] x0,x1=np.meshgrid(x0,x1) z=k1*x0+k2*x1+b ax.plot_surface(x0,x1,z) ax.set_xlabel("x") ax.set_ylabel("y") ax.set_zlabel("z") plt.show()
二:sklearn调用实现
import numpy as np import matplotlib.pyplot as plt from sklearn.linear_model import LinearRegression from mpl_toolkits.mplot3d import Axes3D data=np.genfromtxt("data1.csv",delimiter=",") x_data=data[:,:-1] y_data=data[:,-1] model=LinearRegression() model.fit(x_data,y_data) LinearRegression(copy_X=True,fit_intercept=True,n_jobs=1,normalize=False) ax=plt.figure().add_subplot(111,projection="3d") ax.scatter(x_data[:,0],x_data[:,1],y_data,c="r",marker="o",s=100) x0=x_data[:,0] x1=x_data[:,1] x0,x1=np.meshgrid(x0,x1) z=model.coef_[0]*x0+model.coef_[1]*x1+model.intercept_ ax.plot_surface(x0,x1,z) ax.set_xlabel("x") ax.set_ylabel("y") ax.set_zlabel("z") plt.show()
这篇关于多元线性回归实战(二元)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!