Month: June 2012

In previous posts I wrote about the measures of central tendency and how to calculate the mean and the median in Python.

Now let’s see how to calculate the mode (the most common number in a set of numbers).

The first thing to do is to calculate the number of occurrences for each number (how many times it’s present in the set).
This could be done using a second list but a more elegant solution would be to use a dictionary, also calledassociative array : a collection of (key, value) pairs, such that each key is unique.
In this case the keys are the numbers found in the set and the values its occurrences.

In the previous post I wrote about the measures of central tendency and how to calculate the mean in Python.
Now let’s see how doing it for the median (the number in the middle).

The core part is that the input list is sorted (using the standard list function called sorted) and then it is easy to get the number in the middle: newList = sorted(currentList) creates a new list based on the input and sort it; this is useful if you want to re-use the input list later for other scopes. There is also a function called sort() which will do the sorting in place on the input list. But then the original list will be sorted …. [See also the note at the end of the post]
Here is the complete function, with initial description in a comment section. A couple of examples / unit tests follow. Continue reading “Median”→

Welcome!

This is my personal blog, where I write about what I learned, mostly about software, project management and machine learning.
Why this name? The blog should help me to navigate into the future using (and not forgetting) the past experiences.
From Europe to the world.