梯度下降法 python实现

蓝咒 提交于 2020-04-08 10:59:05

 

 

import numpy as np
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScalerclass LinearRegression:
    def __init__(self):
        self.coeff_ = None
        self.interception_ = None
        self._theta = None

    def fit_gd(self, x_train, y_train,eta=0.01,n_iters=1e4):
        assert x_train.shape[0] == y_train.shape[0],'特征维度应当相等'
        def J(theta, X_b, y):
            try:
                return np.sum(y - X_b.dot(theta)) / len(y)
            except:
                return float('inf')

        def dJ(theta, X_b, y):
            return 2 * (X_b.dot(theta) - y).dot(X_b) / len(y)

        def gradient_descent(X_b, y, initial_theta, eta, n_iters=1e4, epsilon=1e-8):
            theta = initial_theta
            n_iter = 0
            while n_iter < n_iters:
                gradient = dJ(theta, X_b, y)
                last_theta =theta
                theta = theta - eta * gradient
                if abs(J(last_theta, X_b,y) - J(theta, X_b, y)) < epsilon:
                    break
                n_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)
        self.interception_ = self._theta[0]
        self.coeff_ = self._theta[1:]
        return self

    def fit_sgd(self,x_train,y_train,n_iters=5,t0=5,t1=50):


        def dJ_sgd(theta, X_b_i, y_i):
            pass
        def sgd(X_b, y, initial_theta, n_iters, t0 = 5,t1 = 50):
           pass


    def predict(self, x_predict):
        pass





if __name__ == '__main__':
    # 使用梯度下降法进行训练
    np.random.seed(666)  # 设置随机种子
    x = 2 * np.random.random(size=100)
    y = x * 3. + 4. + np.random.normal(size=100)
    X = x.reshape(-1, 1)
    reg2 = LinearRegression()
    reg2.fit_gd(X, y)
    print('梯度下降法训练结果')
    print(reg2.interception_)
    print(reg2.coeff_)

    # 使用梯度下降法进行训练给定模型
    boston = datasets.load_boston()
    x = boston.data
    y = boston.target
    x = x[y < 50]
    y = y[y < 50]
    x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3, random_state=42)
    reg3 = LinearRegression()
    # 使用梯度下降法之前最好对数据进行归一化,不然很容易不收敛,得到[nan nan nan nan nan nan nan nan nan nan nan nan nan]
    standardScaler = StandardScaler()
    standardScaler.fit(x_train)
    x_train_standard = standardScaler.transform(x_train)
    reg3.fit_gd(x_train_standard, y_train)
    print('梯度下降法训练给定数据集结果')
    x_test_standard = standardScaler.transform(x_test)
    print(reg3.interception_)
    print(reg3.coeff_)

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!