Monday, January 13, 2014

Reducing to the Map-Reduce paradigm- Thinking Web Scale – Part 1

In physics there are 4 types of forces – gravitational forces among celestial bodies, electro-magnetic forces and strong and weak forces at the sub-atomic level. The equations that seem to work among large bodies don’t seem to apply at the sub-atomic level though there have been several attempts at grand unification theories

Similarly in computing we have: – computing at personal level, enterprise level, data-center level and a web scale level. The problems and paradigms at each level are very different and unique. The sequential processing, relational database accesses or network speeds at the local area network level are very different to the parallel processing requirements, NoSQL based storage accesses  and WAN latencies.

Here is the first of my posts on paradigms at the Web Scale.

The internet now contains in excess of 1 billion hosts.  This is based on a report in the World Fact Book published in 2012.

In these 1 billion and odd hosts there are at least ~1.5 billion pages that have been indexed. There must be several hundred million that are not indexed by the major search engines.

Search engines like Google, Bing or Yahoo have to work on several hundred million pages.  Similarly social web sites like Facebook, Twitter or LinkedIn have to deal with several hundred million users who constantly perform status updates, upload images, tweet etc. To handle large quantities of data efficiently and quickly there is a need for web scale algorithms.

One such algorithm is the map-reduce, that had its origins in Google. The map reduce essentially consists of a set of mappers which take as input a key-value pair and outputs 0 or more key value pairs. The reducer takes all tuples with the same key and combines them based on some function and emits a key value pair

map_reduce

Map-reduce, and its open source avatar, Hadoop, are now used routinely to solve several large scale problems. To be honest, I was and still am, puzzled whether the 2 simple tasks types of mapping & reducing can be used for a large variety of problems. However, it appears so.

I would have assumed that there would have been other flavors, maybe an ‘identify-update’, ‘determine-solve’ or some such equivalent, unless a large set of problems can be expressed as some combination of the map reduce paradigm.

Anyway here a few examples for which the map reduce algorithm is useful.


Word Counting: The standard example for map-reduce is the word counting program. In this the map reduce algorithm generates a list of words with their corresponding word count from a set of input files. The Map task reads each document and breaks it into a sequence of words (w1, w2, w3 …). It then emits a key value pair as follows

(w1,1),(w2,1),(w3,1),(w1,1) and so on. If a word is repeated in the document it occurs multiple times in the output.  Now the entire key, value pairs are grouped by keys and sent to one of the reducer tasks. Each reducer will then sum all the values thus giving the total for each word.

a

Matrix multiplication: Big Data is a typical challenge in the web where there is a need to determine patterns and trends in mountains of data. Machine learning algorithms are utilized to determine structure in data that has 3 characteristics of volume, variety and velocity. Machine learning algorithms typically depend on matrix operations. Map-reduce is ideally suited for this and one of the original purposes of Google for map-reduce was with matrix multiplication.

Let us assume that we have a n x n matrix M whose element in row i and column j is mij

Also let us assume that there is a vector ‘v’ whose jth element is vj . Then the matrix vector product can be is the vector x of the length n whose ith element is given as

xi = ∑ mijvj



Map function: The map function applies to each single element of the matrix M. For each element mij the map task outputs a key-value pair as follows (i, mijvj).  Hence we will have a key-value pairs for all ‘i’ from 1 to n.

Reduce function:  The reduce function takes all pairs with the same key ‘i’ and sum it up.

Hence each reducer will generate

xi = ∑ mijvj

(Reference: Mining of Massive Datasets- Anand Rajaraman, Jure Leskovec, Jeffrey D Ullman)

This link gives a good write-up on a matrix x matrix multiplication,

Map-reduce for Relational Operations: Map-reduce can be used to perform a number of operations on large scale data that are used in database operations. Multiple database operations can be performed on large scale data like selection, projection, union, intersection, difference, natural join, grouping etc.

Here is a an example taken from ‘Web Intelligence & Big Data’ course from Coursera any Gautam Shroff.

Let us assume that there are 2 tables ‘Sales by address’ and “City by address’ and the need is to find the total ‘Sales by City’. The SQL query for this

SELECT SUM(Sale),City FROM Sales, City WHERE Sales.Addr_id = Cities.Addr_id GROUP BY City

This can be done by 2 map-reduce tasks.

The first map-reduce task GROUPs BY Sales as follows

Map1: The first map task will emit (Address, rest of record (SALE/City))

Reduce1: The first reduce task will SUM (Sales) by Address for every City. Clearly this will have multiple occurrences of City.


At this point we will have the sum of the sales for every city. However each city can occur multiple times. Now we have to GROUP BY City

Map2: Now the mapper emits the (City, rest of record (SALES)

Reduce2: The 2nd reduce now SUMS all the sales for each city.


Clearly the map-reduce algorithm does solve some major areas. It is extremely useful when there is a need to perform the same operation on multiple documents. It would definitely be useful in building the inverted index or in Page rank. Also, map-reduce is very powerful in handling matrix operations. Large class of problems like machine learning, computer vision all use matrices extensively and map-reduce is extremely critical when it has done in large volumes of data.  Besides, the ability of map-reduce to perform a large set of database operations is something that can be used in many situations in the web.

However it is no silver bullet for all types of problems.
[polldaddy rating="7295683"]

Find me on Google+

Simplifying Machine Learning – K- Means clusters - Part 6

Our brain is an extraordinary apparatus. It is amazing how we humans can instantaneously perceive shapes, objects, forms. For e.g. when see a scene with many objects we are immediately able to identify the different objects in the scene.  View this against the backdrop of a recent Google’s artificial brain experiment of a neural network with 16000 processors and a billion connections. This artificial brain was fed with 10 million thumbnails of you tube videos before it was able to recognize cat videos.

That’s an awful lot of work to recognize cat videos!

We can see that a lot of work involved getting a computer to do something as simple thing as this.

Consider how a baby learns to recognize objects for e.g. cat, dog, toy etc. The human brain does not try to measure the number of eyes, spacing between the eyes, the mouth shape of face etc. The brain immediately is able to distinguish the different animals. How does it do it? Amazing right?

In any case here is a machine learning algorithm that is capable of identifying structure in data. This is also known as K-Means and is a form of unsupervised learning algorithm.

The K-Means algorithm takes as input an unlabeled data set and identifies groups in the set. It tries to determine structure in the data set.

Take a look at the picture below

a

It is readily obvious that there are 2 clusters in the above diagram. However to the computer this is just a random set of points.

How does the K-Means cluster identify the clusters in the above diagram?

The algorithm is fairly simply and intuitive.

1)    Let us start by choosing 2 random points which we call as ‘cluster centroids

2)    We then associate each centroid with the points in the dataset that are closest to it.

3)    We then compute the average of each group of associated points in the centroid and move the centroid to that average.

4)    We then repeat steps 2 – 4 until there is no significant change in the centroid

This is shown below

4

The above algorithm can be implemented iteratively as follows

For training set (x1, x2, x3 …)

Randomly initialize K cluster centroids μ1, μ2, μ3 … μK



Repeat {

for 1 to m

c(i) = The cluster index from 1 to K that is closest to xi => (A)

end

for k = 1 to K

u(k) = average of all points assigned to K   => (B)

end

}

In step (A) the points xi closest to the centroid k is added to the centroid’s set. Hence if points 1,3,4,8 are in centroid 1 then

x1 = 1, x3 = 1, x4 = 1, x8 =1

In step (B) the mean of the points 1, 3, 4, 8 is taken

So the centroid

c1x = ¼ { x1 + x3 + x4 + x8} and c1y = ¼ { y1 + y3 + y4 + y8}

This becomes the new c1



However there can be occasions where the K-means cluster would get stuck in local optima. To choose optimum cluster centroid we have to determine the least cost. This can be done with the optimization objective.

The optimization objective of K-Means is as follows

K-Mean cluster determination is the problem of minimizing the distance of each point from its centroid. This is also known as the K-Means cost function or distortion function.

J(c1,c2…cm,, μ1,… μ2) = 1/m Σ|| xi – μc(i) ||2

I like to visualize the algorithm as follows.

In step 1 we can visualize that there is a force of attraction between the datapoints and the cluster centroid based on proximity of the centroid.

In step 2 we can visualize that each datapoint attracts the centroid towards it. The centroid moves to the point where the attraction among all the datapoints balances out. This is average mean squared difference.

As can be seen the objective is to determine the average of the mean squared error of each data point to its closest centroid.

Given a set of data points how we choose the random centroids? One way is to initially pick some random data points themselves as the cluster centroid. The algorithm is then iterated to identify the real cluster centroids.

As mentioned before the algorithm can sometimes get stuck in local optima.  One option is to choose another random set of data points and continue to iterate. We need to run this several times to determine the best clustering

There is also the problem of determining the number of cluster centroids. How we to determine how many clusters are would be there in a random data set? Visually we can easily identify the number of clusters. But a machine cannot.

One technique that can be used to determine the number of clusters is as follows. Start with 2, 3… 10 clusters and plot the cost function. Then pick the one with the least cost.

Note: This post, line previous posts on Machine Learning,  is based on the Coursera course on Machine Learning by Professor Andrew Ng
[polldaddy rating="7295683"]
Find me on Google+

Wednesday, January 8, 2014

The thing about the Internet of Things

km
Introduction: It is now common knowledge that the world is becoming more connected, instrumented and data driven. In a world of 7 billion people we have almost 10 billion devices connected to the internet. A recent report from Cisco suggests that the number of connected devices will almost touch 50 billion by the year 2020.
This huge increase in the number connected devices will come largely from a couple of new technology trends namely Internet of Things (IoT), Smart grids etc.
What exactly is the Internet of Things?
The first formal definition of the Internet of Things happened when ITU-T the telecom wing of United Nations came with a report titled “The Internet of Things” in 2005. In this report ITU-T added a fourth dimension of ‘anything’ to the existing anyone, anywhere, anytime network. This report visualized a world where millions and millions of devices either passive, intelligent or sensors collected data from the environment and sent it through the network to a backend processing system.
In Mark Weiser’s classic words, “the most profound technologies are those that disappear and weave themselves into the fabric of everyday life until they are indistinguishable from it”. Embedded intelligence in the things themselves will further enhance the power of the network. IoT is just this vision of Mark Weiser.
This fourth dimension of ‘things’ or intelligent sensors give the ability to gather data from the environment which is then sent back through the wireless network to the internet for back end processing. Analysis of the gathered data helps in forecasting events ahead of the time.
The Internet of Things is also known as M2M or machine–to–machine computing, pervasive computing or ubiquitous computing.
The Maha Kumbh Mela experiment: Last year, 2013, coincided with the 12 year cycle of the Maha Kumbh mela festival. More than 100+ million people would have passed through the city of Allahabad for a holy dip in river Sangam at the confluence of Ganges & Yamuna. Almost 95% of this human mass would have carried mobile phones equipped with location sensors.  Harvard Business University with the help of mobile Telecom Operators ran an experiment to track the movement of people through the city of Allahabad to understand the behavior of people. It was  hoped that the study of this large amount of data, as people moved through the city, would help in identifying signatures of disaster and how they can be avoided.
This is possible because mobile phones have the ability to send their location data back to the net for processing. This is an example of the Internet of Things.
Some applications of the Internet of Things is outlined below

RFID or Radio Frequency Identification: RFID was one of the early enablers of this technology; The RFID is a passive device that responds with its identity when it is in the presence of a RFID receiver. The RFID receiver transmits a signal and a RFID tag responds with its unique tag id. The RFID technology has been used extensively by large retail stores like Walmart of US and Tesco of UK etc. These stores RFID tag all their products in the central warehouse. In the presence of an RFID receiver the RFID tags of all the products are read. So the warehouse has a complete list of its inventory. As the products move from the central warehouse to the regional warehouse and finally to the retail store the products are tracked. So the retail stores know exactly how many of each product is present in all its warehouses and stores. As customers buy products and check it out at the counter the count of the products in the store is also updated. So at any point in time each store will know the count of each of its products. So stores like Walmart can now forecast if there is a going o be a shortage of any of it products and can move some of them to the concerned store. In fact we can imagine a scenario where each shopping cart is equipped with a RFID receiver. As we keep putting products into our cart the cart can add each of the items we have taken so that we have the bill ready when we reach the counter. We need not scan the products at the check out counter.
Highway Tolls: An interesting application of IoT, is the payment of highway tools in which the vehicle do not need to stop to pay the toll. Toll is deducted from a device, with a driver, which is RFID tagged. There are also applications in which the tires of cars are embedded with sensors to detect the wear & tear of the tires. Insurance companies can use the driving data from these sensors to give discounts to safe drivers.
Car-to-car networks: Another certainty in the evolution of IoT is car-to-car networks. Vehicular Communication along with the Intelligent Transport Systems (ITS) achieves safety by enabling communication between vehicles, people and roads. Vehicle-to-vehicle communications are the fundamental building block of autonomous, self-driving cars. It enables the exchange of data between vehicles and allows automobiles to “see” and adapt to driving obstacles more completely, preventing accidents besides resulting in more efficient driving.
Intelligent homes:  Rapid advances in technology will be closer to the home both literally and figuratively. The future home will have the ability to detect the presence of people, pets, smoke and changes to humidity, moisture, lighting, temperature. Smart devices will monitor the environment and take appropriate steps to save energy, improve safety and enhance security of homes.  Devices will start learning your habits and enhance your comfort and convenience. Everything from thermostats, fire detectors, washing machines, refrigerators will be equipped electronics that will be capable of adapting to the environment. ‘Nest’ is a smart thermostat that made headlines recently. The thermostat learns your requirements and adjusts the temperature accordingly. All gadgets in the Smart Home will be accessible through laptops, tablets or smartphones from anywhere. Others gadgets in Intelligent Homes are smart locks, smart lighting etc. Hence, we will be able to monitor all aspects of our intelligent home from anywhere.
Intelligent offices: Smart devices will also make major inroads into offices leading to the birth of intelligent offices where the lighting, heating, cooling will be based on the presence of people in the offices. This will result in an enormous savings in energy. The advances in intelligent homes and intelligent offices will be in the greater context of the Smart Grid.
eHealth: IoT is being used by some hospitals for monitoring of heart patients Here a device is  implanted into the patient. The device regularly sends data to a doctor who can monitor the patient’s pulse rate, heart rate, blood pressure etc.  It can warn the physician when it detects an irregularity in the patient’s heart rhythm who can then call the patient and advice on appropriate medication to take avoiding a real cardiac arrest.
Smart Cities: How often we sit fretting and fuming in a traffic jam contributing to air pollution. Smart Cities are equipped with multiple devices that identify and measure traffic speed and volume on city roads. At the back end the systems analyze this continuous stream of real time and provide alternative routes based on predictive analytics based on real time and historical data. Studies have also shown that it is possible to control traffic by offering discounts to drivers on less crowded roads.
Smart Grid: The grid or the legacy electrical network has three components to it namely energy generation, energy transmission and energy distribution. The conventional electrical grid which is prevalent in most countries throughout the world has extremely high transmission losses besides having other issues. Typically an outage in one part of the network would cause a cascading effect throughout the network. Remember the infamous blackout in US in 2003 which was the largest black in US in history. More closer to home, in India, we had a blackout in Dec 2012 which was the largest black out ever. This is because of the domino effect where an issue causes a cascading effect. Closer to home we had the world’s biggest blackout in Jul 31 which left 600 million powerless for close to 2 days.
With the advent of Smart Grid the legacy electrical grid will have millions of electrical sensors which monitor the flow of energy. If there is a fault in any part of the network the sensors ensure that the failure is isolated so that outage does not spread to other parts.
Besides instead of the regular electrical meters Smart Grids include the concept of the Smart home equipped with smart meters. These smart meters have a two way communication. The price of energy which we get from the grid varies like the stock price. With the smart meters and smart appliances these appliances turn on when the price of drawing energy is low.
Wearable Technologies: he latest entrants to IoT are the wearable technology like Smart watches, Google Glass, Health bands. These technologies constantly monitor measure and send the data for processing to the backend.  For e.g. Google’s glass can immediately recognize prominent landmarks and display it. Similarly health bands like Fitbit, Nike FuelBand etc can now measure steps, heart rate and provide feedback.
Challenges: There are still many challenges on the way to a future filled with M2M. There is still no universally accepted protocol. There are many competing protocols like WiFi, Zigbee, MQPP, XMPP etc and there is yet to be a single common standard between devices and the networks for the Internet Of Things.
In any case, the Internet of Things or M2M is happening technology and will soon come into our neighborhood and we should all be pretty swamped by this tidal wave in our future
[polldaddy rating="7295683"]

Simplifying ML: Impact of degree of polynomial degree on bias, variance and other insights

This post takes off from my earlier post Simplifying Machine Learning: Bias, variance, regularization and odd facts- Part 4. As discussed earlier a poor hypothesis function could either underfit or overfit the data.  If the number of features selected were small of the order of 1 or 2 features, then we could plot the data and try to determine how the hypothesis function fits the data. We could also see whether the function is capable of predicting output target values for new data.

However if the number of features were large for e.g. of the order of 10’s of features then there needs to be method by which one can determine if the learned hypotheses is a ‘just right’ fit for all the data.
The following technique can be used to determine the ‘goodness’ of a hypothesis or how well the hypothesis can fit the data and can also generalize to new examples not in the training set.
Several insights on how to evaluate a hypothesis is  given below
Consider a hypothesis function
hƟ (x) = Ɵ0 + Ɵ1x1 + Ɵ2x22 + Ɵ3x33  +  Ɵ4x44
a1
The above hypothesis does not generalize well enough for new examples in the data set.
Let us assume that there 100 training examples or data sets. Instead of using the entire set of 100 examples to learn the hypothesis function, the data set is divided into training set and test set in a 70%:30% ratio respectively
The hypothesis is learned from the training set. The learned hypothesis is then checked against the 30% test set data to determine whether the hypothesis is able to generalize on the test set also.
This is done by determining the error when the hypothesis is used against the test set.
For linear regression the error is computed by determining the average mean square error of the output value against the actual value as follows
The test set error is computed as follows
Jtest(Ɵ) = 1/2mtest Σ(hƟ (xtesti  - ytesti)2
For logistic regression the test set error is similarly determined as
Jtest(Ɵ) = = 1/mtest Σ -ytest * log(hƟ (xtest))  - (1-ytest) * (log(1 - hƟ (xtest))
The idea is that the test set error should as low as possible.
Model selection
A typical problem in determining the hypothesis is to choose the degree of the polynomial or to choose an appropriate model for the hypothesis
The method that can be followed is to choose 10 polynomial models
  1. hƟ (x) = Ɵ0 + Ɵ1x1
  2. hƟ (x) = Ɵ0 + Ɵ1x1 + Ɵ2x22
  3. hƟ (x) = Ɵ0 + Ɵ1x12 + Ɵ2x22 + Ɵ3x33
  4. …
Here‘d’ is the degree of the polynomial. One method is to train all the 10 models. Run each of the model’s hypotheses against the test set and then choose the model with the smallest error cost.
While this appears to a good technique to choose the best fit hypothesis, in reality it is not so. The reason is that the hypothesis chosen is based on the best fit and the least error for the test data. However this does not generalize well for examples not in the training or test set.
So the correct method is to divide the data into 3 sets  as 60:20:20 where 60% is the training set, 20% is used as a test set to determine the best fit and the remaining 20% is the cross-validation set.
The steps carried out against the data is
  1. Train all 10 models against the training set (60%)
  2. Compute the cost value J against the cross-validation set (20%)
  3. Determine the lowest cost model
  4. Use this model against the test set and determine the generalization error.
Degree of the polynomial versus bias and variance
How does the degree of the polynomial affect the bias and variance of a hypothesis?
Clearly for a given training set when the degree is low the hypothesis will underfit the data and there will be a high bias error. However when the degree of the polynomial is high then the fit will get better and better on the training set (Note: This does not imply a good generalization)
We run all the models with different polynomial degrees on the cross validation set. What we will observe is that when the degree of the polynomial is low then the error will be high. This error will decrease as the degree of the polynomial increases as we will tend to get a better fit. However the error will again increase as higher degree polynomials that overfit the training set will be a poor fit for the cross validation set.
This is shown below
a2
Effect of regularization on bias & variance
Here is the technique to choose the optimum value for the regularization parameter λ
When λ is small then Ɵi values are large and we tend to overfit the data set. Hence the training error will be low but the cross validation error will be high. However when λ is large then the values of Ɵi become negligible almost leading to a polynomial degree of 1. These will underfit the data and result in a high training error and a cross validation error. Hence the chosen value of λ should be such that the cross validation error is the lowest
a3
Plotting learning curves
This is another technique to identify if the learned hypothesis has a high bias or a high variance based on the number of training examples
A high bias indicates an underfit. When the number of samples in training set if low then the training error and cross validation error will be low as it will be easy to create a hypothesis if there are few training examples. As the number of samples increase the error will increase for the training set and will slightly decrease for the cross validation set. However for a high bias, or underfit, after a certain point increasing the number of samples will not change the error. This is the case of a high bias
a4
In the case of high variance where a high degree polynomial is used for the hypothesis the training error will be low for smaller number of training examples. As the number of training examples increase the error will increase slowly. The cross validation error will be high for lesser number of training samples but will slowly decrease as the number of samples grow as the hypothesis will learn better. Hence for the case of high variance increasing the number of samples in the training set size will decrease the gap between the cross validation and the training error as shown below
a5
Note: This post, line previous posts on Machine Learning,  is based on the Coursera course on Machine Learning by Professor Andrew Ng

Simplifying Machine Learning: Bias, Variance, regularization and odd facts – Part 4

In both linear and logistic regression the choice of the degree of the polynomial for the hypothesis function is extremely critical. A low degree for the polynomial can result in an underfit, while a very high degree can overfit the data as shown below
41
The figure on the left the data is underfit as we try to fit the data with a first order polynomial which is a straight line. This is a case of strong ‘bias’
The rightmost figure a much higher polynomial is used. All the data points are covered by the polynomial curve however it is not effective in predicting other values. This is a case of overfitting or a high variance.
The middle figure is just right as it intuitively fits the data points the best possible way.
A similar problem exists with logistic regression as shown below
42
There are 2 ways to handle overfitting
a)      Reducing the number of features selected
b)      Using regularization
In regularization the magnitude of the parameters Ɵ is decreased to reduce the effect of overfitting
Hence if we choose a hypothesis function
hƟ (x) = Ɵ0 + Ɵ1x12 + Ɵ2x22 + Ɵ3x33 +  Ɵ4x44

The cost function for this without regularization as mentioned in earlier posts
J(Ɵ) = 1/2m Σ(hƟ (xi  - yi)2
Where the key is minimize the above function for the least error
The cost function with regularization becomes
J(Ɵ) = 1/2m Σ(hƟ (xi  - yi)2 + λ Σ Ɵj2

As can be seen the regularization now adds a factor Ɵj2  as a part of the cost function which needs to be minimized.
Hence with the regularization factor the problem of underfitting/overfitting can be solved
43
However the trick is determine the value of λ. If λ is too big then it would result in underfitting or resulting in a high bias.
Similarly the regularized equation for logistic regression is as shown below
J(Ɵ) = |1/m Σ  -y * log(hƟ (x))  - (1-y) * (log(1 - hƟ (x))  | + λ/2m Σ Ɵj2
Some tips suggested by Prof Andrew Ng while determining the parameters and features for regression
a)      Get as many training examples. It is worth spending more effort in getting as much examples
b)      Add additional features
c)      Observe changes to the learning algorithm with different values of λ
Note: This post, in line with my previous posts on Machine Learning,  is based on the Coursera course on Machine Learning by Professor Andrew Ng


Wednesday, December 11, 2013

A method to crowd source pothole marking on (Indian) roads

In, India, roads and potholes are 2 sides of the same coin! You cannot think of one in exclusion of another. This post of mine looks at a novel technique of rapidly identifying & marking potholes in (Indian) roads. This approach can be used for any city in the world but is very pertinent to Indian roads.
This idea of mine provides a technique of quickly marking pothole in roads through the method of crowd sourcing

Introduction: It is a well known fact that Indian roads are riddled with potholes. Some may even say that there are potholes with patches of road in between them. This disclosure looks at a novel technique of rapidly identifying & marking potholes in (Indian) roads. The approach can be used for any city in the world. However this disclosure will focus on Indian roads. This disclosure proposes a novel technique of crowd-sourcing the marking of potholes on roads rather than having any single government body (NHAI etc) travel on roads to make the markings.
Description: This post proposes a novel crowd-sourced method for pothole marking that will be easy to conduct and extremely rapid The crowd-sourced pothole marking application will be made of the following components namely Pot-hole marking app, Backend server, Map Matching utility, Pothole ranking utility.
Pothole marking App: A location based smartphone app will need to be created preferably both on Android and iOS. The app will display the map with buttons to mark the following
a)   Points in map of potholes
b)   Bad segments of roads with potholes
Backend Server: The backend server will collect all the data (marked potholes) and bad segments of roads and will update a database.  A map-matching utility will map the latitude, longitude of the marked point on to a map. When the geographical location of a pothole is received (latitude, longitude) the backend server will also store the time stamp.
Pothole ranker: This module will run on a periodical basis, say once every 3 minutes. This module will determine all the potholes that have been entered in the last 3 minutes and add to the accumulated count of marked potholes. Each marked pothole will hold the count of the marks and also the time stamp of the mark. It will also rank the criticality of the pothole based on the accumulated count of potholes over the period.
The pothole ranker will maintain the following metrics
  1. Pothole criticality = Total accumulated count/ Total time
  2. Pothole impact measure = Max rate of pothole marks (Pothole marks/hr)
  3. Bad stretches of roads with many potholes =
Number of adjacent potholes/ Distance in meters
cs
Description: This how the scheme will work in practice. The app will be uploaded into Google Play and Apple’s App store.  All users who would like to participate in the pothole marking exercise can download and install the app on their smart phones. These users when they are traveling on a road can mark potholes as they encounter them. It is assumed that the users are passengers in vehicles or pillion riders. The fact that users all over the city can simultaneously mark potholes as they encounter them will make the gathering of pothole data rapid and extremely accurate. A map of a city would need to be generated with the circles/points for locations of potholes, color-coded appropriately. We could use the color red for higher ranked potholes and yellow for lower ranked potholes with intermediate colors like purple, pink etc.  This data can then be used by Government bodies in addressing roads in fixing the roads.
There are three advantages of crowd sourcing the pothole marking
1)   The process of gathering data is rapid
2)   Roads where the traffic is heaviest will have potholes with a higher rank and can be addressed first
3)   The process will be very accurate
Crowd sourcing of pothole marking will have the following benefits
  1. The marking of potholes will be extremely rapid
  2. The potholes will be ranked based on accumulated count
  3. Ranking of potholes can be done on
- Total accumulated count/Total time
- Rate of pothole mark
- Critical segments with major potholes
4. It will be easy to segregate
- Critical potholes
- Max impactful potholes
- Bad road segment
  1. The process will be very accurate
Conclusion: The process of crowd sourcing pothole marking of Indian roads will be extremely efficient in marking potholes and bringing it to the attention of the Government.
A map of a city with the circles for locations of potholes, color-coded appropriately, to indicate higher marked potholes versus the lower ranked potholes could be generated. This map can be used to bring to the attention of the government the really bad roads and terrible road segments. Rather than having a couple of vehicles trying to ply roads and mark roads this will be very fast and extremely accurate.
Afterword: The concept of crowd sourcing for traffic is not new. Waze, which Google bought for close to $2bn does just that. It crowd sources traffic conditions and alerts users of the app. Also I did a Google search on using mobile apps for potholes marking and, not surprisingly, there were others who had also thought of a similar idea in Boston & Florida see the links below
  1. http://www.cityofboston.gov/doit/apps/citizensconnect.asp
  2. http://dailycrowdsource.com/20-resources/projects/421-crowdsourced-pothole-database-to-track-road-repair
However, I personally think that the situation in India is different, where there are ‘roads in between potholes’ ;-). While in the above 2 cases in US, only the location of the potholes is important, my idea ranks potholes based on the accumulated count and the rate of pothole marks. These metrics can be used by the government in addressing those sections of roads where the potholes have a higher rank i.e. where the traffic is highest.
Your thoughts are welcome.

Saturday, November 16, 2013

Simplifying ML: Neural networks- Part 3

Neural networks try to overcome the shortcomings of logistic regression in which  we have to choose a non-linear hypothesis. Logistic regression requires that we choose an appropriate combination of polynomial terms and the order of the equation. The problem with this is sometimes we either tend to overfit or underfit. Neural networks allow the ability to learns new model parameters from the basis raw parameters.
The neural network is modeled on the neural networking ability of the human brain. The brain is made of trillions of neurons. Each neuron is a processing unit which has several inputs in the dendrites and an output the axon. The neurons communicate thro a combination of electro chemical signal at the synapses or the spaces between the neuron.
neuron
A neural network mimics the working of the neuron.
So in a neural network the features of the problem serve as input. For e.g in the case of being able to determine if a mail is spam or not the features could be the words in the subject line, the from address, the contents etc. Based on a combination of these features we need to classify whether the mail is spam or not.
31
The above diagram shows a simple neural network with features x1, x2, x3 and a bias unit x0

With a hypothesis function hƟ(x) = 1/(1 + e-x)
The edges from the features xi  are the model parameters Ɵ. In other words the edges represent weights.
A typical neural network is a network of many logistic units organized in layers. The output of each layer forms the input to the next subsequent layer. This is shown below
32
As can be seen in a multi-layer neural network at the left we have the features x1,x2, .. xn.
This at the layer becomes the activation unit. The key advantage of neural networks over regular logistic regression that learns the models parameters is that learned model parameters are input to the next subsequent layers which learn the model parameters more finely. Hence this gives a better fit for the combination of parameters.
The activation parameters at the next layer are
a12 = g(Ɵ101x0+ Ɵ111x1+ Ɵ121x2 + Ɵ131x3) where g is the logistic function or the sigmoid function discussed in my previous post Simplifying ML: Logistic regression - Part 2
33
Here a12 is the activation parameter at layer 1
Ɵ10 is the model parameter at layer 1 and is the 0th parameter. Similarly Ɵ11 is the model parameter at layer 1 and is the 1st parameter and so on.
Similarly the other activation parameters can be written as
a22 = g(Ɵ201x0+ Ɵ211x1+ Ɵ221x2 + Ɵ231x3)
a32 = g(Ɵ301x0+ Ɵ311x1+ Ɵ321x2 + Ɵ331x3)
hƟ(x) = a13 = g(Ɵ102a0+ Ɵ112a1+ Ɵ122a2 + Ɵ132a3  - (A)

The crux of neural networks is that instead of creating a hypothesis based on the set of raw features, the neural network with multiple hidden layers can learn its own features. In the equation (A) we can see that the hypothesis is not a function of the input raw features x1,x2,… xn  but on a new set of features or the activation units a1,a2, … an . In other words the network has ‘learned’ its own features.
As mentioned above the output of each layer is the logistic function or the sigmoid function
The beauty of neural networks based on logistic functions is that we can easily realize the equivalent of logic gates like AND, OR, NOT, NOR etc.
The hypothesis for the above network would be
34
hƟ(x) = g(-30 + 20 * x1 + 20 * x2)
So for x1= 0 and x2 = 0 we would have
hƟ(x) = g(-30 + 0 + 0) = g(-30)
Since g(-30) < g(0) < 0.5 = 0
37
Similarly a NOT gate can be constructed with a neural network as follows
35
38
Neural networks can also be used for multi class classification.
36
Hence there are multiple advantages to neural networks. Neural networks are amenable to a) creating complex logic models of combinations of AND, NOT, OR gates
b) The model parameters are learned from the raw parameters and can be more flexible.
It appears that the interest in neural networks surged in the 1980s and then waned, The neural networks were similar to the above and were based on forward propagation. However it appears that in recent time’s backward propagation has been used successfully in areas of research known as ‘deep learning’
This is based on the Coursera course on Machine Learning by Professor Andrew Ng. A highy enjoyable and classic course!!!