各位,帮忙看看这个基于批量梯度下降的线性回归哪里有问题?
为什么我的损失函数会越来越大啊
import numpy as np
import pandas as pd
from sklearn import datasets
from sklearn.model_selection import KFold
folder=KFold(n_splits=10,shuffle=True,random_state=50)
#from sklearn.preprocessing import MinMaxScaler
#scaler = MinMaxScaler( )
def target_fuc(theta,X,Y):
predict = np.dot(X,theta)
metric1 = predict - Y.reshape(Y.shape[0],1)
j_theta = 0.5*np.dot(metric1.T,metric1)[0][0]
return j_theta
def batch_gredient_descent(theta,alpha,dataset,target,epsilon,m,max_literation):
error0 = 0 # 记录前一个损失函数
#print('theta{}正在处理'.format(j))
for i in range(max_literation):
print('第{}次迭代'.format(i))
delta = np.dot(dataset, theta).reshape(m, 1) - target.reshape(m, 1) # 预测值和真实值的差
deri = alpha * np.dot(delta.T,dataset)
theta = theta - deri.T
error1 = target_fuc(theta=theta,X=dataset,Y=target)
print(error1)
if abs(error1-error0)<=epsilon:
break
else:
error0 = error1
return theta
boston = datasets.load_boston()
data = pd.DataFrame(boston.data, columns=boston.feature_names)
feature = data.columns.tolist()
data = np.array(data)
#scaler.fit(data)
#data=scaler.transform(data)
m,n = data.shape #m个训练样本,n维特征
I = np.vstack(np.ones((m, 1)))
data = np.hstack((I, data))
y = boston.target
print(data.shape)
for train_index,test_index in folder.split(data):
x_train = data[train_index, :].astype('float64')
x_test= data[test_index, :].astype('float64')
y_train = y[train_index].astype('float64')
y_test = y[test_index].astype('float64')
theta = np.array([0] * (n+1)).reshape(n+1,1)
final_theta = batch_gredient_descent(theta=theta,alpha=0.01,dataset=x_train,target=y_train,epsilon=1e-6 ,m=x_train.shape[0],max_literation=1000)