def cost_elem_(theta, X, Y): if X.shape[1] + 1 != theta.size or X.shape[0] != Y.size: print("Inc dim") return Z = predict_(theta, X) l = [0.5 / Y.size * (i - j)**2 for i, j in zip(Z, Y)] return (np.array(l))
def cost_elem_(theta, X, Y): m = len(X) n = len(X[0]) J_elem = np.zeros((m, 1)) for i in range(m): J_elem[i] = ((predict_(theta, X)[i] - Y[i])**2) / (2 * m) return J_elem
def cost_elem_(theta, X, Y): """ Description: Calculates all the elements 0.5*M*(y_pred - y)^2 of the cost function. Args: theta: has to be a numpy.ndarray, a vector of dimension (number of features + 1, 1). X: has to be a numpy.ndarray, a matrix of dimension (number of training examples, number of features). Returns: J_elem: numpy.ndarray, a vector of dimension (number of the training examples,1). None if there is a dimension matching problem between X, Y or theta. Raises: This function should not raise any Exception. """ y_hat = predict_(theta, X) if len(y_hat) == 0 or Y.shape[0] != X.shape[0] or Y.shape[1] != 1: print("dimensions incompatibles") return None #print(y_hat) array = [] m = X.shape[0] n = X.shape[1] for i in range(0, m): array.append((y_hat[i] - Y[i])**2) # mean(predicted - real ^ 2) for i in range(0,m): array[i] = array[i] / (2 * m) return np.array(array)
def fit_(theta, X, Y, alpha=0.01, n_cycle=2000): # pred = predict_(theta, X) # cost_elem = cost_elem_(theta, X, Y) # print(pred) # print(cost_elem) cost = cost_(theta, X, Y) previous_cost = 0 #while cost > 0: for i in range(0, n_cycle): cost = cost_(theta, X, Y) # if (abs(cost - previous_cost) <= 0.000001): # break ; previous_cost = cost pred = predict_(theta, X) D_theta1 = 1 / X.shape[0] * (alpha * np.sum((pred - Y) * X)) D_theta0 = 1 / X.shape[0] * (np.sum(pred - Y)) theta[1] = theta[1] - D_theta1 theta[0] = theta[0] - D_theta0 print(cost_(theta, X, Y)) print(theta) #print(D_theta0) #print(D_theta1) #print(cost_(theta, X, Y)) # for n iter: # L = 0.0001 -> learning rate #pred = predict_(new_theta, X) #D_theta1 = (-2/m) * np.sum(X * (Y - Y_PRED)) #D_theta0 = (-2/m) * sum(Y - Y_PRED) #Theta[1] = theta[1] - D_theta1 #Theta[0] = theta[0] - D_theta0 return theta
def fit_(theta, X, Y, alpha, n_cycle): M = len(X) N = len(X[0]) new_theta = np.zeros((N + 1, 1)) coef = alpha / M for cycle in range(n_cycle): err_pred = predict_(theta, X) - Y new_theta[0] = theta[0] - (sum(err_pred) * coef) for feat in range(N): tmp_theta = np.sum(dot(err_pred, X.T[feat])) * coef new_theta[feat + 1] = theta[feat + 1] - tmp_theta theta = new_theta return new_theta
import numpy as np from pred import predict_ X1 = np.array([[0.], [1.], [2.], [3.], [4.]]) theta1 = np.array([[2.], [4.]]) res = predict_(theta1, X1) print(res) X2 = np.array([[1], [2], [3], [5], [8]]) theta2 = np.array([[2.]]) res = predict_(theta2, X2) print(res) X3 = np.array([[0.2, 2., 20.], [0.4, 4., 40.], [0.6, 6., 60.], [0.8, 8., 80.]]) theta3 = np.array([[0.05], [1.], [1.], [1.]]) res = predict_(theta3, X3) print(res)
from pred import predict_ def vec_gradient(x, y, theta): gradient = (x.dot(theta) - y) / len(x) gradient = x.T.dot(gradient) return gradient def fit_(theta, X, Y, alpha, n_cycle): new = np.full((len(X), 1), 1) X = np.hstack((new, X)) for n in range(n_cycle): theta = theta - alpha * vec_gradient(X, Y, theta) return theta X1 = np.array([[0.], [1.], [2.], [3.], [4.]]) Y1 = np.array([[2.], [6.], [10.], [14.], [18.]]) theta1 = np.array([[1.], [1.]]) theta1 = fit_(theta1, X1, Y1, alpha=0.01, n_cycle=2000) print(theta1) print(predict_(theta1, X1)) X2 = np.array([[0.2, 2., 20.], [0.4, 4., 40.], [0.6, 6., 60.], [0.8, 8., 80.]]) Y2 = np.array([[19.6], [-2.8], [-25.2], [-47.6]]) theta2 = np.array([[42.], [1.], [1.], [1.]]) theta2 = fit_(theta2, X2, Y2, alpha=0.0005, n_cycle=42000) print(theta2) print(predict_(theta2, X2))
from pred import predict_ import numpy as np X = np.array([[0.], [1.], [2.], [3.], [4.]]) theta = np.array([[2.], [4.]]) print(predict_(theta, X)) #X3 = np.array([[0.2, 2., 20.], [0.4, 4., 40.], [0.6, 6., 60.], [0.8, 8., #80.]]) #theta3 = np.array([[0.05], [1.], [1.], [1.]]) #print(predict_(theta3, X3))
def cost_elem_(theta, X, Y): if (len(theta) != X.shape[1] + 1 or len(X) != len(Y) or len(X) == 0): return None return ((predict_(theta, X) - Y)**2) / (len(X) * 2)