{"id":21191633,"url":"https://github.com/edgeimpulse/example-custom-ml-block-scikit","last_synced_at":"2025-07-10T03:30:41.567Z","repository":{"id":104672436,"uuid":"534654360","full_name":"edgeimpulse/example-custom-ml-block-scikit","owner":"edgeimpulse","description":"Scikit learn block","archived":false,"fork":false,"pushed_at":"2024-11-12T15:26:13.000Z","size":37,"stargazers_count":3,"open_issues_count":1,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-11-12T16:33:11.905Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/edgeimpulse.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-09-09T13:28:42.000Z","updated_at":"2024-11-12T15:26:16.000Z","dependencies_parsed_at":null,"dependency_job_id":"adf6d226-3bae-44d8-8aca-a8597129e241","html_url":"https://github.com/edgeimpulse/example-custom-ml-block-scikit","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edgeimpulse%2Fexample-custom-ml-block-scikit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edgeimpulse%2Fexample-custom-ml-block-scikit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edgeimpulse%2Fexample-custom-ml-block-scikit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edgeimpulse%2Fexample-custom-ml-block-scikit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/edgeimpulse","download_url":"https://codeload.github.com/edgeimpulse/example-custom-ml-block-scikit/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225615188,"owners_count":17496944,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-20T19:04:10.628Z","updated_at":"2025-07-10T03:30:41.561Z","avatar_url":"https://github.com/edgeimpulse.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Custom scikit-learn ML block example for Edge Impulse\n\nThis repository is an example on how to [add a custom learning block](https://docs.edgeimpulse.com/docs/adding-custom-transfer-learning-models) to Edge Impulse. This repository contains a small logistic regression model built with scikit-learn.\n\nNOTE: Only the following scikit-learn models are supported via the pickle conversion workflow presented below:\n- [Support vector Classification](https://scikit-learn.org/1.5/modules/generated/sklearn.svm.SVC.html) with linear kernel\n- [Linear regression](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LinearRegression.html)\n- [Ridge](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.Ridge.html)\n- [RidgeCV](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.RidgeCV.html)\n- [Random forest regresssor](https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.RandomForestRegressor.html)\n- [Logistic regression](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LogisticRegression.html)\n- [Ridge classifier](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.RidgeClassifier.html)\n- [Ridge classifier CV](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.RidgeClassifierCV.html)\n- [Random forest classifier](https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.RandomForestClassifier.html)\n\nAs a primer, read the [Custom learning blocks](https://docs.edgeimpulse.com/docs/edge-impulse-studio/learning-blocks/adding-custom-learning-blocks) page in the Edge Impulse docs.\n\n\u003e **Note on epochs**: You'll want a lot of training cycles (1,000+ or so) here.\n\n## Running the pipeline\n\nYou run this pipeline via Docker. This encapsulates all dependencies and packages for you.\n\n### Running via Docker\n\n1. Install [Docker Desktop](https://www.docker.com/products/docker-desktop/).\n2. Install the [Edge Impulse CLI](https://docs.edgeimpulse.com/docs/edge-impulse-cli/cli-installation) v1.16.0 or higher.\n3. Create a new Edge Impulse project, and add data from the [continuous gestures](https://docs.edgeimpulse.com/docs/continuous-gestures) dataset.\n4. Under **Create impulse** add a 'Spectral features' processing block, and a random ML block.\n5. Open a command prompt or terminal window.\n6. Initialize the block:\n\n    ```\n    $ edge-impulse-blocks init\n    ```\n\n7. Fetch new data via:\n\n    ```\n    $ edge-impulse-blocks runner --download-data data/\n    ```\n\n8. Build the container:\n\n    ```\n    $ docker build -t custom-ml-scikit .\n    ```\n\n9. Run the container to test the script (you don't need to rebuild the container if you make changes):\n\n    ```\n    $ docker run --rm -v $PWD:/app custom-ml-scikit --data-directory /app/data --epochs 30 --learning-rate 0.01 --out-directory out/\n    ```\n\n10. This creates a .pkl file in the 'out' directory. This is a pickle file of the sklearn model which can be reinstantiated in python. The file will be picked up in the custom block and converted to a TFLite model within studio.\n\n#### Adding extra dependencies\n\nIf you have extra packages that you want to install within the container, add them to `requirements.txt` and rebuild the container.\n\n#### Adding new arguments\n\nTo add new arguments, see [Custom learning blocks \u003e Arguments to your script](https://docs.edgeimpulse.com/docs/edge-impulse-studio/learning-blocks/adding-custom-learning-blocks#arguments-to-your-script).\n\n## Fetching new data\n\nTo get up-to-date data from your project:\n\n1. Install the [Edge Impulse CLI](https://docs.edgeimpulse.com/docs/edge-impulse-cli/cli-installation) v1.16 or higher.\n2. Open a command prompt or terminal window.\n3. Fetch new data via:\n\n    ```\n    $ edge-impulse-blocks runner --download-data data/\n    ```\n\n## Pushing the block back to Edge Impulse\n\nYou can also push this block back to Edge Impulse, that makes it available like any other ML block so you can retrain your model when new data comes in, or deploy the model to device.\n\n1. Push the block:\n\n    ```\n    $ edge-impulse-blocks push\n    ```\n\n2. The block is now available under any of your projects via **Create impulse \u003e Add new learning block**.\n\n## Changing the block type (e.g. regression)\n\nIf you want to change the block type because you're classifying a different data type, or build a model with a different output format, run:\n\n```\n$ rm parameters.json  .ei-block-config\n$ edge-impulse-blocks init\n```\n\nAnd answer the wizard. This'll create a new parameters.json file.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fedgeimpulse%2Fexample-custom-ml-block-scikit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fedgeimpulse%2Fexample-custom-ml-block-scikit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fedgeimpulse%2Fexample-custom-ml-block-scikit/lists"}