【问题标题】:Python: ValueError: could not convert string to float when apply for down-samplingPython:ValueError:申请下采样时无法将字符串转换为浮点数
【发布时间】:2018-03-10 17:31:36
【问题描述】:

我有如下不平衡数据集

id text               category
1  comment1               0 
2  comment2               0 
3  comment3               1 
4  comment4               0 

我已经通过删除数值和应用词干对“文本”进行了预处理。

接下来,我将数据拆分为训练集和测试集以进行验证。

X_train, X_test, y_train, y_test = train_test_split(data['text'], data['category'])

然后,我在我的训练数据集上应用下采样方法

from imblearn.under_sampling import RandomUnderSampler

rus = RandomUnderSampler(return_indices=True)

train_X_resampled, train_y_resampled, idx_resampled = rus.sample(X_train, y_train)

但是,当我收到如下错误时。我可以知道如何修复错误吗?

ValueError: could not convert string to float: 'comment2'

【问题讨论】:

  • 您不能使用文本数据。您需要使用不同的技术完全替换它。
  • 在将数据拆分到训练和测试集进行验证之前,我先应用下采样如何?我知道如果使用 SMOTE 或其他合成采样方法会容易出现数据泄漏。但是,由于我使用的是下采样,因此我的数据不需要替换。我是否可以应用下采样方法然后只进行训练测试拆分?因为只有在我对训练数据应用采样后才会出现错误
  • 没关系。您使用的库不支持文本数据。你需要替换它。
  • 我明白了。那么如果我尝试更改为其他库的采样方法呢? (例如 sklearn.utils.resample)因为我尝试了另一种方法,首先是下采样,然后只应用训练测试拆分,并且在使用 sklearn.utils.resample 后没有任何问题。但是,我只是担心在训练测试拆分后应用下采样是一种错误的方法

标签: python string scikit-learn text-classification downsampling


【解决方案1】:

imblearn 不支持dataframes,将您感兴趣的列转换为列表,然后使用np.array(list(data['text'])).reshape(-1, 1) 将其重塑为二维数组,这样就可以了。

【讨论】:

    猜你喜欢
    • 2015-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-15
    • 1970-01-01
    相关资源
    最近更新 更多