基于python的多个机器学习的集成学习代码
时间: 2024-05-10 17:18:42 浏览: 80
以下是一个基于Python的集成学习代码示例,使用了多个机器学习算法:
```python
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier, VotingClassifier
from sklearn.svm import SVC
from sklearn.linear_model import LogisticRegression
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score
# 读取数据
data = pd.read_csv("data.csv")
# 准备数据
X = data.iloc[:, :-1]
y = data.iloc[:, -1]
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 定义模型
svm = SVC(probability=True, random_state=42)
logreg = LogisticRegression(random_state=42)
knn = KNeighborsClassifier()
rf = RandomForestClassifier(random_state=42)
# 训练模型
svm.fit(X_train, y_train)
logreg.fit(X_train, y_train)
knn.fit(X_train, y_train)
rf.fit(X_train, y_train)
# 预测结果
svm_pred = svm.predict(X_test)
logreg_pred = logreg.predict(X_test)
knn_pred = knn.predict(X_test)
rf_pred = rf.predict(X_test)
# 计算准确率
svm_acc = accuracy_score(y_test, svm_pred)
logreg_acc = accuracy_score(y_test, logreg_pred)
knn_acc = accuracy_score(y_test, knn_pred)
rf_acc = accuracy_score(y_test, rf_pred)
print("SVM准确率:", svm_acc)
print("Logistic Regression准确率:", logreg_acc)
print("KNN准确率:", knn_acc)
print("Random Forest准确率:", rf_acc)
# 定义投票集成模型
voting_clf = VotingClassifier(estimators=[("svm", svm), ("logreg", logreg), ("knn", knn), ("rf", rf)], voting="soft")
# 训练投票集成模型
voting_clf.fit(X_train, y_train)
# 预测结果
voting_pred = voting_clf.predict(X_test)
# 计算准确率
voting_acc = accuracy_score(y_test, voting_pred)
print("投票集成模型准确率:", voting_acc)
```
这个代码演示了如何使用SVM、Logistic Regression、KNN和Random Forest等多个机器学习算法,并使用投票集成方法将它们合并在一起。
阅读全文