不平衡学习的函数Sampler抛出值

2024-09-29 17:15:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我想使用来自imblearn的类FunctionSampler来创建我自己的自定义类,以便对数据集进行重新采样。

我有一个包含每个主题路径的一维功能系列和一个包含每个主题标签的标签系列。两者都来自pd.DataFrame。我知道我必须首先重塑特征数组,因为它是一维的。

当我使用类RandomUnderSampler时,一切正常,但是如果我先将特性和标签传递给FunctionSamplerfit_resample方法,该方法随后创建RandomUnderSampler的实例,然后在此类上调用fit_resample,则会出现以下错误:

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

下面是一个产生错误的最小示例:

import pandas as pd
from imblearn.under_sampling import RandomUnderSampler
from imblearn import FunctionSampler

# create one dimensional feature and label arrays X and y
# X has to be converted to numpy array and then reshaped. 
X = pd.Series(['path_1','path_2','path_3'])
X = X.values.reshape(-1,1)
y = pd.Series([1,0,0])

第一种方法(工程)

^{pr2}$

第二种方法(不起作用)

def resample(X, y):
    return RandomUnderSampler().fit_resample(X, y)

sampler = FunctionSampler(func=resample)
X_res, y_res = sampler.fit_resample(X, y)

有人知道这里出了什么问题吗?似乎FunctionSamplerfit_resample方法不等于RandomUnderSamplerfit_resample方法。。。在


Tags: andtopath方法fromimport主题错误
1条回答
网友
1楼 · 发布于 2024-09-29 17:15:51

您的FunctionSampler的实现是正确的。问题出在你的数据集上。在

RandomUnderSampler似乎也适用于文本数据。没有使用^{}进行检查。在

但是FunctionSampler()有这个检查,见{a2}

from sklearn.utils import check_X_y

X = pd.Series(['path_1','path_2','path_2'])
X = X.values.reshape(-1,1)
y = pd.Series([1,0,0])

check_X_y(X, y)

这将引发一个错误

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

下面的例子就可以了!在

^{pr2}$

相关问题 更多 >

    热门问题