Monday, 13 April 2015

Will you be a sane 80 year old?

What is the statistical likelihood that a man in his early 70's will comfortably live ten more years, with all mental faculties present?

Let us look at the answer...
You know what? I am just gonna copy paste my answer on the website here...

Assumption: I am assuming that you are an Indian.
The Statistics:

India population -1,282,741,906 (1.28 billion) As of March 28, 2015.
Sex Ratio:
Current Sex Ratio of India 2015 -943 females for every 1,000 males
That means you have a 51.45% chance of being a male.

Age consideration:
India has the following age distribution-
age 65 and above-5.8% (male 34,133,175/female 37,810,599) (2014 est.)
This means that you have a of 2.75% of being a male over the age of 65.

So, lets be a tad bit pessimistic (this sort of also factors in your old age) and say that you have a 20% chance of going senile. The good news is that there is an 80% chance that you wont go senile.

So factoring in all this we calculate:
1,282,741,906 *0.5145*0.0275*0.8=14519355.63
probability=14519355.63/ 1,282,741,906 =0.0113= 1.13%

So there you have it!
There is less than 1.13% chance that you will grow up to be a sane 80 year old.
Please bear in mind that this too is a very generous estimate. A more accurate assessment would depend on other unmentioned factors like religion, social status, income, number of children and their gender, your region of residence, crime rate, lifestyle choices etc. All these factors mostly contribute towards making your chances even slimmer.

Hey! I am not pessimistic; it's just that the answer to your question was almost bound to be a tad bit depressing.
But, look at the brighter side...

The life expectancy of an average Indian is 66.21 years. Which means you are actually really lucky to have crossed the age of 70! So the possibility that you will reach 80 is quite low but if your luck and current age were to be considered as any sort of factors here, then I would say that you are definitely one of the exceptions to the rule.

Remember what they say about life: It's a short trip! Better make it a good one.
Good luck for the rest of your life.

Sunday, 5 April 2015

K-Means Clustering in Python (Unsupervised Learning)

The K-means clustering algorithm is a class of unsupervised learning algorithm that takes an unlabeled dataset and divides it into a user defined number of clusters. These clusters consists of data-points which are more similar to each other than the members of the other cluster.

It can be thought of as a crude form of pattern recognition.

Image taken from the web

The algorithm can handle any number of features for each data-point but with the condition that all of them are numerical values rather than nominal. Categorical data needs to be converted into binary tags.

Usually playing around with the number of clusters and plotting them for visual verification is a good practice.

The python code has been provided below. You can also download it and all of my other code from my Github page. As always, any improvement or contribution will be appreciated.

Note: You will need to have pre-installed numpy and pandas libraries before running the code . The program itself is plug and play

The code...

###################################################################

"""K means clustering"""

import pandas as pd

import numpy as np

if __name__=="__main__":

print("welcome to the k means clustering package. this program will help you\n\

create a user defined number(K) of clusters of similar data points\n\

the clusters are created based on a crude techineqe of pattern recognition\n\

Classical K-Nearest Neighbor Algorithm in Python

The K-nearest neighbor algorithm is a supervised learning algorithm used to classify data-points based on multiple features. It can handle both qualitative and quantitative data. The core logic of the algorithm is to compare the new data point to all the existing data-points in it's database and assign it to the group which it most resembles. Apart from the dataset, the algorithm also requires the user to define a parameter 'K'. To understand why this parameter is needed, one must have at-least a rough idea of the inner workings of the algorithm.

Image taken from the web

What the algorithm basically does is it sorts every data-point in it's database according to decreasing degree of similarity to the new data-point. Now, this sorted list may have points from every class of the data-set; so, to give a "best guess" answer of the classification problem, it takes a vote. For this vote it chooses the top K items in the list and assigns the new data-point to the class which has a majority in this population. A good value of K usually depends on the size of the population and for large data-sets, it is enough to choose a K at around 10% of the total population.

It's application may range anywhere from segregating customers of a supermarket into different categories to writing software for solving captcha autonomously.

Example of a captcha (taken from the web)

The python code for this algorithm has been given below (I use the Spyder IDE).You can also download it from my Github page. Any contribution and improvement to the code is duly welcome. The code is plug and play.

Note: Apart from the standard python packages, you will also need numpy and pandas to use this code.

if __name__ == "__main__":
print ("Welcome to K-nearest neighbor supervised learning classifier.\n\
All you will need to do is give your dataset and your unclassified feature set and this program will classify it for you\n\
Please keep in mind that the data must be in a csv file or else you will have to modify the source code\n\
\n\
so lets begin...")

inx=raw_input("please enter your input parameters in the form of a list ")

x=np.mat(inx)
if x.shape[1]!=dataset.shape[1]:
print ("invalid input\n please run the program again")

kay=raw_input("how many nearest neighbours(k) would you like to count the majority from\n")
k=int(kay)
siz=dataset.shape[0]
bl=np.zeros([dataset.shape[0],dataset.shape[1]])
for i in range(len(bl)):
bl[i,:]=x
difmat=bl-dataset

For the code to work without you needing to poke around the script itself, your data needs to be stored in a .csv file. The given code can handle alphanumeric designation of the class but its feature description needs to be strictly numeric.

Something like so...

The code can handle an arbitrary number of features and does a better job than the Matlab code seen before on this blog.
Remember, the input that you give to the algorithm must be in the form of a list.