ML实战:封装成类并应用到Boston数据集
代码实现
LinearRegression类
import numpy as np
from sklearn.preprocessing import StandardScaler
class LinearRegression:
def __init__(self,x,y):
#初始化函数
transfer = StandardScaler()
x = transfer.fit_transform(x)
self.x=x
self.y=np.array(y).reshape(-1,1)
self.theta=np.random.rand(len(x[0])+1,1)
self.theta[0]=1
one=np.ones(len(x))
self.x=np.c_[one,x]
def h(self):
#h(x)
return np.matmul(self.x,self.theta)
def single_iter(self,alpha):
#单次迭代函数
theta_size = len(self.theta)
size = len(self.x)
res = self.h()
res = res - self.y
for i in range(theta_size):
temp = self.theta[i]
temp = temp - alpha / size * (np.matmul(self.x[:, i], res))
self.theta[i]= temp
def fit(self,alpha=0.3,iter_count=500):
#参数拟合主函数
for i in range(iter_count):
self.single_iter(alpha)
def predict(self,x):
#最终预测函数,用来计算测试集拟合程度
transfer = StandardScaler()
x = transfer.fit_transform(x)
one=np.ones(len(x))
res_x=np.c_[one,x]
return np.matmul(res_x,self.theta)
主函数
import numpy as np
import sys
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from Linear_GraDec import LinearRegression
from sklearn import datasets
from sklearn.preprocessing import StandardScaler
np.set_printoptions(suppress=True)
x=datasets.load_boston().data
y=datasets.load_boston().target
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.2,random_state=np.random.randint(0,30))
X = np.arange(1, len(y_test) + 1)
linear=LinearRegression(x,y)
linear.fit(0.000002,3000)#设置α=0.000002,迭代次数为3000
y_predict=linear.predict(x_test)
print(linear.theta)
plt.figure(figsize=(20, 8), dpi=80)
plt.plot(X, y_test, label='real', color='yellow')
plt.scatter(X, y_test, color='blue')
plt.plot(X,y_predict,label='predict',color='green')
plt.scatter(X,y_predict,color='blue')
plt.legend(loc=[1, 0])
plt.savefig('E:/Python/ml/pic/Linear_Boston_Myself.png')
sys.exit(0)
结果
![]()