np.power()整数的负整数次幂不被允许

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()函数的两个参数至少有一个应为浮点数。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值