# keras-balanced-batch-generator: A Keras-compatible generator for creating balanced batches
[![PyPI](https://img.shields.io/pypi/v/keras-balanced-batch-generator.svg)](https://pypi.org/project/keras-balanced-batch-generator/)
[![MIT license](https://img.shields.io/badge/license-MIT-brightgreen.svg)](https://opensource.org/licenses/MIT)
## Installation
```bash
pip install keras-balanced-batch-generator
```
## Overview
This module implements an over-sampling algorithm to address the issue of class imbalance.
It generates *balanced batches*, i.e., batches in which the number of samples from each class is on average the same.
Generated batches are also shuffled.
The generator can be easily used with Keras models'
[`fit`](https://www.tensorflow.org/api_docs/python/tf/keras/Model#fit) method.
Currently, only [NumPy arrays](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html) for single-input, single-output models are supported.
## API
```python
make_generator(x, y, batch_size,
categorical=True,
seed=None)
```
- **`x`** *(numpy.ndarray)* Input data. Must have the same length as `y`.
- **`y`** *(numpy.ndarray)* Target data. Must be a binary class matrix (i.e., shape `(num_samples, num_classes)`).
You can use [`keras.utils.to_categorical`](https://www.tensorflow.org/api_docs/python/tf/keras/utils/to_categorical) to convert a class vector to a binary class matrix.
- **`batch_size`** *(int)* Batch size.
- **`categorical`** *(bool)* If true, generates binary class matrices (i.e., shape `(num_samples, num_classes)`) for batch targets.
Otherwise, generates class vectors (i.e., shape `(num_samples,)`).
- **`seed`** Random seed (see the [docs](https://docs.python.org/3/library/random.html#random.seed)).
- Returns a Keras-compatible generator yielding batches as `(x, y)` tuples.
## Usage
```python
import keras
from keras_balanced_batch_generator import make_generator
x = ...
y = ...
batch_size = ...
steps_per_epoch = ...
model = keras.models.Sequential(...)
generator = make_generator(x, y, batch_size)
model.fit(generator, steps_per_epoch=steps_per_epoch)
```
## Example: Multiclass Classification
```python
import numpy as np
import keras
from keras_balanced_batch_generator import make_generator
num_samples = 100
num_classes = 3
input_shape = (2,)
batch_size = 16
x = np.random.rand(num_samples, *input_shape)
y = np.random.randint(low=0, high=num_classes, size=num_samples)
y = keras.utils.to_categorical(y)
generator = make_generator(x, y, batch_size)
model = keras.models.Sequential()
model.add(keras.layers.Dense(32, input_shape=input_shape, activation='relu'))
model.add(keras.layers.Dense(num_classes, activation='softmax'))
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(generator, steps_per_epoch=10, epochs=5)
```
## Example: Binary Classification
```python
import numpy as np
import keras
from keras_balanced_batch_generator import make_generator
num_samples = 100
num_classes = 2
input_shape = (2,)
batch_size = 16
x = np.random.rand(num_samples, *input_shape)
y = np.random.randint(low=0, high=num_classes, size=num_samples)
y = keras.utils.to_categorical(y)
generator = make_generator(x, y, batch_size, categorical=False)
model = keras.models.Sequential()
model.add(keras.layers.Dense(32, input_shape=input_shape, activation='relu'))
model.add(keras.layers.Dense(1, activation='sigmoid'))
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['binary_accuracy'])
model.fit(generator, steps_per_epoch=10, epochs=5)
```
Raw data
{
"_id": null,
"home_page": "https://github.com/soroushj/keras-balanced-batch-generator",
"name": "keras-balanced-batch-generator",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.0",
"maintainer_email": null,
"keywords": "keras, generator",
"author": "Soroush Javadi",
"author_email": "soroush.javadi@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/83/81/fa6d65eec8b79d06c658548240eae447746d35911ee89297ca53af6b92e3/keras-balanced-batch-generator-0.0.3.tar.gz",
"platform": null,
"description": "# keras-balanced-batch-generator: A Keras-compatible generator for creating balanced batches\n\n[![PyPI](https://img.shields.io/pypi/v/keras-balanced-batch-generator.svg)](https://pypi.org/project/keras-balanced-batch-generator/)\n[![MIT license](https://img.shields.io/badge/license-MIT-brightgreen.svg)](https://opensource.org/licenses/MIT)\n\n## Installation\n\n```bash\npip install keras-balanced-batch-generator\n```\n\n## Overview\n\nThis module implements an over-sampling algorithm to address the issue of class imbalance.\nIt generates *balanced batches*, i.e., batches in which the number of samples from each class is on average the same.\nGenerated batches are also shuffled.\n\nThe generator can be easily used with Keras models'\n[`fit`](https://www.tensorflow.org/api_docs/python/tf/keras/Model#fit) method.\n\nCurrently, only [NumPy arrays](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html) for single-input, single-output models are supported.\n\n## API\n\n```python\nmake_generator(x, y, batch_size,\n categorical=True,\n seed=None)\n```\n\n- **`x`** *(numpy.ndarray)* Input data. Must have the same length as `y`.\n- **`y`** *(numpy.ndarray)* Target data. Must be a binary class matrix (i.e., shape `(num_samples, num_classes)`).\n You can use [`keras.utils.to_categorical`](https://www.tensorflow.org/api_docs/python/tf/keras/utils/to_categorical) to convert a class vector to a binary class matrix.\n- **`batch_size`** *(int)* Batch size.\n- **`categorical`** *(bool)* If true, generates binary class matrices (i.e., shape `(num_samples, num_classes)`) for batch targets.\n Otherwise, generates class vectors (i.e., shape `(num_samples,)`).\n- **`seed`** Random seed (see the [docs](https://docs.python.org/3/library/random.html#random.seed)).\n- Returns a Keras-compatible generator yielding batches as `(x, y)` tuples.\n\n## Usage\n\n```python\nimport keras\nfrom keras_balanced_batch_generator import make_generator\n\nx = ...\ny = ...\nbatch_size = ...\nsteps_per_epoch = ...\nmodel = keras.models.Sequential(...)\n\ngenerator = make_generator(x, y, batch_size)\nmodel.fit(generator, steps_per_epoch=steps_per_epoch)\n```\n\n## Example: Multiclass Classification\n\n```python\nimport numpy as np\nimport keras\nfrom keras_balanced_batch_generator import make_generator\n\nnum_samples = 100\nnum_classes = 3\ninput_shape = (2,)\nbatch_size = 16\n\nx = np.random.rand(num_samples, *input_shape)\ny = np.random.randint(low=0, high=num_classes, size=num_samples)\ny = keras.utils.to_categorical(y)\n\ngenerator = make_generator(x, y, batch_size)\n\nmodel = keras.models.Sequential()\nmodel.add(keras.layers.Dense(32, input_shape=input_shape, activation='relu'))\nmodel.add(keras.layers.Dense(num_classes, activation='softmax'))\nmodel.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\nmodel.fit(generator, steps_per_epoch=10, epochs=5)\n```\n\n## Example: Binary Classification\n\n```python\nimport numpy as np\nimport keras\nfrom keras_balanced_batch_generator import make_generator\n\nnum_samples = 100\nnum_classes = 2\ninput_shape = (2,)\nbatch_size = 16\n\nx = np.random.rand(num_samples, *input_shape)\ny = np.random.randint(low=0, high=num_classes, size=num_samples)\ny = keras.utils.to_categorical(y)\n\ngenerator = make_generator(x, y, batch_size, categorical=False)\n\nmodel = keras.models.Sequential()\nmodel.add(keras.layers.Dense(32, input_shape=input_shape, activation='relu'))\nmodel.add(keras.layers.Dense(1, activation='sigmoid'))\nmodel.compile(optimizer='adam', loss='binary_crossentropy', metrics=['binary_accuracy'])\nmodel.fit(generator, steps_per_epoch=10, epochs=5)\n```\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "A Keras-compatible generator for creating balanced batches",
"version": "0.0.3",
"project_urls": {
"Homepage": "https://github.com/soroushj/keras-balanced-batch-generator"
},
"split_keywords": [
"keras",
" generator"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "15511b9422c0eaffbcfdb1218d84ed7d19b0baf4812e311be77a8b7572ecb7b7",
"md5": "28a0b5b7b9b9f8ee11b29360e1fea9b8",
"sha256": "451be1436210fae2cabed652714443031dca6089d8f7403916ae4077b36add35"
},
"downloads": -1,
"filename": "keras_balanced_batch_generator-0.0.3-py3-none-any.whl",
"has_sig": false,
"md5_digest": "28a0b5b7b9b9f8ee11b29360e1fea9b8",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.0",
"size": 4692,
"upload_time": "2024-03-23T06:53:21",
"upload_time_iso_8601": "2024-03-23T06:53:21.664677Z",
"url": "https://files.pythonhosted.org/packages/15/51/1b9422c0eaffbcfdb1218d84ed7d19b0baf4812e311be77a8b7572ecb7b7/keras_balanced_batch_generator-0.0.3-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "8381fa6d65eec8b79d06c658548240eae447746d35911ee89297ca53af6b92e3",
"md5": "19ecb2f4985026041cfb211dcd14b1dc",
"sha256": "b074cec865b4afa2422a68368b84b7ec32bc7d7ba853564d873f92f94d8b3719"
},
"downloads": -1,
"filename": "keras-balanced-batch-generator-0.0.3.tar.gz",
"has_sig": false,
"md5_digest": "19ecb2f4985026041cfb211dcd14b1dc",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.0",
"size": 4146,
"upload_time": "2024-03-23T06:53:23",
"upload_time_iso_8601": "2024-03-23T06:53:23.161526Z",
"url": "https://files.pythonhosted.org/packages/83/81/fa6d65eec8b79d06c658548240eae447746d35911ee89297ca53af6b92e3/keras-balanced-batch-generator-0.0.3.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-03-23 06:53:23",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "soroushj",
"github_project": "keras-balanced-batch-generator",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "keras-balanced-batch-generator"
}