Programmation Orientée Objet en Python
  • Back to Main Website
  • Home
  • Introduction: Histoire et Concepts
    • Introduction: Histoire et Concepts
    • Histoire de la programmation
    • Première Structuration des données
    • Naissance de la POO
    • Python: tout n’est qu’objet
    • Python : Simplicité des objets et performance sous-jacente
    • Classes en Python : Concepts fondamentaux

    • Travaux Pratiques
    • Construire sa propre Liste
    • Construire son propre DataFrame
  • Encapsulation, Héritage, Composition et Dunder
    • Encapsulation, Heritage, Composition et Dunder
    • Encapsulation en Python
    • Héritage en Python : Concept et intérêt
    • Héritage vs Composition
    • Méthodes Dunder en Python
    • Python call Method: A Fun Exploration

    • Travaux Pratiques
    • TP: Heritage avec le pricing d’option
    • TP : Ajouter des méthodes dunder à DataFrameSimple
    • TP : Étendre la classe Liste avec des méthodes dunder
    • TP: Dunder Method with Tensor for Automatic Differentiation
  • Polymorphisme et Surcharge
    • Polymorphisme et Surcharge
    • Polymorphism in Object-Oriented Programming
    • Polymorphism in Python: Function Overloading and Type Checking
    • Class Creation: Standard vs type()
    • Type Hinting, Typing Module, and Linters in Python
    • Abstract Classes
    • Protocol Classes

    • Travaux Pratiques
    • TP
  • Decorators
    • Design Patterns
    • The decorator pattern
    • Decorator Practically
    • Built-in Decorators and Standard Library Decorators in Python
    • Practical Decorators in Python Libraries

    • Travaux Pratiques
    • TP: Monte Carlo Option Pricing with Decorators
    • TP: Optimizing Heston Model Monte Carlo Simulation
  • Project Management and Packaging
    • Project and Package
    • Organizing Python Projects
    • Understanding imports
    • Python Package Management and Virtual Environments
    • Unit Testing in Python

    • Travaux Pratiques
    • TP: Creating a Linear Regression Package
  • Design Patterns
    • OOP Design Patterns
    • Python-Specific Design Patterns
    • Creation Design Patterns
    • Structural Design Patterns
    • Behavioral Design Pattern

    • Travaux Pratiques
    • TP
  • Sujets de Projets possibles
    • Projets
    • Projets POO - 2024-2025
  • Code source
  1. Travaux Pratiques
  2. TP: Creating a Linear Regression Package
  • Project and Package
  • Organizing Python Projects
  • Understanding imports
  • Python Package Management and Virtual Environments
  • Unit Testing in Python
  • Travaux Pratiques
    • TP: Creating a Linear Regression Package

On this page

  • Introduction
  • Objectives
  • Prerequisites
  • Step 1: Setting Up the Project
  • Step 2: Implementing Linear Regression
  • Step 3: Writing Tests
  • Step 4: Setting Up Basic GitHub Actions
  • Step 5: Manual Package Publishing
  • Step 6: Automating Package Publishing
  1. Travaux Pratiques
  2. TP: Creating a Linear Regression Package

TP: Creating a Linear Regression Package

Course
Financial Programming
A hands-on practical exercise on creating a simple machine learning package for linear regression, exploring package management, testing, manual publishing, and automating the publishing process.
Author

Remi Genet

Published

2024-10-21

Introduction

In this TP, you’ll create a simple machine learning package focused on linear regression. You’ll use modern Python tools like Poetry for package management, implement basic classes, write tests, set up continuous integration, manually publish your package, and finally automate the publishing process.

Objectives

By the end of this TP, you will:

  1. Set up a Python project using Poetry
  2. Implement a basic linear regression class
  3. Write unit tests for your implementation
  4. Use GitHub Actions for continuous integration
  5. Manually publish your package to PyPI
  6. Automate the publishing process using GitHub Actions

Prerequisites

  • Basic understanding of Python and object-oriented programming
  • Familiarity with linear regression concepts
  • Git and GitHub account
  • Python 3.8 or higher installed

Step 1: Setting Up the Project

Set up your project using Poetry.

  1. Install Poetry if you haven’t already.
  2. Create a new project named finance-ml.
  3. Edit the pyproject.toml file to add necessary dependencies (numpy for calculations, pytest for testing).
  4. Install the dependencies using Poetry.

Exercise 1: Set up the project structure as described above. Familiarize yourself with the pyproject.toml file and Poetry commands.

Step 2: Implementing Linear Regression

Implement a basic linear regression class.

Create a new file src/finance_ml/linear_models.py and implement a LinearRegression class with the following methods:

  • __init__(self, use_intercept=True): Initialize the model parameters.
  • fit(self, X, y): Fit the model to the training data.
  • predict(self, X): Make predictions using the trained model.

Your implementation should handle cases with and without an intercept term and use numpy for efficient calculations.

Exercise 2: Implement the LinearRegression class as described above.

Step 3: Writing Tests

Write tests for your LinearRegression class to ensure it works correctly.

Create a new file tests/test_linear_models.py and write test functions to cover various scenarios and edge cases.

Exercise 3: Implement the test functions for your LinearRegression class.

Step 4: Setting Up Basic GitHub Actions

Set up a basic GitHub Actions workflow to run your tests automatically.

Create a new file .github/workflows/tests.yml:

name: Run Tests

on: [push, pull_request]

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - name: Set up Python
      uses: actions/setup-python@v2
      with:
        python-version: '3.8'
    - name: Install dependencies
      run: |
        pip install poetry
        poetry install
    - name: Run tests
      run: poetry run pytest

Exercise 4: 1. Set up a GitHub repository for your project and add the GitHub Actions workflow as shown above. 2. Push your code and verify that the tests run automatically on push and pull requests.

Step 5: Manual Package Publishing

Now, let’s publish your package to PyPI manually using Poetry.

  1. Register an account on PyPI (https://pypi.org/)
  2. Build your package using Poetry
  3. Publish your package to PyPI using Poetry

Exercise 5: Follow the steps above to manually publish your package to PyPI. Verify that you can install your package from PyPI using pip.

Step 6: Automating Package Publishing

Finally, let’s automate the publishing process using GitHub Actions.

Exercise 6: 1. Modify the GitHub Actions workflow you created in Step 4 to include a job for publishing the package to PyPI when pushing to the main branch. 2. Research how to securely add your PyPI credentials as secrets in GitHub Actions. 3. Update your workflow to use these secrets for authentication when publishing. 4. Test your automated publishing process by pushing a change to the main branch.

Hint: You’ll need to add a new job to your workflow, use conditional execution based on the branch, and incorporate the PyPI credentials as secrets.

Back to top
Unit Testing in Python

Programmation Orienté Object en Python, Rémi Genet.
Licence
Code source disponible sur Github

 

Site construit avec et Quarto
Inspiration pour la mise en forme du site ici
Code source disponible sur GitHub