Skip to main content

Sega Learn is a Python package for machine learning and data science.

Project description

SEGA_LEARN

SEGA_LEARN is a custom package implementating machine learning algorithms built from the Python standard library as well as NumPy and SciPy. It includes scratch implementations of various machine learning algorithms, including clustering, linear models, neural networks, and trees. The project also includes scripts for testing, documentation generation, and other tasks.

The project is organized into several directories, each with its own purpose. The SEGA_LEARN/ directory contains the main library code, while the tests/ directory contains unit and performance tests. The examples/ directory contains example usages of the library, and the docs/ directory contains the generated documentation. The scripts/ directory contains PowerShell scripts to help with various tasks.

This project was created with the goal of learning about the internals of machine learning algorithms and how they work under the hood. As well as for exploration of Python packages for speed and performance. It is not intended for production use and should be used for educational purposes only (See performance tests for scalability). Many of the algorithms are not optimized for performance and may not be suitable for large datasets.

This project was heavily inspired by scikit-learn, and pytorch.

Navigation

Module Level READMEs

For more detailed information on each module, see the following READMEs:

Features

The SEGA_LEARN library includes the following features:

Automated Machine Learning

  • Automated ML (auto): Simplified AutoClassifier and AutoRegressor interfaces for automatically selecting and training suitable models from the Sega Learn library, providing quick baselines for classification and regression tasks.

Clustering

  • DBSCAN: Density-Based Spatial Clustering of Applications with Noise (DBSCAN) is a clustering algorithm that groups together points that are closely packed, marking as outliers points that lie alone in low-density regions.
  • KMeans: KMeans is a clustering algorithm that partitions n data points into k clusters in which each point belongs to the cluster with the nearest mean.

Linear Models

  • Bayesian Regression: Bayesian regression is a probabilistic approach to linear regression that allows incorporating prior knowledge about the model parameters.
  • Lasso Regression: Lasso (Least Absolute Shrinkage and Selection Operator) regression is a linear regression model that uses L1 regularization to perform feature selection.
  • Ridge Regression: Ridge regression is a linear regression model that uses L2 regularization to prevent overfitting by penalizing large coefficients.
  • Linear Discriminant Analysis: Linear Discriminant Analysis (LDA) is a classification algorithm that finds the linear combination of features that best separates two or more classes.
  • Ordinary Least Squares: Ordinary Least Squares (OLS) is a linear regression model that minimizes the sum of the squared differences between the observed and predicted values.
  • Passive Aggressive Regressor: Passive Aggressive Regressor is a linear regression model that updates its parameters using a passive or aggressive strategy based on the loss function.
  • Quadratic Discriminant Analysis: Quadratic Discriminant Analysis (QDA) is a classification algorithm that finds the quadratic combination of features that best separates two or more classes.
  • RANSAC Regression: Random Sample Consensus (RANSAC) regression is a linear regression model that fits a model to the data by iteratively selecting a subset of inliers and estimating the model parameters.
  • Logistic Regression: Logistic regression is a linear model for binary classification that uses the logistic function to model the probability of a class label.
  • Perceptron: The Perceptron is a simple linear binary classifier that makes its predictions based on a linear predictor function combining a set of weights with the feature vector.

Nearest Neighbors

  • KNeighborsClassifier: Implements the K-Nearest Neighbors algorithm for classification tasks.
  • KNeighborsRegressor: Implements the K-Nearest Neighbors algorithm for regression tasks.

Neural Networks

  • Neural Networks: Flexible implementation with support for various layers, activations, optimizers, and backends (NumPy, Numba, CuPy), now supporting both classification and regression tasks.
    • Optimizers: Implements various optimizers like Adadelta, Adam, and SGD.
    • Loss Functions: Implements loss functions like BCEWithLogitsLoss, CrossEntropyLoss, MeanSquaredErrorLoss, MeanAbsoluteErrorLoss, and HuberLoss.
    • Schedulers: Implements learning rate schedulers like StepLR and ReduceLROnPlateau.
    • Activation Functions: Implements activation functions like ReLU, Sigmoid, Softmax and more.

Support Vector Machines (SVMs)

  • Linear SVM Classifier: A linear SVM classifier for binary and multi-class classification tasks.
  • Linear SVM Regressor: A linear SVM regressor for predicting continuous target values.
  • Generalized SVM Classifier: Supports multiple kernels (e.g., RBF, polynomial) for binary and multi-class classification.
  • Generalized SVM Regressor: Supports multiple kernels for regression tasks.
  • One-Class SVM: Used for anomaly detection by identifying inliers and outliers in the data.
  • Kernel Functions: Includes linear, polynomial, RBF, and sigmoid kernels for handling non-linear problems.

Trees

  • Classifier Tree: Implements a decision tree classifier that recursively splits the data based on the feature that maximizes the information gain.
  • Regressor Tree: Implements a decision tree regressor that recursively splits the data based on the feature that minimizes the variance.
  • Random Forest Classifier: Implements an ensemble classifier that fits multiple decision trees on random subsets of the data and averages their predictions.
  • Random Forest Regressor: Implements an ensemble regressor that fits multiple decision trees on random subsets of the data and averages their predictions.
  • Gradient Boosted Regressor: Implements a gradient boosted regressor that fits multiple decision trees sequentially, each one correcting the errors of the previous trees.
  • Gradient Boosted Classifier: Implements a gradient boosted classifier that fits multiple decision trees sequentially, each one correcting the errors of the previous trees.
  • Isolation Forest: Implements an anomaly detection algorithm that isolates anomalies by randomly partitioning the data.
  • AdaBoost Classifier: Implements an ensemble classifier that combines multiple weak classifiers to create a strong classifier.
  • AdaBoost Regressor: Implements an ensemble regressor that combines multiple weak regressors to create a strong regressor.

Time Series

  • ARIMA: Implements the AutoRegressive Integrated Moving Average (ARIMA) model for time series forecasting.
  • SARIMA: Implements the Seasonal AutoRegressive Integrated Moving Average (SARIMA) model for time series forecasting with seasonality.
  • SARIMAX: Implements the Seasonal AutoRegressive Integrated Moving Average with eXogenous variables (SARIMAX) model for time series forecasting with seasonality and exogenous variables.
  • Additive Decomposition: Implements the additive decomposition of time series data into trend, seasonal, and residual components.
  • Multiplicative Decomposition: Implements the multiplicative decomposition of time series data into trend, seasonal, and residual components.
  • Simple Exponential Smoothing: Implements simple exponential smoothing for time series forecasting.
  • Double Exponential Smoothing: Implements double exponential smoothing for time series forecasting, also known as Holt's linear trend method.
  • Triple Exponential Smoothing: Implements triple exponential smoothing for time series forecasting, also known as Holt-Winters seasonal method.
  • Simple Moving Average: Implements the simple moving average for time series forecasting.
  • Weighted Moving Average: Implements the weighted moving average for time series forecasting.
  • Exponential Moving Average: Implements the exponential moving average for time series forecasting.

Pipelines

  • Generic Pipeline: Implements a generic pipeline for machine learning tasks: Classification and Regression.
  • Forecasting Pipeline: Implements a custom forecasting pipeline for time series forecasting.

Utilities

  • Animator: Implements a custom animation class for visualizing machine learning models, updating one parameter and observing the changes in the model's performance.
  • Data Preparation: Implements utility functions for data preparation like train-test split, normalization, and standardization.
  • Voting Regressor: Implements a voting regressor that combines the predictions of multiple regressors using a weighted average.
  • Voting Classifier: Implements a voting classifier that combines the predictions of multiple classifiers using a weighted average.
  • Voting Forcaster: Implements a voting forecaster that combines the predictions of multiple forecasters using a weighted average.
  • Polynomial Transformation: Implements polynomial transformation of features to create higher-order polynomial features.
  • Evaluation Metrics: Implements evaluation metrics like mean squared error, mean absolute error, and R-squared.
  • Model Selection Algorithms: Implements model selection algorithms like Grid Search Cross Validation and Random Search Cross Validation.
  • Data Augmentation: Implements data augmentation for imbalanced classification tasks using SMOTE (Synthetic Minority Over-sampling Technique), Under-sampling, Over-sampling, and/or a combination of each.

Planned Features - Future Work

  • Implement OPTICS clustering algorithm
  • Implement new model selection algorithms like Bayesian Optimization, Bayesian Model Averaging, and Bayesian Model Selection.

Project Structure

The project directory structure is as follows:

sega_learn/
|
├── sega_learn/      # Main library source code
│   ├── auto/               # Automated model creation/comparison
│   ├── clustering/         # Clustering algorithms
│   ├── linear_models/      # Linear models
│   ├── nearest_neighbors/  # K-Nearest Neighbors
│   ├── neural_networks/    # Neural Network components
│   ├── pipelines/          # Pipelines for machine learning tasks
│   ├── svm/                # Support Vector Machines
│   ├── trees/              # Tree-based models
│   ├── time_series/        # Time series models
│   └── utils/              # Utility functions (metrics, data prep, etc.)
|
├── examples/           # Usage examples for different modules
|
├── tests/              # Unit tests for the library code
|
├── tests_performance/  # Performance benchmark tests
|
├── docs/               # Documentation source files (e.g., Markdown)
|
├── scripts/            # Helper scripts (e.g., for building docs, environment setup)
|
├── .github/                      # GitHub specific files (workflows for CI)
│   ├── PULL_REQUEST_TEMPLATE.md  # Pull request template
│   └── workflows/
|       ├── lint-format.yml       # Ruff CI check workflow
│       └── run-tests.yml         # Unit tests CI check workflow
|
├── .gitattributes          # Git attributes for the repository
├── .gitignore              # Files/directories ignored by Git
├── .pre-commit-config.yaml # Configuration for pre-commit hooks
├── DEVELOPMENT.md          # This file: Guide for developers
├── environment.yml         # Conda environment definition
├── environment_pypy.yml    # Conda environment definition for pypy
├── pyproject.toml          # Project metadata, build config, Ruff config
├── pypdoc-markdown.yml     # Pydoc markdown config
├── README.md               # Main project README for users
└── uv.lock                 # Lock file for uv

Usage Examples

Automated Machine Learning

  • classifier.py: Demonstrates the AutoClassifier on a simple classification problem.
  • regressor.py: Demonstrates the AutoRegressor on a simple regression problem.

Clustering

Linear Models

Nearest Neighbors

Neural Networks

SVM

Trees

Time Series

Pipelines

Utils

Helper Scripts

The following PowerShell scripts are included in the scripts/ folder to help with various tasks:

  • _run_all_scripts.ps1: Runs all PowerShell scripts in the scripts/ folder sequentially.
  • todo_comments.ps1: Finds and lists all TODO comments in Python files.
  • count_lines.ps1: Counts the number of lines in each Python file, sorts the files by line count in descending order, and calculates the total number of lines.
  • comment_density.ps1: Calculates the comment density (percentage of lines that are comments) in Python files.
  • documentation_html.ps1: Generates HTML documentation for Python files in the SEGA_LEARN/ folder, and moves the generated HTML files to the docs/ folder.
  • documentation_md.ps1: Generates markdown documentation for Python files in the SEGA_LEARN/ folder.
  • export_env.ps1: Exports the conda environment to a YAML file. Remove the prefix from the environment name to make it compatible with other systems.
  • file_contents.ps1: Exports the contents of all python files in the SEGA_LEARN/ folder to a text file.

Documentation

HTML Documentation

Pydoc documentation is generated from the PowerShell script documentation_html.ps1. To see live version: https://santiagoenriquega.github.io/sega_learn/sega_learn

Self host documentation, run the following command in the terminal: python -m pydoc -p 8080 Then open a web browser and navigate to http://localhost:8080/SEGA_LEARN.html

Markdown Documentation

Pydoc Markdown is also availible and is generated from the PowerShell script documentation_md.ps1. The output file is located in docs/documentation.md

Tests

An extensive suite of unit tests is included in the tests/ directory. These tests ensure the functionality and reliability of each module in the library, as well as the overall package. The tests are organized into subdirectories based on the module they test. Each module is tested for imports, functionality, and example usage.

Test Logic

  • Imports: Tests to ensure that all modules can be imported without errors.
  • Functionality: Tests to ensure that the functions and classes in each module work as expected.
  • Examples: Tests to ensure that the example scripts run without errors.

Running Tests

To run the tests, use the following command: python -m unittest discover -s tests Or run the all tests file: python run_all_tests.py

Test Results

The following are the results of running the tests:

(sega_learn) PS .../sega_learn/tests/run_all_tests.py
Testing Imports - Auto....
Testing Imports - Clustering....
Testing Imports - Main Package............
Testing Imports - Linear Models.............
Testing Imports - Nearest Neighbors....
Testing Imports - Neural Networks.........................
Testing Imports - Pipelines....
Testing Imports - SVM........
Testing Imports - Time Series.............
Testing Imports - Trees............
Testing Imports - Utils..............................
Testing AutoClassifier Model...................
Testing AutoRegressor Model.......................
Testing DBSCAN...............
Testing DBSCAN Numba...............
Testing KMeans..................................
Testing Bayesian Regression Model..........
Testing Lasso Regression Model............
Testing Linear Discriminant Analysis.......
Testing Logistic Regression....
Testing Ordinary Least Squares Model.......
Testing Passive Aggressive Regressor Model..........
Testing Perceptron....
Testing Quadratic Discriminant Analysis.......
Testing RANSAC Regression Model...........
Testing Ridge Regression Model............
Testing KNeighborsClassifierKNeighborsBase............
Testing KNeighborsRegressor............
Testing TrainingAnimator...........
Testing NeuralNetwork class with base backend.......................
Testing Comparing NeuralNetwork Base and Numba Activations............
Testing Comparing NeuralNetwork Base and Numba Loss Functions........
Testing Comparing NeuralNetwork Base and Numba Optimizers.........
Testing Comparing NeuralNetwork Base and Numba Train Functions.......
Testing Comparing NeuralNetwork Base and Numba Utilities......
Testing ConvLayer.................
Testing DenseLayer.....................
Testing FlattenLayer....
Testing BCEWithLogitsLoss...
Testing CrossEntropyLoss.....
Testing HuberLoss.....
Testing MeanAbsoluteErrorLoss...
Testing MeanSquaredErrorLoss...
Testing JITHuberLoss.....
Testing JITBCEWithLogitsLoss...
Testing JITCrossEntropyLoss.....
Testing Comparing JIT and non-JIT loss implementations.....
Testing JITMeanAbsoluteErrorLoss...
Testing JITMeanSquaredErrorLoss...
Testing NeuralNetwork class with Numba backend.........................
Testing AdadeltaOptimizer.....
Testing AdamOptimizer.....
Testing SGDOptimizer.....
Testing JITAdadeltaOptimizer.....
Testing JITAdamOptimizer.....
Testing JITSGDOptimizer.....
Testing General Pipeline................
Testing ForecastingPipeline.................................
Testing GeneralizedSVC...........
Testing GeneralizedSVR........
Testing LinearSVC...........
Testing LinearSVR........
Testing OneClassSVM......
Testing ARIMA.....................
Testing SARIMA............
Testing SARIMAX..........
Testing AdditiveDecomposition..........
Testing _centered_moving_average.............
Testing MultiplicativeDecomposition.............
Testing DoubleExponentialSmoothing.........
Testing SimpleExponentialSmoothing...........
Testing TripleExponentialSmoothing...........
Testing ExponentialMovingAverage......
Testing SimpleMovingAverage......
Testing WeightedMovingAverage.......
Testing AdaBoostClassifier..................
Testing AdaBoostRegressor................
Testing Gradient Boosted Classifier..........................
Testing Classifier Tree.................
Testing Classifier Tree Utility...................................
Testing Random Forest Classifier..................
Testing Isolation Forest.....
Testing Isolation Tree....
Testing Isolation Tree Utility.
Testing Gradient Boosted Regressor................
Testing Random Forest Regressor...............
Testing Regressor Tree................
Testing Regressor Tree Utility.......................................
Testing Data Augmentation......................
Testing Decomposition.....
Testing Data Prep...............
Testing Forcast Regressor....
Testing GridSearchCV.......................
Testing Metrics...............
Testing Model Selection Utils..........
Testing Polynomial Transform....
Testing RandomSearchCV.........................
Testing Voting Classifier.......
Testing Voting Regressor....
Testing Animation Integration....
Testing ClassificationAnimation Class.........
Testing ForcastingAnimation Class.......
Testing RegressionAnimation Class.........
Testing Categorical Preprocessing Functions.........
Testing Scaler..........
Testing train_test_split function..........
Testing makeData Utilities...................................
Testing example file: classifier.py.
Testing example file: regressor.py.
Testing example file: dbscan.py.
Testing example file: dbscan_3d.py.
Testing example file: dbscan_3d_aimated.py.
Testing example file: kmeans.py.
Testing example file: bayes.py.
Testing example file: lasso.py.
Testing example file: lda.py.
Testing example file: lda_comparison.py.
Testing example file: lda_vs_qda_comparison.py.
Testing example file: logisticRegression.py.
Testing example file: ols.py.
Testing example file: passiveAggressive.py.
Testing example file: passiveAggressive_vis.py.
Testing example file: perceptron.py.
Testing example file: qda.py.
Testing example file: ransac.py.
Testing example file: ransac_vis.py.
Testing example file: ridge.py.
Testing example file: nearestNeighborsClassifier.py.
Testing example file: nearestNeighborsRegressor.py.
Testing example file: neuralNetwork_cancer.py.
Testing example file: neuralNetwork_classifier.py.
Testing example file: neuralNetwork_classifier_hyper.py.
Testing example file: neuralNetwork_diabetes.py.
Testing example file: neuralNetwork_hyper.py.
Testing example file: neuralNetwork_iris.py.
Testing example file: neuralNetwork_layers.py...
Testing example file: neuralNetwork_layers_numba.py..
Testing example file: neuralNetwork_regressor.py.
Testing example file: pipeline_classification.py.
Testing example file: pipeline_classification_tuning.py.
Testing example file: pipeline_forecasting.py.
Testing example file: pipeline_regression.py.
Testing example file: pipeline_regression_tuning.py.
Testing example file: generalizedSVC_binary.py.
Testing example file: generalizedSVC_multi.py.
Testing example file: generalizedSVR.py.
Testing example file: linearSVC_binary.py.
Testing example file: linearSVC_multi.py.
Testing example file: linearSVR.py.
Testing example file: oneClassSVM.py.
Testing example file: arima_arima.py.
Testing example file: arima_sarima.py.
Testing example file: arima_sarimax.py.
Testing example file: decom_additiveDecomposition.py.
Testing example file: decom_multiplicativeDecomposition.py.
Testing example file: mvg_exponentialMovingAverage.py.
Testing example file: mvg_exponentialMovingAverage_anim.py.
Testing example file: mvg_simpleMovingAverage.py.
Testing example file: mvg_simpleMovingAverage_anim.py.
Testing example file: mvg_weightedMovingAverage.py.
Testing example file: mvg_weightedMovingAverage_anim.py.
Testing example file: smth_doubleExponentialSmoothing.py.
Testing example file: smth_simpleExponentialSmoothing.py.
Testing example file: smth_tripleExponentialSmoothing.py.
Testing example file: adaBoostClassifier.py.
Testing example file: adaBoostRegressor.py.
Testing example file: gradientBoostedClassifier.py.
Testing example file: gradientBoostedRegressor.py.
Testing example file: isolationForest_blob.py.
Testing example file: isolationForest_boundary.py.
Testing example file: isolationForest_reg.py.
Testing example file: randomForestClassifier.py.
Testing example file: randomForestRegressor.py.
Testing example file: treeClassifier.py.
Testing example file: treeRegressor.py.
Testing example file: animator_exponentialMovingAverage.py.
Testing example file: animator_logisticRegression.py.
Testing example file: animator_perceptron.py.
Testing example file: animator_ridge.py.
Testing example file: animator_weightedMovingAverage.py.
Testing example file: dataAugmentation_combined.py.
Testing example file: dataAugmentation_randOver.py.
Testing example file: dataAugmentation_randUnder.py.
Testing example file: dataAugmentation_smote.py.
Testing example file: dataPreprocessing.py.
Testing example file: gridSearchCV_bayes.py.
Testing example file: gridSearchCV_gbr.py.
Testing example file: gridSearchCV_passiveAggReg.py.
Testing example file: gridSearchCV_rfc.py.
Testing example file: gridSearchCV_rfr.py.
Testing example file: makeData.py.
Testing example file: pca_classification.py.
Testing example file: pca_regression.py.
Testing example file: polynomialTransform.py.
Testing example file: randomSearchCV_bayes.py.
Testing example file: randomSearchCV_gbr.py.
Testing example file: randomSearchCV_passiveAggReg.py.
Testing example file: randomSearchCV_rfc.py.
Testing example file: randomSearchCV_rfr.py.
Testing example file: segaSearchCV_rfr.py.
Testing example file: svd_classification.py.
Testing example file: svd_regression.py.
Testing example file: votingClassifier.py.
Testing example file: votingRegressor.py.
----------------------------------------------------------------------
Ran 1319 tests in 414.621s

OK

Installation

To set up the project environment, you can use the provided environment.yml file to create a conda environment with all the necessary dependencies.

  1. Open a terminal or command prompt.
  2. Navigate to the directory where your repository is located.
  3. Run the following command to create the conda environment: conda env create -f environment.yml
  4. Activate the newly created environment: conda activate sega_learn

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

sega_learn-0.1.1.tar.gz (309.2 kB view details)

Uploaded Source

Built Distribution

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

sega_learn-0.1.1-py3-none-any.whl (267.1 kB view details)

Uploaded Python 3

File details

Details for the file sega_learn-0.1.1.tar.gz.

File metadata

  • Download URL: sega_learn-0.1.1.tar.gz
  • Upload date:
  • Size: 309.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.3

File hashes

Hashes for sega_learn-0.1.1.tar.gz
Algorithm Hash digest
SHA256 266c312703de6259007ca31d81da6d6e0faaa4dcebe7d53964860fcdca1a4f38
MD5 b63df1c2f02450b2a96106aaf477c104
BLAKE2b-256 159eccf9c7f96a1c8b99e9a6489cac4de76142211d7dbfcf72a6e06e96b6753a

See more details on using hashes here.

File details

Details for the file sega_learn-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: sega_learn-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 267.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.3

File hashes

Hashes for sega_learn-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4de6bd6d6069d50d77fdf1ad3712ef317f2f1e76f2e7d698a619c6fb1528c320
MD5 4ee22109f5310941320ac4afa40b23a6
BLAKE2b-256 af73e27c8f5704bf00dc25a61ac1aeb77b30ecf53a8e1fb15cc144a4bdf6ac79

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