给一个模型融合的Python代码
时间: 2023-11-21 08:05:51 浏览: 109
以下是一个模型融合的Python代码示例:
```python
# 导入所需的库和模型
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
# 加载数据集
data = pd.read_csv("data.csv")
# 分离特征和目标变量
X = data.drop("target",axis=1)
y = data["target"]
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 定义三个不同的分类器
clf1 = LogisticRegression()
clf2 = DecisionTreeClassifier()
clf3 = RandomForestClassifier()
# 训练三个分类器
clf1.fit(X_train, y_train)
clf2.fit(X_train, y_train)
clf3.fit(X_train, y_train)
# 预测测试集结果并计算准确率
pred1 = clf1.predict(X_test)
pred2 = clf2.predict(X_test)
pred3 = clf3.predict(X_test)
acc1 = accuracy_score(y_test, pred1)
acc2 = accuracy_score(y_test, pred2)
acc3 = accuracy_score(y_test, pred3)
print("Logistic Regression accuracy:", acc1)
print("Decision Tree accuracy:", acc2)
print("Random Forest accuracy:", acc3)
# 定义一个投票分类器,将三个分类器融合起来
from sklearn.ensemble import VotingClassifier
estimators = [('lr', clf1), ('dt', clf2), ('rf', clf3)]
ensemble = VotingClassifier(estimators)
# 训练投票分类器
ensemble.fit(X_train, y_train)
# 预测测试集结果并计算准确率
pred_ensemble = ensemble.predict(X_test)
acc_ensemble = accuracy_score(y_test, pred_ensemble)
print("Ensemble accuracy:", acc_ensemble)
```
在这个示例中,我们加载了一个二分类数据集,然后使用三个不同的分类器(逻辑回归、决策树和随机森林)对数据进行训练和预测。然后,我们定义了一个投票分类器,将三个分类器融合起来,并使用训练集对其进行训练。最后,我们使用测试集评估了每个分类器的准确率和投票分类器的准确率。