好吧我在这里有点困惑,我有一个看起来像的列表:
>>> _list
['-1.24235365387e-07', '-2.31373100323e-07', '-3.4561064219e-07', '-4.5226775879e-08', '-4.8495857305e-06', '-6.05262333229e-07', '-6.87756245459e-07', '1.01130316722e-06', '1.12310282664e-07', '1.49359255132e-06', '1.56048010364e-06', '2.43283432336e-07', '3.04787966681e-07', '3.44224562526e-06', '3.89199793328e-07', '4.61725496189e-07', '4.91574219806e-07', '6.42046115267e-07', '6.52594949337e-07', '7.29511505567e-07', '8.38829381985e-07', '8.59463647511e-07', '8.89956059753e-07']
>>> len(_list)
23
中值为:
>>> _list[int(len(_list)/2)]
'2.43283432336e-07'
但是当我这样做时:
>>> median(array(_list,dtype=float))
4.6172549618900001e-07
我认为这是一个中值,我在这里做错了。当我不使用花车时:
>>> median([-1,-2,-3,-4,-5,-6,-7,-8,-9,0,1,2,3,4,5,6,7,8,9])
0.0
>>> [-1,-2,-3,-4,-5,-6,-7,-8,-9,0,1,2,3,4,5,6,7,8,9][int(len([-1,-2,-3,-4,-5,-6,-7,-8,-9,0,1,2,3,4,5,6,7,8,9])/2)]
0
删除dtype会产生:
>>> median(array(_list))
Traceback (most recent call last):
File "<pyshell#42>", line 1, in <module>
median(array(_list))
File "C:\Python27\lib\site-packages\numpy\lib\function_base.py", line 2718, in median
return mean(part[indexer], axis=axis, out=out)
File "C:\Python27\lib\site-packages\numpy\core\fromnumeric.py", line 2716, in mean
out=out, keepdims=keepdims)
File "C:\Python27\lib\site-packages\numpy\core\_methods.py", line 62, in _mean
ret = um.add.reduce(arr, axis=axis, dtype=dtype, out=out, keepdims=keepdims)
TypeError: cannot perform reduce with flexible type
如果有人能指引我走向正确的方向,我将不胜感激,谢谢。
答案 0 :(得分:4)
我猜它是因为_list包含字符串 - 您的值是按字典排序顺序,但不是数字。尝试在转换后使用数据浮动。
答案 1 :(得分:0)
抱歉,完整的用户错误,这些值是从我之前制作的txt文件中读取的,所以它们的类型为str而不是float,显然效果numpy,浮动它们会使一切正常,总用户错误,我的错