Reputation: 137
Alright I am a bit confused here, I have a list that looks like:
>>> _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
With a median value of:
>>> _list[int(len(_list)/2)]
'2.43283432336e-07'
but when I do:
>>> median(array(_list,dtype=float))
4.6172549618900001e-07
I get that as a median value, I am doing something wrong here. When I don't use floats:
>>> 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
Dropping the dtype gives:
>>> 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
If someone could steer me in the right direction I would appreciate it, thanks.
Upvotes: 1
Views: 1827
Reputation: 137
Sorry, complete user error, these values are being read in from a txt file I made earlier, so they have the type str instead of float, apparently that effects numpy, floating them makes everything work, total user error, my fault.
Upvotes: 0
Reputation: 1396
I'm guessing it's because _list contains strings - your values are in lexicographic sort order, but not numerical. Try resorting the data after the conversion to float.
Upvotes: 4