dannyxn
dannyxn

Reputation: 422

Polynomial Regression Failure

I am trying to implement my own Polynomial Regression model from scratch, here's what I've written so far:

import numpy as np



class Polynomial:

    def __init__(self, eta=0.2, degree=1, epochs=100):
        self.eta = eta
        self.degree = degree
        self.coef = np.ones(shape = (self.degree,))
        self.epochs = epochs

    def fit_coef(self, X_train, y_train):
        temp_coef = np.ones(shape=(self.degree, ))
        size = len(X_train)
        for _ in range(3):
            for x, y in zip(X_train, y_train):
                arr = np.array([x**i for i in range(self.degree)])
                for i in range(self.degree):
                    err = np.sum(np.transpose(arr)*self.coef)*2/size
                    err -= y
                    err *= x**i
                    err *= self.eta
                    temp_coef[i] -= err
                    print(temp_coef[i])
                self.coef = temp_coef

After trying to fit my model to any examplatory dataset, I'm encountering a problem, coefficients of my model become NaN values, I've noticed that they growth very fast to big values. I can't explain this, so fix it. Majority of online tutorials, articles about Polynomial Regression focus on using sklearn or other packages so I haven't found solution. Are you able to help me inspect this issue?

Upvotes: 2

Views: 144

Answers (1)

joyzaza
joyzaza

Reputation: 186

First of all Polynomial Regression use Mean Absolute Error, L1 Loss or Mean Square Error, Quadratic loss, L2 Loss to avoid the error goes too large

Second what's the shape of your x ? the coef should have the shape of x

Upvotes: 0

Related Questions