用python实现采用线性核、多项式核、高斯核、S 形核这4种核函数对多种类型数据集进行二分类
时间: 2023-07-10 13:11:15 浏览: 110
核主成分分析KPCA,核主成分分析 matlab,Python
5星 · 资源好评率100%
好的,下面是使用Python实现采用四种核函数对多种数据集进行二分类的示例代码:
首先,我们需要导入必要的库,包括numpy、matplotlib、sklearn等:
``` python
import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets, svm
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
```
接下来,我们可以选择几个常用的数据集进行分类实验,例如鸢尾花数据集、手写数字数据集和乳腺癌数据集:
``` python
# 加载数据集
iris = datasets.load_iris()
digits = datasets.load_digits()
breast_cancer = datasets.load_breast_cancer()
# 数据预处理
X_iris, y_iris = iris.data[:, :2], iris.target # 只选取前两个特征
X_digits, y_digits = digits.data, digits.target
X_cancer, y_cancer = breast_cancer.data[:, :10], breast_cancer.target # 只选取前10个特征
# 划分数据集
X_train_iris, X_test_iris, y_train_iris, y_test_iris = train_test_split(X_iris, y_iris, random_state=0)
X_train_digits, X_test_digits, y_train_digits, y_test_digits = train_test_split(X_digits, y_digits, random_state=0)
X_train_cancer, X_test_cancer, y_train_cancer, y_test_cancer = train_test_split(X_cancer, y_cancer, random_state=0)
```
接下来,我们可以定义一个函数,用于训练SVM模型并计算分类精度:
``` python
def train_test_svm(kernel, X_train, X_test, y_train, y_test):
# 训练模型
clf = svm.SVC(kernel=kernel)
clf.fit(X_train, y_train)
# 预测并计算精度
y_pred = clf.predict(X_test)
acc = accuracy_score(y_test, y_pred)
print("Kernel: {}, Accuracy: {:.2f}%".format(kernel, acc * 100))
```
最后,我们可以调用上述函数,分别使用四种核函数对三个数据集进行二分类实验:
``` python
# 使用四种核函数对三个数据集进行二分类实验
kernels = ['linear', 'poly', 'rbf', 'sigmoid']
X_trains = [X_train_iris, X_train_digits, X_train_cancer]
X_tests = [X_test_iris, X_test_digits, X_test_cancer]
y_trains = [y_train_iris, y_train_digits, y_train_cancer]
y_tests = [y_test_iris, y_test_digits, y_test_cancer]
datasets_names = ["Iris", "Digits", "Breast Cancer"]
for i in range(3):
print("\nDataset: {}".format(datasets_names[i]))
for j in range(4):
train_test_svm(kernels[j], X_trains[i], X_tests[i], y_trains[i], y_tests[i])
```
完整代码如下:
阅读全文