如何对一个3D数组进行过采样?

问题描述 投票:1回答:1

我试图根据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个回答
0
投票

你必须将一个二维数组传递给超采样器。因此,尝试将作者和摘要的特征连接到同一行

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)
© www.soinside.com 2019 - 2024. All rights reserved.