梯度下降算法提高valueError

时间:2019-01-31 11:13:26

标签: machine-learning linear-regression gradient-descent

我有这些梯度下降算法可用于多元回归,但它提出了一个

ValueError: operands could not be broadcast together with shapes (3,) (3,140).

我检查了关于stackoverflow上的广播错误的其他答案,并且该文档指出矩阵的尺寸必须相同或任一矩阵都必须为1。但是如何使我的theta的尺寸相同。

请不标记复制它。

我的x的亮度为(140,3),y的亮度为(140,1),alpha = 0.0001

def find_mse(x,y,theta):
    return np.sum(np.square(np.matmul(x,theta)-y))*1/len(x)       



def gradientDescent(x,y,theta,alpha,iteration):
    theta=np.zeros(x.shape[1])
    m=len(x)
    gradient_df=pd.DataFrame(columns=['coeffs','mse'])

    for i in range(iteration):
        gradient = (1/m) * np.matmul(x.T, np.matmul(x, theta) - y)
        theta = np.mat(theta) - alpha * gradient
        cost = compute_cost(X, y, theta)
        gradient_df.loc[i] = [theta,cost]

    return gradient_df   

1 个答案:

答案 0 :(得分:0)

您正在将 public bool CheckDuplicate3() { DbConnection connection = null; try { connection = GetFactory().CreateConnection(); if (connection != null) { connection.ConnectionString = "user id=XXXX;password=XXXX;data source=XXXX"; connection.Open(); using (DbCommand command = connection.CreateCommand()) { command.CommandText = "mca_test_package.checkDuplicate"; command.CommandType = CommandType.StoredProcedure; command.AddParameter("o_result", DbType.Decimal, 0, ParameterDirection.ReturnValue); command.AddParameter("i_vendor", DbType.String, tx.Vendor); command.AddParameter("i_transactionnumber", DbType.String, tx.TransactionNumber.Trim()); command.AddParameter("i_txId", DbType.Binary, tx.Id.ToByteArray(), ParameterDirection.Input, 16); command.ExecuteNonQuery(); var result = Convert.ToInt32(command.Parameters["o_result"].Value); if (result == 1) { tx.status = "Success"; Console.WriteLine("No Duplicate {0}", tx); } else { Console.WriteLine("Duplicate {0}", tx); tx.status = "RejectedDuplicate"; } } using (DbCommand command = connection.CreateCommand()) { command.CommandType = CommandType.Text; command.CommandText = "update test_tx_log tx set tx.status = :status where id = :id"; command.AddParameter("status", DbType.String, tx.status); //command.AddParameter("id", DbType.Decimal, tx.Id); command.AddParameter("id", DbType.Binary, tx.Id.ToByteArray()); command.ExecuteNonQuery(); } } return true; } finally { if (connection != null) connection.Close(); } } 与形状x乘以(140, 3)以产生应具有形状theta的输出。为此,您的(140, 1)的形状应为theta。您需要按照以下步骤初始化(3, 1)

theta