def cost_elem_(y, y_hat): if len(y_hat.shape) == 1: y_hat = add_intercept(y_hat) if len(y.shape) == 1: y = add_intercept(y) m = y.shape[0] return (1 / (2 * m)) * np.sum((y_hat - y)**2, axis=1)
def test(): # Example 1: x = np.arange(1, 6) assert (add_intercept(x) == np.array([[1., 1.], [1., 2.], [1., 3.], [1., 4.], [1., 5.]])).all() # Example 2: y = np.arange(1, 10).reshape((3, 3)) assert (add_intercept(y) == np.array([[1., 1., 2., 3.], [1., 4., 5., 6.], [1., 7., 8., 9.]])).all()
def gradient(x, y, theta): """Computes a gradient vector from three non-empty numpy.ndarray, without any for-loop. The ,→ three arrays must have compatible dimensions. Args: x: has to be an numpy.ndarray, a vector of dimension m * 1. y: has to be an numpy.ndarray, a vector of dimension m * 1. 11 theta: has to be an numpy.ndarray, a 2 * 1 vector. Returns: The gradient as a numpy.ndarray, a vector of dimension 2 * 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. """ # try: x_bias = add_intercept(x) y_pred = np.dot(x_bias, theta) y_pred = y_pred.reshape(len(y), 1) y = y.reshape(len(y), 1) error = y_pred - y # print(y_pred) # print(y) # print(error) error_columns = error.reshape(len(y), 1) error_dot_x = np.dot(error_columns.T, x_bias) grad = 1 / len(x) * error_dot_x return grad.reshape(len(theta), )
def fit_(x, y, theta, alpha, n_cycles): """ Description: Fits the model to the training dataset contained in x and y. Args: x: has to be a numpy.ndarray, a matrix of dimension m * n: (number of training examples, number of features). y: has to be a numpy.ndarray, a vector of dimension m * 1: (number of training examples, 1). theta: has to be a numpy.ndarray, a vector of dimension (n + 1) * 1: (number of features + 1, 1). alpha: has to be a float, the learning rate n_cycles: has to be an int, the number of iterations done during the gradient descent Returns: new_theta: numpy.ndarray, a vector of dimension (number of features + 1, 1). None if there is a matching dimension problem. Raises: This function should not raise any Exception. """ theta = theta.reshape((-1, 1)) x_ = add_intercept(x) m = x_.shape[0] for i in range(n_cycles): theta -= alpha / m * (x_.T @ (x_ @ theta - y)) return theta
def vec_gradient(x, y, theta): """Computes a gradient vector from three non-empty numpy.ndarray, without any for-loop. ,→ The three 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 of dimension (n, 1). Returns: The gradient as a numpy.ndarray, a vector of dimensions (n, 1), containg the result of ,→ the formula for all j. 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. """ x_ = add_intercept(x) m = x_.shape[0] n = x_.shape[1] if y.shape[0] != x.shape[0] or theta.shape[0] != x.shape[1] + 1: print("X shape: ", x.shape) print("Y shape: ", y.shape) print("T shape: ", theta.shape) return None elem = np.matmul(x_, theta) - y answer = np.matmul(x.T, elem) / m return answer
def vec_reg_linear_grad(y, x, theta, lambda_): """Computes the regularized linear gradient of three non-empty numpy.ndarray, without any ,→ for-loop. The three arrays must have compatible dimensions. Args: y: has to be a numpy.ndarray, a vector of dimension m * 1. x: has to be a numpy.ndarray, a matrix of dimesion m * n. theta: has to be a numpy.ndarray, a vector of dimension n * 1. lambda_: has to be a float. Returns: A numpy.ndarray, a vector of dimension n * 1, containing the results of the formula for all ,→ j. 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 x.size == 0 or y.size == 0 or theta.size == 0 or x.shape[0] != y.shape[ 0] or x is None or y is None: return None y_hat = predict_(x, theta) theta2 = np.copy(theta) theta2[0] = 0 gr_vec = (np.matmul(np.transpose(add_intercept(x)), (y_hat - y)) + (lambda_ * theta2)) / y.shape[0] return gr_vec
def predict_(x, theta): """computes the vector of prediction y_hat from two non-empty numpy.ndarray using h(x) = th0 + th1(x), returning a 1d vector containing all y_hat values""" #adds column of 1s to right side of x value vector x = add_intercept(x) #number of theta values must match number of features (n in xn) if theta.shape != (x.shape[1], ): return None return np.dot(x, theta)
def predict_(x, theta): """Computes the vector of prediction y_hat from two non-empty numpy.ndarray. Args: x: has to be an numpy.ndarray, a vector of dimension m * 1. theta: has to be an numpy.ndarray, a vector of dimension 2 * 1. Returns: y_hat as a numpy.ndarray, a vector of dimension m * 1. None if x or theta are empty numpy.ndarray. None if x or theta dimensions are not appropriate. Raises: This function should not raise any Exceptions. """ if len(x) < 1 or theta.shape != (2, ): return None return np.sum(add_intercept(x) * theta, axis=1)
def logistic_predict(x, theta): """Computes the vector of prediction y_hat from two non-empty numpy.ndarray. Args: x: has to be an numpy.ndarray, a vector of dimension m * n. theta: has to be an numpy.ndarray, a vector of dimension (n + 1) * 1. Returns: y_hat as a numpy.ndarray, a vector of dimension m * 1. None if x or theta are empty numpy.ndarray. None if x or theta dimensions are not appropriate. Raises: This function should not raise any Exception. """ if len(x) < 1 or len(theta) < 1 or x is None or theta is None: return None return sigmoid_(np.matmul(add_intercept(x), theta))
def predict_(x, theta): """Computes the vector of prediction y_hat from two non-empty numpy.ndarray. Args: x: has to be an numpy.ndarray, a vector of dimension m * 1. theta: has to be an numpy.ndarray, a vector of dimension 2 * 1. Returns: y_hat as a numpy.ndarray, a vector of dimension m * 1. None if x or theta are empty numpy.ndarray. None if x or theta dimensions are not appropriate. Raises: This function should not raise any Exception. """ try: return np.dot(add_intercept(x), theta) except: return np.nan
def predict_(x, theta): """Computes the prediction vector y_hat from two non-empty numpy.ndarray. Args: x: has to be an numpy.ndarray, a vector of dimensions m * 1. theta: has to be an numpy.ndarray, a vector of dimension 2 * 1. Returns: y_hat as a numpy.ndarray, a vector of dimension m * 1. None if x or theta are empty numpy.ndarray. None if x or theta dimensions are not appropriate. Raises: This function should not raise any Exception. """ if len(x) == 0: return None x = add_intercept(x) if len(theta) != x.shape[1]: return None return x.dot(theta)
def gradient(x, y, theta): """Computes a gradient vector from three non-empty numpy.ndarray, without any for-loop. ,→ The three arrays must have compatible dimensions. Args: x: has to be an numpy.ndarray, a vector of dimension m * 1. y: has to be an numpy.ndarray, a vector of dimension m * 1. theta: has to be an numpy.ndarray, a 2 * 1 vector. Returns: The gradient as a numpy.ndarray, a vector of dimension 2 * 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. """ x_ = add_intercept(x) m = x_.shape[0] # j = (1/m) * (x_.T.dot(x_.dot(theta) - y)) j = (1 / m) * (x_.T @ (x_ @ theta - y)) # j = ((predict_(x, theta) - y).dot(add_intercept(x))) / m return j
def predict_(x, theta): """Computes the vector of prediction y_hat from two non-empty np.ndarray. Args: x: has to be an np.ndarray, a vector of dimension m * 1. theta: has to be an np.ndarray, a vector of dimension 2 * 1. Returns: y_hat as a np.ndarray, a vector of dimension m * 1. None if x or theta are empty np.ndarray. None if x or theta dimensions are not appropriate. Raises: This function should not raise any Exceptions. """ if (not isinstance(x, np.ndarray) or not isinstance(theta, np.ndarray) or len(x) == 0): return None else: x = add_intercept(x) # print(x) # print(theta) return x.dot(theta)
def vec_gradient(x, y, theta): """Computes a gradient vector from three non-empty numpy.ndarray, without any for-loop. The → three arrays must have the 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 m * 1. theta: has to be an numpy.ndarray, a vector (n +1) * 1. Returns: The gradient as a numpy.ndarray, a vector of dimensions n * 1, containg the result of the → formula for all j. 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 len(x) < 1 or len(y) < 1 or len(theta) < 1 or x is None or y is None or theta is None or x.shape[0] != y.shape[0]: return None y_hat = predict_(x, theta) gr_vec = (np.matmul(add_intercept(x).transpose(), (y_hat - y))) / y.shape[0] return gr_vec
def gradient(x, y, theta): """Computes a gradient vector from three non-empty numpy.ndarray, without any for loop. The ,→ three arrays must have compatible dimensions. Args: x: has to be a numpy.ndarray, a matrix of dimension m * 1. y: has to be a numpy.ndarray, a vector of dimension m * 1. theta: has to be a numpy.ndarray, a 2 * 1 vector. Returns: The gradient as a numpy.ndarray, a vector of dimension 2 * 1. None if x, y, or theta is an empty numpy.ndarray. None if x, y and theta do not have compatible dimensions. Raises: This function should not raise any Exception. """ if len(x) < 1 or len(y) < 1 or len( theta) < 1 or x.shape != y.shape or theta.shape[ 0] < 1 or x is None or y is None: return None y_hat = predict_(x, theta) gr_vec = (np.matmul(np.transpose(add_intercept(x)), (y_hat - y))) / y.shape[0] return gr_vec
def predict_(x, theta): """Computes the vector of prediction y_hat from two non-empty numpy.ndarray. Args: x: has to be an numpy.ndarray, a vector of dimension m * 1. theta: has to be an numpy.ndarray, a vector of dimension 2 * 1. Returns: y_hat as a numpy.ndarray, a vector of dimension m * 1. None if x or theta are empty numpy.ndarray. None if x or theta dimensions are not appropriate. Raises: This function should not raise any Exceptions. """ if len(x) == 0: return None x = add_intercept(x) # print("X: ", x) # print("Th: ", theta) if len(theta) != x.shape[1]: return None # print("No sum: ", np.array([(i * theta.T) for i in x])) return np.array([(i * theta.T).sum() for i in x])
def gradient(x, y, theta): m = x.shape[0] Xpr = add_intercept(x) Xt = np.transpose(Xpr) y_hat = predict_(x, theta) return (1/m) * np.dot(Xt, (y_hat - y))
#!/usr/bin/python3 import numpy as np from tools import add_intercept if __name__ == "__main__": print(repr(add_intercept(np.arange(1, 6)))) print(repr(add_intercept(np.arange(1, 10).reshape(3, 3))))
def predict_(x, theta): theta = np.reshape(theta, (theta.shape[0], )) return np.sum(add_intercept(x) * theta, axis=1)
import numpy as np from tools import add_intercept print("Example 1 :") x = np.arange(1, 6) print(add_intercept(x)) print("Example 2 :") y = np.arange(1, 10).reshape((3, 3)) print(add_intercept(y))
if (0 in [len(x), len(y), len(theta)] or y.shape[1] != theta.shape[1] or x.shape[0] != y.shape[0] or x.shape[1] != theta.shape[0]): return None res = (x.T / len(y)).dot(x.dot(theta) - y) return res if __name__ == "__main__": X = np.array([[-6, -7, -9], [13, -2, 14], [-7, 14, -1], [-8, -4, 6], [-5, -9, 6], [1, -5, 11], [9, -11, 8]]) Y = np.array([2, 14, -13, 5, 12, 4, -19]) Y = Y.reshape(len(Y), 1) theta = np.array([3, 0.5, -6]) theta = theta.reshape(len(theta), 1) print(vec_gradient(X, Y, theta)) # array([ -37.35714286, 183.14285714, -393.]) theta = np.array([0, 0, 0]) theta = theta.reshape(len(theta), 1) print(vec_gradient(X, Y, theta)) # array([ 0.85714286, 23.28571429, -26.42857143]) theta1 = np.array([0, 0, 0, 0]) theta1 = theta1.reshape(len(theta1), 1) print(vec_gradient(X, add_intercept(X).dot(theta1), theta)) # array([0., 0., 0.])
def predict_(x, theta): return np.sum(add_intercept(x) * theta, axis=1)
def gradient(x, y, theta): m = x.shape[0] Xpr = add_intercept(x) Xt = np.transpose(Xpr) return theta - (1 / m) * np.dot(Xt, (np.dot(Xpr, theta) - y))
def predict_(x, theta): theta = theta.reshape((1, -1)) x = add_intercept(x) y = x * theta return y.sum(axis=1)
def predict_(x, theta): x = add_intercept(x) y = x * theta return y.sum(axis=1)
def predict_(x, theta): return (add_intercept(x).dot(theta))
#!/usr/bin/env python3 from tools import add_intercept import numpy as np # Example 1: x = np.arange(1, 6) x2 = add_intercept(x) print(x2) assert np.array_equal(x2, [[1., 1.], [1., 2.], [1., 3.], [1., 4.], [1., 5.]]) # Example 2: y = np.arange(1, 10).reshape((3, 3)) y2 = add_intercept(y) print(y2) assert np.array_equal(y2, [[1., 1., 2., 3.], [1., 4., 5., 6.], [1., 7., 8., 9.]])
def predict_(x, theta): return np.sum(np.dot(add_intercept(x), theta), axis=1)