【发布时间】:2023-03-23 20:27:01
【问题描述】:
我想使用 imblearn 中的 FunctionSampler 类来创建我自己的自定义类来重新采样我的数据集。
我有一个一维特征系列,其中包含每个主题的路径和一个标签系列包含每个主题的标签。两者都来自pd.DataFrame。我知道我必须先重塑特征数组,因为它是一维的。
当我使用 RandomUnderSampler 类时,一切正常,但是如果我首先将功能和标签都传递给 FunctionSampler 的 fit_resample 方法,然后创建 RandomUnderSampler 的实例,然后调用fit_resample 在这个类上,我收到以下错误:
ValueError:无法将字符串转换为浮点数:'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])
第一种方法(有效)
rus = RandomUnderSampler()
X_res, y_res = rus.fit_resample(X,y)
第二种方法(不起作用)
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 方法...
【问题讨论】:
标签: python pandas scikit-learn imblearn