174 lines
3.4 KiB
Markdown
174 lines
3.4 KiB
Markdown
---
|
||
tags:
|
||
aliases:
|
||
date: 2025-03-02
|
||
time: 20:53:47
|
||
description:
|
||
---
|
||
|
||
# Project Structure
|
||
```
|
||
mlpredictor/
|
||
│
|
||
├── mlpredictor/
|
||
│ ├── __init__.py
|
||
│ ├── model.py
|
||
│
|
||
├── tests/
|
||
│ ├── test_model.py
|
||
│
|
||
├── LICENSE
|
||
├── README.md
|
||
├── pyproject.toml
|
||
└── .gitignore
|
||
```
|
||
|
||
|
||
## Content of `setup.py`
|
||
```
|
||
from setuptools import setup
|
||
|
||
setup(
|
||
name='mypackage',
|
||
version='0.1',
|
||
packages=['mypackage'],
|
||
install_requires=['requests'])
|
||
```
|
||
|
||
## Content of `pyproject.toml`
|
||
```toml
|
||
[build-system]
|
||
requires = ["setuptools>=42", "wheel"]
|
||
build-backend = "setuptools.build_meta"
|
||
|
||
[project]
|
||
name = "mlpredictor"
|
||
version = "0.1.0"
|
||
description = "A simple machine learning package using scikit-learn"
|
||
authors = [
|
||
{name = "Ebrahim", email = "ebimsv0501@gmail.com"}
|
||
]
|
||
license = {text = "MIT"}
|
||
readme = "README.md"
|
||
requires-python = ">=3.6"
|
||
dependencies = [
|
||
"scikit-learn>=1.0",
|
||
]
|
||
|
||
[project.urls]
|
||
"Homepage" = "https://github.com/ebimsv/mlpredictor"
|
||
```
|
||
- **[build-system]**: Specifies the build system requirements (i.e., using `setuptools` and `wheel`).
|
||
- **[project]**: Contains metadata about the package, like name, version, description, and dependencies.
|
||
|
||
|
||
## Content of `README.md`
|
||
<pre><code>
|
||
# MLPredictor
|
||
|
||
MLPredictor is a simple machine learning package that trains a RandomForest model using the Iris dataset and enables users to make predictions. The package is built using `scikit-learn` and is intended as a demonstration of packaging Python machine learning projects for distribution.
|
||
|
||
## Features
|
||
|
||
- Train a RandomForestClassifier on the Iris dataset.
|
||
- Make predictions on new data after training.
|
||
- Save and load trained models.
|
||
|
||
## Installation
|
||
|
||
You can install the package via **PyPI** or from **source**.
|
||
|
||
### Install from PyPI
|
||
|
||
```bash
|
||
pip install mlpredictor
|
||
```
|
||
|
||
### Install from Source (GitHub)
|
||
|
||
```bash
|
||
git clone https://github.com/ebimsv/mlpredictor.git
|
||
cd mlpredictor
|
||
pip install .
|
||
```
|
||
|
||
## Usage
|
||
|
||
After installation, you can use `MLPredictor` to train a model and make predictions.
|
||
|
||
### Example: Training and Making Predictions
|
||
|
||
```python
|
||
from mlpredictor import MLPredictor
|
||
|
||
# Initialize the predictor
|
||
predictor = MLPredictor()
|
||
|
||
# Train the model on the Iris dataset
|
||
predictor.train()
|
||
|
||
# Make a prediction on a sample input
|
||
sample_input = [5.1, 3.5, 1.4, 0.2]
|
||
prediction = predictor.predict(sample_input)
|
||
|
||
print(f"Predicted class: {prediction}")
|
||
```
|
||
</code></pre>
|
||
|
||
## Content of `.gitignore`
|
||
```
|
||
*.pyc
|
||
__pycache__/
|
||
*.pkl
|
||
dist/
|
||
build/
|
||
```
|
||
|
||
|
||
# Test
|
||
## Content of `tests/test_model.py`
|
||
```python
|
||
import pytest
|
||
from mlpredictor import MLPredictor
|
||
|
||
def test_train_and_predict():
|
||
model = MLPredictor()
|
||
model.train()
|
||
result = model.predict([5.1, 3.5, 1.4, 0.2])
|
||
assert len(result) == 1
|
||
|
||
if __name__ == "__main__":
|
||
pytest.main()
|
||
```
|
||
|
||
## Run test
|
||
```bash
|
||
pytest tests
|
||
```
|
||
|
||
|
||
# Install
|
||
## Test locally
|
||
```
|
||
pip install .
|
||
```
|
||
|
||
## Publish on PyPI
|
||
1. **Install 'Twine' and 'build'**:
|
||
```
|
||
pip install twine build
|
||
```
|
||
|
||
2. **Build the Package**:
|
||
```
|
||
python -m build
|
||
```
|
||
|
||
3. **Upload to PyPI**
|
||
```
|
||
twine upload dist/*
|
||
```
|
||
|
||
# 參考來源
|
||
- [Building Python Packages. A Comprehensive Guide to setup.py and… | by Ebrahim Mousavi | Medium](https://medium.com/@ebimsv/building-python-packages-07fbfbb959a9)
|