def sigmoid(intX):
return 1.0/(1+exp(-intX))
dataMatrix = mat(dataMatIn)
weights = ones((n, 1))
h = sigmoid(dataMatrix*weights)
出错:
return 1.0/(1+math.exp(-intX))
TypeError: only length-1 arrays can be converted to Python scalars
因为
dataMatrix
与weights
均为numpy矩阵,相乘也是numpy矩阵,而math.exp()函数只处理python标准数值。 此处需要用numpy的exp()方法,如下:
import numpy as np
def sigmoid(self, intX):
return 1.0/(1+np.exp(-intX))
【TypeError: only length-1 arrays can be converted to Python scalars】也可以在文件头添加from numpy import *,就可以直接用exp(-intX)了
def smoSimple(dataMatIn, classLabels, C, toler, maxIter):
dataMatrix = mat(dataMatIn)
labelMat = mat(classLabels).transpose()
iter = 0
while iter < maxIter:
alphaPairChanged = 0
for i in range(m):
fXi = float(multiply(alphas, labelMat).T * (dataMatrix * dataMatrix[i,:].T)) + b
.....
出错:
fXi = float(multiply(alphas, labelMat).T * (dataMatrix * dataMatrix[i,:].T)) + b
TypeError: only length-1 arrays can be converted to Python scalars
print
multiply(alphas, labelMat).T * (dataMatrix * dataMatrix[i,:].T)[[ 0.]
[ 0.]
[ 0.]
[ 0.]
...
[ 0.]]
可见float()函数中是一个numpy数组,此例又证明标准python函数对numpy数组不适用。
推荐阅读
- 推荐系统论文进阶|CTR预估 论文精读(十一)--Deep Interest Evolution Network(DIEN)
- Python专栏|数据分析的常规流程
- Python|Win10下 Python开发环境搭建(PyCharm + Anaconda) && 环境变量配置 && 常用工具安装配置
- Python绘制小红花
- Pytorch学习|sklearn-SVM 模型保存、交叉验证与网格搜索
- OpenCV|OpenCV-Python实战(18)——深度学习简介与入门示例
- python|8. 文件系统——文件的删除、移动、复制过程以及链接文件
- 爬虫|若想拿下爬虫大单,怎能不会逆向爬虫,价值过万的逆向爬虫教程限时分享
- 分布式|《Python3网络爬虫开发实战(第二版)》内容介绍
- java|微软认真聆听了开源 .NET 开发社区的炮轰( 通过CLI 支持 Hot Reload 功能)