Data Preprocessing with R using IBM Cloud:
First to build any machine learning model on any dataset firstly we split the data into train and test with data to be trained by model is around 80% and other 20% of data is to be used to test the built model.
Data pre-processing involves following key processes before we start building any model
o Mission values
o Missing value imputation
o Identifying outliers
o Outlier treatment
o Pattern or relationships of the variables
o Variable transformation
o Binning
o Scaling
Let us understand above each point by implementing a dataset using R in IBM Cloud. Following are the steps to be followed
I. Register to IBM Cloud
II. Create Watson Studio service from Catalog
III. Create a Project in it with name ‘Data Preprocessing in R’
IV. Launch R Studio IDE, upload dataset and start giving code
I. Register to IBM Cloud:
(a) open https://cloud.ibm.com/login url
(b) Click on Create an IBM Cloud account and signup with your gmail id or intranet id
(c) After signup you will get an acknowledgement email and once after verifying it you can be able to login
(d) By default your account is lite account (lifetime access with limited services) upon applying promo code your account will convert to trail account (6 months access with more services). But to perform this demo lite account is good enough.
II. Create Watson Studio service from Catalog
(a) Click on Catalog tab on the Menu bar
(b) In Catalog, search for ‘watson studio’ it filters out and make available ‘Watson Studio’ Service from ‘AI’ category of services
(c) Click on Watson studio Service appearing right in the above page and click on Create in the next page
III. Create a Project in it with name ‘Data Preprocessing in R’
(a) Now Watson Studio service got created with a default name, in my case it is Watson Studio – vv click on Get Started to create a project
(c)Click on Create an empty project
(d) Give project name as ‘Data Preprocessing using R’ and click on Add to add an object storage service
(e) In next page of Cloud object storage service at bottom click on Create and Confirm. This helps in creating a storage service for our project.
(f) Click on Refresh so that cloud object storage service is tagged to Data Preprocessing using R project and click on Create therefore projects gets created
IV. Launch R Studio IDE, upload dataset and start giving code
(a) From Launch IDE click on RStudio
(b)Click on Launch to launch the R Studio session
(c) Following page is the perfect R Studio IDE where we can write R scripts
R Studio is an IDE for R Language, It looks like 4 windows clubbed together at 1 place
1. R script – Where we write R code
2. R console – Where we can see output of R code
3. R environment – Where we can see objects which got loaded with memory
4. R Graphical output – Where we can see graphical representation of data
(d) Let’s take famous ‘Titanic’ dataset which is used to predict survival of passengers and get familiarize with data preprocessing techniques. This we can download from https://www.kaggle.com/c/titanic
(e) Now upload the “titanic_train.csv” data set into RStudio. Click on upload ->Choose File ->Ok
(f) Now you can see data set uploaded in the RStudio IDE
(e) Let us read this ‘titanic_train.csv’ data set and perform data preprocessing techniques
Missing value:
In R, missing values are represented by the symbol NA (not available). Impossible values (e.g., dividing by zero) are represented by the symbol NaN (not a number). R uses the same symbol for character and numeric data.
Missing values can be found using is.na() function
Following code gives us positions of missing values in Age column. It gives TRUE value if value is missing else it gives FALSE
>is.na(titanic_train$Age)
Missing value imputation:
Generally, we replace missing values with the mean value of the variable, below code shows that there are 177 missing values in Age column at first and after we replace it with mean value it shows 0 missing values.
From here on type below all code in R Window 1 and click CTRL+ENTER for execution
Identifying the Outliers:
Outlier is an observation which is extreme different value from other all other observations. Outliers are easy to find in scatter plot and box plot
We can see a person with 80, which is really away from the ages of most of passengers. This is an outlier
Box plot is a best and simple way to find outliers as it creates quartiles and gives a good sense of how the data is distributed overall.
Outlier Treatment:
It is always better to remove outliers from the data as it affects the results and assumptions. The common definition of outlier is a point which falls more than 1.5 times the inter quartile range above the third quartile or below the first quartile
We can remove any points above or below the range
In the following code, I have created a function which assigns any value above or below 1.5 times inter quartile range a NA
After removing outliers in Age column
Pattern or relationships of the variables:
We analyse data pattern to understand the relationships and potential transformation in the data. In titanic data, we can see that survival is biased towards gender, so gender played a critical role in survival.
We can see in the plot above that the only a few proportion of male passengers survived comparing against female survival
Now, let’s check if survival depends upon where customers started their journey from
We can see in the above plot passengers who started their journey from point C, survival was a bit higher. So, boarding point also played a role in survival
Variable transformation:
Variable transformation is usually done to make the relationship linear or any other desirable form
In titanic data set, it does not require any major transformation as such. But will discuss an example to understand the variable transformation/ binning/ scaling
For this example we shall use data on the number of electronic academic journals over a seven year period
The first variable mentioned in plot command is plotted in horizontal axis. Not surprisingly the number of electronic journals really took off during 1991 to 97 period. As we have true exponential growth, plotting the logarithms of the growing table versus time should give a straight line. First we need to take logarithms and then make the plot
The relationship looks more linear now. The purpose of transformation using any function is to make relationships simpler and also less noisy
Binning:
Binning is a process of transforming numerical data into categorical parts. An example is to bin values for Age into categories such as 20-39, 40-59 and 60-79. Numerical variables are usually represented in the modelling based on frequency tables. Binning will help in improving accuracy of the predictive models by reducing the noise or non-linearity. Finally, binning allows easy identification of outliers, invalid and missing values of numerical variables
Let us take an example where we cut the data at various points to create several bins
Scaling:
Variables which are measured at different scales do not contribute equally to the analysis. For example, in boundary detection, a variable that ranges between 0 and 100 will outweigh a variable that ranges between 0 and 1. Using these variables without standardization in effect gives the variable with the larger range a weight of 100 in the analysis
Two methods are usually well known for rescaling data. Normalization and Standardization, Normalization scales all numeric variables in the range[0,1]
Standardization is another method to scale any variable in data set. It will transform it to have zero mean and unit variance.
Both the of these techniques have their drawbacks. If you have outliers in your dataset, normalizing your data will certainly scale the “normal” data to a very small interval. And generally, most of data sets have outliers. When using standardization, your new data aren’t bounded (unlike normalization)

























Nice lecture
ReplyDelete