http://stackoverflow.com/questions/35013726/typeerror-ufunc-add-did-not-contain-a-loop-with-signature-matching-types
numpy数据类型dtype转换:
http://www.mamicode.com/info-detail-1180317.html
#############################################################################
今天使用python实现kNN算法时遇到一个问题:
#print "dataSet.shape : {}, type : {}, dtype: {}".format(dataSet.shape, type(dataSet), dataSet.dtype)
#print "da.shape : {}, type : {}, dtype: {}".format(da.shape, type(da), da.dtype)
dataSetSize = dataSet.shape[0]
tempSet = np.tile(da, (dataSetSize, 1))
#print "tempSet.shape : {}".format(tempSet.shape)
diff = np.array(dataSet) - tempSet其中dataSet,data变量的type类型均为numpy.ndarray,dataSet的大小为[10, 20],data的大小为[1, 20]
我的目的就是把data矩阵扩展到和dataSet相同大小后进行矩阵相减操作,但在最后一步出错:
TypeError: ufunc 'subtract' did not contain a loop with signature matching types dtype('S32')
这让我一头雾水,网上查找中发现了关键字dtype
import numpy as np
arr = np.ones((2,2))
help(arr.dtype)
简而言之,dtype就是数组元素的数据类型。上面的错误表示相减的两个数组的dtype可能不同,所以我就打印了这两个数组的dtype。发现确实如此,一个是'S32',而另一个是'float64'。解决方法就是将dtype转换为'float64'
在网上找到,转换dtype需要使用函数astype:
示例程序:
import numpy as np
x = np.array([1, 2, 2.5])
x.astype(int)
y = np.array([3, 6, 2], dtype='S32')
y = y.astype('float64')
x - y
python TypeError: ufunc 'subtract' did not contain a loop with signature matching types dtype('S32')
参考:TypeError: ufunc 'add' did not contain a loop with signature matching types:http://stackoverflow.com/questions/35013726/typeerror-ufunc-add-did-not-contain-a-loop-with-signature-matching-ty
最近利用
Python
读取txt文件时遇到了一个小问题,就是在计算两个np.narray()类型的数组时,出现了以下错误:
TypeError
: u
func
'
sub
trac
t' did not
contain
a l
oop
with
signature
matching
types
dtype
('<U3')
dtype
('<U3')
dtype
('<U3')
作为一个
Python
新手,遇到这个问题后花费了挺多时间,在网上找了许多大神们写的例子,最后终于解决了。
总结如下:
(1)出现此问题的原因是:目的是想计算两个数组间的差值,但数组中的元素不是数据类型(float
今天拿
python
写AdaBoost的时候碰到的,一看报错提示就知道是类型错误,后来参考了
http://hyry.dip.jp/tech/book/page/scipy/
numpy
_ndarray.html
得知
S32
就是 长度为32个字节的字符串类型,这下就好办了,发现问题是出在读取txt数据时没有转换类型,依旧保持着字符类型。解决方案:
float()
u
func
'multiply'不包含签名匹配类型的循环(
dtype
('<U32'),
dtype
('<U32')) ->
dtype
(' & lt; U32 ')
出现这个报错,十有八九是数据读入有问题。
回去看一下excel的数据格式,果然发现输入的几列数据长度不一样。改了以后就能够顺利运行了。
TypeError
: No l
oop
matching
the specified
signature
and casting was found for u
func
greater
一种解决办法:U
Func
TypeError
: u
func
‘multiply’ did not
contain
a l
oop
with
signature
matching
types
(
dtype
(’<U32’),
dtype
(’<U32’)) ->
dtype
(’<U32’)
初步探索: 参考网上文章,
dtype
(’<U32’)代表字符串,不能乘法计算,要转换类型为 int/float
具体到本文:使用 sklearn:tsne 进行学习,采用 metric=‘pr
【
Python
】u
func
'
sub
trac
t' did not
contain
a l
oop
with
signature
matching
types
dtype
File "F:/Logistic/logistic.py", line 44, in <module>
print(gradAscent(dataArr, labelMat))
File "F:/Logistic/logistic.py", line 36, in gradAscent
error = labelMat - h
TypeError
: u
func
'
sub
trac
t' di
python
报错u
func
'
sub
trac
t' did not
contain
a l
oop
with
signature
matching
types
dtype
('<U32')
dtype
('<U32')
dtype
('<U32')
前面有个warning:FutureWarning: Beginning in version 0.22, arrays of bytes/strings will be converted to decimal numbers if
dtype
='.
TypeError
: u
func
'multiply' did not
contain
a l
oop
with
signature
matching
types
dtype
('&amp;amp;lt;U32')
dtype
('&amp;amp;lt;U32')
dtype
('&amp;amp;lt;U32')
U32就是 长度为32个字节的无符号整数类型,这下就好办了,发现问题是出在读取t...
Basic Tutorial:https://docs.
python
.org/2.7/howto/logging.html#logging-basic-tutorial
###########################################################################################
学习了一段时间p
看所有问题前先看看都有哪些字符编码
ASCII,Unicode,UTF-8,ISO 8859等,以及各种编码之间的关系
同时专门解释了中文字符相关的编码标准,包括GB2312,GBK,GB18030
也专门解释了Windows系统中的Code Page,以及相关的BOM等内容
字符编码详解 https://www.crifan.com/files/doc/docbook/char
最近在测试分销的项目,每次手动计算分销员的佣金,感觉特别麻烦,所以想着用
python
来实现自动计算佣金,但是在计算过程中遇到一个问题,如下:
如图,在佣金矩阵计算相加的时候,
numpy
抛了一个异常,这个异常在网上也没有找到相关资料。
于是我打断点看了一下,发现在计算的时候, list 中的数据,返回的是 str 类型,导致矩阵相加的时候,计算失败了。
然后我又去看了一下自己写的佣金计算规则的代码,需求中有个如果佣金金额大于两位小数的时候,会将数据四舍五入保留两位小数,但是保留两位小数
def createDataSet():
group = array([[1.0, 1.1], [1.0, 1.0], [0, 0], [0, 0.1]])
labels = ['A', 'A', 'B', 'B']
return group, labels
# print(createDataSet())
def classify0(inX, dataSet, labels, k):
numpy
数组抛出异常:
numpy
.core._exceptions.U
Func
TypeError
: u
func
‘add’ did not
contain
a l
oop
with
signature
matching
types
(
dtype
(’<U32’),
dtype
(’<U32’)) ->
dtype
(’<U32’)
我把excel表格读进
numpy
数组,excel中即有字符串类型数据,又有数值类型数据,读到
numpy
之后,
numpy
默认都是str类型,所以,就不能直接对nu