Example HTML page

Statistics Package in python

In this page, i have given only one example program to differentiate the size of the code in C compared with python.

Although the “C” program has it’s own strengths, python takes less lines of code for doing things easily. This is possible by importing the related packages or modules.

Let us take to compute the mean, median, mode of an array of numbers in “C”.

#include<stdio.h>
main()
 {
 int i,j,a[20]={0},sum=0,n,t,b[20]={0},k=0,c=1,max=0,mode;
 float x=0.0,y=0.0;
 printf("\nEnter the limit\n");
 scanf("%d",&n);
 printf("Enter the set of numbers\n");
 for(i=0;i<n;i++)
  {
  scanf("%d",&a[i]);
  sum=sum+a[i];
  }
 x=(float)sum/(float)n;
 printf("Mean\t= %f",x);

 for(i=0;i<n;i++)
  {
  for(j=i+1;j<n;j++)
   {
   if(a[i]>a[j])
    {
    t=a[i];
    a[i]=a[j];
    a[j]=t;
    }
   }
  }
 if(n%2==0)
 y=(float)(a[n/2]+a[(n-1)/2])/2;
 else
 y=a[(n-1)/2];
 printf("\nMedian\t= %f",y);

 for(i=0;i<n-1;i++)
  {
  mode=0;
  for(j=i+1;j<n;j++)
   {
   if(a[i]==a[j])
    {
    mode++;
    }
   }
  if((mode>max)&&(mode!=0))
   {
   k=0;
   max=mode;
   b[k]=a[i];
   k++;
   }
  else if(mode==max)
   {
   b[k]=a[i];
   k++;
   }
  }
 for(i=0;i<n;i++)
  {
  if(a[i]==b[i])
  c++;
  }
 if(c==n)
 printf("\nThere is no mode");
 else
  {
  printf("\nMode\t= ");
  for(i=0;i<k;i++)
  printf("%d ",b[i]);
  }
 }

Mean, Median, Mode calculation in C

 

If you observe the above code, it takes around ’70’ lines. Now the above work can be done in the python as follows.

There are many open source packages that gives more strength to python.

from collections import defaultdict
class StatsList(list):
    def mean(self):
        return sum(self)/len(self)
    def median(self):
        if len(self)%2:
            return self[int(len(self)/2)]
        else:
            idx=int(len(self)/2)
            return (self[idx]+self[idx-1])/2
    def mode(self):
        freqs=defaultdict(int)
        for item in self:
            freqs[item]+=1
        mode_freq = max(freqs.values())
        modes=[]

        for item,value in freqs.items():
            if value==mode_freq:
                modes.append(item)
        return modes

Mean, Median, Mode calculation in python

 

It is observed that python has less number of lines.

Now there is another package supported in python, with this we can compute the mean, mode and median and other statistical operations with a single line of code.

Observe the following snippet.

>>> list1=[1,2,2,3,3,4]
>>> import statistics
>>> statistics.mean(list1)
2.5
>>> statistics.median(list1)
2.5
>>> statistics.stdev(list1)
1.0488088481701516

Statistical calculations in python

 

The second line here is the package “statistics” should be imported before using the methods in this module.

 

Example HTML page

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest