【问题标题】:How to oversample a 3d array?如何过采样 3d 数组?
【发布时间】:2020-08-28 06:00:06
【问题描述】:

我正在尝试根据 2 个特征来预测新闻文章的类别:作者姓名和文章标题。

我使用 CountVectorizer 和 TfidfTransformer 分别转换了两列。因此,我现在拥有的是一个 3D 数组(即数组列表的数组),每一行包含每个数据实例的 [author_tfid, summary_tfid]:

X_train = array([[array([0., 3., 0., ..., 0., 4., 0.]),
                  array([0., 0., 3., ..., 0., 0., 0.])],
                 [array([0., 0., 0., ..., 0., 0., 9.]),
                  array([1., 0., 0., ..., 0., 0., 0.])],
                 [array([2., 0., 0., ..., 0., 0., 0.]),
                  array([0., 0., 0., ..., 0., 5., 0.])],

但是,当我尝试使用 imblearn 的 RandomOversampler.fit_transform(X_train) 时,出现以下错误:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-44-210227188cde> in <module>()
----> 1 X_oversampled, y_oversampled = oversampler.fit_resample(X, y)

4 frames
/usr/local/lib/python3.6/dist-packages/sklearn/utils/validation.py in _assert_all_finite(X, allow_nan, msg_dtype)
     62     # for object dtype data, we only check for NaNs (GH-13254)
     63     elif X.dtype == np.dtype('object') and not allow_nan:
---> 64         if _object_dtype_isnan(X).any():
     65             raise ValueError("Input contains NaN")
     66 

AttributeError: 'bool' object has no attribute 'any'

尝试搜索论坛和谷歌,但似乎无法找到有此问题的人。所以想找出对 3D 数组进行过采样的错误/正确方法。

【问题讨论】:

    标签: python numpy resampling oversampling imblearn


    【解决方案1】:

    您必须将二维数组传递给过采样器。因此,请尝试在同一行上连接作者和摘要特征

    X = np.array([[np.array([0., 3., 0., 0., 4., 0.]),
                      np.array([0., 0., 3., 0., 0., 0.])],
                     [np.array([0., 0., 0., 0., 0., 9.]),
                      np.array([1., 0., 0., 0., 0., 0.])],
                     [np.array([2., 0., 0., 0., 0., 0.]),
                      np.array([0., 0., 0., 0., 5., 0.])]])
    
    X = X.reshape(len(X),-1)
    y = np.array([0,1,1])
    
    oversampler = RandomOverSampler(random_state=42)
    X_oversampled, y_oversampled = oversampler.fit_resample(X, y)
    

    【讨论】:

      猜你喜欢
      • 2018-01-28
      • 2021-02-11
      • 1970-01-01
      • 1970-01-01
      • 2020-02-09
      • 2019-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多