-
Notifications
You must be signed in to change notification settings - Fork 867
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
456 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
235 changes: 235 additions & 0 deletions
235
docs/sources/user_guide/data/make_multiplexer_dataset.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,235 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Make Multiplexer Dataset" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Function that creates a dataset generated by a n-bit Boolean multiplexer for evaluating supervised learning algorithms." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"> `from mlxtend.data import make_multiplexer_dataset` " | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Overview" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"The `make_multiplexer_dataset` function creates a dataset generated by an n-bit Boolean multiplexer. Such dataset represents a dataset generated by a simple rule, based on the behavior of a electric multiplexer, yet presents a relatively challenging classification problem for supervised learning algorithm with interactions between features (epistasis) as it may be encountered in many real-world scenarios [1].\n", | ||
"\n", | ||
"The following illustration depicts a 6-bit multiplexer that consists of 2 address bits and 4 register bits. The address bits converted to decimal representation point to a position in the register bit. For example, if the address bits are \"00\" (0 in decimal), the address bits point to the register bit at position 0. The value of the register position pointed to determines the class label. For example, if the register bit at position is 0, the class label is 0. Vice versa, if the register bit at position 0 is 1, the class label is 1. \n", | ||
"\n", | ||
"![](make_multiplexer_dataset_data_files/6bit_multiplexer.png)\n", | ||
"\n", | ||
"\n", | ||
"In the example above, the address bits \"10\" (2 in decimal) point to the 3rd register position (as we start counting from index 0), which has a bit value of 1. Hence, the class label is 1.\n", | ||
"\n", | ||
"Below are a few more examples:\n", | ||
"\n", | ||
"1. Address bits: [0, 1], register bits: [1, 0, 1, 1], class label: 0\n", | ||
"2. Address bits: [0, 1], register bits: [1, 1, 1, 0], class label: 1\n", | ||
"3. Address bits: [1, 0], register bits: [1, 0, 0, 1], class label: 0\n", | ||
"4. Address bits: [1, 1], register bits: [1, 1, 1, 0], class label: 0\n", | ||
"5. Address bits: [0, 1], register bits: [0, 1, 1, 0], class label: 1\n", | ||
"6. Address bits: [0, 1], register bits: [1, 0, 0, 1], class label: 0\n", | ||
"7. Address bits: [0, 1], register bits: [0, 1, 1, 1], class label: 1\n", | ||
"8. Address bits: [0, 1], register bits: [0, 0, 0, 0], class label: 0\n", | ||
"9. Address bits: [1, 0], register bits: [1, 0, 1, 1], class label: 1\n", | ||
"10. Address bits: [0, 1], register bits: [1, 1, 1, 1], class label: 1\n", | ||
"\n", | ||
"Note that in the implementation of the multiplexer function, if the number of address bits is set to 2, this results in a 6 bit multiplexer as two bit can have 2^2=4 different register positions (2 bit + 4 bit = 6 bit). However, if we choose 3 address bits instead, 2^3=8 positions would be covered, resulting in a 11 bit (3 bit + 8 bit = 11 bit) multiplexer, and so forth.\n", | ||
"\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### References\n", | ||
"\n", | ||
"- [1] Urbanowicz, R. J., & Browne, W. N. (2017). *Introduction to Learning Classifier Systems*. Springer." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Example 1 -- 6-bit multiplexer" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"This simple example illustrates how to create dataset from a 6-bit multiplexer" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Features:\n", | ||
" [[0 1 0 1 0 1]\n", | ||
" [1 0 0 0 1 1]\n", | ||
" [0 1 1 1 0 0]\n", | ||
" [0 1 1 1 0 0]\n", | ||
" [0 0 1 1 0 0]\n", | ||
" [0 1 0 0 0 0]\n", | ||
" [0 1 1 0 1 1]\n", | ||
" [1 0 1 0 0 0]\n", | ||
" [1 0 0 1 0 1]\n", | ||
" [1 0 1 0 0 1]]\n", | ||
"\n", | ||
"Class labels:\n", | ||
" [1 1 1 1 1 0 0 0 0 0]\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"import numpy as np\n", | ||
"from mlxtend.data import make_multiplexer_dataset\n", | ||
"\n", | ||
"\n", | ||
"X, y = make_multiplexer_dataset(address_bits=2, \n", | ||
" sample_size=10,\n", | ||
" positive_class_ratio=0.5, \n", | ||
" shuffle=False,\n", | ||
" random_seed=123)\n", | ||
"\n", | ||
"print('Features:\\n', X)\n", | ||
"print('\\nClass labels:\\n', y)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## API" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"## make_multiplexer_dataset\n", | ||
"\n", | ||
"*make_multiplexer_dataset(address_bits=2, sample_size=100, positive_class_ratio=0.5, shuffle=False, random_seed=None)*\n", | ||
"\n", | ||
"Function to create a binary n-bit multiplexer dataset.\n", | ||
"\n", | ||
"New in mlxtend v0.9\n", | ||
"\n", | ||
"**Parameters**\n", | ||
"\n", | ||
"- `address_bits` : int (default: 2)\n", | ||
"\n", | ||
" A positive integer that determines the number of address\n", | ||
" bits in the multiplexer, which in turn determine the\n", | ||
" n-bit capacity of the multiplexer and therefore the\n", | ||
" number of features. The number of features is determined by\n", | ||
" the number of address bits. For example, 2 address bits\n", | ||
" will result in a 6 bit multiplexer and consequently\n", | ||
" 6 features (2 + 2^2 = 6). If `address_bits=3`, then\n", | ||
" this results in an 11-bit multiplexer as (2 + 2^3 = 11)\n", | ||
" with 11 features.\n", | ||
"\n", | ||
"\n", | ||
"- `sample_size` : int (default: 100)\n", | ||
"\n", | ||
" The total number of samples generated.\n", | ||
"\n", | ||
"\n", | ||
"- `positive_class_ratio` : float (default: 0.5)\n", | ||
"\n", | ||
" The fraction (a float between 0 and 1)\n", | ||
" of samples in the `sample_size`d dataset\n", | ||
" that have class label 1.\n", | ||
" If `positive_class_ratio=0.5` (default), then\n", | ||
" the ratio of class 0 and class 1 samples is perfectly balanced.\n", | ||
"\n", | ||
"\n", | ||
"- `shuffle` : Bool (default: False)\n", | ||
"\n", | ||
" Whether or not to shuffle the features and labels.\n", | ||
" If `False` (default), the samples are returned in sorted\n", | ||
" order starting with `sample_size`/2 samples with class label 0\n", | ||
" and followed by `sample_size`/2 samples with class label 1.\n", | ||
"\n", | ||
"\n", | ||
"- `random_seed` : int (default: None)\n", | ||
"\n", | ||
" Random seed used for generating the multiplexer samples and shuffling.\n", | ||
"\n", | ||
"**Returns**\n", | ||
"\n", | ||
"- `X, y` : [n_samples, n_features], [n_class_labels]\n", | ||
"\n", | ||
" X is the feature matrix with the number of samples equal\n", | ||
" to `sample_size`. The number of features is determined by\n", | ||
" the number of address bits. For instance, 2 address bits\n", | ||
" will result in a 6 bit multiplexer and consequently\n", | ||
" 6 features (2 + 2^2 = 6).\n", | ||
" All features are binary (values in {0, 1}).\n", | ||
" y is a 1-dimensional array of class labels in {0, 1}.\n", | ||
"\n", | ||
"\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"with open('../../api_modules/mlxtend.data/make_multiplexer_dataset.md', 'r') as f:\n", | ||
" s = f.read() \n", | ||
"print(s)" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"anaconda-cloud": {}, | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"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.6.1" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 1 | ||
} |
Binary file added
BIN
+18.9 KB
docs/sources/user_guide/data/make_multiplexer_dataset_data_files/6bit_multiplexer.pdf
Binary file not shown.
Binary file added
BIN
+47.9 KB
...ources/user_guide/data/make_multiplexer_dataset_data_files/6bit_multiplexer.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
# Sebastian Raschka 2014-2017 | ||
# mlxtend Machine Learning Library Extensions | ||
# | ||
# A function for creating a multiplexer dataset for classification. | ||
# Author: Sebastian Raschka <sebastianraschka.com> | ||
# | ||
# License: BSD 3 clause | ||
|
||
import numpy as np | ||
|
||
|
||
def make_multiplexer_dataset(address_bits=2, sample_size=100, | ||
positive_class_ratio=0.5, shuffle=False, | ||
random_seed=None): | ||
""" | ||
Function to create a binary n-bit multiplexer dataset. | ||
New in mlxtend v0.9 | ||
Parameters | ||
--------------- | ||
address_bits : int (default: 2) | ||
A positive integer that determines the number of address | ||
bits in the multiplexer, which in turn determine the | ||
n-bit capacity of the multiplexer and therefore the | ||
number of features. The number of features is determined by | ||
the number of address bits. For example, 2 address bits | ||
will result in a 6 bit multiplexer and consequently | ||
6 features (2 + 2^2 = 6). If `address_bits=3`, then | ||
this results in an 11-bit multiplexer as (2 + 2^3 = 11) | ||
with 11 features. | ||
sample_size : int (default: 100) | ||
The total number of samples generated. | ||
positive_class_ratio : float (default: 0.5) | ||
The fraction (a float between 0 and 1) | ||
of samples in the `sample_size`d dataset | ||
that have class label 1. | ||
If `positive_class_ratio=0.5` (default), then | ||
the ratio of class 0 and class 1 samples is perfectly balanced. | ||
shuffle : Bool (default: False) | ||
Whether or not to shuffle the features and labels. | ||
If `False` (default), the samples are returned in sorted | ||
order starting with `sample_size`/2 samples with class label 0 | ||
and followed by `sample_size`/2 samples with class label 1. | ||
random_seed : int (default: None) | ||
Random seed used for generating the multiplexer samples and shuffling. | ||
Returns | ||
-------- | ||
X, y : [n_samples, n_features], [n_class_labels] | ||
X is the feature matrix with the number of samples equal | ||
to `sample_size`. The number of features is determined by | ||
the number of address bits. For instance, 2 address bits | ||
will result in a 6 bit multiplexer and consequently | ||
6 features (2 + 2^2 = 6). | ||
All features are binary (values in {0, 1}). | ||
y is a 1-dimensional array of class labels in {0, 1}. | ||
""" | ||
|
||
if not isinstance(address_bits, int): | ||
raise AttributeError('address_bits' | ||
' must be an integer. Got %s.' % | ||
type(address_bits)) | ||
if address_bits < 1: | ||
raise AttributeError('Number of address_bits' | ||
' must be greater than 0. Got %s.' % address_bits) | ||
register_bits = 2**address_bits | ||
total_bits = address_bits + register_bits | ||
X_pos, y_pos = [], [] | ||
X_neg, y_neg = [], [] | ||
|
||
# use numpy's instead of python's round because of consistent | ||
# banker's rounding behavior across versions | ||
n_positives = np.round(sample_size*positive_class_ratio).astype(np.int) | ||
n_negatives = sample_size - n_positives | ||
|
||
rng = np.random.RandomState(random_seed) | ||
|
||
def gen_randsample(): | ||
all_bits = [rng.randint(0, 2) for i in range(total_bits)] | ||
address_str = ''.join(str(c) for c in all_bits[:address_bits]) | ||
register_pos = int(address_str, base=2) | ||
class_label = all_bits[address_bits:][register_pos] | ||
return all_bits, class_label | ||
|
||
while len(y_pos) < n_positives or len(y_neg) < n_negatives: | ||
|
||
all_bits, class_label = gen_randsample() | ||
|
||
if class_label and len(y_pos) < n_positives: | ||
X_pos.append(all_bits) | ||
y_pos.append(class_label) | ||
|
||
elif not class_label and len(y_neg) < n_negatives: | ||
X_neg.append(all_bits) | ||
y_neg.append(class_label) | ||
|
||
X, y = X_pos + X_neg, y_pos + y_neg | ||
X, y = np.array(X, dtype=np.int), np.array(y, dtype=np.int) | ||
|
||
if shuffle: | ||
p = rng.permutation(y.shape[0]) | ||
X, y = X[p], y[p] | ||
|
||
return X, y |
8 changes: 8 additions & 0 deletions
8
mlxtend/data/tests/test_data.py → mlxtend/data/tests/test_datasets.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.