def least_squares_gradient(y, tx, w):
    """
    Compute the gradient of the mean square error with respect to w, and the current error vector e.
    Takes as input the targeted y, the sample matrix w and the feature vector w. 
    This function is used when solving gradient based method, such that least_squares_GD() and least_squares_SGD().
    """  
    e = y - tx.dot(w)
    grad = -tx.T.dot(e) / len(e)
    return grad, e