Tuesday, 11 December 2012

Prototyping with Google's Prediction API, Python, SQLite and some JSON APIs

:: Introduction

I've been playing around lately with the car2go API v2.1 as I was curious about how easy it would be to ingest it's vehicle location data over time and then try to accurately predict certain outcomes using machine learning algorithms.

I've had quite a bit of success in the past using Google's Prediction API for spam comment detection but this "spam" problem was using classification values and not regression values. Regression values  would have to be used in this type of geo-coordinates based problem.

The last time I used the Python programming language was when I worked at Toshiba in Edinburgh, UK a number of years ago. I was interested to get back to using the language as I remember it being a really elegant language and it also seemed like a great fit to prototype things out. I was using a Macbook Air, which had Python pre-installed, so this made it pretty easy to get up and running. I knew I'd need to query the API, parse out the necessary values and store these to a datastore of some kind. I opted to use Mac OS X's built in SQLite database at it integrated well with Python and it provided a great interface from the command line to test out various SQL queries.

:: Inserting Records into SQLite with Python

The following Python program illustrates how to connect to a SQLite database called car2go.db and insert a single data record into the Vehicles table (Note that the Vehicles table schema needs to be pre-defined and created before the code will execute successfully):

#!/usr/bin/python
# -*- coding: utf-8 -*-

import sqlite3 as sqlite
import sys

con = None

try:
    con = sqlite.connect('car2go.db')

    cur = con.cursor()
    
    cur.execute("INSERT INTO Vehicles VALUES(<INSERT_VALUES_BASED_ON_SCHEMA>)")
    
except sqlite.Error, e:
    
    if con:
        con.rollback()
        
    print "Error %s:" % e.args[0]
    sys.exit(1)
    
finally:
    
    if con:
        con.close()

As you can see this code is really lightweight and easy to get up and running when you have a machine with Python installed. 

:: Requesting API Data and Parsing Values with Python

The next step was to query the JSON based API and parse out any necessary vehicle values:

#!/usr/bin/python
# -*- coding: utf-8 -*- import json import urllib2 import sys # Note that you'll need to replace <SECRET_KEY> & <LOCATION> with the appropriate values car2goVehiclesApi = 'https://www.car2go.com/api/v2.1/vehicles&oauth_consumer_key=<SECRET_KEY>&loc=<LOCATION>&format=json' # request vehicle data in json format webReq = urllib2.urlopen(car2goVehiclesApi) vehiclesJson = json.load(webReq)

The vehiclesJson object now has the JSON data from the web request and can be queried directly given the appropriate keys/indexes. And example to output the address of the first vehicle is:

print vehiclesJson['placemarks'][0]['address']

:: Outputting SQLite Query Data to CSV

Once these basics where done I then had the foundation to expand upon it further. I eventually extracted all the necessary JSON vehicle data from the web request and inserted it into my Vehicles table in my car2go.db SQLite database. From there I crafted the specific SQL query I wanted and output that query data to a CSV file using the following sqlite3 command syntax (simply run the SQL query after setting these values):

.headers off
.output vehicleData.txt
.mode csv

:: Using the Google Prediction API

Setting up your Google Prediction API the first time can be slightly tricky so be careful to follow Google's directions carefully. I won't go into these prerequisite details here as Google has already done a great job explaining them here (and will most likely keep them up-to-date in the future). 

After setting everything up the vehicleData.txt file was then uploaded to one of my Google Cloud Storage buckets where it could be queried directly by Google's Prediction API. Note that depending on the data you upload you may need to wrap the strings in double quotes according to Google's specified training data format. After uploading, the first task was to train my model which can take anywhere from a few seconds to a few minutes. Once this is complete (querying for the status will inform you when its complete) you can begin to ask for regression predictions from the predict HTTP request. Note that depending on what you are predicting the outputValue or the outputMulti[].score values can be retrieved from the JSON response and used to interpret your intended outcome.

(Note that I was using Google's Prediction API v1.5 for this prototype).

:: Conclusion

I'd highly recommend giving the Python language a try (if you haven't already) for your next prototype project. Integrating it with a lightweight database, JSON web requests and Google's Prediction API was pretty easy and my overall impressions with the language and available libraries are that its still a pleasure to work with.

Tuesday, 27 November 2012

Mobile Development Trends

:: Facebook on Android vs. iPhone Trend

A colleague of mine, Marwan Haddad, sent me an insightful article today about a poster that was recently seen at Facebook:


Many engineering teams are currently engaged in mobile development and because of this, mobile user trends are very important to keep track of so that each of us ends up building the right mobile applications today and hopefully as far into "tomorrow" as possible. Trends are shifting quickly as new users enter the smartphone market, decide to switch devices or simply change their overall mobile behaviour.

Facebook's poster (or really the IDC) is projecting quite far into the future as the date when this poster was mostly likely seen was around November 2012 (which puts us somewhere in the middle of the graph). The projection spans approximately 3 years from today till somewhere in 2016. In the technology business that's a very very long time to project anything as few people really know what will happen in 1 year from now let alone 3 (the iPhone 3GS was released just over 3 years ago and now look where we are with Android phones dominating). Maybe Windows Phone, the forthcoming Firefox OS or even a yet unnamed player will be lucky enough to steal some market from Google or Apple but really only time will tell. All I know right now is that the competition among these tech giants is fierce and as a result its producing an upward spiral of innovation within the smartphone market which all of us will benefit from for years to come.

The really interesting nugget of information within that Facebook poster projection is the dominating trend that Android currently has over iPhone and looks to have and increase in the months/years to come. If you or your company is still solely focused on iOS development at this point it may be worth reconsidering where the bulk of your customers are today let alone tomorrow. There was much talk way back when about Open vs. Closed Platforms and many predicted that even with Apple's initial dominance Android's "open system" would eventually prevail and it looks like that prediction is happening in full force (for the foreseeable future at least).

:: Kleiner Perkins' Mary Meeker on Mobile Trends

For an even deeper analysis of various Internet and mobile trends, Mary Meeker's year end trend report is an absolute gold mine of information. I've included a screenshot of slide #16 below where she notes that Android phone adoption was nearly 6x that of the iPhone... WOW.


:: TechCrunch's MG Siegler on Building for Mobile 

TechCrunch's MG Siegler wrote a post on Dec 11, 2012 titled Mobile Burst where he argues that "the mobile device is a different beast than a computer". He believes that companies/developers should focus a large amount of their resources on building the right type of user experience that is not only optimize for, but created exclusively for mobile use. In that post he links to his own personal blog post where he makes some very pointent statements:
  • "Don’t build an app based on your website. Build the app that acts as if websites never existed in the first place. Build the app for the person who has never used a desktop computer. Because they’re coming. Soon."
  • "What matters is that in the next five years every person on this planet is going to be using a mobile device. And these devices are going to be used far more than any traditional computer ever has been and ever will be."
So whether you're building mobile applications with HTML5 or going native, make sure to build the right experience for that specific mobile form factor instead of replicating or mimicking what you've previously built for the desktop.

:: Updates

Following on from MG Siegler's article above, here is an interview with Vibhu Norby on TechCrunch TV who disagrees that Mobile is unanimously the future of all development.

Megan Quinn of Kleiner Perkins Caufield & Byers (who formally worked at Google and Square) was interviewed on TechCrunch TV and had some things to say about the emerging mobile landscape:
  • "mobile is not an evolution this is a revolution."
  • "every human activity is being re-imagined through the prism of a mobile device"
She went on to say that the 3 areas of mobile development that are seeing huge potential are: Education, Consumer Health and the Internet of Things.

Another Mobile Internet assessment from Business Insider called "The Future of Mobile".


Saturday, 17 November 2012

RESAAS Wins Prestigious Microsoft Award

Yesterday my company, RESAAS, was announced as the winner of the 2012 Microsoft Impact Award in the category of Windows Azure Platform ISV Partner of the Year.

Our press release of can be found on Bloomberg and Microsoft has a few more details about our category and who we were up against on the Microsoft Partner Network.

Tuesday, 14 August 2012

RESAAS Windows 8 Application Approved by Microsoft

The engineering team at RESAAS recently started working on a Windows 8 styled application (using the Metro design language) for our real-time Question and Answer feed. I'm happy to say that Microsoft has approved the application and it is now available in the Microsoft Windows 8 Store.

RESAAS also pushed out a press release which found its way onto Yahoo Finance: http://finance.yahoo.com/news/resaas-enterprise-platform-real-estate-160000716.html

A PDF version of the press release can be found on the CSNX where our company's stock is listed: http://www.cnsx.ca/Storage/1472/134962_NRAug92012.pdf

Tuesday, 17 July 2012

Interviewed by Jonathan Rozenblit for "Canada Does Windows Azure"

Jonathan Rozenblit who is a Developer Advisor at Microsoft interviewed me for a series he does called "Canada does Windows Azure". We spent 17 min talking about how RESAAS uses Azure, why it decided to use Azure and how our developers ramped up on the platform when we first started.


Here are links to the blog posts Jonathan put on his blogs: