【问题标题】:SMOTE is giving array size / ValueError for all-categorical datasetSMOTE 为所有分类数据集提供数组大小/ValueError
【发布时间】:2020-09-01 14:42:37
【问题描述】:

我正在使用 SMOTE-NC 对分类数据进行过采样。我只有 1 个特征和 10500 个样本。

在运行以下代码时,我收到了错误:

   ---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-151-a261c423a6d8> in <module>()
     16 print(X_new.shape) # (10500, 1)
     17 print(X_new)
---> 18 sm.fit_sample(X_new, Y_new)

~\AppData\Local\Continuum\Miniconda3\envs\data-science\lib\site-packages\imblearn\base.py in fit_resample(self, X, y)
     81         )
     82 
---> 83         output = self._fit_resample(X, y)
     84 
     85         y_ = (label_binarize(output[1], np.unique(y))

~\AppData\Local\Continuum\Miniconda3\envs\data-science\lib\site-packages\imblearn\over_sampling\_smote.py in _fit_resample(self, X, y)
    926 
    927         X_continuous = X[:, self.continuous_features_]
--> 928         X_continuous = check_array(X_continuous, accept_sparse=["csr", "csc"])
    929         X_minority = _safe_indexing(
    930             X_continuous, np.flatnonzero(y == class_minority)

~\AppData\Local\Continuum\Miniconda3\envs\data-science\lib\site-packages\sklearn\utils\validation.py in check_array(array, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, warn_on_dtype, estimator)
    592                              " a minimum of %d is required%s."
    593                              % (n_features, array.shape, ensure_min_features,
--> 594                                 context))
    595 
    596     if warn_on_dtype and dtype_orig is not None and array.dtype != dtype_orig:

ValueError: Found array with 0 feature(s) (shape=(10500, 0)) while a minimum of 1 is required.

代码:

from imblearn.over_sampling import SMOTE
from imblearn.over_sampling import SMOTENC

sm = SMOTENC(random_state=27,categorical_features=[0,])

X_new = np.array(X_train.values.tolist())
Y_new = np.array(y_train.values.tolist())

print(X_new.shape) # (10500,)
print(Y_new.shape) # (10500,)

X_new = np.reshape(X_new, (-1, 1)) # SMOTE require 2-D Array, Hence changing the shape of X_mew

print(X_new.shape) # (10500, 1)
print(X_new)
sm.fit_sample(X_new, Y_new)

如果我理解正确,X_new 的形状应该是 (n_samples, n_features),即 10500 X 1。我不确定为什么在 ValueError 中将其视为 shape=(10500,0)

有人可以帮我吗?

【问题讨论】:

  • 愿意接受答案,因为它使用代码和理论清楚明确地解释和证明了您的问题?

标签: python machine-learning imbalanced-data imblearn smote


【解决方案1】:

我已经复制了您的问题,将 docs 中的示例改编为数据中的单个分类特征:

from collections import Counter
from numpy.random import RandomState
from sklearn.datasets import make_classification
from imblearn.over_sampling import SMOTENC

X, y = make_classification(n_classes=2, class_sep=2,
 weights=[0.1, 0.9], n_informative=1, n_redundant=0, flip_y=0,
 n_features=1, n_clusters_per_class=1, n_samples=1000, random_state=10)

# simulate the only column to be a categorical feature
X[:, 0] = RandomState(10).randint(0, 4, size=(1000))
X.shape
# (1000, 1)

sm = SMOTENC(random_state=42, categorical_features=[0,]) # same behavior with categorical_features=[0]

X_res, y_res = sm.fit_resample(X, y)

同样的错误:

ValueError: Found array with 0 feature(s) (shape=(1000, 0)) while a minimum of 1 is required.

原因其实很简单,但是要对原来的SMOTE paper稍加挖掘;引用相关的section(强调我的):

虽然我们的 SMOTE 方法目前处理具有 all 的数据集 名义特征,它被泛化为处理混合数据集 连续和名义特征。我们称这种方法为综合 少数过采样技术-标称连续[SMOTE-NC]。我们 在 UCI 存储库中的成人数据集上测试了这种方法。这 SMOTE-NC算法描述如下。

  1. 中值计算:计算少数类所有连续特征的标准差的中值。如果名义上 样本与其潜在的最近邻居之间的特征不同, 然后这个中值被包含在欧几里得距离计算中。我们 使用中值对标称特征的差异进行一定程度的惩罚 这与连续特征的典型差异有关 价值观。
  2. 最近邻计算:计算k-最近邻所对应的特征向量之间的欧几里得距离 识别(少数类样本)和其他特征向量 (少数类样本)使用连续特征空间。对于每一个 考虑的特征向量和 其潜在的最近邻,包括标准的中位数 之前计算的偏差,在欧几里得距离计算中。

换句话说,虽然没有明确说明,但很明显,为了使算法工作,它需要至少一个连续特征。这不是这里的情况,因此该算法毫无疑问会失败。

我猜,在内部,在第 1 步(中值计算)期间,算法会暂时从数据中删除所有分类特征;在这里这样做时,它确实面临(1000, 0)(或在您的情况下为(10500, 0))的形状,即没有数据,因此错误消息中的具体参考。

所以,这里没有任何实际的编程问题需要解决,只是你尝试做的事情实际上是不可能使用 SMOTE-NC 算法的(请注意,算法名称中的首字母 NC 表示 Nominal - 连续)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-03
    • 2021-03-28
    • 2021-11-14
    • 1970-01-01
    • 1970-01-01
    • 2022-10-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多