机器学习(8)-- 非线性回归

import numpy as np
import random


def genData(pointCont, bias, variance):
    """
    x是多个二维的点,沿着y=x+b直线附近分布,b为bias,
    variance为y的基础偏差,总偏差为基础偏差+随机偏差
    :param pointCont: 生成的点的数量
    :param bias: 结果的偏差
    :param variance:
    :return: x:平面上的一系列点,y是对应点的标志
    """
    x = np.zeros(shape=(pointCont, 2))
    y = np.zeros(shape=(pointCont))
    for i in range(0, pointCont):
        x[i][0] = 1
        x[i][1] = i
        y[i] = (i + bias) + random.uniform(0, 1) + variance
    return x, y


def gradientDescent(x, y, theta, alpha, itemsCont, iters):
    """
    min cost :cost = sum(loss**2)/2m
                    = sum((h-y)**2)/2m
                    = sum (x*theta - y)**2/2m
            梯度:D(cost) = sum 2*(x*theta - y) * theta/2m
                        = sum 2*loss * theta/2m
                        = sum loss*theta/m
    :param x:
    :param y:
    :param theta: 初始权重参数
    :param alpha: 学习率
    :param itemsCont: 数据集大小
    :param iters: 迭代次数
    :return: 新的权重
    """
    xTran = np.transpose(x)
    for i in range(iters):
        hypothesis = np.dot(x, theta)   #预测值
        loss = hypothesis - y      #偏差
        cost = np.sum(loss**2)/(2*itemsCont)  #损失函数可以自行设置,这只是最简单的
        gradient = np.dot(xTran, loss)/itemsCont
        theta = theta - alpha*gradient
    return theta


x, y = genData(100,25,10)
print(x, y)
theta = np.ones(2)
theta = gradientDescent(x, y, theta, 0.0005, 100, 10000)
print(theta)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值