1

I am trying to find the median of a list. I have tried it two ways and it hasn't worked in either (I have imported what I need and the list is there) selfies is the name of the list

def median():
    return numpy.median(numpy.array(selfies))

this is the error

ret = umr_sum(arr, axis, dtype, out, keepdims)

TypeError: cannot perform reduce with flexible type

and the other way was

def median():
    med = statistics.median(selfies)
    return med

the error is

return (data[i - 1] + data[i])/2

TypeError: unsupported operand type(s) for /: 'str' and 'int'

Thank you in advance

User9123
  • 214
  • 1
  • 4
  • 13

2 Answers2

2

Hello User9123,

Main Use of the Medain() function


This module provides functions for calculating mathematical statistics of numeric (Real-valued) data.

Note Unless explicitly noted otherwise, these functions support int, float, decimal.Decimal and fractions.Fraction. Behaviour with other types (whether in the numeric tower or not) is currently unsupported. Mixed types are also undefined and implementation-dependent. If your input data consists of mixed types, you may be able to use map() to ensure a consistent result, e.g. map(float, input_data).

Your Problem


When you use this function for the string so it is not work properly some time so better i suggest you used custom function. I give the solution in my below code.

Explanation Function Median()


statistics.median(data)

Return the median (middle value) of numeric data, using the common “mean of middle two” method. If data is empty, StatisticsError is raised. data can be a sequence or iterator.

The median is a robust measure of central location, and is less affected by the presence of outliers in your data. When the number of data points is odd, the middle data point is returned:

Solution of Problem


If you use this function for the numeric value so try this below code,

def getMedian(numericValues):
  theValues = sorted(numericValues)

  if len(theValues) % 2 == 1:
    return theValues[(len(theValues)+1)/2-1]
  else:
    lower = theValues[len(theValues)/2-1]
    upper = theValues[len(theValues)/2]
    return (float(lower + upper)) / 2 


print getMedian([0,1,2,3,4,5]) # output = 2.5

If you are use median function for the string so try this below code,

def medianFind(mystring):
    return mystring[(len(mystring)-1)/2]  #len returns the length of the string

print medianFind("vmr")  #prints m

I hope my answer is helpful.
If any query so comment please.

Er CEO Vora Mayur
  • 894
  • 1
  • 14
  • 24
-1

Use a .sort() or .sorted() to sort your list according to how you want, then retrieve list[math.floor(len(list)/2)].

If you don't need to sort it and just want the middle value, use

list[math.floor(len(list)/2)].
Julian Chan
  • 450
  • 2
  • 8
  • The error messages are not by the calculation of the median itself, but by the data type. Your answer does not answer the OP – eyllanesc Jul 10 '17 at 05:06
  • @eyllanesc But using .sort() he can then use his own comparison method for sorting (which seems easier than declaring __cmp__ or __eq__, especially if the object is imported in from external libraries), so wouldn't this solve his problem? Ultimately, I am under the impression that he is looking for the median, and his issues lie with the numpy library and the data type he is using, but since median is not overly complex, I am just suggesting he does it manually, since using my method would probably resolve TypeError. – Julian Chan Jul 10 '17 at 05:32
  • Your solution answers the question if the list were strings? For example `l = ['1', '2', '3', '4']` – eyllanesc Jul 10 '17 at 05:34
  • @eyllanesc Yes it does: l.sorted(l,key = lambda x: int(x)) then get the median (assuming int() doesn't throw an error and all the strings can be converted to int). You could also use other comparison "standards" like ASCII or anything in the world you could think of as long as it allows for valid comparison. It can also handle other data types as well. – Julian Chan Jul 10 '17 at 05:43