"IndexError: list index out of range." Need help fixing this error

Viewed 27

I am trying to modify the code so that if the change in loss is less than 1%, it exits the iterations.

class MyLinReg(object):
    
    def __init__(self, activation_function):
        self.activation_function = activation_function
        
    def fit(self, X, y, alpha = 0.001, epochs = 10):
        self.theta = np.random.rand(X.shape[1] + 1)
        self.errors =[]
        n = X.shape[0]
        
        for _ in range(epochs):
            errors = 0
            sum_1 = 0
            sum_2 = 0
            for xi, yi in zip(X, y):
                sum_1 += (self.predict(xi) - yi)*xi
                sum_2 += (self.predict(xi) - yi)
                errors += ((self.predict(xi) - yi)**2)
            self.theta[:-1] -= 2*alpha*sum_1/n
            self.theta[-1] -= 2*alpha*sum_2/n
            self.errors.append(errors/n)
            if (((self.errors[-1] - self.errors[-2])/self.errors[-1]) < 0.01):
                break
        return self
    
    def predict(self, X):
        weighted_sum = np.dot(X, self.theta[:-1]) + self.theta[-1]
        return self.activation_function(weighted_sum)
1 Answers
def fit(self, X, y, alpha = 0.001, epochs = 10):
    self.theta = np.random.rand(X.shape[1] + 1)
    self.errors =[]
    n = X.shape[0]
    
    for _ in range(epochs):
        errors = 0
        sum_1 = 0
        sum_2 = 0
        for xi, yi in zip(X, y):
            sum_1 += (self.predict(xi) - yi)*xi
            sum_2 += (self.predict(xi) - yi)
            errors += ((self.predict(xi) - yi)**2)
        self.theta[:-1] -= 2*alpha*sum_1/n
        self.theta[-1] -= 2*alpha*sum_2/n
        self.errors.append(errors/n)
        if (((self.errors[-1] - self.errors[-2])/self.errors[-1]) < 0.01):
            break
    return self

You are initializing self.errors =[] (so size = 0)

first time you get in for loop you append to it ( self.errors.append(errors/n) so size is 1, but in the next line you try to access an index that doesn't exists since size is 1 ( if (((self.errors[-1] - self.errors[-2])/self.errors[-1]) < 0.01): )ence why you have an error

For self.errors[-2] to not throw an exception your list should be at least size 2

Related