機器學習(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)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章