import matplotlib.pyplot as plt from six.moves import xrange # 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帮我整理下格式
时间: 2023-12-01 20:02:07 浏览: 83
```python
import matplotlib.pyplot as plt
from six.moves import xrange
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
```
阅读全文