Showing posts with label algorithm. Show all posts
Showing posts with label algorithm. Show all posts

Wednesday, 3 February 2016

Switching to ML concepts

As I'm getting a lot of assignments these days I've decided to pause app development and first work on them and they are interesting too.

I'll be detailing about every algorithm which I've learnt and will be learning.
So, lets start with k means algorithm.

K means Algorithm -

Suppose we have been given data about something and we wish to find pattern in the given data like some clusters where we can group similar type of data together.
To achieve this objective we can use k means algorithm.
To use this algorithm we need to have the information about the number of groups/clusters will be there. Let this number is k.

The algorithm -
1. Input - set of points (x1,x2,...xn) of size say n
2. Randomly generate k numbers and assume them to be the centroids at the moment.
3. Repeat until convergence:
    for each point xi:
        Find nearest centroid Cj (Can use eucledian distance to find the nearest centroid)
        Assign the point xi to cluster j
    for each cluster j=i...k
        new centroid Cj = mean of all points xi assigned to cluster j in previous step
4.Stop when none of the cluster assignments change


Note - 
After each iteration centroid will change and get placed optimally(better position than before)
We need to stop updating the centroids when there is no change in the cluster arrangement i.e. if clusters are remaining same for 2 consecutive iterations.

Time Complexity of this algorithm - O(#iteration*#clusters*#instances*#dimensions)

I've implemented this on python - See

The problem was - Given 150 flower's data with (petal length,petal width,sepal length,sepal width) we need to group them into 3 clusters.

I hope this will be helpful.

In addition to this I've rolled out an update for Medicator app. Now the app covers over 125 diseases.
You can check it out here - Medicator

Friday, 10 October 2014

Triangle Triangle everywhere not a point to think

Suppose we have 3 points which are making a triangle and we need to find whether a given point P is in the triangle or not .

There are many ways to do this problem but a very simple method is -

Lets say the triangle is made by three vertices A,B,C and the coordinates of the points are (x1,y1) , (x2,y2) , (x3,y3) and lets say point P is (a,b) so we just need to check whether the sum of the area of the triangle formed by the points A,B,C taken 2 at a time and P is equal to the area of the triangle ABC or not . Not getting it right ? No need to worry at all :)

Let ar(ABC) denotes the area of the triangle ABC. So, we just need to check whether

ar(ABC) = ar(ABP) + ar(BCP) + ar(CAP)

we can calculate the area of the triangle by the formula -
area = abs((x1*(y2-y3) + x2*(y3-y1)+ x3*(y1-y2))/2.0)

If it is equal then the given point P is in the triangle ABC .

To understand it better lets look at the figures below -

When P lies inside the triangle ABC


In this figure P is inside the triangle ABC and this can be easily seen that ar(ABP) + ar(BCP) + ar(CAP) = ar(ABC)

so we conclude that P lies inside ABC .






When P lies outside the triangle ABC

 In this figure P is outside the triangle ABC so the ar(ABP) + ar(BCP) + ar(CAP) is not equal to ar(ABC) as they include additional area of triangles BDP and DCP . 












TIP :  This observation can be applied to find whether a given point lies inside a given n-polygon or not by taking the area of triangles and equating it to the total area of the polygon . 

I hope you would've found this post informative and useful . For any suggestions just post a comment below. 

Have a good day .