How Retailers Use Latest Techniques from Machine Learning

Published: 03/07/2017  

Last Updated: 03/07/2017

In recent years, machine learning and automation has taken over. Technology has adapted machines to the needs of a wide range of domains; even outperforming humans. Decisions made based only on life experience and intuition are no longer enough to grow a business. Instead, decisions based on past data are far more powerful and indicative of future trends. We as humans can extend our intelligence to train machines to imitate us, to do things as we ourselves would.

Furthermore, technology is advancing from software based on a certain set of rules predefined by humans, to platforms that can learn these rules by themselves, based on the data, and with much higher precision and speed than humans themselves.

In today’s fast-paced world, the competition is intense, even between the smallest of businesses. Retailers across the world cannot afford to ignore any insights about their products and services from current or potential customers. There was a time when we had to pay hefty amounts for this information, and the collection methods were often tedious. Long surveys, focus groups, and feedback forms were among the preferred ways for collecting customer insights. In spite of the pain and the cost there was a problem with these methodologies—the sample size was very small and therefore not always representative of the actual customer base. These methods were also intrusive and not regarded by the customers as a commitment toward offering better products and services. A loss-loss situation at best!

With the evolution of machine learning it becomes possible to use technology to capture customer sentiment without letting them know that they are being observed. We can now base our important business decisions on these rich customer insights and innovate continuously to offer solutions that align with customer needs.

Today we’ll take a close look at exactly how retailers are using machine learning technologies to maximize their business. To do so, we’ll talk about the application programming interface (API). If you have a technical background, chances are that you might be familiar with and using this important terminology.

If you are new to APIs, just follow along. We will walk you through the basics. We will explain an API, the types of APIs, and various APIs that can be used for building data science applications. Finally, we will discuss a simple yet powerful way in which retailers can use Twitter* data to gain value with just a few lines of code.


  1. Application Programming Interface
  2. Types of API
  3. Difference between API and Library
  4. Popular APIs
  5. Top Five APIs to Extract Data
  6. Top Five APIs to Perform Cool Tasks
  7. Using the Twitter API to Perform Opinion Mining
  8. End Notes

1 Application Programming Interface 

The concept of API is straightforward. Just as we humans use a graphical user interface (GUI) or command-line interface (CLI) to interact with programs, computer programs use an application programming interface (API) to interact with other programs. The syntax, or the language in which this interaction takes place, is defined by the author of the API.

An API provides the framework for end users to incorporate programs written by other users into their own programs using simple steps, instead of rewriting the complete code themselves.

APIs make the life of a programmer simpler by allowing structured access to a program that was written and implemented by someone else. Generally, an API works as a function that provides the user the ability to pass certain inputs in a predefined format, and gives out the result in a predefined format, which is accessible to the person making the API call.

REST stands for representational state transfer. It is a lighter-weight alternative to simple object access protocol (SOAP) and web services description language (WSDL) XML-based API protocols. REST uses a client-server model, where the server is an HTTP server and the client sends an HTTP request (such as GET, POST, PUT, and DELETE), along with a URL and variable parameters that are URL-encoded. The URL describes the object to act upon and the server replies with a result code and valid JavaScript* object notation (JSON).

2 Types of API 

2.1 Web-based API

Web-based APIs act as an interface at the server or the browser end. Most modern web applications use these APIs. They allow the user to incorporate various functionalities into their web applications or websites using APIs authored by someone else.

For example, whenever you visit a company’s website and look for the Contact Us tab, you might find a map with tagged office locations on it. Most of the time all this functionality is implemented with the help of the Google Map* API and can be incorporated with just a few lines of code! This is how powerful an API can be.


Study the following code:


<!DOCTYPE html>
    <link rel="stylesheet" href="/maps/documentation/javascript/demos/demos.css">
    <div id="map"></div>
      function initMap() {
        var sanjose = {lat: 37.33, lng: -121.88};
        var lasvegas = {lat: 36.16, lng: -115.13};

        var map = new google.maps.Map(document.getElementById('map'), {
          center: sanjose,
          scrollwheel: false,
          zoom: 5

In this example, a developer is using the Google API for directions from San Jose to Las Vegas. This simple example shows that the developer does not need to learn how to draw a map, calculate distance, and so on, and is simply using the Google Maps API Directions. All the developer needs to input is longitude and latitude of source and destination. In simple terms, there is a contract between the developer of this code and Google API—you give me longitude and latitude and I will give back a map with directions.

Google API


2.2 Operating system API

Creating an application for a Windows* or Mac* OS would be a very complex task. But thanks to the operating system APIs, a wide range of functionalities are provided that make the creation of Windows or Mac applications easier. The following diagram shows the Linux* API, which is composed of the system call interface of the Linux kernel.

Linux* API Diagram

2.3 Database API

Today, requests and response to almost all databases are handled using APIs. Instead of writing a complete query every time an update or retrieval is needed, a simple command to update the database or to retrieve the data from it can be used.

The request is passed to the database in this predefined format and the database server provides the response to the request in a format defined by the author of that API. These APIs make it rather easy to communicate with databases and ensure standardization and speed.

2.4 Hardware API

Hardware APIs act as a crucial connection between the hardware and software worlds. From detecting the low fuel status in your car to detecting the tire pressure of a jumbo jet, APIs are used extensively in a wide range of domains.

The hardware generally passes along the information it recorded in a format that can be captured and processed with the help of software.

Look at the following code using the Python* OBD library. Python OBD is a library for handling data from a car's on-board diagnostics port (OBD-II). It can stream real-time sensor data and perform diagnostics (such as reading check-engine codes). This library is designed to work with standard ELM327 OBD-II adapters.

Python Code

3 Difference between API and Library 

As this article progresses, you may feel that APIs and libraries are the same things, when in fact they are not. So, it becomes crucial to explain the difference at this stage.

An API is simply an interface that can establish a communication between two applications. Certain rules and protocols govern this communication. The expected behavior of the API is predefined by its author, and anyone who wants to use it must follow these rules and protocols.

A library is much bigger than that. In fact, a library also has an API, which signifies the region in the library that is accessible to the outside user for communication or interaction. If the input is as specified, the output generated will follow the same format as specified.

4 Popular APIs 

Now, let’s look at some of the most popular APIs in the data science domain. We’ll break these APIs into two categories:

  1. APIs to extract data: Those APIs which give us access to datasets on which we can build cool data science projects.
  2. APIs to perform cool analysis: APIs to which we pass the data to perform very complex operations and return the results of that operation.

5 Top Five APIs to Extract Data 

Gone are the days when we need to work in a top firm at high positions to get data. Today, even the most valued datasets are available online for free. All credit goes to the developers who have worked hard in making the access to these datasets easy by creating APIs for them.

A list of popular category 1 APIs that extract data:

  • Facebook* API.
  • Twitter API.
  • YouTube* API.
  • GitHub* API.
  • Instagram* API.


Figure 1.

6 Top Five APIs to Perform Cool Tasks 

A list of popular category 2 APIs that perform analysis:

  • IBM Watson* API
  • BigML* API
  • Microsoft Azure* Cognitive Services API
  • Google Cloud* Prediction API
  •* API

Figure 2

Figure 2. Google Prediction API.

7 Using the Twitter API for Opinion Mining 

Now, we will take you through the implementation of opinion mining on data retrieved using the Twitter API. It will give you the power to know the opinions of people on any topic that you desire, across the globe, while sitting in your comfortable chair. Sounds interesting, right?

Let’s get started.

        #set up twitter authentication
    Cred <- OAuthFactory$new(consumerKey=consumerKey,


#Select search term      

#Select no. of tweets to invoke

#Invoke tweets        
        list <- searchTwitter(searchterm, n= num, lang="en", since=NULL, until=NULL, retryOnRateLimit=10) 
        l <- sapply(list, function(x) x$getText())
        l <- iconv(l, "latin1", "ASCII//TRANSLIT")
        l <- iconv(l, to='ASCII//TRANSLIT')
       #create corpus
        lc <- Corpus(VectorSource(l))
        #clean up 
#Convert every word to lower
        lc <- tm_map(lc, content_transformer(tolower))

#Remove punctuation        
        lc <- tm_map(lc, removePunctuation)

#Remove stop words
        lc <- tm_map(lc, function(x)removeWords(x,stopwords()))
        pal2 <- brewer.pal(8,"Dark2")

#create word cloud
        wordcloud(lc,min.freq=num/200,max.words=500, random.order=T, colors=pal2)



The output for the above code is generated in the form of a word cloud. This word cloud contains all the terms that are being frequently used in the tweets with your search term. Note that the stop words have been removed from the tweets. Each word that you see in the word cloud contains tons of importance. You can know the views of people across the globe on any topic that you wish.

Retailers use this technology extensively to know what their customers are thinking. It gives them an edge over their competitors and they can use the power of insights to grow their business.

8 End Notes 

APIs provide a lot of convenience to programmers. Instead of having to code every functionality that they want to implement in their applications, they can simply use APIs authored by someone else to do it for them.

A lot of social media giants are making their data publicly available. This opens a very exiting dimension which can provide deep business insights and market trends. It especially opens an opportunity in the field of natural language processing (NLP) in which retailers are getting to know their customers’ opinions and sentiments in a much-advanced manner.

References for images and research:

Product and Performance Information


Performance varies by use, configuration and other factors. Learn more at