Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sklearn wrapper #197

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install numpy flake8 pycodestyle pytest
python -m pip install numpy flake8 pycodestyle pytest scikit-learn scipy
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Lint with flake8
run: |
Expand All @@ -37,7 +37,7 @@ jobs:
flake8 *.py --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Checking style
run: |
pycodestyle *.py
pycodestyle minisom
- name: Unit testing
run: |
pytest minisom.py
pytest minisom
58 changes: 56 additions & 2 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,60 @@ with open('som.p', 'rb') as infile:

Note that if a lambda function is used to define the decay factor MiniSom will not be pickable anymore.

Scikit-learn wrapper for MiniSom library
---------------------

Just use it like any other scikit-learn cluster algorithm.

Let's start with importing required libraries and dataset.

```python
from sklearn.datasets import load_wine
from minisom.sklearn import MiniSOM
from sklearn.preprocessing import StandardScaler

data = load_wine()
X = data.data
X = StandardScaler().fit_transform(X)
```

You can use fit and predict separately.
```python
som = MiniSOM(3, 1, random_seed=40)
som.fit(X)
y = som.predict(X)
```

Or simply use convenient function.
```python
som = MiniSOM(3, 1, random_seed=40)
y = som.fit_predict(X)
```
Same for transform.
```python
som = MiniSOM(3, 1, random_seed=40)
som.fit(X)
y = som.transform(X)
```

```python
som = MiniSOM(3, 1, random_seed=40)
y = som.fit_transform(X)
```

Alternatively you can also use SciKit-learn pipelines.
```python
from sklearn.pipeline import Pipeline

pipeline = ([
('scaler', StandardScaler()),
('classifier', MiniSOM(3, 1, random_seed=40))
])

y = pipeline.fit_predict(X)
```


Examples
---------------------

Expand Down Expand Up @@ -125,11 +179,11 @@ Guidelines to contribute
2. Give your pull request a helpful title that summarises what your contribution does.
3. Write unit tests for your code and make sure the existing ones are up to date. `pytest` can be used for this:
```
pytest minisom.py
pytest minisom
```
4. Make sure that there are no stylistic issues using `pycodestyle`:
```
pycodestyle minisom.py
pycodestyle minisom
```
5. Make sure your code is properly commented and documented. Each public method needs to be documented as the existing ones.

2 changes: 2 additions & 0 deletions minisom/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from .minisom import (MiniSom, _build_iteration_indexes,
_wrap_index__in_verbose, fast_norm)
Loading