欠采样-随机森林模型算法,帮我举个例子,写出代码
时间: 2024-08-17 16:02:42 浏览: 73
欠采样-随机森林模型是一种处理不平衡数据集的策略,其中少数类别样本占比较少。它通过从多数类别中随机抽取样本,使得各类别样本的数量相近,然后构建随机森林进行训练。下面是一个简单的Python示例,使用sklearn库实现随机森林模型结合欠采样SMOTE技术:
首先,安装必要的库:
```bash
pip install scikit-learn pandas imblearn
```
然后,假设我们有一个二分类问题的数据集`df`,标签列名为`target`,特征列名在其他列中:
```python
import pandas as pd
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from imblearn.over_sampling import RandomOverSampler
# 加载数据
data = pd.read_csv('your_dataset.csv')
# 划分特征和目标变量
X = data.drop('target', axis=1)
y = data['target']
# 数据划分(假设80%训练,20%测试)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 使用欠采样SMOTE
ros = RandomOverSampler(random_state=42)
X_resampled, y_resampled = ros.fit_resample(X_train, y_train)
# 创建并训练随机森林模型
clf = RandomForestClassifier(n_estimators=100, random_state=42)
clf.fit(X_resampled, y_resampled)
# 预测测试集
y_pred = clf.predict(X_test)
# 评估模型性能
accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy: {accuracy}")
阅读全文