numpy not returning the correct median value

user747944

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.

AMacK

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.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Windows Form Not Returning Correct Value from Public Method

분류에서Dev

map.find(var) isn't returning correct value

분류에서Dev

PHP function not returning value

분류에서Dev

Returning Value from Actor

분류에서Dev

AngularJS : service not returning value

분류에서Dev

ArrayList returning null value

분류에서Dev

Returning a value from a Method

분류에서Dev

Returning a value from a Thread?

분류에서Dev

strlen returning wrong value

분류에서Dev

Custom Forum function not returning correct ID?

분류에서Dev

Linq to sql query not returning correct items

분류에서Dev

GraphicsMagick using nodejs not returning correct image

분류에서Dev

Why is diff returning an incorrect value?

분류에서Dev

Returning a value from a recursive function

분류에서Dev

Conditional SUM not returning any value

분류에서Dev

Functions keeps returning the same value

분류에서Dev

Function not returning the Value of its Key

분류에서Dev

Is checking if a variable is equal to a value and then returning that value redundant?

분류에서Dev

Rails: The correct way of returning json from get request?

분류에서Dev

How to print the row name if the maximum value is higher compare to the median value of the others

분류에서Dev

Return statement in JavaScript is not returning the right value

분류에서Dev

$(li).width() returning value which includes padding?

분류에서Dev

function returning wrong value while executing

분류에서Dev

Jquery UI Mobile returning empty value for form

분류에서Dev

ListView using findViewById is returning null value

분류에서Dev

Retrieve URL Value but keeps returning (NULL)

분류에서Dev

Why is AES function returning different value?

분류에서Dev

array_search returning wrong value

분류에서Dev

Mysql_num_rows not returning any value

Related 관련 기사

뜨겁다태그

보관