需要帮助了解python中的矩阵函数乘法

时间:2019-11-11 03:15:59

标签: python-3.x matrix linear-regression matrix-multiplication gradient-descent

我正在尝试使用Python从Andrew Ng机器学习课程中找到优化的theta值以最小化成本函数。我的程序中的theta值不正确。我在这里看不懂什么?我正在尝试在这里学习...抱歉菜鸟问题

#Shapes of numpy arrays
#popx=(97,1)
#popxx=(97,2)#x in function
#profy=(97,1)#y in function

def gradientDescent(x,y,iterations=1500,alpha=0.01,theta=np.zeros(2).reshape(1,2)):
        cost=np.zeros(iterations)
        theta_Hist=[]
        m=len(profy)
        for i in range(iterations):
            print("Before Loop\n",theta)
            t=(alpha/m)*np.dot(popx.T,(np.dot(x,theta.T)-y))
            print(np.dot(popx.T,(np.dot(x,theta.T)-y)))
            theta=theta-t
            theta_Hist.append(theta)
            print("after\n",theta)
            cost[i]=computeCost(x,y,theta)
    gradientDescent(popxx,profy)

theta的值应为-3.24140214,1.1272942

1 个答案:

答案 0 :(得分:0)

您的代码混乱,请尝试避免不必要的嵌套操作。试试这个

import numpy as np

def computeCost(X,y,theta):
    m = len(y)
    predictions = np.dot(X,theta)
    squared_error = (predictions-y)**2
    cost = np.sum(squared_error)/(2*m)
    return cost


def gradientDescent(x,y,theta,iterations=1500,alpha=0.01):
    cost_Hist=[]
    theta_Hist=[]
    n=len(y)
    for i in range(iterations):
        prediction = np.dot(x,theta)
        error = prediction - y
        grad = np.dot(x.T,error)
        theta = theta - (alpha/n)*grad
        co = computeCost(x,y,theta)

        theta_Hist.append(theta)
        cost_Hist.append(co)
    return theta

init_thetas = np.zeros(shape=(2,1))
thetas = gradientDescent(X,y,init_thetas)