Pages

C3 Health Services

Visit Official Website 9278982994

Expert Healthcare at Your Doorstep

Monday, 15 April 2019

Data Visualization using Box Plot (Seaborn Library)

Lets visualize our data with Box Plot which is present in Seaborn library. Box Plots are very useful in finding outliers in a variable. We can also combine Box Plot with Swarm Plot.

We can pass various parameters to boxplot like hue, order, orient, palette, color etc. 

Lets explore Box Plot using Tips dataset.

Step 1: Import required libraries

import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline

Step 2: Load Tips dataset

tips=sns.load_dataset('tips')
tips.head()

Step 3: Explore data using Box Plot

Box Plot is both univariate and bivariate. Lets analyze it first by using one variable and then we will use two variables. 

Visualizing one variable using Box Plot

sns.boxplot(x=tips['tip'])

sns.boxplot(x=tips['total_bill'])

sns.boxplot(x='total_bill', data=tips)

Visualizing two variables using Box Plot

sns.boxplot(x='sex', y='total_bill', data=tips)

sns.boxplot(x='day', y='total_bill', data=tips)

Add hue parameter

sns.boxplot(x='day', y='total_bill', data=tips, hue='sex')

sns.boxplot(x='day', y='total_bill', data=tips, hue='sex', palette='husl')

sns.boxplot(x='day', y='total_bill', data=tips, hue='smoker', palette='coolwarm')

sns.boxplot(x='day', y='total_bill', data=tips, hue='time', palette='coolwarm') 

Note: If you run the above line, you will find that there is no hue corresponding to "Sat" and "Sun" as there is no data for "Lunch" for "Sat" and "Sun".

sns.boxplot(x='day', y='total_bill', data=tips, order=['Sat', 'Sun', 'Thur', 'Fri'])

Change orientation of box plot

sns.boxplot(data=tips)

sns.boxplot(data=tips, orient='horizontal')
sns.boxplot(data=tips, orient='h')

sns.boxplot(data=tips, orient='vertical')
sns.boxplot(data=tips, orient='v')

Combining Box Plot and Swarm Plot

sns.boxplot(x='day', y='total_bill', data=tips, palette='husl')
sns.swarmplot(x='day', y='total_bill', data=tips, color='black')

sns.boxplot(x='day', y='total_bill', data=tips, palette='husl')
sns.swarmplot(x='day', y='total_bill', data=tips, color='0.35')

You can download my Jupyter notebook from here. I recommend to also try above code with Iris dataset.

Related:
What is Boxplot? How is it used to find outliers in a dataset?
Boxplot Grouping: Visualizing one variable based on another variable using boxplot

Sunday, 14 April 2019

Data Visualization using Strip Plot (Seaborn Library)

Lets visualize our data with Strip Plot which is present in Seaborn library. We can also use Strip Plot in conjunction with Box Plot and Violin Plot.

We can pass various parameters to stripplot like jitter, hue, dodge, order, palette, color, edgecolor, alpha, linewidth, marker, size etc. 

Lets explore Strip Plot using Tips dataset.

Step 1: Import required libraries

import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline

Step 2: Load Tips dataset

tips=sns.load_dataset('tips')
tips.head()

Step 3: Explore data using Strip Plot

Strip Plot is both univariate and bivariate. Lets analyze it first by using one variable and then we will use two variables. 

Visualizing one variable using Strip Plot

sns.stripplot(x=tips['tip'])

sns.stripplot(x=tips['total_bill'])

sns.stripplot(x='total_bill', data=tips)

sns.stripplot(x='total_bill', data=tips, color='green')

Visualizing two variables using Strip Plot

sns.stripplot(x='day', y='total_bill', data=tips)

sns.stripplot(x='total_bill', y='day', data=tips)

Add jitter parameter

sns.stripplot(x='day', y='total_bill', data=tips, jitter=False)

sns.stripplot(x='day', y='total_bill', data=tips, jitter=0.3)

sns.stripplot(x='day', y='total_bill', data=tips, jitter=0.3, linewidth=1.2)

Add hue and dodge parameter

sns.stripplot(x='day', y='total_bill', data=tips, hue='sex')

sns.stripplot(x='day', y='total_bill', data=tips, hue='sex', jitter=False)

sns.stripplot(x='day', y='total_bill', data=tips, hue='sex', dodge=True)

sns.stripplot(x='day', y='total_bill', data=tips, hue='sex', dodge=True, palette='winter_r')

sns.stripplot(x='day', y='total_bill', data=tips, hue='sex', dodge=True, palette='winter_r', order=['Sat', 'Sun', 'Thur', 'Fri'])

sns.stripplot(x='day', y='total_bill', data=tips, hue='sex', dodge=True, marker='D')

sns.stripplot(x='day', y='total_bill', data=tips, hue='sex', dodge=True, marker='D', size=10)

sns.stripplot(x='day', y='total_bill', data=tips, hue='sex', dodge=True, marker='D', size=10, edgecolor='gray', alpha=0.3)

Combining Strip Plot and Box Plot

sns.stripplot(x='day', y='total_bill', data=tips)
sns.boxplot(x='day', y='total_bill', data=tips)

sns.stripplot(x='day', y='total_bill', data=tips, jitter=False, palette='husl', color=0.1)
sns.boxplot(x='day', y='total_bill', data=tips)

Combining Strip Plot and Violin Plot

sns.stripplot(x='day', y='total_bill', data=tips, jitter=False, palette='husl', color=0.1)
sns.violinplot(x='day', y='total_bill', data=tips)

sns.stripplot(x='day', y='total_bill', data=tips, jitter=False, palette='husl', color=0.1)
sns.violinplot(x='day', y='total_bill', data=tips, color='grey')

You can download my Jupyter notebook from here. I recommend to also try above code with Iris dataset.

Friday, 12 April 2019

Creating Pandas DataFrame using CSV, Excel, Dictionary, List and Tuple

We can create pandas data frame in different ways. We can load data from CSV and Excel files. We can also create data frame using dictionary, lists and tuples. Following are some of the examples of loading data into pandas data frame:

Creating Pandas DataFrame using CSV

data_frame_csv = pd.read_csv("dataset.csv")
data_frame_csv 

Creating Pandas DataFrame using Excel Sheet

data_frame_xlsx = pd.read_excel("dataset.xlsx", "Sheet1")
data_frame_xlsx 

Note: You also have to specify sheet name of the Excel.

Creating Pandas DataFrame using Python Dictionary

dataset={
'day' : ['Sunday', 'Monday', 'Tuesday'],
'temperature' : [31, 25, 32],
'windspeed' : [6, 7, 5],
'event' : ['Rain', 'Sunny', 'Humid']
}

data_frame_dictionary = pd.DataFrame(dataset)
data_frame_dictionary

Creating Pandas DataFrame using Python List of Dictionary

dataset=[
{'day' : 'Sunday',  'temperature' : 31, 'windspeed' : 6, 'event' : 'Rain'},
{'day' : 'Monday', 'temperature' : 25, 'windspeed' : 7, 'event' : 'Sunny'},
{'day' : 'Tuesday', 'temperature' : 32, 'windspeed' : 5, 'event' : 'Humid'}
]

data_frame_dictionary_list = pd.DataFrame(dataset)
data_frame_dictionary_list

Creating Pandas DataFrame using Python List of Tuples

dataset=[
('Sunday',  31, 6, 'Rain'),
('Monday',  25, 7, 'Sunny'),
('Tuesday', 32, 5, 'Humid')
]

data_frame_tuple_list = pd.DataFrame(dataset, columns=['day', 'temperature', 'windspeed', 'event'])
data_frame_tuple_list

Note: You need to specify column names explicitly.

Documentation: Pandas IO Tools

Wednesday, 10 April 2019

How to create bins for continuous numeric variables using cut function of Pandas library?

In binning technique, we divide continuous numeric values in some groups or ranges called bins. It helps in better understanding of some of the continuous numeric features. To know more about binning technique, you can visit my this post. I have written a complete theory on it. Today, we will see how to create bins using cut function of pandas library?

Consider a Load Prediction dataset. We will create bins of LoanAmount variable. We will divide it into four bins: low, medium, high, very high.

Step 1: Import the required libraries

import pandas as pd
import numpy as np

Step 2: Load the dataset

dataset = pd.read_csv("C:/train_loan_prediction.csv")

Step 3: Create bins of a numeric variable using cut function

We will define cut points for binning in our variable and pass it to binning function so that it can create bins based upon the cut points which we have passed to it as a parameter.

#Create a binning function
def binning(col, cut_points, labels=None):
  
  #Define min and max values:
  minval = col.min()
  maxval = col.max()

  #Create a list by adding min and max to cut_points
  break_points = [minval] + cut_points + [maxval]

  #If no labels provided, use default labels 0 ... (n-1)
  if not labels:
    labels = range(len(cut_points)+1)

  #Binning using cut function of pandas
  colBin = pd.cut(col, bins=break_points, labels=labels, include_lowest=True)
  return colBin

#Binning LoanAmount variable:
cut_points = [90,140,190]
labels = ["low","medium","high","very high"]
dataset["LoanAmount_Bin"] = binning(dataset["LoanAmount"], cut_points, labels)
print (pd.value_counts(dataset["LoanAmount_Bin"], sort=False))

In the above code, we have passed 3 cut points and it will create 4 bins:
First bin contains all the values from minimum values to 90 (Label: low).
Second bin contains all the values from 91 values to 140 (Label: medium).
Third bin contains all the values from 141 values to 190 (Label: high).
Fourth bin contains all the values from 191 values to maximum value (Label: very high).

Instead of "low", "medium", "high" and "very high" labels, you can pass numeric values like 0, 1, 2 and 3 etc.

Now print the new variable dataset["LoanAmount_Bin"] and see the results. Instead of actual values, you will see labels in the data.

How to encode and transform all the categorical variables to numeric variables using LabelEncoder?

Machine Learning algorithms require all inputs to be numeric, so we should convert all our categorical variables into numeric variables by encoding the categories. Before that, please make sure that you have imputed all the missing values in all the categorical variables. We will use LabelEncoder which is present in Scikit Learn library to encode and transform categorical variables.

Consider a Load Prediction dataset. We will encode and transform all the categorical variables to numeric variables.

Step 1: Import the required libraries

import pandas as pd
import numpy as np
from sklearn.preprocessing import LabelEncoder

Step 2: Load the dataset

dataset = pd.read_csv("C:/train_loan_prediction.csv")

Step 3: Encode categorical variables using LabelEncoder

Categorical variables are Gender, Married, Dependents, Education, Self_Employed, Property_Area, Loan_Status. Lets encode and transform all these categorical variables to numeric variables in one go using following Python code.

categorical_vars = ['Gender','Married','Dependents','Education','Self_Employed','Property_Area','Loan_Status']
label_encoder = LabelEncoder()
for i in categorical_vars:
    dataset[i] = label_encoder.fit_transform(dataset[i])

Now, look at the datatypes of variables:

dataset.dtypes 

You will see that datatype of all the categorical variables has been changed from object to other datatypes like int32, float64 etc. So, now our dataset is ready for Machine Leaning algorithms.

Related: Difference between Label Encoder and One Hot Encoder

About the Author

I have more than 10 years of experience in IT industry. Linkedin Profile

I am currently messing up with neural networks in deep learning. I am learning Python, TensorFlow and Keras.

Author: I am an author of a book on deep learning.

Quiz: I run an online quiz on machine learning and deep learning.