# 源程序在这里

``````from numpy import *
import time
import matplotlib.pyplot as plt

# calulate kernel value
def calcKernelValue(matrix_x, sample_x, kernelOption):
kernelType = kernelOption[0]
numSamples = matrix_x.shape[0]
kernelValue = mat(zeros((numSamples, 1)))

if kernelType == 'linear':
kernelValue = matrix_x * sample_x.T
elif kernelType == 'rbf':
sigma = kernelOption[1]
if sigma == 0:
sigma = 1.0
for i in xrange(numSamples):
diff = matrix_x[i, :] - sample_x
kernelValue[i] = exp(diff * diff.T / (-2.0 * sigma**2))
else:
raise NameError('Not support kernel type! You can use linear or rbf!')
return kernelValue

# calculate kernel matrix given train set and kernel type
def calcKernelMatrix(train_x, kernelOption):
numSamples = train_x.shape[0]
kernelMatrix = mat(zeros((numSamples, numSamples)))
for i in xrange(numSamples):
kernelMatrix[:, i] = calcKernelValue(train_x, train_x[i, :], kernelOption)
return kernelMatrix

# define a struct just for storing variables and data
class SVMStruct:
def __init__(self, dataSet, labels, C, toler, kernelOption):
self.train_x = dataSet # each row stands for a sample
self.train_y = labels  # corresponding label
self.C = C             # slack variable
self.toler = toler     # termination condition for iteration
self.numSamples = dataSet.shape[0] # number of samples
self.alphas = mat(zeros((self.numSamples, 1))) # Lagrange factors for all samples
self.b = 0
self.errorCache = mat(zeros((self.numSamples, 2)))
self.kernelOpt = kernelOption
self.kernelMat = calcKernelMatrix(self.train_x, self.kernelOpt)

# calculate the error for alpha k
def calcError(svm, alpha_k):
output_k = float(multiply(svm.alphas, svm.train_y).T * svm.kernelMat[:, alpha_k] + svm.b)
error_k = output_k - float(svm.train_y[alpha_k])
return error_k

# update the error cache for alpha k after optimize alpha k
def updateError(svm, alpha_k):
error = calcError(svm, alpha_k)
svm.errorCache[alpha_k] = [1, error]

# select alpha j which has the biggest step
def selectAlpha_j(svm, alpha_i, error_i):
svm.errorCache[alpha_i] = [1, error_i] # mark as valid(has been optimized)
candidateAlphaList = nonzero(svm.errorCache[:, 0].A)[0] # mat.A return array
maxStep = 0; alpha_j = 0; error_j = 0

# find the alpha with max iterative step
if len(candidateAlphaList) > 1:
for alpha_k in candidateAlphaList:
if alpha_k == alpha_i:
continue
error_k = calcError(svm, alpha_k)
if abs(error_k - error_i) > maxStep:
maxStep = abs(error_k - error_i)
alpha_j = alpha_k
error_j = error_k
# if came in this loop first time, we select alpha j randomly
else:
alpha_j = alpha_i
while alpha_j == alpha_i:
alpha_j = int(random.uniform(0, svm.numSamples))
error_j = calcError(svm, alpha_j)

return alpha_j, error_j

# the inner loop for optimizing alpha i and alpha j
def innerLoop(svm, alpha_i):
error_i = calcError(svm, alpha_i)

### check and pick up the alpha who violates the KKT condition
## satisfy KKT condition
# 1) yi*f(i) >= 1 and alpha == 0 (outside the boundary)
# 2) yi*f(i) == 1 and 0<alpha< C (on the boundary)
# 3) yi*f(i) <= 1 and alpha == C (between the boundary)
## violate KKT condition
# because y[i]*E_i = y[i]*f(i) - y[i]^2 = y[i]*f(i) - 1, so
# 1) if y[i]*E_i < 0, so yi*f(i) < 1, if alpha < C, violate!(alpha = C will be correct)
# 2) if y[i]*E_i > 0, so yi*f(i) > 1, if alpha > 0, violate!(alpha = 0 will be correct)
# 3) if y[i]*E_i = 0, so yi*f(i) = 1, it is on the boundary, needless optimized
if (svm.train_y[alpha_i] * error_i < -svm.toler) and (svm.alphas[alpha_i] < svm.C) or\
(svm.train_y[alpha_i] * error_i > svm.toler) and (svm.alphas[alpha_i] > 0):

# step 1: select alpha j
alpha_j, error_j = selectAlpha_j(svm, alpha_i, error_i)
alpha_i_old = svm.alphas[alpha_i].copy()
alpha_j_old = svm.alphas[alpha_j].copy()

# step 2: calculate the boundary L and H for alpha j
if svm.train_y[alpha_i] != svm.train_y[alpha_j]:
L = max(0, svm.alphas[alpha_j] - svm.alphas[alpha_i])
H = min(svm.C, svm.C + svm.alphas[alpha_j] - svm.alphas[alpha_i])
else:
L = max(0, svm.alphas[alpha_j] + svm.alphas[alpha_i] - svm.C)
H = min(svm.C, svm.alphas[alpha_j] + svm.alphas[alpha_i])
if L == H:
return 0

# step 3: calculate eta (the similarity of sample i and j)
eta = 2.0 * svm.kernelMat[alpha_i, alpha_j] - svm.kernelMat[alpha_i, alpha_i] \
- svm.kernelMat[alpha_j, alpha_j]
if eta >= 0:
return 0

# step 4: update alpha j
svm.alphas[alpha_j] -= svm.train_y[alpha_j] * (error_i - error_j) / eta

# step 5: clip alpha j
if svm.alphas[alpha_j] > H:
svm.alphas[alpha_j] = H
if svm.alphas[alpha_j] < L:
svm.alphas[alpha_j] = L

# step 6: if alpha j not moving enough, just return
if abs(alpha_j_old - svm.alphas[alpha_j]) < 0.00001:
updateError(svm, alpha_j)
return 0

# step 7: update alpha i after optimizing aipha j
svm.alphas[alpha_i] += svm.train_y[alpha_i] * svm.train_y[alpha_j] \
* (alpha_j_old - svm.alphas[alpha_j])

# step 8: update threshold b
b1 = svm.b - error_i - svm.train_y[alpha_i] * (svm.alphas[alpha_i] - alpha_i_old) \
* svm.kernelMat[alpha_i, alpha_i] \
- svm.train_y[alpha_j] * (svm.alphas[alpha_j] - alpha_j_old) \
* svm.kernelMat[alpha_i, alpha_j]
b2 = svm.b - error_j - svm.train_y[alpha_i] * (svm.alphas[alpha_i] - alpha_i_old) \
* svm.kernelMat[alpha_i, alpha_j] \
- svm.train_y[alpha_j] * (svm.alphas[alpha_j] - alpha_j_old) \
* svm.kernelMat[alpha_j, alpha_j]
if (0 < svm.alphas[alpha_i]) and (svm.alphas[alpha_i] < svm.C):
svm.b = b1
elif (0 < svm.alphas[alpha_j]) and (svm.alphas[alpha_j] < svm.C):
svm.b = b2
else:
svm.b = (b1 + b2) / 2.0

# step 9: update error cache for alpha i, j after optimize alpha i, j and b
updateError(svm, alpha_j)
updateError(svm, alpha_i)

return 1
else:
return 0

# the main training procedure
def trainSVM(train_x, train_y, C, toler, maxIter, kernelOption = ('rbf', 1.0)):
# calculate training time
startTime = time.time()

# init data struct for svm
svm = SVMStruct(mat(train_x), mat(train_y), C, toler, kernelOption)

# start training
entireSet = True
alphaPairsChanged = 0
iterCount = 0
# Iteration termination condition:
#   Condition 1: reach max iteration
#   Condition 2: no alpha changed after going through all samples,
#                in other words, all alpha (samples) fit KKT condition
while (iterCount < maxIter) and ((alphaPairsChanged > 0) or entireSet):
alphaPairsChanged = 0

# update alphas over all training examples
if entireSet:
for i in xrange(svm.numSamples):
alphaPairsChanged += innerLoop(svm, i)
print '---iter:%d entire set, alpha pairs changed:%d' % (iterCount, alphaPairsChanged)
iterCount += 1
# update alphas over examples where alpha is not 0 & not C (not on boundary)
else:
nonBoundAlphasList = nonzero((svm.alphas.A > 0) * (svm.alphas.A < svm.C))[0]
for i in nonBoundAlphasList:
alphaPairsChanged += innerLoop(svm, i)
print '---iter:%d non boundary, alpha pairs changed:%d' % (iterCount, alphaPairsChanged)
iterCount += 1

# alternate loop over all examples and non-boundary examples
if entireSet:
entireSet = False
elif alphaPairsChanged == 0:
entireSet = True

print 'Congratulations, training complete! Took %fs!' % (time.time() - startTime)
return svm

# testing your trained svm model given test set
def testSVM(svm, test_x, test_y):
test_x = mat(test_x)
test_y = mat(test_y)
numTestSamples = test_x.shape[0]
supportVectorsIndex = nonzero(svm.alphas.A > 0)[0]
supportVectors      = svm.train_x[supportVectorsIndex]
supportVectorLabels = svm.train_y[supportVectorsIndex]
supportVectorAlphas = svm.alphas[supportVectorsIndex]
matchCount = 0
for i in xrange(numTestSamples):
kernelValue = calcKernelValue(supportVectors, test_x[i, :], svm.kernelOpt)
predict = kernelValue.T * multiply(supportVectorLabels, supportVectorAlphas) + svm.b
if sign(predict) == sign(test_y[i]):
matchCount += 1
accuracy = float(matchCount) / numTestSamples
return accuracy

# show your trained svm model only available with 2-D data
def showSVM(svm):
if svm.train_x.shape[1] != 2:
print "Sorry! I can not draw because the dimension of your data is not 2!"
return 1

# draw all samples
for i in xrange(svm.numSamples):
if svm.train_y[i] == -1:
plt.plot(svm.train_x[i, 0], svm.train_x[i, 1], 'or')
elif svm.train_y[i] == 1:
plt.plot(svm.train_x[i, 0], svm.train_x[i, 1], 'ob')

# mark support vectors
supportVectorsIndex = nonzero(svm.alphas.A > 0)[0]
for i in supportVectorsIndex:
plt.plot(svm.train_x[i, 0], svm.train_x[i, 1], 'oy')

# draw the classify line
w = zeros((2, 1))
for i in supportVectorsIndex:
w += multiply(svm.alphas[i] * svm.train_y[i], svm.train_x[i, :].T)
min_x = min(svm.train_x[:, 0])[0, 0]
max_x = max(svm.train_x[:, 0])[0, 0]
y_min_x = float(-svm.b - w[0] * min_x) / w[1]
y_max_x = float(-svm.b - w[0] * max_x) / w[1]
plt.plot([min_x, max_x], [y_min_x, y_max_x], '-g')
plt.show()

``````

# 测试代码在这里

`````` from numpy import *
import SVM

################## test svm #####################
dataSet = []
labels = []
fileIn = open('D:\Python33\SVM\testSet.txt')
lineArr = line.strip().split('\t')
dataSet.append([float(lineArr[0]), float(lineArr[1])])
labels.append(float(lineArr[2]))

dataSet = mat(dataSet)
labels = mat(labels).T
train_x = dataSet[0:81, :]
train_y = labels[0:81, :]
test_x = dataSet[80:101, :]
test_y = labels[80:101, :]

## step 2: training...
print ("step 2: training...")
C = 0.6
toler = 0.001
maxIter = 50
svmClassifier = SVM.trainSVM(train_x, train_y, C, toler, maxIter, kernelOption = ('linear', 0))

## step 3: testing
print ("step 3: testing...")
accuracy = SVM.testSVM(svmClassifier, test_x, test_y)

## step 4: show the result
print ("step 4: show the result...")
print ('The classify accuracy is: %.3f%%' % (accuracy * 100))
SVM.showSVM(svmClassifier)

``````

# 测试的数据在这里

3.542485 1.977398 -1
3.018896 2.556416 -1
7.551510 -1.580030 1
2.114999 -0.004466 -1
8.127113 1.274372 1
7.108772 -0.986906 1
8.610639 2.046708 1
2.326297 0.265213 -1
3.634009 1.730537 -1
0.341367 -0.894998 -1
3.125951 0.293251 -1
2.123252 -0.783563 -1
0.887835 -2.797792 -1
..........

# 现在我想把这个二维主成分的SVM改成分类五维主成分的

3.125951 0.293251 2.123252 -0.783563 0.887835 -1
0.887835 -2.797792 3.634009 1.730537 -2.797792 -1

PCA主成分数量（降维维度）选择

Python之数组维度

a=np.array([[[1],[2]] ,[[3],[4]] ,[[5],[6]]]) print(a.shape)&amp;gt;&amp;gt;(3,2,1)

（点击上方公众号，可快速关注）：）推荐程序员必备微信号 ▼程序员内参微信号：programmer0001推荐理由：在这里，我们分享程序员相关技术，职场生活，行业热点资讯。不定期还会分享IT趣文和趣图。这里属于我们程序员自己的生活，工作和娱乐空间。 ▼长按下方↓↓↓二维码识别关注

python PCA主成分分析进行降维度

#PCA对数据进行降维 from numpy import * def confloat(x): r=[float(i) for i in x] return r def loadDataSet(fileName, delim='\t'): fr = open(fileName) stringArr = [line.strip().split(delim) f...

SD敢达改机详细资料

SVM分类器-LS-SVM（二）

LS-SVM工具箱的使用： 一、安装LS-SVM 下载ls-svm，然后安装即可，可以参考1. 二、使用LS-SVM （1）分类 （2）回归 参考1.http://blog.csdn.net/ahuang1900/article/details/8971670

python 按不同维度求和，最值，均值

SVM-SVR

VC维在有限的训练样本情况下，当样本数 n 固定时，此时学习机器的 VC 维越高学习机器的复杂性越高。VC 维反映了函数集的学习能力，VC 维越大则学习机器越复杂(容量越大)。         所谓的结构风险最小化就是在保证分类精度（经验风险）的同时，降低学习机器的 VC 维，可以使学习机器在整个样本集上的期望风险得到控制。         经验风险和实际风险之间的关系，注意引入这个原因是

import numpy as np a = [[1,2],[3,4]] print(np.array(a).shape)

numpy求平均值的维度设定

>>> a = np.array([[1, 2], [3, 4]]) >>> np.mean(a) # 将上面二维矩阵的每个元素相加除以元素个数（求平均数） 2.5 >>> np.mean(a, axis=0) # axis=0，计算所有子数组的平均值 array([ 2., 3.]) >>> np.mean(a, axis=1) # axis=1，对每一个子数组，计算它的平均

python：numpy库ndarray多维数组的维度变换方法：reshape、resize、swapaxes、flatten

numpy库对多维数组有非常灵巧的处理方式，主要的处理方法有： .reshape(shape) ： 不改变数组元素，返回一个shape形状的数组，原数组不变 .resize(shape) ： 与.reshape()功能一致，但修改原数组 In [22]: a = np.arange(20) #原数组不变 In [23]: a.reshape([4,5]) Out[23]: array([[ ...

R语言手动计算主成分分析（PCA)及其在R函数的实现

R语言手动计算主成分分析（PCA)及其在R函数的实现了解PCA的原理，但总是无法用R语言实现，这次算是有个教程。  转自：http://blog.163.com/xiaoji0106@126/blog/static/13613466120133185842687/       主成分分析（principal component analysis,PCA）是一种降维技术，把多个变量化为能够反映原始变

opencv类：PCA降低数据的维度（主成分分析法的学习）

PCA降维的学习以下代码是在opencv中的函数pca.cpp的基础上进行了一些更改，主要更改体现在读取降维样本方面，并对代码加上了一些自己理解的注释： `#include <iostream> #include <fstream> #include <sstream> #include <opencv2/core/core.hpp> #include <opencv2/highgui/high

cat &amp;gt; ca-config.json &amp;lt;&amp;lt;EOF { &quot;signing&quot;: { &quot;default&quot;: { &quot;expiry&quot;: &quot;87600h&quot; }, &quot;profiles&quot;: { &quot;kubernetes&quot;: { &quot;usages&quot;: [

tf.transpose()交换张量位置（矩阵转置）

tf.transpose()交换张量位置（矩阵转置） tf.transpose() 是TensorFlow中用于矩阵，张量转置用的，除了可以交换二维矩阵的行和列之外，还可以交换张量的不同轴之间的顺序，函数使用方法如： transpose( a, perm=None, name='transpose' ) 其中，perm参数用于指定交换后的张量的轴是原先张量的轴的位

&amp;lt;form action=&quot;https://www.baidu.com/s&quot;&amp;gt;&amp;lt;div align=&quot;center&quot;&amp;gt; &amp;lt;input name=tn type=hidden value=baidu&amp;gt;&amp;lt;a href=&quot;http://www.baidu.com/&quot;&amp;gt;&amp;lt;img src=&quot;https://ss0.bdsta

SVM学习笔记（三）---核函数

SVM—当样本数小于维度的时候

//加载腾讯视频 （注意，默认情况下，视频处为最高级，总是处于页面的最上层，应当设置其透明，player.addParam("wmode","transparent");） 一般： Js:  type="text/javascript" src="js/swfobject.js"> type="text/javascript" src="http://qzs.qq.c

SVM中matlab版本的交叉验证和参数选择

Python_主成分分析

Python实现简单主成分分析

Python 之 sklearn 计算 SVM 隶属度

python中的sklean已经集成SVM算法，其中包含fit(), predict()等，我们只要输入训练样本和标记，以及模型参数，就可得到分类的结果。 关于这个的代码实现已有很多，SVC 参数详见： 详址：http://scikit-learn.org/stable/modules/generated/sklearn.svm.SVC.html#sklearn.svm.SVC 但对于libsvm之中实现的隶属度计算仍未找到相似讲解与示例，先给出源码如下。

matlab实现主成分分析 并计算累积贡献率

【Python那些事儿】主成分分析PCA

SVM(Support Vector Machine)读书笔记一（最佳分割超平面）

SVM 处理高维小样本数据

1.降维的时候 用PCA n_comments=0.99 保留99%的信息 2.SVM  分为SVC(分类）SVM（回归） 3.Leave One Out 进行训练集和测试集的划分 具体代码见 https://github.com/qqxze/SVM

axis维度 python中 numpy中数列维度的运算

axis=0维度是一从最外层开始算[  [9,0,1]  [1,2,3][4,5,6][7,8,9] ]也就是第一维度是4第二维度是 三运算第一维度数列时生成一个第一维度内的元素  即[x,y,z]

SVM支持向量机中PCA函数的使用注意事项

python数组改变维数

python数组改变维数

Tensorflow获取张量Tensor的具体维数