Natural Language Processing

My Way via Google Natural Language Highway šŸŒ‰

Custom categorizations using Googleā€™s AI Platform

Akanksha Sinha
Analytics Vidhya
Published in
8 min readSep 17, 2020

--

Custom categorizations using Googleā€™s AI Platform

You may have heard of following scenarios -

i. Categorizing emails in your inbox as important/social/promotional

ii. Categorizing FB users to say passive/socially engaging/extroverts/town criers.

iii. Categorization as complex as DNA Sequence Classification

Now, let me tell you a story -

Vaibhavi works as a marketing analyst at a leading e-commerce website for women clothing. As part of her monthly analysis, she analyzed the review comments posted by the users.

For taking appropriate actions, she needed to categorize this data into departments. This way it will help her notify respective Departments and identify the ones who deserve accolades, attention and action items accordingly to improve overall customer experience.

Vaibhavi decided to take your help as you are data analyst in the Tech team of their firm. She approached you and explained her problem. You understood that Vaibhavi needed to categorize the review comments/feedbacks as per the companyā€™s department categorization. She shared the dataset with you.

For this story ā€” I have downloaded a sample dataset from Kaggle which is on review of Womenā€™s Apparel ā†’ Kaggle Data set. This data set categorizes product category from given invoice.

This dataset contains following columns -

  • Clothing ID
  • Age
  • Title Review
  • Text Rating
  • Recommended IND
  • Positive Feedback Count
  • Division Name
  • Department Name
  • Class Name

What will you do now ? šŸ¤·šŸ½ā€ā™€ļø

Well, you do have few approaches in your head -

  1. Using Natural Language APIs available in market
  2. Using python libraries (Tensorflow/sci-kit/nltk/numpy/pyTorch)to build custom model to categorize the data

You started with Step 1 -

Natural Language APIs

Now, if I feed in this dataset to -

  1. Google NLP API

Go to https://cloud.google.com/natural-language and click ā€˜Analyzeā€™ button.

Below is the result that Googleā€™s NLP API gave us -

With 85% confidence, Google categorized this review comment as Womenā€™s Clothing. Well, this is a very generic categorization in your case as all the review comments belong to Womenā€™s Clothing. This is not helping you šŸ˜«.

Letā€™s try one more API -

2. IBM Watson Natural Language Understanding

IBM Watson Natural Language API

Alas, this is not helping your case either. šŸ˜„ The Departments are like ā€˜topsā€™ and ā€˜bottomsā€™. There is no department as ā€˜skirtsā€™. They come under ā€˜bottomsā€™ in this firm.

Looks like APIs do not seems to fit in the solution you are looking for.

You want to look at the data from a different viewpoint. The problem at your hand is to categorize them by Dept Names like Dresses, Bottoms, Tops etc.

The categorization being offered by the APIs in market are not aligning to your categorize into Departments.

Step 2 i.e. creating custom models using Python libraries- Oh boy ! you do not have the required knowledge to create custom models using python. So, this approach is ruled out too.

Did we just reach dead-end ? šŸ™†šŸ½ā€ā™€ļø

This is where Googleā€™s Natural Language comes to rescue ! šŸ¦¹ā€ā™€ļø

Prerequisites :

  1. Basic knowledge of GCP and its console
  2. Very basic understanding of natural language (optional)
  3. No coding knowledge required

To access it, under in the Menu options in GCP console -

Artificial Intelligence ā†’ Natural Language ā†’ AutoML text and document classification

Natural Language Dashboard

I. Data Prep

a. As part of data prep, we will need to divide the dataset into Training and Test data. There are multiple ways to do that. In this case, here weā€™ll divide them in 80ā€“20 ratio. Weā€™ll use 80% of data for training and rest 20% for testing.

b. For categorization to Departments, we will not require parameters like Age, Recommended IND, Positive Feedback Count, Title, Class etc. Therefore, weā€™ll delete these columns. The only columns remaining would be Reviews and Department

c. Missing Date in ā€˜Review Textā€™ field ā€”

i. Out of 18k records, 674 are blank. One option could be to delete them. But, with this approach we may risk the decrease in training data. This may not be a good idea here. Therefore, weā€™ll fill up these blank rows with text same as Department name.

ii. There is one row (Row#13789) which has all columns as empty. We are removing this row.

iii. There are 8 rows where Department column is blank. How about we move these aside for evaluation of model ? We will delete these rows from Training data. Let our model determine the Department.

iv. As per recommendation provided in Googleā€™s Natural Language ā€” Each label should have at least 100 items for best results. So, we need to check our datasets for that.

Now, our training dataset is not having blank data points.

Training Dataset

P.S. The method for working with missing data differs case by case.

a. Removing ā€” Deleting the rows

b. Approximating

i. Applying mode ā€” This is more applicable in cases of binary/categorical data

ii. Applying median ā€” This is more applicable in case of columns having continuous (integer/decimal) values

d. Computed ā€” Computing missing values using some advanced algorithms

Use data prep guide from Google for preparing your training data.

II. Uploading Data/Documents

  1. Uploading the data in Cloud Datastore

a. Traverse to Cloud Storage ā†’ Browser

b. Create a bucket. It should have a unique name.

Creating Cloud Storage Bucket

Leave other default values as is.

Cloud Storage Bucket

c. Upload your .csv file here

Your file path will be gs://bucket-name/filename

For uploading the Training data, click on ā€˜New Data Setā€™ option as shown below -

Creating the dataset

  • Single label classification assigns a single label to each classified document
  • Multi-label classification allows a document to be assigned multiple labels

Importing the dataset

Dataset can be imported either from you machine locally or from Google Cloud Storage. Here, we have the data stored locally. Therefore, weā€™ll select the file and import.

Data import can take several minutes or more. You will be emailed once importing has completed.

Upload Errors:-

i. Labels here were in Letter case i.e. Dresses. Due to this, we got error for label names. We need to change the label names to be in all small caps.

ii. For a given label ā€˜Trendsā€™, the count of records was 99. We need to add a row from test data and make the count 100 as Auto ML requires at least 100 records for a given label

iii. Labels not having enough annotation. This happened due to presenece of Headers. Therefore, we can remove the header.

After fixing above issues, we will upload the training data file again in Cloud Storage and then in Auto ML.

Yay! our data has been successfully uploaded. Now that we reached uptil here, a BIG Applause šŸ‘šŸ½ for us, as Data prep is one of the most detail oriented and exhausting task in this whole exercise.

Training Data Uploaded

III. Training Custom Model

Now, letā€™s train our custom model. For this, go to ā€˜Trainā€™ tab and click ā€˜Start Trainingā€™

This will ask you to enter your model name. We may leave the default name provided. Ensure that the checkbox to deploy model after training is checked

Training of model may take few hours and will involve cost. Therefore, please do check pricing details before this step. An email will be triggered once the training is complete. Therefore, you need not be on GCP console all this while.

In this case, it took 5 hrs to train my custom model. This time may vary case by case.

IV. Evaluation

Here comes the report card day ! Just kidding. Letā€™s see the evaluation results to see how our custom model is doing.

On going to the console, this is what we see -

You may see the details by clicking on ā€˜See full evaluationā€™.

An interesting thing to see in evaluation detail is Confusion Matrix which shows how often the model classified the label correctly and how often was it confused in blue and grey respectively.

IV. Testing & Usage

How about now testing our model ? This can be done by traversing to the ā€˜Test & Useā€™ tab.

The data to be tested can be pasted directly in text box below or stored in Cloud storage and picked up from there.

We can pick sample records from the test data that we had set aside during Data Prep phase.

Letā€™s do the prediction with our custom model.

Sample Test record

Yey !! My custom model categorized the review comments under ā€˜Topsā€™ department with 97% confidence.

and one more -

This review comment was categorized under ā€˜Bottomsā€™ department with 97% confidence.

Your customized categorization was done. We now got the freedom to categorize our data our way as per the problem I need to solve at hand.

It indeed was MY WAY via this HIGHWAY :D

You then rushed back to Vaibhavi and shared your analysis. Vaibhavi was delighted ! You were happy to solve her problem.šŸ˜Š

Hope you enjoyed the story and it helped you in throwing light on an aspect of Natural Language and Data Analyis.

But, what if Vaibhavi came back with more datasets and came back again next month ? Are you kidding ! You canā€™t keep doing this all the time. Itā€™ll soon be time to automate this :). Watch out this space for more.

Please do let me know your feedbacks in comments. They would motivate me write further.

References:

Google Cloud Natural Language

Training Data Preparation

--

--

Akanksha Sinha
Analytics Vidhya

A digital knowledge seeker who loves to write about Automation, Digital Transformation, Google Technologies & Web Development