我想使用来自imblearn
的类FunctionSampler
来创建我自己的自定义类,以便对数据集进行重新采样。
我有一个包含每个主题路径的一维功能系列和一个包含每个主题标签的标签系列。两者都来自pd.DataFrame
。我知道我必须首先重塑特征数组,因为它是一维的。
当我使用类RandomUnderSampler
时,一切正常,但是如果我先将特性和标签传递给FunctionSampler
的fit_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])
def resample(X, y):
return RandomUnderSampler().fit_resample(X, y)
sampler = FunctionSampler(func=resample)
X_res, y_res = sampler.fit_resample(X, y)
有人知道这里出了什么问题吗?似乎FunctionSampler
的fit_resample
方法不等于RandomUnderSampler
的fit_resample
方法。。。在
您的
FunctionSampler
的实现是正确的。问题出在你的数据集上。在RandomUnderSampler
似乎也适用于文本数据。没有使用^{但是
FunctionSampler()
有这个检查,见{a2}这将引发一个错误
下面的例子就可以了!在
^{pr2}$相关问题 更多 >
编程相关推荐