Technologies: SAP HANA, R, HTML5, D3, Google Maps, JQuery and JSON
For this fun exercise, I analyzed more than 200 million data points using SAP HANA and R and then brought in the aggregated results in HTML5 using D3, JSON and Google Maps APIs. The 2008 airlines data is from the data expo and I have been using this entire data set (123 million rows and 29 columns) for quite sometime. See my other blogs
The results look beautiful:
Each airport icon is clickable and when clicked displays an info-window describing the key stats for the selected airport:
I then used D3 to display the aggregated result set in the modal window (light box):
Unfortunately, I can't provide the live example due to the restrictions put in by Google Maps APIs and I am approaching my free API limits.
Fun fact: The Atlanta airport was the largest airport in 2008 on many dimensions: Total Flights Departed, Total Miles Flew, Total Destinations. It also experienced lower average departure delay in 2008 than Chicago O'Hare. I always thought Chicago O'Hare is the largest US airport.
As always, I just needed 6 lines of R code including two lines of code to write data in JSON and CSV files:
################################################################################
airports.2008.hp.summary <- airports.2008.hp[major.airports,
list(AvgDepDelay=round(mean(DepDelay, na.rm=TRUE), digits=2),
TotalMiles=prettyNum(sum(Distance, na.rm=TRUE), big.mark=","),
TotalFlights=length(Month),
TotalDestinations=length(unique(Dest)),
URL=paste("http://www.fly", Origin, ".com",sep="")),
by=list(Origin)][order(-TotalFlights)]
setkey(airports.2008.hp.summary, Origin)
#merge the two data tables
airports.2008.hp.summary <- major.airports[airports.2008.hp.summary,
list(Airport=airport,
AvgDepDelay, TotalMiles, TotalFlights, TotalDestinations,
Address=paste(airport, city, state, sep=", "),
Lat=lat, Lng=long, URL)][order(-TotalFlights)]
airports.2008.hp.summary.json <- getRowWiseJson(airports.2008.hp.summary)
writeLines(airports.2008.hp.summary.json, "airports.2008.hp.summary.json")
write.csv(airports.2008.hp.summary, "airports.2008.hp.summary.csv", row.names=FALSE)
##############################################################################
Happy Coding and remember the possibilities are endless!
Showing posts with label JSON. Show all posts
Showing posts with label JSON. Show all posts
Wednesday, March 28, 2012
Big Data, R and HANA: Analyze 200 Million Data Points and Later Visualize Using Google Maps
Labels:
Advanced Analytics,
Business Analytics,
Business Intelligence,
D3,
D3.js,
Google,
Google Maps,
Google Maps API,
HTML5,
iPad,
JavaScript,
Jquery,
JSON,
R,
R-bloggers,
R-project,
SAP,
SAP HANA
Monday, January 30, 2012
Updated Sentiment Analysis and a Word Cloud for Netflix - The R Way!
The Netflix investors must be happy and cheerful as the stock is up more than 78% since the beginning of the year (YES, 78%, Source: Yahoo Finance!). I am not going to talk about what turned the stock around after a much talked/hyped about Netflix debacle of the late 2011 that earned Reed Hastings quite a few UNWANTED title and every one demanded his resignation from the top post. Not so fast, Mr. Bear! Reed Hastings must be smiling! After a stellar performance this year including carefully released stats on viewership, streaming hours as well as a solid Q4'11 earnings, Netflix is back and most importantly viewers are back!
Well, is is not coincidental that the sentiment for Netflix is also improving, 68% of the tweets now have positive sentiment. See the table below:
*Make sure you understand and interpret this analysis correctly. This analysis is not based on NLP.
I updated the sentiment analysis that I did last year, http://goo.gl/fkfPy , (I was then just beginning to play with Twitter and Text Mining packages in R) and used advanced packages like "TM" and "WordCloud". The new analysis is based on more than 6,800 words which are most commonly prescribed in various sentiment analysis blogs/books. (Check out Hu and Liu http://www.cs.uic.edu/~liub/ FBS/sentiment-analysis.html)
I came across this excellent blog by Jeffrey Bean, @JeffreyBean, (http://goo.gl/RPkFX) and his tutorial. Thank you Mr. Bean! Please follow the instructions from Bean's slides and the R code listed there as well as the R code here:
Here is the updated R code snippets -
#Populate the list of sentiment words from Hu and Liu (http://www.cs.uic.edu/~liub/ FBS/sentiment-analysis.html)
Saving the best for the last, here is a word cloud (also called tag cloud) for Netflix built in R-
I will be putting the R code up here for building a word cloud after scrubbing it.
Happy Analyzing!
Well, is is not coincidental that the sentiment for Netflix is also improving, 68% of the tweets now have positive sentiment. See the table below:
| Total | Positive | Negative | Average | Total | Sentiment |
| Tweets Fetched | Tweets | Tweets | Score | Tweets | |
| 499 | 171 | 80 | 0.281 | 251 | 68% |
*Make sure you understand and interpret this analysis correctly. This analysis is not based on NLP.
I updated the sentiment analysis that I did last year, http://goo.gl/fkfPy , (I was then just beginning to play with Twitter and Text Mining packages in R) and used advanced packages like "TM" and "WordCloud". The new analysis is based on more than 6,800 words which are most commonly prescribed in various sentiment analysis blogs/books. (Check out Hu and Liu http://www.cs.uic.edu/~liub/
I came across this excellent blog by Jeffrey Bean, @JeffreyBean, (http://goo.gl/RPkFX) and his tutorial. Thank you Mr. Bean! Please follow the instructions from Bean's slides and the R code listed there as well as the R code here:
Here is the updated R code snippets -
#Populate the list of sentiment words from Hu and Liu (http://www.cs.uic.edu/~liub/
huliu.pwords <- scan('opinion-lexicon/ positive-words.txt', what='character', comment.char=';')
huliu.nwords <- scan('opinion-lexicon/ negative-words.txt', what='character', comment.char=';')
# Add some words
huliu.nwords <- c(huliu.nwords,'wtf','wait',' waiting','epicfail', 'crash', 'bug', 'bugy', 'bugs', 'slow', 'lie')
#Remove some words
huliu.nwords <- huliu.nwords[!huliu.nwords==' sap']
huliu.nwords <- huliu.nwords[!huliu.nwords==' cloud']
#which('sap' %in% huliu.nwords)
twitterTag <- "@Netflix"
# Get 1500 tweets - an individual is only allowed to get 1500 tweets
tweets <- searchTwitter(tag, n=1500)
tweets.text <- laply(tweets,function(t)t$ getText())
sentimentScoreDF <- getSentimentScore(tweets.text)
sentimentScoreDF$TwitterTag <- twitterTag
# Get rid of tweets that have zero score and seperate +ve from -ve tweets
sentimentScoreDF$posTweets <- as.numeric(sentimentScoreDF$ SentimentScore >=1)
sentimentScoreDF$negTweets <- as.numeric(sentimentScoreDF$ SentimentScore <=-1)
#Summarize finidings
summaryDF <- ddply(sentimentScoreDF," TwitterTag", summarise,
TotalTweetsFetched=length( SentimentScore),
PositiveTweets=sum(posTweets) , NegativeTweets=sum(negTweets),
AverageScore=round(mean( SentimentScore),3))
summaryDF$TotalTweets <- summaryDF$PositiveTweets + summaryDF$NegativeTweets
#Get Sentiment Score
summaryDF$Sentiment <- round(summaryDF$ PositiveTweets/summaryDF$ TotalTweets, 2)
Saving the best for the last, here is a word cloud (also called tag cloud) for Netflix built in R-
I will be putting the R code up here for building a word cloud after scrubbing it.
Happy Analyzing!
Labels:
BA,
BI,
BI On Demand,
Business Analytics,
Business Intelligence,
Business Objects,
Cloud,
geo,
geocoding,
Google,
JSON,
Netflix,
On Demand,
RevolutionAnalytics,
SAP,
Sentiment Analysis,
Social BI,
TechEd,
Twitter
Tuesday, January 24, 2012
Geocode your data using, R, JSON and Google Maps' Geocoding API
First and foremost, I absolutely love the topic of Location Analytics (Geo-Spatial Analysis) and see tremendous business potential in not so distant future. I would go out on a limb to predict that the Location Analytics will soon go viral in the enterprise space because it has the capability to WOW us. Look no further than your iPhone or an Android phone and count how many location aware apps you have. We all have at lease one app - Google Maps. Mobile is one of the strongest catalyst for enterprise adoption of Location aware apps. All right, enough of business talk, let's get dirty with the code.
Over the last year and half, I have faced numerous challenges with geocoding the data that I have used to showcase my passion for location analytics. In 2012, I decided to take thing in my control and turned to R. Here, I am sharing a simple R script that I wrote to geo-code my data whenever I needed it, even BIG Data.
To geocode my data, I use Google's Geocoding service which returns the geocoded data in a JSON. I will recommend that you register with Google Maps API and get a key if you have large amount of data and would do repeated geo coding.
Here is function that can be called repeatedly by other functions:
Lat Lng
"37.4418834" "-122.1430195"
You can run this on the entire column of a data frame or a data table:
Over the last year and half, I have faced numerous challenges with geocoding the data that I have used to showcase my passion for location analytics. In 2012, I decided to take thing in my control and turned to R. Here, I am sharing a simple R script that I wrote to geo-code my data whenever I needed it, even BIG Data.
To geocode my data, I use Google's Geocoding service which returns the geocoded data in a JSON. I will recommend that you register with Google Maps API and get a key if you have large amount of data and would do repeated geo coding.
Here is function that can be called repeatedly by other functions:
getGeoCode <- function(gcStr)
{
library("RJSONIO") #Load Library
gcStr <- gsub(' ','%20',gcStr) #Encode URL Parameters
#Open Connection
connectStr <- paste('http://maps.google.com/maps/api/geocode/json?sensor=false&address=',gcStr, sep="")
con <- url(connectStr)
data.json <- fromJSON(paste(readLines(con), collapse=""))
close(con)
#Flatten the received JSON
data.json <- unlist(data.json)
lat <- data.json["results.geometry.location.lat"]
lng <- data.json["results.geometry.location.lng"]
gcodes <- c(lat, lng)
names(gcodes) <- c("Lat", "Lng")
return (gcodes)
}
Let's put this function to test:
geoCodes <- getGeoCode("Palo Alto,California")
> geoCodes
Lat Lng
"37.4418834" "-122.1430195"
Here is my sample data frame with three columns - Opposition, Ground.Country and Toss. Two of the columns, you guessed it right, need geocoding.
> head(shortDS,10)
Opposition Ground.Country Toss
1 Pakistan Karachi,Pakistan won
2 Pakistan Faisalabad,Pakistan lost
3 Pakistan Lahore,Pakistan won
4 Pakistan Sialkot,Pakistan lost
5 New Zealand Christchurch,New Zealand lost
6 New Zealand Napier,New Zealand won
7 New Zealand Auckland,New Zealand won
8 England Lord's,England won
9 England Manchester,England lost
10 England The Oval,England won
To geo code this, here is a simple one liner I execute:
> head(shortDS, 10)
Opposition Ground.Country Toss Ground.Lat Ground.Lng
1 Pakistan Karachi,Pakistan won 24.893379 67.028061
2 Pakistan Faisalabad,Pakistan lost 31.408951 73.083458
3 Pakistan Lahore,Pakistan won 31.54505 74.340683
4 Pakistan Sialkot,Pakistan lost 32.4972222 74.5361111
5 New Zealand Christchurch,New Zealand lost -43.5320544 172.6362254
6 New Zealand Napier,New Zealand won -39.4928444 176.9120178
7 New Zealand Auckland,New Zealand won -36.8484597 174.7633315
8 England Lord's,England won 51.5294 -0.1727
9 England Manchester,England lost 53.479251 -2.247926
10 England The Oval,England won 51.369037 -2.378269
Happy Demoing and Coding!
shortDS <- with(shortDS, data.frame(Opposition, Ground.Country, Toss,
laply(Ground.Country, function(val){getGeoCode(val)} )))
> head(shortDS, 10)
Opposition Ground.Country Toss Ground.Lat Ground.Lng
1 Pakistan Karachi,Pakistan won 24.893379 67.028061
2 Pakistan Faisalabad,Pakistan lost 31.408951 73.083458
3 Pakistan Lahore,Pakistan won 31.54505 74.340683
4 Pakistan Sialkot,Pakistan lost 32.4972222 74.5361111
5 New Zealand Christchurch,New Zealand lost -43.5320544 172.6362254
6 New Zealand Napier,New Zealand won -39.4928444 176.9120178
7 New Zealand Auckland,New Zealand won -36.8484597 174.7633315
8 England Lord's,England won 51.5294 -0.1727
9 England Manchester,England lost 53.479251 -2.247926
10 England The Oval,England won 51.369037 -2.378269
Labels:
Analytics,
Big Data,
Business Analytics,
Business Intelligence,
Business Objects,
Cloud,
Cognos,
geo,
geocoding,
Google Maps,
HANA,
In-Memory,
JSON,
Location Analytics,
Location Intelligence,
Maps,
R
Subscribe to:
Posts (Atom)

