CValues = (0.01, 0.03, 0.1, 0.3, 1, 3, 10, 30)
sigmaValues = CValues
bestPair, bestScore = (0, 0), 0
for C in CValues:
for sigma in sigmaValues:
gamma = np.power(sigma, -2)/2
clf = SVC(C=C, kernel='rbf',gamma=gamma)
clf.fit(X, y)
currentScore = clf.score(Xval, yval)
if currentScore > bestScore:
bestPair = (C, sigma)
bestScore = currentScore
print('bestPairs = {}, bestScores = {}'.format(bestPair, bestScore))
我在写这段代码的时候,出现了问题,报错如下:大概意思是不允许出现整数的负整数次幂
于是,我修改了代码:gamma = np.power(sigma, -2.0)/2,将-2次幂,改为-2.0次幂,程序能正确运行。
我又如下修改了程序,程序同样能正常运行。
CValues = (0.01, 0.03, 0.1, 0.3) #删除了整数值
sigmaValues = CValues
bestPair, bestScore = (0, 0), 0
for C in CValues:
for sigma in sigmaValues:
gamma = np.power(sigma, -2.)/2
clf = SVC(C=C, kernel='rbf',gamma=gamma)
clf.fit(X, y)
currentScore = clf.score(Xval, yval)
if currentScore > bestScore:
bestPair = (C, sigma)
bestScore = currentScore
print('bestPairs = {}, bestScores = {}'.format(bestPair, bestScore))
综上,np.power()函数的两个参数至少有一个应为浮点数。