Calculating the Mean from Scratch using Python

In this post, I will explain a Python program that I wrote which calculates the mean without using the analytics libraries (NumPy, SciPy, etc.). The mean is a standard statistical expression used to calculate the average of a set of real numbers. There are two functions in Python used to calculate the mean from scratch. The first function is sum(), which calculates the sum of elements declared in a program. The program I wrote applies the function to add up a list of numbers and divides them by the len()function, which counts the number of elements declared in a list. When you divide sum()by len(), you get the equation for the mean, which you can declare in a program. The following screenshots show the code and a basic histogram of the values displayed in the program:

The screenshot shows the program where I declared a list of dummy values. Once I did this, I then built the equation for the mean, stored in the values_mean variable. Then, I printed the result to standard output. Finally, I generated a basic histogram using the matplotlib library and used hist() to output the histogram as we see here:

The graph is far from perfect, but it does give a simple visualization of values. In my next post, I will improve the program using the Ames data set, a standard set used in regression modeling. I will also improve upon the graphics to create a more descriptive distribution of values.

## Top comments (0)