[Python] gradient descent machine learning

Discussão em 'Python' iniciado por Stack, Julho 25, 2025.

  1. Stack

    Stack Membro Participativo

    I'm attending cousera class by Andrew Ng and I saw gradient descent with multiple variable. I'm trying to run gradient descent in python.These are my code in python

    import sys
    import matplotlib.pyplot as plt

    def gradient_descent(y_actual, alpha, iterations, m_current, b_current, x_dataset):
    if len(y_actual) != len(x_dataset):
    print(f"Dofferent lenght of input which is {len(y_actual)} and {len(x_dataset)}")
    sys.exit(1)
    else:
    pass
    m_current = m_current
    b_current = b_current
    m_derivative = 0
    b_derivative = 0
    new_m = []
    new_b = []
    itr = []
    for z in range(iterations):
    for i in range(len(x_dataset)):
    m_derivative += (y_actual - ((m_current * x_dataset)+b_current))*x_dataset
    b_derivative += (y_actual - ((m_current * x_dataset) + b_current))
    itr.append(z)
    m_current = m_current - (alpha*(m_derivative/len(x_dataset)))
    b_current = b_current - (alpha * (b_derivative / len(x_dataset)))
    new_m.append(m_current)
    new_b.append(b_current)
    m_derivative = 0
    b_derivative = 0
    return m_current, b_current, itr, new_m, new_b


    m, b, iteration, m_history, b_history = gradient_descent([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 0.0001, 1000, 0, 0, [5, 8, 1, 4, 9, 3, 9, 8, 3, 5])

    plt.subplot(121)
    plt.plot(iteration, m_history)
    plt.subplot(122)
    plt.plot(iteration,b_history)
    plt.show()


    I plot the iteration and relationship between the iteration and the decrease in b which is the intercept and relationship between the iteration and the decrease in m which is the coefficient. This is the graph that I get:

    [​IMG]

    But the problem is that I saw a lot of graphs about gradient descent in Google like this:

    [​IMG]

    What is the problem of my code that give me this result?

    Continue reading...

Compartilhe esta Página