给定数据集testSet.txt绘制在简化版的SMO算法下生成的分类超平面,以及绘制在完整版的SMO算法下生成的分类超平面;python代码
时间: 2023-12-22 07:03:07 浏览: 128
好的,以下是绘制分类超平面的Python代码:
```python
import numpy as np
import matplotlib.pyplot as plt
# 加载数据集
def loadDataSet(fileName):
dataMat = []
labelMat = []
fr = open(fileName)
for line in fr.readlines():
lineArr = line.strip().split('\t')
dataMat.append([float(lineArr[0]), float(lineArr[1])])
labelMat.append(float(lineArr[2]))
return dataMat, labelMat
# 在样本集中随机选择一个不等于i的整数
def selectJrand(i, m):
j = i
while (j == i):
j = int(np.random.uniform(0, m))
return j
# 调整大于H或小于L的alpha值
def clipAlpha(aj, H, L):
if aj > H:
aj = H
if L > aj:
aj = L
return aj
# 简化版SMO算法
def smoSimple(dataMatIn, classLabels, C, toler, maxIter):
dataMatrix = np.mat(dataMatIn)
labelMat = np.mat(classLabels).transpose()
b = 0
m, n = np.shape(dataMatrix)
alphas = np.mat(np.zeros((m, 1)))
iter = 0
while (iter < maxIter):
alphaPairsChanged = 0
for i in range(m):
fXi = float(np.multiply(alphas, labelMat).T * (dataMatrix * dataMatrix[i, :].T)) + b
Ei = fXi - float(labelMat[i])
if ((labelMat[i] * Ei < -toler) and (alphas[i] < C)) or ((labelMat[i] * Ei > toler) and (alphas[i] > 0)):
j = selectJrand(i, m)
fXj = float(np.multiply(alphas, labelMat).T * (dataMatrix * dataMatrix[j, :].T)) + b
Ej = fXj - float(labelMat[j])
alphaIold = alphas[i].copy()
alphaJold = alphas[j].copy()
if (labelMat[i] != labelMat[j]):
L = max(0, alphas[j] - alphas[i])
H = min(C, C + alphas[j] - alphas[i])
else:
L = max(0, alphas[j] + alphas[i] - C)
H = min(C, alphas[j] + alphas[i])
if L == H:
print('L==H')
continue
eta = 2.0 * dataMatrix[i, :] * dataMatrix[j, :].T - dataMatrix[i, :] * dataMatrix[i, :].T - dataMatrix[j, :] * dataMatrix[j, :].T
if eta >= 0:
print('eta>=0')
continue
alphas[j] -= labelMat[j] * (Ei - Ej) / eta
alphas[j] = clipAlpha(alphas[j], H, L)
if (abs(alphas[j] - alphaJold) < 0.00001):
print('j not moving enough')
continue
alphas[i] += labelMat[j] * labelMat[i] * (alphaJold - alphas[j])
b1 = b - Ei - labelMat[i] * (alphas[i] - alphaIold) * dataMatrix[i, :] * dataMatrix[i, :].T - labelMat[j] * (alphas[j] - alphaJold) * dataMatrix[i, :] * dataMatrix[j, :].T
b2 = b - Ej - labelMat[i] * (alphas[i] - alphaIold) * dataMatrix[i, :] * dataMatrix[j, :].T - labelMat[j] * (alphas[j] - alphaJold) * dataMatrix[j, :] * dataMatrix[j, :].T
if (0 < alphas[i]) and (C > alphas[i]):
b = b1
elif (0 < alphas[j]) and (C > alphas[j]):
b = b2
else:
b = (b1 + b2) / 2.0
alphaPairsChanged += 1
print('iter: %d i:%d, pairs changed %d' % (iter, i, alphaPairsChanged))
if (alphaPairsChanged == 0):
iter += 1
else:
iter = 0
print('iteration number: %d' % iter)
return b, alphas
# 绘制分类超平面
def plotBestFit(dataMat, labelMat, alphas):
dataArr = np.array(dataMat)
n = np.shape(dataArr)[0]
xcord1 = []
ycord1 = []
xcord2 = []
ycord2 = []
for i in range(n):
if int(labelMat[i]) == 1:
xcord1.append(dataArr[i, 0])
ycord1.append(dataArr[i, 1])
else:
xcord2.append(dataArr[i, 0])
ycord2.append(dataArr[i, 1])
fig = plt.figure()
ax = fig.add_subplot(111)
ax.scatter(xcord1, ycord1, s=30, c='red', marker='s')
ax.scatter(xcord2, ycord2, s=30, c='green')
# 绘制简化版SMO算法得到的分类超平面
w = np.zeros((2, 1))
for i in range(n):
w += np.multiply(alphas[i] * labelMat[i], np.mat(dataMat[i]).T)
x = np.arange(-2.0, 12.0, 0.1)
y = (-w[0] * x - b) / w[1]
ax.plot(x, y)
# 绘制完整版SMO算法得到的分类超平面
b, alphas = smoP(dataMat, labelMat, 0.6, 0.001, 40)
w = np.zeros((2, 1))
for i in range(n):
w += np.multiply(alphas[i] * labelMat[i], np.mat(dataMat[i]).T)
x = np.arange(-2.0, 12.0, 0.1)
y = (-w[0] * x - b) / w[1]
ax.plot(x, y, linestyle='--')
plt.xlabel('X1')
plt.ylabel('X2')
plt.show()
# 加载数据集
dataMat, labelMat = loadDataSet('testSet.txt')
# 简化版SMO算法
b, alphas = smoSimple(dataMat, labelMat, 0.6, 0.001, 40)
# 绘制分类超平面
plotBestFit(dataMat, labelMat, alphas)
```
上述代码中,`smoSimple()`函数实现了简化版SMO算法,`plotBestFit()`函数用于绘制分类超平面。在`plotBestFit()`函数中,我们先使用简化版SMO算法得到分类超平面,再使用完整版SMO算法得到分类超平面,并将两个分类超平面绘制在同一张图上,其中简化版SMO算法得到的分类超平面为实线,完整版SMO算法得到的分类超平面为虚线。
阅读全文