40 lines
1.0 KiB
Python
40 lines
1.0 KiB
Python
|
# Sebastian Raschka 2014-2020
|
||
|
# mlxtend Machine Learning Library Extensions
|
||
|
#
|
||
|
# Base Clusteer (Clutering Parent Class)
|
||
|
# Author: Sebastian Raschka <sebastianraschka.com>
|
||
|
#
|
||
|
# License: BSD 3 clause
|
||
|
|
||
|
import numpy as np
|
||
|
|
||
|
|
||
|
class _MultiClass(object):
|
||
|
|
||
|
def __init__(self):
|
||
|
pass
|
||
|
|
||
|
def _one_hot(self, y, n_labels, dtype):
|
||
|
"""Returns a matrix where each sample in y is represented
|
||
|
as a row, and each column represents the class label in
|
||
|
the one-hot encoding scheme.
|
||
|
|
||
|
Example:
|
||
|
|
||
|
y = np.array([0, 1, 2, 3, 4, 2])
|
||
|
mc = _BaseMultiClass()
|
||
|
mc._one_hot(y=y, n_labels=5, dtype='float')
|
||
|
|
||
|
np.array([[1., 0., 0., 0., 0.],
|
||
|
[0., 1., 0., 0., 0.],
|
||
|
[0., 0., 1., 0., 0.],
|
||
|
[0., 0., 0., 1., 0.],
|
||
|
[0., 0., 0., 0., 1.],
|
||
|
[0., 0., 1., 0., 0.]])
|
||
|
|
||
|
"""
|
||
|
mat = np.zeros((len(y), n_labels))
|
||
|
for i, val in enumerate(y):
|
||
|
mat[i, val] = 1
|
||
|
return mat.astype(dtype)
|