Ejemplo n.º 1
0
def calculates_error_metrics(models, X_train, y_train, X_test, y_test):
    scores = {}
    scores_tr = {}
    scores_te = {}

    pred_values = {}
    pred_values_tr = {}
    pred_values_te = {}

    for k, v in models.items():
        y_pred_train = v.predict(X_train)
        y_pred_test = v.predict(X_test)

        pred_values_tr[k] = y_pred_train
        pred_values_te[k] = y_pred_test

        scores_tr[k + '_MSE'] = metrics.MSE(y_train, y_pred_train)
        scores_tr[k + '_R2'] = metrics.R2(y_train, y_pred_train)

        scores_te[k + '_MSE'] = metrics.MSE(y_test, y_pred_test)
        scores_te[k + '_R2'] = metrics.R2(y_test, y_pred_test)

    pred_values['Train'] = pred_values_tr
    pred_values['Test'] = pred_values_te

    scores['Train'] = scores_tr
    scores['Test'] = scores_te

    return pred_values, scores
Ejemplo n.º 2
0
 def fit(self, X, y, epochs, learning_rate): 
     # Inicializando os coeficientes com 0
     b_0 = 0
     b_1 = 0
     
     n = len(X)  # Número de observações
     cost = np.zeros(epochs)
     
     # Aplicando gradiente descendente
     for e in range(epochs):
         y_pred = b_0 + b_1 * X
   
         # Calculando derivadas (gradientes)
         D_0 = (1/n) * sum(y - y_pred)
         D_1 = (1/n) * sum((y - y_pred) * X) 
         
         # Atualizando betas
         b_0 = b_0 + learning_rate * D_0  
         b_1 = b_1 + learning_rate * D_1
         
         cost[e] = metrics.MSE(y, y_pred)
         
     self.b_0 = b_0
     self.b_1 = b_1
     self.cost = cost
Ejemplo n.º 3
0
 def fit(self, X, y, epochs, learning_rate):
     n = X.shape[0] # Número de amostras
     p = X.shape[1] # Número de variáveis (parâmetros)
     X_ = np.c_[np.ones(n), X]
     
     cost = np.zeros(epochs)
     B = np.zeros(p + 1)
      
     for e in range(epochs):    
         y_pred = X_.dot(B)
         
         D = (1/n) * (X_.T.dot(y_pred - y))
         B = B - learning_rate * D
         cost[e] = metrics.MSE(y, y_pred)
         
     self.B = B
     self.cost = cost
Ejemplo n.º 4
0
    def fit(self, X, y, epochs, learning_rate):
        n = X.shape[0] # Números de amostras
        p = X.shape[1] # Número de variáveis (atributos)
        X_ = np.c_[np.ones(n), X]
        
        cost = np.zeros(epochs)
        B = np.zeros(p + 1)
         
        for e in range(epochs): 
            count = 0.0
            random_permutation = np.random.permutation(n)
            for Xi, yi in zip(X_[random_permutation], y[random_permutation]):
                y_pred = Xi.dot(B)

                B = B - learning_rate * (Xi.T.dot(y_pred - yi))
                count += metrics.MSE(y, y_pred)
            cost[e] = count
            
        self.B = B
        self.cost = cost
Ejemplo n.º 5
0
    def fit(self, X, y, epochs, learning_rate, lamb):
        n = X.shape[0] # Números de amostras
        p = X.shape[1] # Número de variáveis (atributos)
        X_ = np.c_[np.ones(n), X]
        
        cost = np.zeros(epochs)
        B = np.zeros(p + 1)
        b_0 = 0
         
        for e in range(epochs): 
            y_pred = X_.dot(B)

            D_0 = (1/n) * sum(y - y_pred) 
            b_0 = b_0 + learning_rate * D_0

            D = ((1/n) * X_.T.dot(y_pred - y)) - (lamb/n * B)
            B = B - learning_rate * D
            cost[e] = metrics.MSE(y, y_pred)
        B[0] = b_0
        
        self.B = B
        self.cost = cost