样本扩充与平衡问题
在多分类样本集的训练学习中,通常会遇到各类别样本集不平衡的问题,它会导致模型训练过程的总类别样本集训练的病态。
离散化标签数据集
在常规的监督学习过程中,会先将输入数据集经过提取、转换与加载等操作,得到两组数据集:特征数据集 X X X 与标签数据集 y y y。
import numpy
unique = numpy.unique(y) # return a discretized scalar
探索标签数值分布
对于每一个离散的类别数值,需要找到根据标签列值 y y y 误差范围在 1 e − 1 1e-1 1e−1 左右的样本集扩充索引值:
index = np.argwhere(abs(y - value) < 1e-1) # return a scalar with noted index
按扩充比例平衡样本集
在得到每个离散类别的扩充起始位置后,根据默认设置的扩充比例 expand_base 来平衡各类别的样本集:
interval = expand_base // index.shape[0] # ensure examples are balanced
基于numpy的实现方法
如下方法对于多分类样本集的各类别样本信息的重复利用与学习:
def expand(X, y, expand_base: int=10):
'''
:param X: train data with n * m, numpy.ndarray.
:param y: correct labels with scalar at n, numpy.ndarray.
'''
for value in unique:
index = np.argwhere(abs(y - value) < 1e-1) # explore values within eps
interval = expand_base // index.shape[0] # ensure examples are balanced
for i, indice in enumerate(index):
piece = indice[0] + i * interval
example = X[piece].reshape(1, -1)
multiX, multiy = np.repeat(example, interval, axis=0), np.repeat(y[piece], interval)
X = np.insert(X, piece + 1, multiX, axis=0)
y = np.insert(y, piece + 1, multiy)
return X, y