Skip to main content

A simple linear regression library

Project description

LinearRegression Class Documentation

Overview

The LinearRegression class in the tazids library implements a simple linear regression model using gradient descent. This class allows you to fit a model to data, compute predictions, and monitor the learning process via the loss function. The model learns the optimal parameters (slope a and intercept b) to best fit the input data using the Mean Squared Error (MSE) cost function and gradient descent optimization.

Installation

To install the tazids library, you can use the following command:

pip install tazids

Class: LinearRegression Constructor: __init__(self) The constructor initializes the model's parameters. It sets up an empty dictionary, parameters, which will hold the values for the model's parameters (slope a and intercept b).

def __init__(self):
    self.parameters = {}

Method: fw_prop(self, X) This method performs forward propagation, predicting the target variable y_pred using the model's parameters (a and b) and the given input features X. The linear regression equation is used:

image

Parameters: X: The input feature data (numpy array or similar). Returns: y_pred: The predicted target values.

def fw_prop(self, X):
    a = self.parameters['a']
    b = self.parameters['b']
    y_pred = a * X + b
    return y_pred

Method: cost_function(self, y, y_pred) This method calculates the cost using the Mean Squared Error (MSE) formula:

image

Parameters: y: The actual target values. y_pred: The predicted target values. Returns: cost: The mean squared error between y and y_pred.

def cost_function(self, y, y_pred):
    cost = np.mean((y_pred - y) ** 2)
    return cost

Method: back_prop(self, X, y, y_pred) This method computes the gradients (derivatives) of the cost function with respect to the parameters (a and b). These gradients are needed to update the parameters during the optimization process.

Parameters: X: The input features. y: The actual target values. y_pred: The predicted target values. Returns: derivatives: A dictionary containing the gradients for the parameters (da for a, db for b).

def back_prop(self, X, y, y_pred):
    derivatives = {}
    df = y_pred - y
    derivatives['da'] = 2 * np.mean(X * df)
    derivatives['db'] = 2 * np.mean(df)
    return derivatives

Method: update_params(self, derivatives, learning_rate) This method updates the model parameters (a and b) using the gradients computed in back_prop. The update is done using the gradient descent rule:

image

Parameters: derivatives: A dictionary containing the gradients for the parameters (da and db). learning_rate: The step size for updating the parameters.

def update_params(self, derivatives, learning_rate):
    self.parameters['a'] -= learning_rate * derivatives['da']
    self.parameters['b'] -= learning_rate * derivatives['db']

Method: fit(self, X, y, learning_rate=0.1, iters=1000) This method trains the linear regression model using gradient descent. It initializes the parameters (a and b), performs the forward pass, calculates the cost, computes the gradients, updates the parameters, and repeats the process for the specified number of iterations.

Parameters: X: The input features. y: The target values. learning_rate: The learning rate for gradient descent (default is 0.1). iters: The number of iterations for training the model (default is 1000). Returns: None. The model is trained in place, and the loss is printed during training.

def fit(self, X, y, learning_rate=0.1, iters=1000):
    self.parameters['a'] = np.random.uniform(-1, 1)
    self.parameters['b'] = np.random.uniform(-1, 1)
    self.loss = []
    print("Tazi is proud of you!")
    for i in range(iters):
        predictions = self.fw_prop(X)
        cost = self.cost_function(y, predictions)
        derivatives = self.back_prop(X, y, predictions)
        self.update_params(derivatives, learning_rate)
        self.loss.append(cost)
        if i % 100 == 0:
            print(f"Iteration = {i}, Loss = {cost}")

Method: predict(self, X) This method makes predictions using the trained parameters (a and b). It applies the linear regression equation to the input features X.

Parameters: X: The input features. Returns: y_pred: The predicted target values.

def predict(self, X):
    a = self.parameters['a']
    b = self.parameters['b']
    y_pred = a * X + b
    print("Tazi guessed the following values")
    return y_pred

Example Usage

import numpy as np
from tazids.regressor import LinearRegression

# Generate synthetic data
np.random.seed(42)
X = np.random.rand(100, 1) * 10  # Random features between 0 and 10
y = np.random.rand(100) * 100    # Random target variable between 0 and 100

# Initialize and train the model
model = LinearRegression()
model.fit(X, y, learning_rate=0.001, iters=1000)

# Make predictions
predictions = model.predict(X)

Notes

  • The model uses random initialization for the parameters a and b, so results may vary on each run.
  • The learning rate and number of iterations can be adjusted to fine-tune the training process.
  • The loss (Mean Squared Error) is printed every 100 iterations to monitor the progress of training.
  • The method predict provides the model's predictions for the input data.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tazids-1.0.1.tar.gz (3.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tazids-1.0.1-py3-none-any.whl (3.8 kB view details)

Uploaded Python 3

File details

Details for the file tazids-1.0.1.tar.gz.

File metadata

  • Download URL: tazids-1.0.1.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.3

File hashes

Hashes for tazids-1.0.1.tar.gz
Algorithm Hash digest
SHA256 8c9b0960f884106adcbdd1f1d919ff291f4d5082cc5169922b0044ceea4f7b2d
MD5 c80839e7e13c934b3de5371030fb5f94
BLAKE2b-256 b7bbd8b63859e4c04b9cac2f1917aef965ad03ee4ecf843414f1e7a62a517828

See more details on using hashes here.

File details

Details for the file tazids-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: tazids-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 3.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.3

File hashes

Hashes for tazids-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 87fc8285c9ebbeaff1b6866d215403ca7458955f66dfdf9444f8800e56df1ca2
MD5 0a9957da985a482d30d519f779eaa516
BLAKE2b-256 5bf85730aa1657cd8554d656524e78947ff521cb203bbe06e62db675beab6098

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page