代码实现非线性支持向量机
时间: 2023-06-12 09:02:53 浏览: 103
非线性支持向量机(Nonlinear Support Vector Machine,简称NLSVM)是支持向量机(Support Vector Machine,简称SVM)的一种扩展形式,它通过引入核函数来处理非线性可分的情况。
下面是一个简单的Python实现NLSVM的示例代码:
```python
import numpy as np
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score
# 生成随机数据集
X, y = make_classification(n_samples=1000, n_features=10, n_informative=5, n_classes=2, random_state=42)
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 定义核函数
def rbf_kernel(x1, x2, gamma):
return np.exp(-gamma * np.linalg.norm(x1 - x2) ** 2)
# 训练SVM模型
def train(X, y, C, kernel, gamma):
n_samples, n_features = X.shape
# 计算核矩阵
K = np.zeros((n_samples, n_samples))
for i in range(n_samples):
for j in range(n_samples):
K[i,j] = kernel(X[i], X[j], gamma)
# 定义QP问题的参数
P = np.outer(y, y) * K
q = -np.ones(n_samples)
G = np.vstack((-np.eye(n_samples), np.eye(n_samples)))
h = np.hstack((np.zeros(n_samples), C * np.ones(n_samples)))
A = y.reshape(1, -1)
b = np.zeros(1)
# 解决QP问题
from cvxopt import solvers
solvers.options['show_progress'] = False
solution = solvers.qp(matrix(P), matrix(q), matrix(G), matrix(h), matrix(A), matrix(b))
alpha = np.array(solution['x']).flatten()
# 提取支持向量
support_vectors = X[alpha > 1e-5]
support_vectors_y = y[alpha > 1e-5]
support_vectors_alpha = alpha[alpha > 1e-5]
# 计算偏差
bias = np.mean(support_vectors_y - np.sum(support_vectors_alpha * support_vectors_y * K[alpha > 1e-5], axis=0))
return support_vectors, support_vectors_y, support_vectors_alpha, bias
# 预测
def predict(X, support_vectors, support_vectors_y, support_vectors_alpha, bias, kernel, gamma):
y_pred = np.zeros(len(X))
for i in range(len(X)):
s = 0
for alpha, sv_y, sv in zip(support_vectors_alpha, support_vectors_y, support_vectors):
s += alpha * sv_y * kernel(X[i], sv, gamma)
y_pred[i] = s
y_pred = np.sign(y_pred + bias)
return y_pred
# 训练和测试模型
C = 1
gamma = 0.5
kernel = rbf_kernel
support_vectors, support_vectors_y, support_vectors_alpha, bias = train(X_train, y_train, C, kernel, gamma)
y_pred_train = predict(X_train, support_vectors, support_vectors_y, support_vectors_alpha, bias, kernel, gamma)
y_pred_test = predict(X_test, support_vectors, support_vectors_y, support_vectors_alpha, bias, kernel, gamma)
# 计算准确率
print("Train accuracy:", accuracy_score(y_train, y_pred_train))
print("Test accuracy:", accuracy_score(y_test, y_pred_test))
```
该代码使用了CVXOPT库来解决二次规划问题。此外,还使用了Scikit-learn库中的make_classification函数来生成随机数据集,并使用SVC类和accuracy_score函数来评估模型的准确率。
阅读全文