def fit_gd(self, X_train, y_train, eta=0.01, n_iters=1e4):
assert X_train.shape[0] == y_train.shape[0], \
"the size of X_train must be equal to the size of y_train"
def J(theta, X_b, y):
try:
return np.sum((y - X_b.dot(theta)) ** 2) / len(y)
except:
return float('inf')
def dJ(theta, X_b, y):
return X_b.T.dot(X_b.dot(theta) - y) * 2. / len(y)
def gradient_descent(X_b, y, initial_theta, eta, n_iters=1e4, epsilon=1e-8):
theta = initial_theta
cur_iter = 0
while cur_iter < n_iters:
gradient = dJ(theta, X_b, y)
last_theta = theta
theta = theta - eta * gradient
if (abs(J(theta, X_b, y) - J(last_theta, X_b, y)) < epsilon):
break
cur_iter += 1
return theta
X_b = np.hstack([np.ones((len(X_train), 1)), X_train])
initial_theta = np.zeros(X_b.shape[1])
self._theta = gradient_descent(X_b, y_train, initial_theta, eta, n_iters)
self.intercept_ = self._theta[0]
self.coef_ = self._theta[1:]
return self
import numpy as np
from sklearn import datasets
boston = datasets.load_boston()
X = boston.data
y = boston.target
X = X[y < 50.0]
y = y[y < 50.0]
from myAlgorithm.LinearRegression import LinearRegression
from myAlgorithm.model_selection import train_test_split
X_train,
X_test,
y_train,
y_test = train_test_split(X, y, seed=666)
lin_reg1 = LinearRegression()%time
lin_reg1.fit_normal(X_train, y_train)
lin_reg1.score(X_test, y_test)
lin_reg2 = LinearRegression()
lin_reg2.fit_gd(X_train, y_train, )
lin_reg2.coef_
lin_reg2.fit_gd(X_train, y_train, eta=0.000001)lin_reg2.score(X_test, y_test)"""输出:0.27556634853389195"""
from sklearn.preprocessing import StandardScalerstandardScaler = StandardScaler()standardScaler.fit(X_train)X_train_std = standardScaler.transform(X_train)lin_reg3 = LinearRegression()lin_reg3.fit_gd(X_train_std, y_train)X_test_std = standardScaler.transform(X_test)lin_reg2.score(X_test, y_test)"""输出:0.8129802602658466"""