博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
机器学习之线性回归学习,梯度学习算法(利用梯度函数确定系数)
阅读量:3939 次
发布时间:2019-05-24

本文共 4075 字,大约阅读时间需要 13 分钟。

#利用Sklearn来确定theta值做简单的二维线性回归的预测

import numpy as npfrom sklearn.linear_model import LinearRegression#模拟生成100行1列的数据x = 2*np.random.rand(100,1)y = 4 + 3*x + np.random.randn(100,1)#实例化lin_reg = LinearRegression()#fit方法就是训练模型的方法lin_reg.fit(x,y)# intercept指的是相当于截距, coef是参数print(lin_reg.intercept_,lin_reg.coef_)x_new = np.array([[0],[2]])print(lin_reg.predict(x_new))

通过将梯度损失函数降到最小来确定theta值(自变量只有一个)—批梯度下降法

# 批梯度下降法来进行参数的调试,利用的是将损失函数降到最小来确定theta值import numpy as np#模拟100行1列的x数据,X = 2 * np.random.rand(100, 1)y = 4 + 3 * X + np.random.randn(100, 1)#这一步是生成100*2的矩阵,主要是为了后面的计算:y = theta0*x0 + theta1*x1 (x0=1)X_b = np.c_[np.ones((100, 1)), X]# 表达式中的阿尔法值,即学习率learning_rate = 0.1# 通常在做机器学习的时候,一般不会等到他收敛,因为太浪费时间,所以会设置一个收敛次数n_iterations = 100000# 表示训练集的总数m = 100# 1.初始化theta值,theta0和theta1theta = np.random.randn(2, 1)# 4. 不会设置阈值,之间设置超参数,迭代次数,迭代次数到了,我们就认为收敛了for iteration in range(n_iterations):    # 2. 接着求梯度gradient    gradients = 1/m * X_b.T.dot(X_b.dot(theta)-y)    # 3. 应用公式调整theta值, theta_t + 1 = theta_t - grad * learning_rate    # 同步更新    theta = theta - learning_rate * gradientsprint(theta)

通过将梯度损失函数降到最小来确定theta值(多个自变量)—批梯度下降法

import numpy as npX1 = 2 * np.random.rand(100, 1)X2 = 4 * np.random.rand(100, 1)X3 = 6 * np.random.rand(100, 1)y = 4 + 3 * X1 + 4 * X2 + 5 * X3 + np.random.randn(100, 1)X_b = np.c_[np.ones((100, 1)), X1, X2, X3]print(X_b)learning_rate = 0.1# 通常在做机器学习的时候,一般不会等到他收敛,因为太浪费时间,所以会设置一个收敛次数n_iterations = 1000#训练集的总数m = 100# 1.初始化theta0,theta1,theta2,theta3theta = np.random.randn(4, 1)count = 0# 4. 不会设置阈值,之间设置超参数,迭代次数,迭代次数到了,我们就认为收敛了for iteration in range(n_iterations):    count += 1    # 2. 接着求梯度gradient    gradients = 1/m * X_b.T.dot(X_b.dot(theta)-y)    # 3. 应用公式调整theta值, theta_t + 1 = theta_t - grad * learning_rate    theta = theta - learning_rate * gradientsprint(count)print(theta)

梯度下降的公式

在这里插入图片描述

应用随机梯度下降进行线性回归的训练

import numpy as npimport randomX = 2 * np.random.rand(100, 1)y = 4 + 3 * X + np.random.randn(100, 1)X_b = np.c_[np.ones((100, 1)), X]n_epochs = 100a0 = 0.1#指的是a0的衰减率,decay_rate = 1m = 1num = [i for i in range(100)]def learning_schedule(epoch_num):    return (1.0 / (decay_rate * epoch_num + 1)) * a0theta = np.random.randn(2, 1)# epoch 是轮次的意思,意思是用m个样本做一轮迭代for epoch in range(n_epochs):    # 生成100个不重复的随机数    rand = random.sample(num, 100)    for i in range(m):        random_index = rand[i]        #通过每遍历一次(也就是每下降一步),计算一次theta值,然后输出最终的较为准确的值        xi = X_b[random_index:random_index+1]        # print(xi)        yi = y[random_index:random_index+1]        # print(yi)        gradients = xi.T.dot(xi.dot(theta)-yi)        learning_rate = learning_schedule(epoch + i)        theta = theta - learning_rate * gradientsprint(theta)

批梯度下降法

import numpy as npX1 = 2 * np.random.rand(100, 1)X2 = 4 * np.random.rand(100, 1)X3 = 6 * np.random.rand(100, 1)y = 4 + 3 * X1 + 4 * X2 + 5 * X3 + np.random.randn(100, 1)X_b = np.c_[np.ones((100, 1)), X1, X2, X3]print(X_b)learning_rate = 0.1# 通常在做机器学习的时候,一般不会等到他收敛,因为太浪费时间,所以会设置一个收敛次数n_iterations = 1000#训练集的总数m = 100# 1.初始化theta0,theta1,theta2,theta3theta = np.random.randn(4, 1)# 4. 不会设置阈值,之间设置超参数,迭代次数,迭代次数到了,我们就认为收敛了for iteration in range(n_iterations):    # 2. 接着求梯度gradient    gradients = 1/m * X_b.T.dot(X_b.dot(theta)-y)    # 3. 应用公式调整theta值, theta_t + 1 = theta_t - grad * learning_rate    theta = theta - learning_rate * gradientsprint(theta)

min_batch梯度下降

import numpy as npimport randomX = 2 * np.random.rand(100, 1)y = 4 + 3 * X + np.random.randn(100, 1)X_b = np.c_[np.ones((100, 1)), X]n_epochs = 500t0, t1 = 5, 50m = 100num = [i for i in range(100)]def learning_schedule(t):    return float(t0) / (t + t1)theta = np.random.randn(2, 1)batch_num = 5batch_size = int(m / 5)# epoch 是轮次的意思,意思是用(i+1)*batch_size-i*batch_size个样本做一轮迭代,一共迭代n_epochs代for epoch in range(n_epochs):    for i in range(batch_num):        start = i*batch_size        end = (i+1)*batch_size        xi = X_b[start:end]        yi = y[start:end]        gradients = 1.0/batch_size * xi.T.dot(xi.dot(theta)-yi)        learning_rate = learning_schedule(epoch*m + i)        theta = theta - learning_rate * gradientsprint(theta)

转载地址:http://umbwi.baihongyu.com/

你可能感兴趣的文章
文章中运用到的数学公式
查看>>
Projective Dynamics: Fusing Constraint Projections for Fast Simulation
查看>>
从2D恢复出3D的数据
查看>>
glm 中 数据类型 与 原始数据(c++ 数组)之间的转换
查看>>
Derivatives of scalars, vector functions and matrices
查看>>
the jacobian matrix and the gradient matrix
查看>>
VS2010 将背景设为保护色
查看>>
ubutun里面用命令行安装软件
查看>>
ubuntu 常用命令
查看>>
SQLite Tutorial 4 : How to export SQLite file into CSV or Excel file
查看>>
Optimizate objective function in matrix
查看>>
Convert polygon faces to triangles or quadrangles
查看>>
read obj in matlab
查看>>
find out the neighbour matrix of a mesh
查看>>
Operators and special characters in matlab
查看>>
As-Conformal-As-Possible Surface Registration
查看>>
qmake Variable Reference
查看>>
Lesson 2 Gradient Desent
查看>>
find border vertex
查看>>
matlab sliced variable
查看>>