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

我正在尝试使用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,theta.T)-y)))
            theta=theta-t
            theta_Hist.append(theta)
            print("after\n",theta)
            cost[i]=computeCost(x,theta)
    gradientDescent(popxx,profy)

theta的值应为-3.24140214,1.1272942

gush5051 回答:需要帮助了解python中的矩阵函数乘法

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

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,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,theta)

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

init_thetas = np.zeros(shape=(2,1))
thetas = gradientDescent(X,init_thetas)
本文链接:https://www.f2er.com/3126702.html

大家都在问