Skip to content

Commit

Permalink
Merge pull request #15 from yu9824/dev
Browse files Browse the repository at this point in the history
v2.2.0dev0
  • Loading branch information
yu9824 authored Dec 3, 2023
2 parents 358f4bd + d840335 commit f8ed5e3
Show file tree
Hide file tree
Showing 17 changed files with 647 additions and 151 deletions.
9 changes: 6 additions & 3 deletions .github/workflows/pytest-on-each-version.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@
# - https://enu23456.hatenablog.com/entry/2022/11/24/195744
name: Test on each version

on: [push]
on:
push:
branches:
- 'main'

jobs:
build-linux:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.7", "3.8", "3.9", "3.10", "3.11"]
max-parallel: 5
python-version: ["3.7", "3.8", "3.9", "3.10", "3.11", "3.12"]
max-parallel: 6

steps:
- uses: actions/checkout@v3
Expand Down
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
This is an algorithm for evenly partitioning data in a `scikit-learn`-like interface.
(See [References](#References) for details of the algorithm.)

![simulation_gif](https://github.com/yu9824/kennard_stone/blob/main/example/simulate.gif?raw=true "simulation_gif")
![simulation_gif](https://github.com/yu9824/kennard_stone/blob/main/examples/simulate.gif?raw=true "simulation_gif")

## How to install

Expand All @@ -41,7 +41,7 @@ You need `numpy>=1.20` and `scikit-learn` to run.

You can use them like [scikit-learn](https://scikit-learn.org/stable/modules/classes.html#module-sklearn.model_selection).

See [example](https://github.com/yu9824/kennard_stone/tree/main/example) for details.
See [examples](https://github.com/yu9824/kennard_stone/tree/main/examples) for details.

In the following, `X` denotes an arbitrary explanatory variable and `y` an arbitrary objective variable.
And, `estimator` indicates an arbitrary prediction model that conforms to scikit-learn.
Expand Down Expand Up @@ -135,7 +135,7 @@ print(cross_validate(estimator, X, y, cv=5))
There is no notion of `random_state` or `shuffle` because the partitioning is determined uniquely for the dataset.
If these arguments are included, they do not cause an error. They simply have no effect on the result. Please be careful.

If you want to run the notebook in example directory,
If you want to run the notebook in examples directory,
you will need to additionally install `pandas`, `matplotlib`, `seaborn`, `tqdm`, and `jupyter` other than the packages in requirements.txt.

## Distance metrics
Expand Down Expand Up @@ -252,3 +252,8 @@ Copyright (c) 2021 yu9824

- Improve typing in `kennard_stone.train_test_split`
- Add some docstrings.

### v2.2.0

- Supports GPU calculations. (when metric is 'euclidean', 'manhattan', 'chebyshev' and 'minkowski')
- Supports Python 3.12
File renamed without changes.
151 changes: 151 additions & 0 deletions examples/cpu_vs_gpu.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# CPU vs GPU"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Device info\n",
"\n",
"- https://support.apple.com/kb/SP825"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from time import perf_counter\n",
"\n",
"from sklearn.datasets import fetch_california_housing\n",
"\n",
"# You need to install 'pytorch' when you use GPU\n",
"import torch\n",
"\n",
"from kennard_stone import train_test_split\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(20640, 8)"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"X = fetch_california_housing().data\n",
"X.shape\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## CPU"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Calculating pairwise distances using scikit-learn.\n",
"Calculating pairwise distances using scikit-learn.\n",
"CPU: 178.731 sec\n"
]
}
],
"source": [
"t_start = perf_counter()\n",
"_ = train_test_split(\n",
" X, test_size=0.2, n_jobs=-1, device=\"cpu\", metric=\"euclidean\"\n",
")\n",
"t_end = perf_counter()\n",
"t_cpu = t_end - t_start\n",
"print(f\"CPU: {t_cpu:.3f} sec\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## GPU"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Calculating pairwise distances using PyTorch on 'mps'.\n",
"Calculating pairwise distances using PyTorch on 'mps'.\n",
"mps: 33.525 sec\n"
]
}
],
"source": [
"# cuda\n",
"if torch.cuda.is_available():\n",
" device = \"cuda\"\n",
"# mps (M1 Mac)\n",
"elif torch.backends.mps.is_available():\n",
" device = \"mps\"\n",
"else:\n",
" raise RuntimeError(\"GPU is not available.\")\n",
"\n",
"t_start = perf_counter()\n",
"_ = train_test_split(\n",
" X, test_size=0.2, n_jobs=-1, device=device, metric=\"euclidean\"\n",
")\n",
"t_end = perf_counter()\n",
"t_gpu = t_end - t_start\n",
"print(f\"{device}: {t_gpu:.3f} sec\")\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "kennard-stone",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.12"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes
2 changes: 1 addition & 1 deletion kennard_stone/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from .kennard_stone import KFold, train_test_split

__version__ = "2.1.6"
__version__ = "2.2.0dev0"
__license__ = "MIT"
__author__ = "yu9824"
__copyright__ = "Copyright © 2021 yu9824"
Expand Down
Loading

0 comments on commit f8ed5e3

Please sign in to comment.