コード例 #1
0
def gradient(x, y, theta):
    """
    Computes a gradient vector from three non-empty numpy.ndarray, using
    a for-loop. The two arrays must have the compatible dimensions.
    Args:
    x: has to be an numpy.ndarray, a matrice of dimension m * n.
    y: has to be an numpy.ndarray, a vector of dimension m * 1.
    theta: has to be an numpy.ndarray, a vector n * 1.
    Returns:
    The gradient as a numpy.ndarray, a vector of dimensions n * 1.
    None if x, y, or theta are empty numpy.ndarray.
    None if x, y and theta do not have compatible dimensions.
    Raises:
    This function should not raise any Exception.
    """
    if (not s.elements(x) or not s.elements(y) or not s.elements(theta)):
        return (None)
    if (len(list(filter(lambda l: len(l) == len(theta), x))) != len(x)):
        return (None)
    l = []
    for xj in range(len(x[0])):
        ss = s.sum_(
            np.array([(d.dot(theta, i) - j) * i[xj] for i, j in zip(x, y)]),
            lambda l: l)
        if (ss == None):
            return (None)
        l.append(ss / len(y))
    return (np.array(l))
コード例 #2
0
def vec_mse(y, y_hat):
    """
    Computes the mean squared error of two non-empty numpy.ndarray,
    without any for loop. The two arrays must have the same dimensions.
    Args:
    y: has to be an numpy.ndarray, a vector.
    y_hat: has to be an numpy.ndarray, a vector.
    Returns:
    The mean squared error of the two vectors as a float.
    None if y or y_hat are empty numpy.ndarray.
    None if y and y_hat does not share the same dimensions.
    Raises:
    This function should not raise any Exception.
    """
    if (not s.elements(y) or not s.elements(y_hat) or len(y) != len(y_hat)):
        return (None)
    dt = d.dot(y_hat - y, y_hat - y)
    if (dt != None):
        return (dt / len(y))
    return (0)
コード例 #3
0
def mat_vec_prod(x, y):
    """
    Computes the product of two non-empty numpy.ndarray, using a
    for-loop. The two arrays must have compatible dimensions.
    Args:
    x: has to be an numpy.ndarray, a matrix of dimension m * n.
    y: has to be an numpy.ndarray, a vector of dimension n * 1.
    Returns:
    The product of the matrix and the vector as a vector of dimension m *
    1.
    None if x or y are empty numpy.ndarray.
    None if x and y does not share compatibles dimensions.
    Raises:
    This function should not raise any Exception.
    """
    if (not s.elements(x) or not s.elements(y)):
        return (None)
    if (len(list(filter(lambda l: len(l) == len(y), x))) == len(x)):
        return (np.array(list(map(lambda l: d.dot(l, y), x))))
    return (None)
コード例 #4
0
def mat_mat_prod(x, y):
    """
    Computes the product of two non-empty numpy.ndarray,
    for-loop. The two arrays must have compatible dimensions.
    Args:
    x: has to be an numpy.ndarray, a matrix of dimension m
    y: has to be an numpy.ndarray, a vector of dimension n
    Returns:
    The product of the matrices as a matrix of dimension m
    None if x or y are empty numpy.ndarray.
    None if x and y does not share compatibles dimensions.
    Raises:
    This function should not raise any Exception.
    """
    if (not s.elements(x) or not s.elements(y)):
        return (None)
    r = np.rot90(y[::-1], 3)
    res = []
    for row in x:
        res.append([d.dot(row, i) for i in r])
    return (np.array(res))
コード例 #5
0
def linear_mse(x, y, theta):
    """
    Computes the mean squared error of three non-empty numpy.ndarray,
    using a for-loop. The three arrays must have compatible dimensions.
    Args:
    y: has to be an numpy.ndarray, a vector of dimension m * 1.
    x: has to be an numpy.ndarray, a matrix of dimesion m * n.
    theta: has to be an numpy.ndarray, a vector of dimension n * 1.
    Returns:
    The mean squared error as a float.
    None if y, x, or theta are empty numpy.ndarray.
    None if y, x or theta does not share compatibles dimensions.
    Raises:
    This function should not raise any Exception.
    """
    if (not s.elements(x) or not s.elements(y) or not s.elements(theta)):
        return (None)
    if (len(list(filter(lambda l: len(l) == len(theta), x))) != len(x)):
        return (None)
    ss = s.sum_(np.array([(d.dot(theta, i) - j)**2 for i, j in zip(X, Y)]),
                lambda l: l)
    if (ss != None):
        return (ss / len(Y))
    return (0)