from imblearn.over_sampling import SMOTE
from imblearn.under_sampling import RandomUnderSampler
from imblearn.pipeline import Pipeline
# Define features and target
X = df.drop('infected', axis=1)
y = df['infected']
# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Define the resampling strategy
over = SMOTE(sampling_strategy=0.5) # Oversample the minority class to 50% of the majority class
under = RandomUnderSampler(sampling_strategy=0.8) # Undersample the majority class to 80% of its original size
pipeline = Pipeline(steps=[('o', over), ('u', under)])
# Apply the resampling
X_resampled, y_resampled = pipeline.fit_resample(X_train, y_train)
# Show the new class distribution
print("Resampled class distribution:", pd.Series(y_resampled).value_counts())
这是我的代码
这是我遇到的错误
AttributeError Traceback (most recent call last)
Cell In[7], line 19
16 pipeline = Pipeline(steps=[('o', over), ('u', under)])
18 # Apply the resampling
---> 19 X_resampled, y_resampled = pipeline.fit_resample(X_train, y_train)
21 # Show the new class distribution
22 print("Resampled class distribution:", pd.Series(y_resampled).value_counts())
File ~\anaconda3\Lib\site-packages\imblearn\pipeline.py:372, in Pipeline.fit_resample(self, X, y, **fit_params)
342 """Fit the model and sample with the final estimator.
343
344 Fits all the transformers/samplers one after the other and
(...)
369 Transformed target.
370 """
371 self._validate_params()
--> 372 fit_params_steps = self._check_fit_params(**fit_params)
373 Xt, yt = self._fit(X, y, **fit_params_steps)
374 last_step = self._final_estimator
AttributeError: 'Pipeline' object has no attribute '_check_fit_params'
我已经尝试了一切。我的所有包都已更新。我尝试使用的所有方法都在 sklearn 和 imblearn 这两个网站上查看。
我有同样的问题,但是将不平衡学习器的软件包版本升级到0.12.4后,我发现它工作得很好。我原来的版本是0.11.1。你可以尝试一下。