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))
Exemple #2
0
def dot(x, y):
    """
    Computes the dot product of two non-empty numpy.ndarray, using a
    for-loop. The two arrays must have the same dimensions.
    Args:
    x: has to be an numpy.ndarray, a vector.
    y: has to be an numpy.ndarray, a vector.
    Returns:
    The dot product of the two vectors as a float.
    None if x or y are empty numpy.ndarray.
    None if x and y does not share the same dimensions.
    Raises:
    This function should not raise any Exception.
    """
    if (not s.elements(x) or not s.elements(y) or len(x) != len(y)):
        return (None)
    ss = s.sum_(np.array([i * j for i, j in zip(x, y)]), lambda X: X)
    if (ss != None):
        return (ss)
    return (0)
Exemple #3
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)
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)
Exemple #5
0
def mse(y, y_hat):
    """
    Computes the mean squared error of two non-empty numpy.ndarray, using
    a 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)):
        return (None)
    f = s.sum_(np.array(list((x - y)**2 for x, y in zip(y, y_hat))),
               lambda l: l)
    if (f != None):
        return (f / len(y))
    return (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))
Exemple #7
0
def mean(x):
    """
    Computes the mean of a non-empty numpy.ndarray, using a for-loop.
    Args:
    x: has to be an numpy.ndarray, a vector.
    Returns:
    The mean as a float.
    None if x is an empty numpy.ndarray.
    Raises:
    This function should not raise any Exception.
    """
    if (not s.elements(x)):
        return (None)
    return (s.sum_(x, lambda X: X) / len(x))
Exemple #8
0
def std(x):
    """
    Computes the standard deviation of a non-empty numpy.ndarray, using a
    for-loop.
    Args:
    x: has to be an numpy.ndarray, a vector.
    Returns:
    The standard deviation as a float.
    None if x is an empty numpy.ndarray.
    Raises:
    This function should not raise any Exception.
    """
    if (not s.elements(x)):
        return (None)
    return (np.sqrt(v.variance(x)))
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)