Build the Linear Regression Model and Plot the regression line

 class LinearRegression: 

 

    def __init__(self): 

        self.parameters = {} 

    def forward_propagation(self, train_input): 

        m = self.parameters['m'] 

        c = self.parameters['c'] 

        predictions = np.multiply(m, train_input) + c 

        return predictions 

    def cost_function(self, predictions, train_output): 

        cost = np.mean((train_output - predictions) ** 2) 

        return cost 

 

    def backward_propagation(self, train_input, train_output, predictions): 

        derivatives = {} 

        df = (predictions-train_output) 

        # dm= 2/n * mean of (predictions-actual) * input 

        dm = 2 * np.mean(np.multiply(train_input, df)) 

        # dc = 2/n * mean of (predictions-actual) 

        dc = 2 * np.mean(df) 

        derivatives['dm'] = dm 

        derivatives['dc'] = dc 

        return derivatives 

    def update parameters(self, derivatives, learning_rate): 

        self.parameters['m'] = self.parameters['m'] - learning_rate * derivatives['dm'] 

        self.parameters['c'] = self.parameters['c'] - learning_rate * derivatives['dc'] 

    def train(self, train_input, train_output, learning_rate, iters): 

 

# Initialize random parameters 

        self.parameters['m'] = np.random.uniform(0, 1) * -1

        self.parameters['c'] = np.random.uniform(0, 1) * -1

 

# Initialize loss 

        self.loss = [] 

 

# Initialize figure and axis for animation 

        fig, ax = plt.subplots() 

        x_vals = np.linspace(min(train_input), max(train_input), 100) 

        line, = ax.plot(x_vals, self.parameters['m'] * x_vals +

                        self.parameters['c'], color='red', label='Regression Line') 

        ax.scatter(train_input, train_output, marker='o', 

                color='green', label='Training Data') 

 

# Set y-axis limits to exclude negative values 

        ax.set_ylim(0, max(train_output) + 1) 

        def update(frame): 

 

 # Forward propagation 

            predictions = self.forward_propagation(train_input) 

 

# Cost function 

            cost = self.cost_function(predictions, train_output) 

 

 # Back propagation 

            derivatives = self.backward_propagation( 

            train_input, train_output, predictions) 

 

 

 # Update parameters 

            self.update_parameters(derivatives, learning_rate) 

 

# Update the regression line 

            line.set_ydata(self.parameters['m'] * x_vals + self.parameters['c']) 

 

  # Append loss and print 

 

            self.loss.append(cost) 

            print("Iteration = {}, Loss = {}".format(frame + 1, cost)) 

            return line, 

 

 # Create animation 

        ani = FuncAnimation(fig, update, frames=iters, interval=200, blit=True) 

 

 # Save the animation as a video file (e.g., MP4) 

        ani.save('linear_regression_A.gif', writer='ffmpeg') 

        plt.xlabel('Input') 

        plt.ylabel('Output') 

        plt.title('Linear Regression') 

        plt.legend() 

        plt.show() 

        return self.parameters, self.loss 

 


Comments

Popular posts from this blog

About me

A set of documents that need to be classified, use the Naive Bayesian Classifier

Keras