Monday, August 28, 2017

Predictive Models in R

Environment: RStudio

Make sure you have R and RStudio installed on your machine. If not, you can follow the links below for the same.

Install R
Install R on your machine

Install RStudio
Install RStudio on your machine. 

We'll make predictive model using "mtcars" dataset which is one of the built-in dataset in RStudio. This dataset has 11 variables namely "mpg","cyl","disp","hp","drat","wt","qsec","vs","am","gear" and "carb" for 32 car models.
Load this dataset in variable myData
Let's make a model to predict "mpg" using “qsec”, “hp” and “wt” attributes. We'll use "caret" package to make models. Install and load the caret package
Separate the chunk of data we need 
Replace all the blank cells with NA and then remove all the rows with any cell as "NA"
Define the train control for training the model using Repeated K-fold Cross Validation
Let's build a model using the linear regression 
To view the model summary,
Predict the "mpg" using Linear Regression
Calculate the accuracy of model. For Regression, there are two main metrics to calculate the accuracy of a model. First is RMSE(Root Mean Square Error) and the other is R2(R Squared). R2 has value between 0 and 1 and RMSE has a value greater than 0. Higher the value of R2, better the model and lower the value of RMSE, better the model. R2 is a better metric as it gives a better view of the accuracy of a model.
Let us make another model using Random Forest Algorithm. Only one step changes. To cater the randomness of algorithm, use set.seed() to get the same metric measurements everytime you run the model
train the model using Random Forest Algorithm
Predict the "mpg" using random forest
Measure accuracy of the model RMSE and R2 
To see the metrics of both the models in a single graph, use the following
This plot will show the metrics of both the models in the form of a bwplot and also arranges the models in the ascending order of accuracy. The model at the lowest is the most accurate. When we apply repeated cv, bwplot plots multiple RMSEs and finds the model which is overall the best among multiple iterations.

Thursday, February 23, 2017

DB2 Express C installation on Ubuntu 16.04 using Docker

Environment: Using Vagrant and Virtual Box

Open the terminal of your host machine.

Create a folder:


mkdir db2
cd db2

Initiate vagrant project using ubuntu 16.04 xenial64:


vagrant init ubuntu/xenial64









Vagrant up to spawn the VM


vagrant up --provider virtualbox












Machine starts.

To login into the VM:

vagrant ssh














You will be logged in as ubuntu user.

To install Docker, follow this link.

After docker is successfully installed, run the following command to check whether it is properly installed or not:

docker ps

this will list the docker processes running on your VM.

Start the container:

sudo docker run -it -p 50000:50000 -e DB2INST1_PASSWORD=db2inst1-pwd -e LICENSE=accept ibmcom/db2express-c:latest bash

You will have the bash command of the container.








Start DB2 and create sample DB

su - db2inst1
db2start
db2sampl

This will start DB2 and download the "SAMPLE" Database available on DB2 server.

If you wish to come out of the container bash,

Ctrl + P
Ctrl + Q

To enable private network to connect to DB2 using clients on host machine like razor (download), exit the VM and edit the Vagrantfile.


vi Vagrantfile

Vagrantfile:

# -*- mode: ruby -*-
# vi: set ft=ruby :

  Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/xenial64"
#This will create a private network using host-only network on the Virtual Box
  config.vm.network "private_network", :type => 'dhcp'
end

Reload vagrant 

vagrant reload

vagrant ssh
ifconfig

You will see a new interface attached to the host only adapter











Sunday, February 19, 2017

MPIR library installation on Ubuntu 16.04

Environment: Using Vagrant and Virtual Box

Open the terminal of your host machine.

Create a folder:


mkdir mpir
cd mpir

Initiate vagrant project using ubuntu 16.04 xenial64:


vagrant init ubuntu/xenial64









Vagrant up to spawn the VM


vagrant up --provider virtualbox












Machine starts.

To login into the VM:

vagrant ssh














You will be logged in as ubuntu user.

To resynchronize the package index files from its sources:

sudo apt-get update

Install these prerequisites before compiling:

sudo apt-get install yasm m4 build-essential unzip wget -y

Download the mpir library:

wget http://mpir.org/mpir-3.0.0-alpha2.zip

Unzip the downloaded zipped file:

unzip mpir-3.0.0-alpha2.zip

Change directory to mpir-3.0.0

cd mpir-3.0.0/

Run the following commands:

./configure
make
make check

Once the above commands are successful, run:

sudo make install

MPIR produces libraries named libmpir, etc. under /usr/local/lib,  and the header file mpir.h under /usr/local/include


Create a soft link or set the variable to find mpir library which is not on a standard location:

sudo ln -s /usr/local/lib/libmpir.so.23 /usr/lib/
OR
export LD_LIBRARY_PATH=/usr/local/lib/


To test library is working, copy paste the code snippet 

/*Program to check the Working of mpir library */

#include<mpir.h>
#include<stdio.h>
#include<iostream>

using namespace std;

int main()
{
 mpf_t a;         //mpir float variable
 mpf_init(a);     //initialise a
 mpir_ui two = 2; //mpir unsigned integer variable
 FILE* stream;    //file type pointer to output on standard output (console)
 mpf_init_set_ui (a, 2);          //set value of a to 2
 mpf_out_str (stream, 10, 2, a); //output value of a
 cout << "\nMPIR working" << "\n" ;
}

and run:

g++ test.cpp -lmpir -o test
./test