使用SVM作为基分类器的Adaboost算法:Python代码示例
以下是基于SVM模型的Adaboost代码示例:
from sklearn.svm import SVC
from sklearn.ensemble import AdaBoostClassifier
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
# 生成一个二分类数据集
X, y = make_classification(n_samples=1000, n_features=10, n_informative=5, n_redundant=0, random_state=42)
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
# 定义SVM模型作为Adaboost的基分类器
svm = SVC(kernel='linear', C=0.1)
# 定义Adaboost模型
adaboost = AdaBoostClassifier(base_estimator=svm, n_estimators=50, learning_rate=1.0)
# 训练模型
adaboost.fit(X_train, y_train)
# 预测测试集结果
y_pred = adaboost.predict(X_test)
# 计算模型精度
accuracy = accuracy_score(y_test, y_pred)
print('Accuracy:', accuracy)
在这个示例中,我们使用make_classification函数生成了一个二分类数据集,并将其划分为训练集和测试集。我们使用SVC模型作为Adaboost的基分类器,并定义了Adaboost模型。然后,我们训练模型,预测测试集结果,并计算模型精度。
原文地址: https://www.cveoy.top/t/topic/oehX 著作权归作者所有。请勿转载和采集!