Python Programming for Data Analysis
Python Programming for Data Analysis
net/publication/343442500
CITATIONS READS
0 21,721
2 authors, including:
SEE PROFILE
All content following this page was uploaded by Dr. Asha Jindal on 05 August 2020.
Department of Statistics
under the aegis of DBT-STAR Status Scheme
Editor
Dr. Asha Angnamal Jindal
Department of Statistics
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
Preface
Python is a powerful programming language that is capable of conducting any kind of data analysis
in the Sciences, Social Sciences, Corporate world and Management. Mathematics is generally
thought to be the language of science whereas Data Analysis is the language of research. Research is
important for human progress, so as long as there is research there will be a need to analyze data.
The present book is designed to meet this challenge of analysis of data. In our teaching, we
generally say "Analyze this data using python, get the manuals or visit website". If you don't know
how, then this book titled “Analyzing and Visualizing Data using Free Open Source Software-
Python Programming with case studies” (Jupyter Notebook 6.0.3 and Python 3.7.6) is an answer
to all such queries. It is designed in such a way that Python procedures become clear to the
beginner. Also, it works as an effective reference for anyone conducting data analysis.
This involved the expansion of the original concept to include essentially all major statistical
procedure as well as regression and advance models. This is the result of years of
teaching/research experience of contributors that you now hold in your hands.
I, Dr. Asha A. Jindal, Associate Professor and Head, Department of Statistics wish to acknowledge all
the contributors who put their creative efforts in writing the chapters. I sincerely acknowledge
Principal Dr. Hemlata K. Bagla, K. C. College and Dr. Sagarika V. Damle, DBT-Star Status Coordinator
for their support and encouragement.
I express sincere gratitude to Mr. Ashutosh Tiwari, Shailja Prakashan, Kanpur for wholeheartedly
giving ISBN number for this book and encouraging this initiative. I thankfully acknowledge Mr.
Roshan Khilani for the book cover page designing to final formatting of the present book, Mr.
Shubham Niphadkar who contributed substantially to the design and format chapters of present
book.
i
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
Message
It is heartening to learn that Dr. Asha Jindal, Head, Department of Statistics along with team
of enthusiastic budding researchers has undertaken this exercise of preparing this handy
book on free open source software "Python Programming with Case Studies", to meet
various challenges, while analysing Datasets generated from different fields.
This book contains simple and doable data set and codes on how to prepare for analytics
and what to do when caught in an analytical challenge . I am sure this crisp and informative
document would enable the students and researcher community to prepare and manage
analytics more effectively.
I am confident that it will also be widely useful for Educational institutions, Experts, Data
Scientist and other Stakeholders as a ready recknor.
I congratulate Department of Statistics for introducing this book , under the aegis of DBT-
Star College Status Scheme.
ii
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
Message
“A good teacher can inspire hope, ignite the imagination, and instill a love of learning.”
As Principal of K C College it gives me the greatest joy to see my teachers fulfilling their role and
going beyond for the cause of education and students! A heartfelt congratulations to the
Department of Statistics, under the aegis of DBT-Star College Status Scheme, for preparing a
comprehensive list of articles, algorithm and codes in a book as well as e- book form titled "Python
Programming with Case studies". We, as mentors, have a great responsibility of fortifying our
students, making them capable by developing skills in them so that they are successful in their
careers.
This skill oriented book will help teachers to be at par with the new methodology & technology -
and this in turn will help them guide students and develop skills in them. The book keeps abreast of
the latest technology, demands and needs of the industry and uses a detailed, step-by-step
approach with the help of Information, Education and Communication (IEC) tools to enhance the
skills of reader’s.
The Book is a valuable addition untertaken by the Statistics Department to build good analysts and
to empower their learning. In today’s times, technological tools must be utilized to create audio-
visual clips for quicker dissemination of knowledge through digital platforms!
iii
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
Foreword
The novelty of this book, “Analyzing and Visualizing Data using free
open-source software Python Programming with case studies”
emerges out of its context and from within its content. In contextual
terms, Python is very popular and cutting-edge programming
language which is widely used in big data analysis. This language is
very user friendly and has wide applications with Python libraries.
It can be easily integrated with other programming languages. The
substantial practical relevance of this book is underlined through its
purported attempt to introduce Python programming language in a
very simple way to the beginners as well as advance learners.
In terms of the contents, this book certainly merits a leading position as there is a wide
variety of applications of Python programming language with applications, and case studies
are covered. The innovative qualities evidenced in various chapters are excellent and to my
mind, this is demonstrated originality of a high order. This edited book consists of 16
chapters and 4 case studies. The first chapter introduces Python programming. Optimum
strategies in Python are covered in the next couple of chapters. Subsequent chapters
discuss graphical representation, descriptive statistics, correlation and regression, and
probability distribution, which are covered in the introductory statistics course. Advanced
statistical programming techniques using Python are discussed in the next few chapters,
which include ANOVA, ANCOVA, Factor Analysis, Cluster Analysis, and Non-parametric test.
The unique part of the book is the four case studies on Polio, Air pollution, Diabetes, and
analysing happiness development index using Python. The book offers a unique insight into
the linking of innovative ideas very much on the academic plane to the potential
applications, as discussed in the case studies. Last but not the least, I will like to
congratulate the Editor, Dr Asha Jindal for tackling such a refractory and complex area of
Python programming with keen insight, modern tools, and fresh ideas to produce an
excellent book. It is my fervent hope that this book will act as a torch bearer not only for
the students of statistics but also for researchers in the area of big data.
Dr Kuldeep Kumar
PhD(Kent), FSS, [Link],
Professor, Bond Business School
Bond University, Gold Coast, Queensland 4229, AUSTRALIA
iv
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
Index
7 Descriptive Statistics
Mr. Shubham Niphadkar
…052
Assistant Professor, Department of Statistics, K. C. College
9 Probability Distribution
Mr. Sachin Sahmrao Bhaskar
Assistant Professor, Department of Statistics, Anandibai Raorane College. …079
Vaibhavwadi
10 Statistical Tests
Ms. Divya Poojari …096
Tata Consultacy Services, Statistical Programmer
v
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
12 ANCOVA Procedure
Mr. Shubham Niphadkar
…118
Assistant Professor, Department of Statistics, K. C. College
14 Factor Analysis
Mr. Prathamesh Thite
…156
Data Analyst, Dinero Software Pvt. Ltd.
15 Cluster Analysis
Mr. Abhay Deshpande
…165
Freelance Researcher
Case Studies
20 Analysis of Diabetes
Mr. Aditya Shrivastava and Mr. Hruturaj Nikam
…236
DBT- Star College Scheme Researchers, Department of Computer Science
vi
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
Chapter 1
Introduction to Python Programming
Python is a very simple yet very powerful programming language. Python is developed by
Guido van Rossum. Guido van Rossum started implementing Python in 1989.
1. Web framework like Django and Flask are based on Python. They help you write server
side code which helps you manage database, write backend programming logic, mapping
urls etc.
2. There are many machine learning applications written in Python. Face recognition and
Voice recognition in your phone is another example of machine learning.
3. Data analysis and data visualization in form of charts can also be developed using
Python.
4. Scripting is writing small programs to automate simple tasks such as sending automated
response emails etc.
5. You can develop games using Python.
1. Indentation is used in Python to delimit blocks. The number of spaces is variable, but all
statements within the same block must be indented the same amount.
1
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
2. The header line for compound statements, such as if, while, def, and class should be
terminated with a colon (:)
1.f 5>2:
print("True")
print("Yes Its True")
File "<[Link]-2-977590f094d0>", line 3
print("Yes Its True")
IndentationError: unindent does not match any outer indentation level
3. The semicolon (;) is optional at the end of statement.
4. Printing a statement to screen
print{"Hello”); print{"World”)
Hello
World
5. Reading keyboard input
Name= input(“what is Your Name: '")
what is Your Name: Pravesh
6. Comments
-Single Line
-Multiple Lines
# single Line coment ‘’’Multiple
Line
Coments’’’
1.3 Variables
Variables are containers for storing data values. A variable is created the moment you first
assign a value to it.
z=0.05 x=6 y=”storing value”
print(z) print(x) print(y)
0.05 6 storing value
Variables do not need to be declared with any particular type and can even change type after
they have been set. Variables can change type, simply by assigning them a new value of a
different type.
x = 85
print (“First value is:”, x)
x =”new value”
print(“changed value is:” + x)
First value is: 85
Changed value is: new value
String variables can be declared either by using single or double quotes.
x = "John“ is the same as x = 'John’
Python allows you to assign a single value to several variables simultaneously.
X = y = Z = 25
print(x,y,z)
25 25 25
2
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
Note:
A variable can have a short name (like x and y) or a more descriptive name (age, carname,
total_volume).
Data type defines the type of the variable, whether it is an integer variable, string variable,
tuple, dictionary, list etc. Python data types are divided in two categories, mutable data types
and immutable data types.
The data stored in memory can be of many types. For example, a person's age is stored as a
numeric value and his or her address is stored as alphanumeric characters. Python has
various standard data types that are used to define the operations possible on them and the
storage method for each of them.
Mathematical functions
Functions Description
abs(x) The absolute value of x: the (positive) distance between x and
zero.
exp(x) The exponential of x: ex
log(x) The natural logarithm of x, for x> 0
pow(x, y) The value of x**y.
round(x, n) x rounded to n digits from the decimal point.
max(x1, x2,...) The largest of its arguments: the value closest to positive infinity
min(x1, x2,...) The smallest of its arguments: the value closest to negative
infinity
sqrt(x) The square root of x for x > 0
4
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
Python accepts single ('), double (") and triple (''' or """) quotes to denote string literals.
example1 = ‘single quote’
example2 = “double quote”
example3 = ‘’’multiline
quote example’’’
A list contains items separated by commas and enclosed within square brackets.
List indexes like strings starting at 0 in the beginning of the list and working their way from
-1 at the end.
Similar to strings, Lists operations include slicing ([ ] and [:]) , concatenation (+), repetition
(*), and membership (in).
Access Items
In (7): temp1ist = ["Cherry","88",98,"coin",88.568]
print ("First 3 element:" ,temp1ist[0])
print ("Last element:” ,temp1ist[-1])
First element: Cherry
Last element: 88.568
Range of Indexes
In [8]: templist = ["Cherry", "88", 98, "coin", 88.568]
print ("First 3 elements : ", templist[0:3])
First elements: ["Cherry", ‘88’, 98]
5
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
Update List
In [14]: print(“Original list:”, templist)
templist[0]=”Guava”
print(“changed list:”, templist)
Original list: [‘Guava’, ‘88’, 98,’coin’, 88.568]
Changed list: [‘Guava’,’88’, 98,’coin’, 88.568]
A tuple contains items separated by commas and enclosed in parentheses instead of square
brackets. You can update an existing tuple by (re)assigning a variable to another tuple.
The rules for tuple indices are the same as for lists and they have the same operations,
functions as well. To write a tuple containing a single value, you have to include a comma,
even though there is only one value. For e.g. tup1 = (50, );
6
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
Creating Dictionary
In [8]: thisdict = {
“brand”: “Ford”
”model1”:”Mustang”,
”year”:1964
}
print(thisdict)
(‘brand’:’Ford’, ’model’: ‘mustang’,’year’: 1964)
Slicing Dictionary
In [10]: print(“Brand name: ”, thisdict[“brand”])
(‘Brand name:’, ‘Ford’)
Updating Dictionary
In [14]: print(thisdict)
thisdict["brand"] ="Mercedes"; #update existing entry
print(thisdict)
{‘brand’: ‘Ford’, ‘model’: ‘Mustang’, "year’: 1964}
{‘brand’: ‘Mercedes’, ‘model’: ‘Mustang’, ‘year’: 1964}
7
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
Operators are the constructs which can manipulate the value of operands. Consider the
expression 4 + 5 = 9. Here, 4 and 5 are called operands and + is called operator.
8
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
9
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
//= FloorIt performs floor division on operators and assign c //= a is equivalent to c
Division value to the left operand = c // a
Bitwise operator works on bits and performs bit by bit operation. Assume if a = 60 and b =
13. Now in binary format they will be as follows:
a = 0011 1100 and b = 0000 1101
>> Binary The left operands value is moved a >> 2 = 15 (means 0000 1111)
Right Shift right by the number of bits specified
by the right operand.
In general, statements are executed sequentially: The first statement in a function is executed
first, followed by the second, and so on. There may be a situation when you need to execute
a block of code several number of times. Programming languages provide various control
structures that allow for more complicated execution paths. A loop statement allows us to
execute a statement or group of statements multiple times.
11
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
statement_2 else:
print(num"is not a positive number")
(-1,'is a positive number')
Syntax Example
if test expression: num= -5
statement_1 if num> 0:
elif: print(num, "is a positive number.")
statement_2 elif num> 0:
else: print(num, "is a negative number.")
statement_3 else:
print("number is zero.")
Here, val is the variable that takes the value of the item inside the sequence on each iteration.
Loop continues until we reach the last item in the sequence. The body of for loop is separated
from the rest of the code using indentation.
Examples
In [6]: Names = ["Vinay", "Pramod", "Richa", "Kanak"]
for i in Names:
print(i)
Vinay
Pramod
Richa
Kanak
12
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
In Python programming, pass is a null statement. The difference between a comment and
pass statement in Python is that, while the interpreter ignores a comment entirely, pass is
not ignored.
In [7]: for num in range(1,6):
If num==3:
pass
else:
print(“Num = {}” .format(num))
Num =1
Num =2
Num =4
Num =5
1.7 References
1. [Link]
2. [Link]
3. [Link]
13
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
Chapter 2
Functions, Modules, Advanced Operations and
Pandas in Python
A function is a block of organized, reusable code that is used to perform a single, related
action. Functions provide better modularity for your application and a high degree of code
reusing.
Defining a Function:
1. Keyword def marks the start of function header.
2. Parameters (arguments) through which we pass values to a function. They are optional.
3. A colon (:) to mark the end of function header.
4. Optional documentation string (docstring) to describe what the function does.
5. One or more valid python statements that make up the function body. Statements must
have same indentation level.
6. An optional return statement to return a value from the function.
In [9]: def printme(name):
“this prints a passed name with string into this function”
print(“Hi”+ name + “, welcome in the class”)
In [10]: print(“Sumit”)
Hi Sumit, welcome in the class
You can send any data types of parameter to a function (string, number, list, dictionary etc.),
and it will be treated as the same data type inside the function.
14
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
A module is a file consisting of Python code. A module can define functions, classes and
variables. A module can also include run-able code. We use modules to break down large
programs into small manageable and organized files. Furthermore, modules provide
reusability of code.
We can define our most used functions in a module and import it, instead of copying their
definitions into different programs.
Python modules can be imported using import statement. For e.g. import pandas.
Python's from statement lets you import specific attributes from a module into the current
namespace.
from modname import name1[, name2[, ... nameN]]
It is also possible to import all names from a module into the current namespace by using the
following import statement.
from modname import *
A module can be installed using pip command for e.g. pip install pandas
def add(x,y): import os
return(x+y) [Link]
def subtract(x, y): import calculation
return(x-y) #to Rename
from calculation import calc
16
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
[‘mean’,’variance’,25,0,1,2,’67’]
my_list=['p','r','o','b','l','e','m'] my_list=['p','r','o','b','l','e','m']
del my_list[2] del my_list[1:5]
my_list my_list
['p','r','b','l','e','m'] ['p','e','m']
We can use remove() method to remove the given item or pop() method to remove an item
at the given index and clear() to completely remove all elements.
my_list=['p','r','o','b','l','e','m'] my_list=['p','r','o','b','l','e','m']
my_list.remove('p') my_list.pop(1)
my_list my_list
['r','o','b','l','e','m'] ['p','o','b','l','e','m']
my_list=['p','r','o','b','l','e','m'] my_list=['p','r','o','b','l','e','m']
my_list[2:4]=[] my_list.clear()
my_list my_list
['p','r','l','e','m'] []
Other built in functions for list:
count() sort()
vowels=['a','e','i','o','u'] vowels=['e','a','u','o','i']
[Link]('i') [Link]()
2 vowels
['a','e','i','o','u']
reverse()
months = ["January", "February", "March","April","May"]
[Link]()
months
["May","April","March","February","January"]
del keyword to remove individual items or the entire dictionary itself. All the items can be
removed at once using the clear() method.
alphabets= {"a":11, "b":24, "c":32, "d":42, "e":16}
del alphabets["a"]
alphabets
{'b': 24, 'c': 32, 'd': 42, 'e': 16}
"model": "Mustang",
"year": 1964
}
[Link]({"color": "White"})
car
{'brand': 'Ford', 'model': 'Mustang', 'year': 1964, 'color': 'White'}
In [30]: thisdict = {
"Name": "Kartik",
"College": "[Link]",
"Qualification": "[Link]"
}
for x in thisdict:
print(thisdict[x])
Kartik
[Link]
[Link]
List
In [31]: for fruit in ["apple","banana","mango"]:
print(fruit)
apple
banana
19
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
mango
Slicing of String
In [4]: string= "Hello world" In [5]: strinc = "Hello world"
print(string[0]) print(strinc[25])
print(string(-1]) Traceback (most recent call last):
print(string[2:7]) File"<ipython-input-5-cd88bcd3b787>"
H ,line 2,in <module>
d print(string[25])
llo w IndexError: string index out of range
String Concatenation
a= "Hello" a= "Hello"
b = "World" b = "World"
a + b a + " " + b
"HelloWorld" 'Hello World'
profession= "Developer"
txt= "My name is John, I am a" + profession
txt
"My name is John, I am a Developer"
The format() method takes the passed arguments, formats them, and pass them inside the
placeholders {}.
In [10]: profession = “Analyst”
txt = “My name is John, I am a {}”
print([Link](profession))
My name is John, I am a Analyst
To check if a certain phrase or character is present in a string, we can use the keywords in or
not in.
txt = “The rain in Spain stays mainly in the plain”
x=”ain” in txt
x
True
20
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
[Link]("#") [Link]()
' Goa Beaches ' 'strings'
replace() upper()
txt= “ #GOA #Beaches #BestTemples txt = "strings"
#AwesomeSeafood " [Link]()
newtxt = [Link]("#","") 'STRINGS'
newtxt
'GOA Beaches BestTemples AwesomeSeafood’
split()
txt = "Mean|Median|Mode|Geometric mean|Arithmetic mean"
newtxt = txt.sp1it("|")
newtxt
['Mean', 'Median', 'Mode', 'Geometric mean', 'Arithmetic mean']
count()
txt = "I love apples, apple are my favorite fruit"
x = [Link]("apple")
x
2
join() isdigit()
mytuple = ("John","Peter","Vicky") txt = "50800"
x = "_".join(mytuple) x = [Link]()
x x
'John_Peter_Vicky' True
partition()
In [23]: txt = "I could eat Oranges all day"
x= [Link]("Oranges")
print(x)
('I could eat',''Oranges','all day')
21
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
22
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
23
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
Join / Concatenation
Pandas provide various facilities for easily combining together Series or DataFrame with
various kinds of set logic for the indexes and relational algebra functionality in the case of
join / merge-type operations. Here are the different types of the Joins:
Inner Join: Returns records that have matching values in both tables
Left Join: Returns all records from the left table, and the matched records from the right table
Right Join: Returns all records from the right table, and the matched records from the left
table
24
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
Full Join: Returns all records when there is a match in either left or right table
25
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
26
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
2.6 References
1. [Link]
2. [Link]
3. [Link]
27
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
Chapter 3
Optimisation Strategy in Python- I
(Linear Programming Problem and Integer
Programming Problem using pulp code )
3.1 Introduction
Operations Research (O.R.) is the discipline of applying advanced analytical methods to help
to make sound and effective decisions. The particular area of operations research where pulp
is useful for the development and modelling of Linear Programming (LP) and Integer
Programming (IP) problems. Mathematically, an LP problem is a point in an n-dimensional
linearly constrained region that maximises a given linear objective function. Integer
Programming is an LP where the solution must contain discrete variables which take an
integer value at the solution.
Solution
To carry out this Analysis one has to install pulp library and steps are as follows:
1. Go to search in start button and type Anaconda Prompt which will open the window.
2. Type pip install pulp and press Enter key
3. Installation process will begin and it will complete in approximately 2 minutes depends
on speed of Internet.
28
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
In [18]: [Link]()
[Link](x),[Link](y),[Link]([Link])
Out [18]:
(33.333333, 33.333333, 1666.6666500000001)
Note: Decisions Variables are not integers. So, LPP is solved in the form of IPP by adding
category argument in defining variables.
In [19]: import pulp
#Create Lp Maximization Problem
prob=[Link]('maximizeProfit',[Link])
#create a variable x>=0
x=[Link]("x",lowBound=0, cat=('Integer'))
#create another variable y>=0
y=[Link]("y",lowBound=0, cat=('Integer'))
prob+=20*x+30*y #objective function
prob+=1*x+2*y<=100 #Constraint 1
prob+=2*x+1*y<=100 #Constraint 2
prob
Out [19]:maximizeProfit:
MAXIMIZE
20*x + 30*y + 0
SUBJECT TO
_c1: x + 2y <= 100
_c2: 2x + y<= 100
VARIABLES
0 <= x Integer
29
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
0 <= y Integer
In [20]: [Link]()
[Link](x),[Link](y),[Link]([Link])
Out [20]:
(32.0,34.0,1660.0)
3.2 References
1. Python Tuto[Link]
2. [Link]
3. [Link]
30
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
Chapter 4
Optimisation Strategy in Python-II
(Dynamic Programming)
Principal Dr. C. S. Kakade, Anandibai Raorane Arts, Commerce & Science College,
Vaibhavwadi
4.1 Introduction
Python is one of the most popular and fastest growing languages nowadays. It is probably
the best alternative for C++ and Java. It serves you with the requisite speed, leverages easy
readability and also ensures shorter codes. Developers from the remotest corners of the
world are embracing the presence of Python.
Python is currently the most widely used multi-purpose, high-level programming language.
It allows programming in Object-Oriented and Procedural paradigms. The programs
generally are smaller than other programming languages like Java. Programmers have to
type relatively less and indentation requirement of the language, makes them readable all
the time. Python language is being used by almost all tech-giant companies like – Google,
Amazon, Facebook, Instagram, Dropbox, Uber etc.
Dynamic programming is a method for solving a complex problem by breaking it down into
a collection of simpler subproblems, solving each of those subproblems just once, and
storing their solutions using a memory-based data structure (array, map, etc). Each of the
subproblem solutions is indexed in some way, typically based on the values of its input
parameters, so as to facilitate its lookup. So, the next time the same subproblem occurs,
instead of recomputing its solution, one simply looks up the previously computed solution,
31
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
thereby saving computation time. We shall be deep diving into python programming with
the help of an interesting case study.
Consider a thief gets into a home to rob and he carries a knapsack. There are fixed number
of items in the home, each with its own weight and value. E.g.: Jewellery, with less weight
and highest value v/s tables, with less value but a lot heavy. To add fuel to the fire, the thief
has an old knapsack which has limited capacity. Obviously, he can’t split the table into half
or jewellery into 3/4ths. He either takes it or leaves it. Determine the items which the thief
needs to steal so that the final contents in the knapsack have maximum value.
4.4 Approach
32
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
represents the maximum value obtainable with all the items and the full capacity of the
knapsack.
Below is the source code of a Python program to solve the 0/1 knapsack problem using
dynamic programming with memorization.
w = W
for i in range(n, 0, -1):
if res <= 0:
break
# either the result comes from the
# top (K[i-1][w]) or from (val[i-1]
# + K[i-1] [w-wt[i-1]]) as in Knapsack
# table. If it comes from the latter
# one/ it means the item is included.
if res == K[i - 1][w]:
continue
else:
# Driver code
val = [4, 2, 1, 10, 2]
wt = [12, 2, 1, 4, 1]
W = 15
33
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
n = len(val)
Solution
The maximum value that the knapsack can contain is $15 with item 2, item 3, item 4 and item
5 having weights 2kg, 1kg, 4kg and 1kg respectively. Thus, the thief can steal four items and
obtain maximum value of $15. Analyzing the complexity of the solution is pretty straight-
forward. We just have a loop for W within a loop of n => O (nW).
4.5 References
1. [Link]
2. [Link]
3. [Link]
4. [Link]
34
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
Chapter 5
Graphs and Diagrams (2D and 3D using
Python)
In this chapter we are going to learn how to create simple 2D and 3D plots using Python. For
plotting in python we use pyplot module which is stored in Matplotlib package. Also we
need an array of numbers to plot which we import as plt alias. Various array functions are
defined in the NumPy library which is imported with the np alias.
[Link] contains command style functions which make Matplotlib work like
MATLAB. Each pyplot function makes some change to a figure.
Following table shows the pyplot function for specific types of plots:
bar Bar plot
barh Horizontal bar plot
boxplot Box plot/ Whisker plot
hist Histogram
pie Pie chart
plot Lines and markers to the
axes
scatter Scatter plot
stackplot Stacked area plot
stem Stem plot
step Step plot
quiver 2D field of arrows
35
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
Also when we create the different plots, instead of line we can use various symbols and
different colours to the plot.
36
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
Another example of plot function that displays the graph of the function 𝑦 = 𝑥 2 on the
domain [-2,2]. Default plot colour is Blue..
from matplotlib import pyplot as plt
import numpy as np
import math
x=[Link](-2, 2, 0.05)
y=pow(x,2)
[Link](x,y)
[Link]("x")
[Link]("x^2")
[Link](“y=x^2”)
[Link]()
Plot symbols are available in * library which is available in a module called pylab which is a
procedural interface for matplotlib. So in the following illustration, we plot dotted curve with
red dots. The format ‘r.’ in the plot statement specified represents red coloured dotted plot.
from matplotlib import pyplot as plt
import numpy as np
import math
x=[Link](-2, 2, 0.05)
y=pow(x,2)
[Link]("x")
[Link]("x^2")
[Link](“y=x^2”)
plot(x,y,'r.')
37
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
[Link]()
Multiple plot commands can be used in a single program to plot multiple plots together.
Following example shows graphs of sin 𝑥 and cos 𝑥 on the domain [−𝜋, 𝜋] . Here ‘r-’
represents red continuous line and ‘g--’ represents green dashed line.
Following example shows the line plots. Line representing y1 is a solid line with blue color
and square markers whereas y2 line is a dashed line with green colour and circle marker.
The add_axes()method is used to add axes to the figure. It requires a list of 4 values
corresponding to left, bottom, width and height of the figure. Each value must be between 0
and 1.
[Link], ax.set_title,ax.set_xlabel, ax.set_ylabel are used to add plot legends, plot title, x axis
label and y axis label respectively.
38
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
A Bar chart is a chart or graph that presents categorical data with rectangular bars with
heights proportional to the values that they represent.
bar(x, height[, width, bottom, align])function is used to plot bar diagram, where x represents
category, height represents height of the bar, other three parameters are optional. width
represents width of the bar (default width is 0.8), bottom represents y coordinates of the bar
(by default its none), align represents alignment which can be either center or edge (by
default its center).
In the following example number of students in the divisions A, B, C and D are presented
using bar chart.
import [Link] as plt
fig=[Link]()
ax=fig.add_axes([0,0,1,1])
divs=['A', 'B', 'c', 'D']
students=[23,17,35,29]
[Link](divs,students)
[Link]()
39
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
Multiple bar charts can be plotted adjusting the thickness and the positions of the bars. In
the following example, the data variable contains three series of three values.
The following script will show three bar charts of three bars. The bars will have a thickness
of 0.25 units. Each bar chart will be shifted 0.25 units from the previous one. The data
contains number of students passed in three subjects (Maths, Stats and Physics) from year
2017 to 2019.
import numpy as np
import [Link] as plt
data=[[30, 25, 50],
[ 40, 23, 51],
[35, 22, 45]]
X = [Link](3)
fig=[Link]()
ax=fig.add_axes([0,0,1,1])
[Link](X + 0.00, data[0], color= 'b', width 0.25)
[Link](X + 0.25, data[1], color='y', width 0.25)
[Link](X + 0.50, data[2], color= 'r', width 0.25)
ax.set_xticks([0.25,1.25,2.25])
ax.set_xticklabels([2017,2018,2019])
[Link](labels=['Maths', 'Stats', 'Physics'])
[Link]()
40
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
This chart represents different groups on top of each other. The height of the resulting bar
shows the combined result of the groups.
The optional bottom parameter of the [Link]() function allows us to specify a starting
value for a bar. So in the following example, in which marks scored by 5 students in maths
and stats are represented by stacked bar plot, where first [Link]() plots the red bars
representing maths marks. The second [Link]() plots the blue bars representing stats
marks, with the bottom as the top of red bars. Data consists of
Maths= (32, 35, 30, 35, 27); Stats= (25, 32, 35, 30, 25)
import numpy as np
import [Link] as plt
N = 5
Maths= (32, 35, 30, 35, 27)
Stats= (25, 32, 35, 30, 25)
ind= [Link](N) # the x Locations far the groups
width= 0.35
fig=[Link]()
ax=fig.add_axes([0,0,1,1])
[Link](ind, Maths, width, color='r')
[Link](ind, Stats, width,bottom=Maths, color='b')
ax.set_ylabel('Marks')
ax.set_title('Marks in Maths and Stats')
ax.set_xticks(ind,('A', 'B', 'C', ·o·, 'E'))
ax.set_yticks([Link](0, 90, 10))
[Link](labels=['Maths', 'Stats'])
[Link]()
41
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
5.6 Histogram
The command hist() used in the following program plots the histogram for the data given
in array ‘a’ with the class intervals -0-25, 25-50, 50-75 and 75-100. edgecolor and
linewidth parameters are optional which set border-colour of histogram as black and
width of border as 1.2 points.
from matplotlib import pyplot as plt
import numpy as np
fig,ax=[Link](1, 1)
a = [Link][ (22,87,5,43,56,73,55,54,11,20,51,5,79,31,27])
[Link](a, bins = [0,25,50,75,100],edgecolor='black', linewidth=l.2)
ax.set_title("histogram of result")
ax.set_xticks([0, 25, 50, 75, 100])
ax.set_xlabel('Marks')
ax.set_ylabel('No. of students')
[Link]()
42
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
It is a type of graph in which a circle is divided into sectors that each represent a proportion
of the whole.
To draw pie chart, pie() function is used. Colors in the following example are basic colors.
One can use an additional argument colors to specify slice colors. One can also set the
distance between graph and labels, slice distance by using optional arguments labeldistance
and slice distance. So the following code plots a pie chart that displays percentage of number
of students who have secured the grades O, A, B, C and D.
The observations (x,y) that are plotted using Cartesian coordinates is called as a Scatter plot.
To draw scatter plot, scatter() function used with the arguments as values of x array, y array.
An optional argument color is used the the following example to set the point color as blue.
Marks of Mathematics are considered as x array values and Marks of Statistics are
considered as y array values.
import [Link] as plt
Mathematics= [89, 90, 70, 89, 80, 80, 90, 100, 80, 34]
Statistics= [30, 29, 49, 48, 70, 48, 38, 45, 20, 30]
fig=[Link]()
ax=fig.add_axes([0,0,1,1])
43
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
For 3D plotting we import mplot3d module from mplot3d toolkit. axes() function has an
argument as projection=’3d’ which enables 3 dimensional setup for plotting. plot3d()
function is used with three parameters as x, y and z coordinates. One can use additional
parameter for pattern and color of the line. In the following example, (sin 15z, cos 15z, z) are
plotted where z varies from 0 to 10.
44
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
Function scatter() is used to draw the scatter plot. arguments are x, yand z coordinate values.
In the following example, points (sin 15z, cos 15z, z) are plotted where z varies from 0 to 10.
from mpl_toolkits import mplot3d
import numpy as np
import [Link] as plt
fig= [Link]()
ax= [Link](projection='3d')
z = [Link](0, 1, 50)
x = z * [Link](15 * z)
y = z * [Link](15 * z)
[Link](x, y, z, 'g')
ax.set_title('3D Scatter plot')
[Link]()
To draw contour plot, we use function contour3d(). The input required for this function is 2
dimensional grid. So X,Y in the following example defines grid. cmap arguments represents
color mapping. Binary stands for black and white color mapping. Contour plot of 𝑧 = 𝑥 2 +
𝑦 2 is plotted using the following code.
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
ax.set_title('3D contour plot: z=x^2+y^2')
[Link]()
To plot surface plot, we use plot_surface() function with arguments : x, y, z, cmap. edgecolor
is an optional argument. Following example plots the surface 𝑧 = sin 𝑥. cos 𝑦. Here colour
theme viridis is used. There are many other themes like ‘BrBG’, ‘twilight_shifted’, ‘jet’ etc. No
edgecolor is set.
from mpl_toolkits import mplot3d
import numpy as np
import math
import [Link] as plt
x = [Link]([Link](0, [Link]*2, 30), [Link](30))
y = [Link]().T # transpose
z = [Link](x)*[Link](y)
fig = [Link]()
ax= [Link](projection='3d')
ax.plot_surface(x, y, z,cmap='viridis', edgecolor='none')
ax.set_title('Surface plot')
[Link]()
import math
import [Link] as plt
x = [Link]([Link](-2, 2, 30), [Link](30))
y = [Link]().T # transpose
z = x**2+y**2
fig= [Link]()
ax= [Link](projection='3d')
ax.plot_surface(x, y, z,cmap='viridis', edgecolor='c')
ax.set_title('Surface plot')
[Link]()
5.13 References
47
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
Chapter 6
Factorization of a Polynomial over a Finite
Field
6.1 Introduction
Finding roots of polynomials or “solving algebraic equations” was the old problem in
mathematics. With the developments in abstract algebra, innovative techniques have
developed for finding roots of polynomials over a field. Factorizing large integers is a hard
problem. In fact, some techniques in cryptography are based on this fact. However, there is
a beautiful algorithm for factorization of a polynomial over a finite field. This Berlekamp’s
algorithm is based on some basic techniques in linear algebra and field theory. We will start
with some necessary results in finite fields.
A finite field ℱ has characteristic 𝑝 for some prime 𝑝, and hence it is a finite dimensional
vector space over ℱ𝑝 ( a finite field with 𝑝 elements, isomorphic to ℤ⁄𝑝ℤ). If the dimension is
𝑛, then ℱ has precisely 𝑝𝑛 elements. We will denote this field by ℱ𝑞 where 𝑞 = 𝑝𝑛 .
A polynomial 𝑓(𝑥) over a field ℱ has a multiple zero in some extension of ℱ if and only if
𝑓(𝑥) and 𝑓′(𝑥) have a common factor of positive degree in ℱ[𝑥] where 𝑓′(𝑥) denotes the
formal derivative of 𝑓(𝑥). We will use this result to make 𝑓(𝑥) square-free as follows:
1. Step 1: Compute 𝑔(𝑥) = gcd(𝑓(𝑥), 𝑓 ′ (𝑥))
2. Step 2: If 𝑔(𝑥) has degree zero then 𝑓(𝑥) is square free and ready for Berlekamp.
3. Step 3: If 𝑓′(𝑥) = 0, then 𝑓(𝑥) is a perfect 𝑝𝑡ℎ power, where 𝑝 is the characteristic of ℱ𝑞 .
4. Else if deg (𝑔(𝑥)) ≥ 1 and 𝑓′(𝑥) ≠ 0 then recursively apply square-free procedure to 𝑔
and 𝑓⁄𝑔.
48
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
Now given polynomial is square free and ready for Berlekamp algorithm.
Let 𝐴𝑓 = ℱ𝑞 [𝑥]/ < 𝑓 >. This is a vector space over ℱ𝑞 with dimension 𝑛.
Let 𝐵𝑓 = {ℎ ∈ 𝐴𝑓 ∶ ℎ𝑞 ≡ ℎ (𝑚𝑜𝑑 𝑓)}
Define 𝑄𝑓 ∶ 𝐴𝑓 → 𝐴𝑓 by 𝑎(𝑥) ↦ 𝑎(𝑥)𝑞 for each 𝑎(𝑥) in 𝐴𝑓 .
Since ℱ𝑞 is a field of characteristic 𝑝 and 𝑞 = 𝑝𝑛 , (𝑎(𝑥) + 𝑏(𝑥))𝑞 = (𝑎(𝑥))𝑞 + (𝑏(𝑥))𝑞 and
hence 𝑄𝑓 is a linear transformation over 𝐴𝑓 .
Claim: 𝑩𝒇 = 𝒌𝒆𝒓 (𝑸𝒇 − 𝑰)
𝑞
If 𝑎(𝑥) ∈ 𝐵𝑓 , then (𝑄𝑓 − 𝐼)(𝑎(𝑥)) = 𝑄𝑓 (𝑎(𝑥)) − 𝐼(𝑎(𝑥)) = (𝑎(𝑥)) − 𝑎(𝑥) ≡ 0 (𝑚𝑜𝑑 𝑓)
Hence 𝐵𝑓 ⊆ ker(𝑄𝑓 − 𝐼).
Conversely if 𝑏(𝑥) ∈ ker (𝑄𝑓 − 𝐼), then (𝑄𝑓 − 𝐼)𝑏(𝑥) ≡ 0 (𝑚𝑜𝑑 𝑓) and hence
(𝑏(𝑥))𝑞 ≡ 𝑏(𝑥) (𝑚𝑜𝑑 𝑓) i.e. 𝑏(𝑥) ∈ 𝐵𝑓 . Hence the claim.
By Chinese reminder theorem (CRT),
𝐴𝑓 ≅ ℱ𝑞 [𝑥]/< 𝑒1 > × ℱ𝑞 [𝑥]/< 𝑒2 >× ⋯ × ℱ𝑞 [𝑥]/< 𝑒𝑘 >
Let {1, 𝑥, 𝑥 2 , ⋯ , 𝑥 𝑛−1 } be a standard basis for 𝐴𝑓 . Let 𝑀 be a matrix of 𝑄𝑓 𝑤. 𝑟. 𝑡. this standard
basis.
We can compute basis of 𝐵𝑓 = ker (𝑄𝑓 − 𝐼) by applying row reduction on 𝑀 − 𝐼.
Claim: Nullity of 𝑸𝒇 − 𝑰 is the number of irreducible factors of 𝒇(𝒙).
By hypothesis, 𝑓(𝑥) = 𝑒1 𝑒2 ⋯ 𝑒𝑘
By CRT, 𝐴𝑓 ≅ ℱ𝑞 [𝑥]/< 𝑒1 > × ℱ𝑞 [𝑥]/< 𝑒2 >× ⋯ × ℱ𝑞 [𝑥]/< 𝑒𝑘 >
Hence for each ℎ ∈ 𝐵𝑓 , CRT maps sends ℎ to (𝑟1, 𝑟2 , ⋯ , 𝑟𝑘 )
Since ℎ ∈ 𝐵𝑓 , 0 ≡ ℎ𝑞 − ℎ ≡ (𝑟1𝑞 − 𝑟1 , 𝑟2𝑞 − 𝑟2 , ⋯ , 𝑟𝑘𝑞 − 𝑟𝑘 )
Therefore, 𝑟𝑖𝑞 = 𝑟𝑖 , for each 𝑖. So, 𝑟𝑖 ∈ ℱ𝑞 (Again by a result in finite field: ℱ𝑞 is a spitting field
of a polynomial 𝑥 𝑞 − 𝑥).
Hence, 𝐵𝑓 = ker(𝑄𝑓 − 𝐼) ≅ ℱ𝑞 𝑘 . i.e. dim(𝐵𝑓 ) = 𝑘.
Therefore, Nullity of 𝑄𝑓 − 𝐼 is the number of irreducible factors of 𝑓(𝑥).
That means, now we can find number 𝑘 by row reduction on 𝑀 − 𝐼. It remains to find the
actual irreducible factors.
For each ℎ ∈ 𝐵𝑓 , 𝑓(𝑥) = ∏𝑐∈ℱ𝑞[gcd(𝑓, ℎ − 𝑐)]. (Note that, LHS divides RHS and vice versa)
Hence for each ℎ ∈ 𝐵𝑓 with deg ℎ > 0, there exists 𝑐 ∈ ℱ𝑞 such that 𝑔 = gcd (𝑓, ℎ − 𝑐) ≠ 1.
Such 𝑔 is a non-trivial factor of 𝑓.
49
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
Now write the matrix 𝑀 and reduce (𝑀 − 𝐼) to row echelon form with basis of the kernel
(1,1,1,0,0) and (0,0,0,0,1)
Hence ℎ1 is 11100 and ℎ2 is 00001
gcd (𝑓, ℎ1 ) is 111 and gcd (𝑓, ℎ1 + 1) is 1011
Hence required factorization is 𝑥 5 + 𝑥 4 + 1 = (𝑥 2 + 𝑥 + 1)(𝑥 3 + 𝑥 + 1) over ℤ/2ℤ.
[Note that 110001 = 111 ∙ 1011]
Now the Python program to find such factorization will be as follows:
import fractions
p=[Link]([1,0,1,1,1])
p1=[Link]([1])
p2=[Link]([1,O,O])
p3=[Link]([1,0,0,0,0])
p4=[Link]([1,0,0,0,0,0,0])
al=[Link]([Link](p1,p)[1])[::-1].tolist()
a2=[Link]([Link](p2,p)[1])[::-1].tolist()
a3=[Link]([Link](p3.p)[1])[::-1].tolist()
a4=[Link]([Link](p4.p)[1])[::-1].tolist()
50
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
[Link](0)
for i in range (len(a4)-len(a2)):
[Link](0)
for i in range (len(a4)-len(a3)):
[Link](0)
for i in range (len(a4)-len(a4)):
[Link](0)
x=[Link]([a1,a2,a3,a4])
y=[Link]([Link]([[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]]))
print (y)
r=matrix_rank(y)
print(r)
6.6 References
51
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
Chapter 7
Descriptive Statistics
7.1 Introduction
Descriptive Statistics deals with description and understanding of data and its features with
the help of short summaries about the sample and measures of the data. The basic two types
of descriptive statistics are:
➢ Measures of central tendency
➢ Measures of dispersion
Central representative value of the entire data is referred to as measure of central tendency.
The various measures of central tendency are as follows:
1. Mathematical Averages:
a) Arithmetic mean
b) Geometric mean
c) Harmonic mean
2. Positional Averages:
a) Partition Values
➢ Median
➢ Quartiles
➢ Deciles
➢ Percentiles
b) Mode
Positional Averages
52
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
Mode: The value which occur maximum number of times is the mode.
Mathematical Averages
Arithmetic Mean (A.M.) Geometric Mean (G.M.) Harmonic Mean (H.M.)
∑𝑛𝑖=1 𝑓𝑖 𝑥𝑖 𝑛 1/𝑁 𝑁
𝐴. 𝑀. = 𝑛 𝐻. 𝑀. =
∑𝑖=1 𝑓𝑖 𝐺. 𝑀. = (∏ 𝑥𝑖 𝑓𝑖 ) 𝑓
∑𝑛𝑖=1 𝑖
𝑖=1 𝑥𝑖
Positional Averages
Median
𝑁+1 𝑡ℎ
If 𝑁 is odd, 𝑀𝑒𝑑𝑖𝑎𝑛 = 𝑉𝑎𝑙𝑢𝑒 𝑜𝑓 ( ) 𝑜𝑏𝑠𝑒𝑟𝑣𝑎𝑡𝑖𝑜𝑛
2
𝑁 𝑡ℎ 𝑁 𝑡ℎ
(𝑉𝑎𝑙𝑢𝑒 𝑜𝑓 ( ) 𝑜𝑏𝑠𝑒𝑟𝑣𝑎𝑡𝑖𝑜𝑛+𝑉𝑎𝑙𝑢𝑒 𝑜𝑓 ( +1) 𝑜𝑏𝑠𝑒𝑟𝑣𝑎𝑡𝑖𝑜𝑛)
2 2
If 𝑁 is even, 𝑀𝑒𝑑𝑖𝑎𝑛 = 2
Quartiles Deciles Percentiles
𝑡ℎ 𝑡ℎ
𝑁 𝑁 𝑁 𝑡ℎ
𝑉𝑎𝑙𝑢𝑒 𝑜𝑓 𝑖 ( ) 𝑜𝑏𝑠𝑒𝑟𝑣𝑎𝑡𝑖𝑜𝑛, 𝑉𝑎𝑙𝑢𝑒 𝑜𝑓 𝑖 ( ) 𝑜𝑏𝑠𝑒𝑟𝑣𝑎𝑡𝑖𝑜𝑛, 𝑉𝑎𝑙𝑢𝑒 𝑜𝑓 𝑖 ( ) 𝑜𝑏𝑠𝑒𝑟𝑣𝑎𝑡𝑖𝑜𝑛,
4 10 100
𝑖 = 1, 2, 3 𝑖 = 1, 2, … , 9 𝑖 = 1, 2, … , 99
Mathematical Averages
Arithmetic Mean (A.M.) Geometric Mean (G.M.) Harmonic Mean (H.M.)
53
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
∑𝑛𝑖=1 𝑓𝑖 𝑥𝑖 𝑛 1/𝑁 𝑁
𝐴. 𝑀. = 𝑓𝑖 𝐻. 𝑀. =
∑𝑛𝑖=1 𝑓𝑖 𝐺. 𝑀. = (∏ 𝑥𝑖 ) 𝑓𝑖
∑𝑛𝑖=1
𝑖=1 𝑥𝑖
Positional Averages
For calculating median, quartiles, deciles, percentiles and mode, we will be dealing with,
median class, 𝑄𝑖 class, 𝐷𝑖 class, 𝑃𝑖 class and modal class respectively.
𝑁
Median: 𝑀𝑒𝑑𝑖𝑎𝑛 = 𝑙1 + (𝑙2 − 𝑙1 ) ( 2 − 𝑐𝑓) /𝑓
Quartiles Deciles Percentiles
𝑖𝑁 𝑖𝑁 𝑖𝑁
𝑙1 + (𝑙2 − 𝑙1 ) ( − 𝑐𝑓) /𝑓, 𝑙1 + (𝑙2 − 𝑙1 ) ( − 𝑐𝑓)⁄𝑓 𝑙1 + (𝑙2 − 𝑙1 ) ( − 𝑐𝑓)⁄𝑓
4 10 100
𝑖 = 1, 2, 3 𝑖 = 1, 2, … , 9 𝑖 = 1, 2, … , 99
The representative value of the data which is used to represent the spread or scatterness of
data is referred to as measure of dispersion. The various measures of dispersion are as
follow:
1. Range
2. Quartile Deviation
3. Mean Deviation about ‘a’
4. Variance
5. Standard Deviation
6. Skewness and Kurtosis
Range:
The range for a data is defined as the difference between maximum value and minimum
value. The corresponding relative measure of dispersion is coefficient of range.
𝐶𝑜𝑒𝑓𝑓𝑖𝑐𝑖𝑒𝑛𝑡 𝑜𝑓 𝑟𝑎𝑛𝑔𝑒
𝑅𝑎𝑛𝑔𝑒 = 𝑀𝑎𝑥 𝑉𝑎𝑙𝑢𝑒 − 𝑀𝑖𝑛 𝑉𝑎𝑙𝑢𝑒 𝑀𝑎𝑥 𝑉𝑎𝑙𝑢𝑒 − 𝑀𝑖𝑛 𝑉𝑎𝑙𝑢𝑒
=
𝑀𝑎𝑥 𝑉𝑎𝑙𝑢𝑒 + 𝑀𝑖𝑛 𝑉𝑎𝑙𝑢𝑒
54
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
Variance:
For raw data For ungrouped and grouped frequency distribution
2
∑𝑛𝑖=1(𝑥𝑖 − 𝑥̅ )2 2
∑𝑛𝑖=1 𝑓𝑖 (𝑥𝑖 − 𝑥̅ )2
𝜎 = 𝜎 =
𝑛 ∑𝑛𝑖=1 𝑓𝑖
Coefficient of Skewness
Karl Pearson’s (𝑺𝑲𝑷 ) Bowley’s (𝑺𝑲𝑩 ) Based on Moments (𝜸𝟏 )
(𝑀𝑒𝑎𝑛 − 𝑀𝑜𝑑𝑒) (𝑄3 + 𝑄1 − 2𝑄2 )⁄(𝑄3 − 𝑄1 ) ±√𝛽1
𝑆. 𝐷.
If 𝛾1 > 0, then the data is positively skewed. If 𝛾1 < 0, then it is negatively skewed. If 𝛾1 = 0,
then the data is symmetric.
Kurtosis gives the idea about the flatness or peakedness. It is given by,
𝝁𝟒
𝜷𝟐 = 𝟐 , 𝜸𝟐 = 𝜷𝟐 − 𝟑
𝝁𝟐
If 𝛽2 > 3 𝑜𝑟 𝛾2 > 0, then the frequency curve is leptokurtic. If 𝛽2 < 3 𝑜𝑟 𝛾2 < 0, then the
frequency curve is platykurtic. If 𝛽2 = 3 𝑜𝑟 𝛾2 = 0, then the frequency curve is mesokurtic.
55
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
7.4 Examples
1. For the following data representing the average rainfall in every month in the year 2017,
find arithmetic mean, geometric mean, harmonic mean. Also find, median, 1st quartile, 4th
decile, 87th percentile and mode. Also find range, quartile deviation, mean deviation
about median, mean deviation about mean, variance, standard deviation, sample
variance, sample standard deviation and their coefficients.
Month Jan Feb Mar Apr May Jun Jul Aug Sept Oct Nov Dec
Rainfall (in mm) 10 10 10 10 11 560 640 520 320 70 20 15
In [1]:import numpy as np
import [Link] as stats
import statistics
rainfall=[10,10,10,11,560,640,520,320,70,20,15]
#arithmetic mean
am=[Link] (rainfall)
print ("Arithmetic Mean :", am)
In [2]:#Geometric Mean
gm=[Link] (rainfall)
print ("Geometric Mean:",gm)
In [3]:#Harmonic Mean
hm=[Link] (rainfall)
print("Harmonic Mean:",hm)
Harmonic Mean: 19.039828944815717
In [4]: #Median
median=[Link] (rainfall)
print ("Median:",median)
Median:17.5
56
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
87th Percentile:542.8
In [6] :#Mode
mode=[Link](rainfall)
print ("Mode:",mode)
# For multiple mode use [Link]() in phyton 3.8
Mode: 10
Range: 630
Coefficient Of Range : 0.9692307692307692
2. For the following data representing the distribution of weight of 50 students in a class.
Find arithmetic mean, geometric mean, harmonic mean. Also find, median, 2nd quartile,
8th decile, 5th percentile and mode. Also find range, quartile deviation, mean deviation
about median, mean deviation about mean, variance, standard deviation, sample
variance, sample standard deviation and their coefficients.
Weight (in kg) 42 46 50 54 58 62 66
No. of 3 7 14 11 8 5 2
Students
In [1]:import statistics
import pandas as pd
import scipy. stats as stats
import numpy as np
data= {'Weight': [42,46,50,54,58,62,66], 'Numbar':
[3,7,14,11,8,5,2]}
df=[Link] (data)
a=list([Link](df['Weight']).repeat(df['Numbar']))
In [2]:#Arithmetic Mean
58
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
am=up,average(a)
print ( "Arithmetic Mean:" ,am)
In [5]: #Median
median=[Link](a)
print ( "Median:" ,median)
Median: 54.0
In [6]: # Second Quartile
q2=[Link](a,0.5)
print(“Second Quantile:”,q2)
#Eighth Decile
d8=[Link](a,0.8)
print(“Eight decile:”,d8)
#5th percentile
p5=[Link](a,0.05)
print(“5th Percentile:”,p5)
Second Quantile: 54.0
Eight Quantile: 58.0
5th percentile: 43.800000000000000004
In [7]: #Mode
mode=[Link](a)
print("Mode:", mode)
#For multiple mode use [Link]() in Phython 3.8
Mode: 50
In [8]: #Range
r=max(a) -min(a)
print("Range:", r)
#Coefficient of Range
crange=(max(a)- min(a) ) / (max(a) +min (a) )
print ("Coefficient of Range:", crange)
Range: 24
Coefficient of Range: 0.22222222222222
q2=[Link] (a,0.5)
q3=[Link](a,0.75)
print("Quartile Deviation:" ,qd)
#Coefficient of Quartile Deviation
cqd=(q3-q1)/(q3+q1)
print("Coefficient of Quartile Deviation:", cqd)
Quartile Deviation:4.0
Coefficient of Quartile Deviation: 0.07407407407407407
Variance: 35.7184
Standard Deviation : 5.97648726598233
60
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
In [2]:#Arithmetic Mean
am=[Link](a)
print ( "Arithmetic Mean:" ,am)
In [3]:#Geometric Mean
gm=[Link](a)
print ( "Geometric Mean:" ,gm)
61
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
In [4]:#Harmonic Mean
hm=[Link](a)
print ( "Harmonic Mean:" ,hm)
In [5]:#Median
med=sum(df['f'])/2
med_class=list(cumf).index(min(cumf[cumf>med]))
median=ll [med_class] + (ul[med_class]) * (sum(df['f'])/2-cumf[med-
class-1])/df['f'] [med_class]
print ( "Median:" ,median)
Median: 41.785714285714285
In [8]:#25 Percentile
p=25*sum(df['f'])/100
p=class=list(cumf).index(min(cumf[cumf>p]) )
p25=ll[p_class]+(ul[p_class]-ll[p_class])*(25*sum(df['f'])/100-
cumf[p_class-1])df['f'][p_class]
print ("25th Percentile:",p25)
In [9]: #Mode
m=max (df['f'])
m_class=list(cumf).index(min(cumf[cumf>p]) )
d1=df [' f '] [m_class] - df [ ' f '] [m_class-1]
d2=df [' f '] [m_class] - df [ ' f '] [m_class+1]
mode=ll[m_class]+(ul[m_class]-ll[m_class])*(d1/(d1+d2) )
print ("Mode:", mode)
62
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
Mode:42.22222222222222
In [10]: #Range
a=list([Link](cm).repeat (df[ ' f ']) )
r=max(ul)-min(ll))/(max(ul)+min(ll) )
print ("Range:" , r)
#Coefficient of Range
crange=(max(ul)-min(ll))/(max(ul)+min(ll) )
print("Coefficient of Range:", crange)
Range: 40
Coefficient of Range: 0.5
In [11]:#Quartile Deviation
q=sum(df[ 'f' ]) /4
q_class=list(cumf).index(min(cumf(cumf>q]))
q1=ll[q_class]+(ul[q_class]-ll(q_class])*(sum(df('f')/4-
cumf[q_class-1])/df['f'][q_class]
qa=3*sum(df['f']))/4
qa_class-list(cumf).index(ll(cumf[cumf>qa]))
q3=ll[qa_class]+(ul[qa class]-ll(qa class])*(3*sum(df['f'])/4-
cumf(qa_class-1))/df['f'][qa_class]
qd=(q3-q1)/2
print("Quartile Deviation:", qd)
#Coefficient of Quartile Deviation
cqd=(q3-q1)/(q3+q1)
print("Coefficient of Quartile Deviation:", cqd)
c1=skew(a)
print("coefficent of skewness based on moments:", c1)
7.5 References
1. [Link]
2. [Link]
3. [Link]
4. [Link]
65
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
Chapter 8
Correlation, Regression and Curve Fitting
When bivariate data are typically organized in a graph can be called as scatter plot. A scatter
plot has two dimensions, a horizontal dimension (called the x-axis) and a vertical dimension
(called the y-axis).
The pattern and direction of the relationship between X and Y can be seen from the scatter
plot. The strength of the relationship between two numerical variables depends on how
closely the data resemble a certain pattern. Correlation coefficient is used to measure the
strength and direction of the linear relationship between two numerical variables X and Y.
Once we find a linear pattern in the scatter plot, and the correlation between the two
numerical variables is moderate to strong, we can create an equation that allows you to
predict one variable using the other. This equation is called as simple linear regression line.
Generally Y is called as the depended variable and X is called as the independent variable.
Ordinary Least Squares (OLS) is the most common estimation method for linear models.
Ordinary least squares (OLS) regression is a statistical method of analysis that estimates the
relationship between one or more independent variables and a dependent variable; the
method estimates the relationship by minimizing the sum of the squares in the difference
between the observed and predicted values of the dependent variable.
Examples:
1. Plot the Scatter diagram from the following pairs of values. Find the correlation
coefficient and covariance between the sales (Rs. Lakhs) and expenses (Rs. Lakhs)
from the data given below :
Sales 50 50 55 60 65 65 65 60 60 50
Expenses 11 13 14 16 16 15 15 14 13 13
Also, calculate the regression equation of Sales on expenses.
Solution
In [1]:import numpy as np
66
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
import pandas as pd
import [Link] as plt
x=[Link]( [50,50,55,60,65,65,60,60,50] )
y=[Link]( [11,13,14,16,16,15,15,14,13,13] )
[Link](x, y)
[Link] ( 'scatterplot' )
[Link] (' Sales ')
[Link] ( ' Expenses ')
[Link] ()
In [4] : [Link](method='pearson')
In [5] : [Link](method='spearman')
67
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
In [6] : [Link](method='kendall')
In [8]: [Link](x,y)
Out [8]: SpearmanrResult (coorelation = 0.7975307304350421, pvalue=
0.005712188346423168
In[9]: [Link](x,y)
Out[9]:KendalltauResult (coorelation = 0.7975307304350421, pvalue=
0.012577800357482053)
Conclusion
H0: Sales(x) and Expenses(y) are not significantly correlated
H1: Sales(x) and Expenses(y) are significantly correlated
As, all the p-value in the test for significance i.e. 0.006954016570582246,
0.005712188346423168 and 0.012577800357482053 are less than 0.05 we can reject H0
at 5% level of significance and say Sales(x) and Expenses(y) are significantly correlated.
#Regression
In [10]: import [Link] as sm
x= sm.add_constant(x)
#to add the coloumn of 1's to the input if we want statsmodels to
calculate the intercept b0
x
68
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
C:\Users\Admin\anaconda3\lib\site-packages\scipy-
packages\[Link]: UserWarning: kurtosistest only valid for n>=20
... continuing anyway, n=10
“anyway, n=%i” %int(n))
Conclusion
From above table we obtain fitted equation as y=2.7222+0.1944*x
Since the value of Adj. R-squared is 0.571=> 57.1% variation in sales is explained by
independent variable expenses.
Test for significance of β0 and β1:
➢ H0: β0=0 against H1: β0≠ 0
69
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
Since, the p-value is 0.412(from above table) which is greater than 0.05 we do not reject H0
at 5% level of significance i.e., β0 is non-significant.
➢ H0: β1=0 against H1: β1≠ 0
Since, the p-value is 0.007(from above table) which is less than 0.05 we reject H0 at 5% level
of significance i.e., β1 is significant.
In [13]: y
Out [13]: array([11, 13, 14, 16, 16, 15, 15, 14, 13, 13] )
In [14]:model = LinearRegression()
[Link](x, y)
0ut[14]: LinearRegression(copy_x=True,fit_intercept=True,n_jobs=None,
normalize=False)
70
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
x=sm.add_constant (x)
model=[Link](y,x)
results=[Link]()
[Link]()
C:\Users\Admin\anaconda3\lib\site-packages\statsmodels\stats\[Link]:
ValueWarning: omni_normtest is not valid with less than 8 observations; 5
samples were given.
"Samples were given." & int(n), ValueWarning)
71
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
The relationship between the two variables may not always be linear. Hence if scatter plot
indicates curvilinear relationship we try to fit curvilinear model instead of linear model.
There are many curvilinear models in this chapter we cover following models.
➢ Quadratic curve:
Quadratic curve is given by y=a+bx+[Link] is second degree polynomial equation.
➢ Power curve:
Power curve is given by y=axb. For simplification, we can write it as log(y)=log(a)+b*log(x).
➢ Exponential curve:
Exponential curve is given by y=abx. For simplification, we can write it as
log(y)=log(a)+x*log(b)
➢ Logarithmic curve:
Logarithmic curve is given by y=a+b*log(x).
Note: log is Log to the base e. y is dependent variable x is independent variable
72
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
Examples:
1. Fit a quadratic curve to the following data and estimate y when x=5.
x 1 2 3 4 5
y 35 100 200 30 540
Solution
In [1]: import numpy as np
from [Link] import PolynomialFeatures
x = [Link] ([1,2,3,4,5] )
y = [Link] ([35,100,200,350,540])
[Link] (x,y,2) #2 indicates degree of the polynomial
73
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
2. Fit a power curve to the following data and estimate y when x=6.
x 1 2 3 4 5
y 20 150 550 1300 2500
Solution
In [1] : import numpy as np
from [Link] import PolynomialFeatures
x = [Link] ( [1,2,3,4,5] )
y = [Link] ( [20,150,550,1300,2500] )
[Link] ([Link] (x) ,[Link] (y),1)
In [4] : b=n
import [Link] as plt
[Link] (x,y, ' o ')
[Link] ( ' scatterplot ' )
[Link] (' x ')
[Link] (' y' )
[Link] ( )
74
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
Solution
In [1] : import numpy as np
from [Link] import PolynomialFeatures
75
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
x = [Link] ( [1,3,5,7] )
y = [Link] ( [5,20,100,400] )
[Link](x,[Link](y),1)
In [2]:slope=0.73777589
intercept=0.84934767
a=[Link] (intercept)
b=[Link](slope)
a
In [3]: b
Out [3]: 2.0912791034618685
76
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
In [2]: a=6.84145708
b=2.99213903
77
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
8.3 References
1. [Link]
2. [Link]
3. Dr Asha jindal (2017), Correlation,Regression and Curve fitting, Dr Asha
jindal(Ed.),Analyzing and visualizing Data with R software-A practical manual, Shailja
Prakashan and K.C. College, Page No:38-48
78
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
Chapter 9
Probability Distribution
The Python core does not include any tools to generate plots. This functionality is added by
other packages. By far the most common package for plotting is matplotlib. matplotlib is
intended to mimic the style of Matlab. As such, users can either generate plots in the Matlab
style. matplotlib contains different modules and features.
[Link] is the module that is commonly used to generate plots. It provides the
interface to the plotting library in matplotlib, and is by convention imported in Python
functions and modules with import [Link] as plt.
For Binomial(n, p)
In [1]: import [Link] as stats # it import package [Link]
n=5 # it assigns the value of shape parameter
p=0.5 # it assigns the value of probability of success.
x=3 # it assign the value of x.
bd=[Link](n,p) # it assigns the value of X
79
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
0.3125
0.8125
For Poisson(𝝀)
In [2]:import [Link] as stats # it import package [Link].
lm=3 #it assigns the vakue of parameterλ.
x=3 # it assigns the value of X.
pd=[Link](lm) #it assigns the Poisson distribution.
prob=[Link] (x) # it calculates P(X=3) of Poissondistribution.
print(prob) # it prints P(X=3).
0.22404180765538775
Negative Binomial(r, p)
In [3]:import [Link] as stats # it import package [Link].
r=4 #it assigns the value of parameter λ.
p=0.5
x=3 # it assigns the Poisson distribution
nbd=[Link] (r,p) #it assigns the Poisson Distribution.
prob=[Link](x) #it calculates P(X=3) of Poisson distribution
print(prob) # it rints P(X=3).
0.15625000000000017
Lognormal
In [4]: import [Link] as stats
m=3
s=2 # it assign the value of parameter λ.
x=3 #it assign the value of X.
lg=[Link](m,) # it assigns the log-Normal distribution.
p=[Link](x) #it calculates probabilities of log-Normal distribution.
print (p)
0.0414521374995849
In python, we can generate random sample of size n from different distributions by using the
following commands. We need to import numpy package after that use the following
commands:
In [9]:r5=[Link](0,5,10)
#sample of size 10 from uniform (0,5)
print ("Random numbers are:", r5)
Random numbbers are: [3.79425599 4.56937883 3.06672609 2.41885944
3.16853992 2.79737245 4.7398593 1.3271012 1.57616696 1.07407285]
In [10]:r6=[Link](0,1,10)
#sample of size 10 from logNormal (0,1)
print ("Random numbers are:", r6)
Random numbers are: [4.11328901 1.8174152 0.35794728 1.82482682
1.18830096 0.32319486 2.59994078 0.4198525 1.00776184]
In [11]:r7=[Link].standard_cauchy(10)
#sample of size 10 from standard_cauchy
print ("Random numbers are:", r7)
Random numbers are:[-0.20393381 -0.57215044 -4.74513244 -2.45038412 -
5.9634032 04568767 70.7991319 -1.20012489 -1.17320447 -1.46330294]
In [12]:r8=[Link](4,3,10)
#sample of size 10 from gamma (with shape 4& scale3)
print ("Random numbers are:", r8)
Random numbers are : [3.7795373 16.00762641 3.1506161619 17.59847979
6.40907394 6.29579046 12.04232884 11.35496534 10.43878574
15.69201329]
In [13]:r9=np,[Link](4,3,10)
# sample of 10 from beta(4, 3)
print ( "Random numbers are:", r9)
Random numbers are: [0.41952739 0.71660687 0.61529927 0.65816305
0.58354979 0.46716569 0.47045574 0.62504264 0.74208358 0.8324287]
81
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
In (15):r11=[Link](5,10)
# Sample of size 10 froa chisquare with 5 d.f.
print("Random numbers are:", r11)
Random number are: [5.8204119 2.02954166 7.92492356 5.37698655
3.60778101 3.32521995 8.57612513 2.96091453 12.61467725 4.84896307]
9.4 Examples
pmf of x
[1.04857600e-04 1.57286400e-03 1.06168320e-02 4.24673280e-02
1.11476736e-01 2.00658125e-01 2.50822656e-01 2.14990848e-01
1.20932352e-01 4.03107840e-02 6.04661760e-03]
cdf of x
[1.04857600e-04 1.67772160e-03 1.22945536e-02 5.47618816e-02
1.66238618e-01 3.66896742e-01 6.17719398e-01 8.32710246e-01
9.53642598e-01 9.93953382e-01 1.00000000e+00]
P(x=<l)= 0.00010485760000000014
P(x=2)= 0.010616832
P(X<=3)= 0.05476188160000002
P(X>5)= 0.6331032576
82
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
pd of x
[0.0407622 0.13043905 0.20870248 0.22261598 0.17809279 0.11397938
0.060789 0.02778926 0.0111157 0.00395225 0.00126472]
cdf of x
[0.0407622 0.17120126 0.37990374 0.60251972 0.78061251 0.89459189
0.9553809 0.98317016 0.99428586 0.99823811 0.99950283]
P(x=0)= 0.04076220397836621
P(x=3)= 0.22261598332718394
P(x=10)= 0.0012647200634353655
P(X<=1)= 0.17120125670913808
P(X>3)= 0.3974802755944429
P(X>=5)= 0.21938748893269577
pmf of x
(0.49565217 0.41304348 0.08695652 0.00434783]
cdf of x
[0.49565217 0.90869565 0.99565217 1.]
P(x=0)= 0.4956521739130437
P(x=2)= 0.08695652173913056
P(x=5)= 0
P(X<=1)= 0.908695652173914
P(X>3)= 0.0
P(X>=2)= 0.09130434782608599
4. Plot probability mass function (pmf) and distribution function for the random variables:
a) 𝑋~Poisson(2.6) b) 𝑋~𝐵𝑖𝑛𝑜(8,0.65) c) 𝑋~𝐻𝑦𝑝𝑒𝑟𝐺𝑒𝑜(𝑁 = 50,𝑀 = 10,𝑛 = 7)
In [19]: import numpy as np
import [Link] as stats
import [Link] as p1t
l=2.6
x=[Link](10)
pd=[Link](1)
prob=[Link](x)
cdf=pd. cdf (x)
[Link](x,0,prob)
[Link]('Plot of probability mass function')
[Link]()
[Link](x,cdf)
[Link]('Plot of distribution function')
[Link]()
84
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
a)
In [20]: import numpy as np
import [Link] as stats
import [Link] as plt
n=8
p=0.65
x=[Link](10)
[Link](n,p)
prob=[Link](x)
cdf-bd. cdf (x)
[Link](x,0,prob)
[Link]('Plot of probability mass function')
plt. show()
[Link](x,cdf)
[Link]('Plot of distribution function')
b)
In [21]: import numpy as np
import [Link] as stats
import [Link] as plt
N=15
m=12
n=10
x=[Link] (20)
hgd=[Link] (N,m,n)
prob=[Link](x)
cdf=[Link] (x)
[Link](x,0,prob)
[Link]('Plot of probability mass function')
[Link]()
[Link](x,cdf)
[Link]('Plot of distribution function')
[Link]()
85
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
5. A set of similar fair coins are tossed 640 times with the following results:
No. of heads 0 1 2 3 4 5 6
Frequency 7 64 140 210 132 5 12
Fit the binomial distribution to the data.
In [22]:import numpy as np
import [Link] as stats
import pandas as pd
N=640
x=[Link]([0,1,2,3,4,5,6))
f=[Link]([7,64,140,210,132,75,12J)
n=6
p=0.5
bd=[Link](n,p)
prob-bd. pmf (x)
E1=[Link] (N*prob)
data={ 'x' :x, 'freq' :f, 'p(x)' :prob, 'Expected Frequency' :
[Link](int)}
df=[Link] (data)
print(df)
6. Fit a Poisson distribution to the following data with respect to the number of red blood
corpuscles (𝑥) per cell:
𝒙 0 1 2 3 4 5
Number of cells 142 156 69 27 5 1
86
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
x=[Link]([0,1,2,3,4,5))
f=[Link]([142,156,69,27,5,1))
N=sum(f)
l=([Link](f))/N
pdis=[Link](l)
prob=[Link](x)
E=[Link] (N*prob)
data=( 'x' :x, 'freq' :f, 'p(x)' :prob, 'Expected Frequency'
:[Link](int))
df=[Link](data)
print(df)
In [24]:import numpy as np
import [Link] as stats
import pandas as pd
x=[Link]([0,1,2,3,4,5))
f=[Link]([213,128,37,18,3,1])
N=sum(f)
m=([Link](f))/N
x2=(x*x) .dot(f)
v=(x2/N)-m**2
p=m/v
q=1-p
r=int(m*p/q)
nbd=[Link](r,p)
prob=[Link](x)
E=[Link] (N*prob)
data=( 'x' :x, 'freq' :f, 'p(x)' :prob, 'Expected Frequency'
:[Link](int))
df=[Link](data)
print(df)
87
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
X follows N(2,1.5)
P(x=1.5)= 0.2515888184619955
P(x<=1.5)= 0.36944134018176367
P(x>3.5)= 0.15865525393145707
P(0<X<2)= 0.4087887802741321
P(-2<X<2)= 0.4961696194324103
ii)
In [26]: import numpy as np
import [Link] as stats
print ('X follows Exp ( 1.5)')
m=2; v=1.5
nd=[Link](m,v)
p=[Link](1.5)
p1=[Link](1.5)
p3=[Link](3.5)
p0=[Link](0)
p2=[Link](2)
p_2=[Link](-2)
print('P(x=1.5)=',p)
print('P(x<=1,5)=',p1)
print('P(x>3,5)=',1-p3)
print('P(O<X<2)=',p2-p0)
print('P(-2<X<2)=",p2-p_2)
X follows Exp(l.5)
P(x=1.5)= 1.0
P(x<=1.5)= 0.0
P(x>3.5)= 0.1353352832366127
P(0<x<2)= 0.3934693402873666
P(-2<x<2)= 0.3934693402873666
88
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
iii)
In [27]: import numpy as np
import [Link] as stats
print ('X follows Uniform ( 0,5)' )
a=0; b=5
ud=[Link](a,b)
p=[Link](1.5)
p1=[Link](1.5)
p3=[Link](3.5)
p0=[Link](0)
p2=[Link](2)
p_2=[Link](-2)
print('P(x=1.5)=',p)
print('P(x<=1,5)=',p1)
print('P(x>3,5)=',1-p3)
print('P(O<X<2)=',p2-p0)
print('P(-2<X<2)=",p2-p_2)
X follows Uniform(0,5)
P(x=1.5)= 0.2
P(x<=1.5)= 0.3
P(x>-3.5)= 0.30000000000000004
P(O<X<2)= 0.4
P(-2<X<2)= 0.4
9. Find the value of 𝑎, 𝑏, 𝑐 and 𝑑 if 𝑃(𝑋 < 𝑎) = 0.8, 𝑃(𝑋 > 𝑏) = 0.9,
𝑃(𝑋 > 𝑐) = 0.2, 𝑃(𝑋 < 𝑑) = 0.3 for i)𝑋~𝑁(2,1.5) ii) 𝑋~𝐸𝑥𝑝(𝜃 = 1.5) iii) 𝑋~𝑈(0,5)
i)
In [28] : import numpy as np
import [Link] as stats
print("X follows N(2,1.5)")
m=2
v=1.5
nd=[Link](m,v)
a=[Link](0.8)
b=[Link](1-0.9)
c=[Link](1-0.2)
d=[Link](0.3)
print( ' a=' , a)
print( 'b=' ,b)
print( 'c= ' , c)
print( "d=" ,d)
X follows N(2,1.5)
a= 3.2624318503593717
b= 0.07767265168309945
c= 3.2624318503593717
d= 1.2133992309379387
89
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
ii)
In [29] : import numpy as np
import [Link] as stats
print('X follows Exp(0,1)')
th=1.5
ex=[Link](th)
a=[Link](0.8)
b=[Link](1-0.9)
c=[Link](1-0.2)
d=[Link] (0. 3)
print(' a=', a)
print( 'b=' ,b)
print( 'c=' ,c)
print( ' d=', d)
X follows Exp(0,1)
a= 3.1094379124341005
b= 1.6053605156578263
c= 3.1094379124341005
d= 1.8566749439387324
iii)
In [30]:import numpy as np
import [Link] as stats
print('X follows Uniform (a,b)
a=[Link](0.8)
b=[Link](1-0.9)
c=[Link](1-0.2)
d=[Link] (0. 3)
print(' a=', a)
print( 'b=' ,b)
print( 'c=' ,c)
print( ' d=', d)
X follows Uniform
a= 4.0
b= 0.4999999999999999
c= 4.0
d= 1.5
10. Plot probability density function (pdf) and distribution function for the following random
variables:
i) 𝑿~𝑼(𝟎, 𝟏)
In [31]:import numpy as np
import [Link] as stats
import [Link] as plt
print('X follows Uniform (0,1)')
a=0
b=1
x=[Link](a,b,100)
pdf=[Link](x,a,b)
cdf=[Link](x,a,b)
90
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
[Link](x,pdf)
[Link]('plot of p.d.f')
[Link]()
[Link](x,cdf)
[Link]('Plot of c.d.f.')
plt show ()
ii) 𝑿~𝑵(𝟎, 𝟏)
In [32]:import numpy as np
import [Link] as stats
import [Link] as plt
import math
m=0
v=1
sd=[Link](v)
x=[Link] (m-3*sd,m+3*sd)
pdf=[Link](x,m,sd)
cdf=[Link](x,m,sd)
[Link](x,pdf)
[Link]('plot of p.d.f')
[Link]()
[Link](x,cdf)
[Link]('Plot of c.d.f.')
plt show ()
91
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
iii) 𝑿~𝑬𝒙𝒑(𝜽 = 𝟏. 𝟓)
In [33] : import numpy as np
import [Link] as stats
import [Link] as plt
th=1.5
x=[Link](0,10,100)
pdf=[Link] (x,th)
cdf=[Link] (x,th)
[Link](x,cdf)
[Link]('Plot of c.d.f.')
plt show ()
iv) 𝑿~𝑬𝒙𝒑(𝑴𝒆𝒂𝒏 = 𝟏. 𝟓)
In [34]:import numpy as np
import [Link] as stats
import [Link] as plt
m=1.5
th=1/m
x=[Link](0,10,100)
pdf=[Link] (x,th)
cdf=[Link] (x,th)
[Link](x,pdf)
[Link]('plot of p.d.f')
[Link](x,cdf)
[Link]('Plot of c.d.f.')
plt show ()
92
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
v) 𝐗~𝐆𝐚𝐦𝐦𝐚(𝟐, 𝟏. 𝟓)
In [35] : import numpy as np
import [Link] as stats
import [Link] as plt
n=2
th=1.5
x=[Link](0,10,100)
pdf=[Link] (x,th)
cdf=[Link] (x,th)
[Link](x,pdf)
[Link]('plot of p.d.f')
plt show ()
[Link](x,cdf)
[Link]('Plot of c.d.f.')
plt show ()
vi) 𝐗~𝐛𝐞𝐭𝐚(𝟐, 𝟏. 𝟓)
In [36] : import numpy as np
import [Link] as stats
import [Link] as plt
n=2
th=1.5
x=[Link](0,10,100)
pdf=[Link] (x,n,th)
cdf=[Link] (x,n,th)
93
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
[Link](x,pdf)
[Link]('plot of p.d.f')
plt show ()
[Link](x,cdf)
[Link]('Plot of c.d.f.')
plt show ()
In [38]:import numpy as np
94
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
import pandas as pd
import [Link] as stats
import [Link] as plt
ll=[Link]([144,150,156,162,168,174,180])
ul=[Link]([150,156,162,168,174,180,186])
f=[Link]([3,12,23,52,61,39,10])
x=(ll+ul)/2
k=len(f)
N=sum(f)
m=([Link](f))/N
x2=(x*x).dot(f)
v=(x2/N)-m**2
sd=[Link] (v)
L1=[Link] ([-9999,144,150,156,162,168,174,180,186])
U1=[Link] ([144,155,156,162,168,174,180,186,9999])
F=[Link] (([0] ,f,[0]))
nd=[Link](m,ad)
cp=[Link](L1)
p=[Link](cp)
p=[Link](p,1-cp[k+1])
ef=[Link](N*p)
data={'Lw':L1, 'UP' :U1 'freq':F, 'p(x)':p, 'F(x)':cp, 'Expected
Frequency':[Link] (int)}
df=[Link](data)
print (df)
References
1. [Link]
2. [Link]
95
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
Chapter 10
Statistical Tests
10.1 t-test
It is used to compare two samples to determine if they came from the same population.
What is t-test?
The t-test is a basic test that is limited to two groups. It is also called Student’s T Test
compares two averages (means) and tells you if they are different from each other. The t test
also tells you how significant the differences are; In other words it lets you know if those
differences could have happened by chance.
For Example: Let’s say you have a cold and you try a naturopathic remedy. Your cold lasts a
couple of days. The next time you have a cold, you buy an over-the-counter pharmaceutical
and the cold lasts a week. You survey your friends and they all tell you that their colds were
of a shorter duration (an average of 3 days) when they took the homeopathic remedy. What
you really want to know is, are these results repeatable? A t test can tell you by comparing
the means of the two groups and letting you know the probability of those results happening
by chance. The basic principle is to test the null hypothesis that the means of the two groups
are equal.
Assumptions of t-test:
➢ The population from which the sample has been drawn should be normally distributed.
➢ Underlying variances are equal.
Use of t-test:
It is used when there is random assignment and only two sets of measurement to compare.
96
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
Types of t-tests:
There are three types of t-tests we can perform based on the data at hand:
➢ One sample t-test
➢ Two sample t-test
➢ Paired sample t-test
1. One sample t-test: The One-sample t test is used to compare a sample mean to a specific
value. The One Sample t-test is a parametric test.
Example:-You have 10 ages and you are checking whether average age is 30 or not.
Interpretation:-Our t-statistic value is 0.59, and along with our degrees of freedom (n-1; 9)
this can be used to calculate a pvalue. The p-value in this case is 0.56, which is greater than
the standard thresholds of 0.05 , so we accept the null hypothesis and we can say there is a
no statistically significant difference between the mean of sample and population mean.
2. Two-sample T-test:-A two sample T-test is used to compare the means of two separate
samples.
Example: In the population, what is the difference between the female’s average score and
male’s average score on the test.
Null hypothesis: There is no statistically significant difference in the mean score of male and
female on the test
Alternative hypothesis: There is a statistically significant difference in the mean score of
male and female on the test
Scipy implements this as ttest_ind()
97
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
Interpretation: Our t-statistic value is -2.79 i.e. 2.79, and along with our degrees of freedom
(18) this can be used to calculate a p-value. The p-value in this case is 0.012, which is less
than the standard thresholds of 0.05, so we reject the null hypothesis and we can say there is
a statistically significant difference between the means of female score on the test and male
score on the test
3. Paired sampled t-test: The paired sample t-test is also called dependent sample t-test.
It’s a univariate test that tests for a significant difference between 2 related variables.
Example: To evaluate the blood pressure of 8 patients before and after treatment.
Interpretation: Our t-statistic value is 9.39, and along with our degrees of freedom (7) this
can be used to calculate a p-value.
The p-value is 0.0000324, which is far less than below standard thresholds of 0.05, so we
reject the null hypothesis and we can say there is a statistically significant difference in blood
pressure readings before and after treatment.
98
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
The closer the correlation value is to -1 or 1 the stronger the relationship, the closer to 0, the
weaker the relationship. It measures how change in one variable is associated with change in
another variable.
99
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
The strength of the correlation matters. The closer the absolute value is to -1 or 1, the
stronger the correlation.
Absolute Strength
r value
0.0 – 0.3 Weak correlation
0.3 – 0.7 Moderate correlation
0.7 – 1.0 Strong correlation
Correlation test:
There are a few common types of tests to measure correlation, they are:
1. Pearson Correlation (r)
2. Spearman Rank Correlation
Each have their own assumptions about the data that needs to be meet in order for the test
to be able to accurately measure the level of correlation. Each type of correlation test is
testing the following hypothesis:
Correlation Examples:
The data used for correlation test in this example is:
If there is a significant relationship between the carat and price of diamonds.
Null hypothesis: There is no relationship between carat and diamonds
Alternative hypothesis: There is a relationship between carat and diamonds
#importing required packages
import pandas as pd
import [Link] as stats
100
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
# Load data
df=pd.read_csv(“D:[Link]”)
#Descriptive Statistics
df[[“carat”, “price”, ‘depth’]].describe()
101
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
It appears that there is a linear relationship present- as the carat increases so does the price.
It is a indication of violating the assumption of homoscedasticity between the variables.
➢ Homogeneity of variances
To formally test homogeneity of variances, use the Levene’s test of homogeneity of variances
which is the [Link]() method from [Link].
#homogeneity of variance check
[Link](df[‘carat’],df[‘price’])
Out [28]: LeveneResult(statistic=40965.26627380512, pvalue=0.0)
Since p-value is less than alpha level 0.05, the Levene’s test for equal variances is significant,
meaning we violate the assumption of homoscedasticity. Given that, the appropriate
correlation test to use would be a non-parametric test such as the Spearman rank correlation.
In [35]: [Link](df[‘carat’],df[‘price’])
Out[35]: (0.9215913011934768,0.0)
102
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
Now to conduct non-parametric measure of correlation, which is, better to the relationship
between the carat and price of a diamond. To do this using [Link], use the .spearmanr()
method.
The Spearman rank correlation method indicates that the correlation is strong and significant
between the size of the carat and the price of the diamond.
In [34]: [Link](df[‘carat’],df[‘price’])
Out [34]: KendalltauResult(correlation=0.8341049107108127, pvalue=0.0)
Using this method, the test indicates that there is a strong, significant correlation between
the size of the carat and the price of the diamond.
103
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
2
(𝑂𝑏𝑠𝑒𝑟𝑣𝑒𝑑 − 𝐸𝑥𝑝𝑒𝑐𝑡𝑒𝑑)2
𝜒 =∑
𝐸𝑥𝑝𝑒𝑐𝑡𝑒𝑑
𝐷𝐹 = (𝑅𝑜𝑤𝑠 − 1) × (𝐶𝑜𝑙𝑢𝑚𝑛𝑠 − 1)
5. Interpret results.
P-value less than 0.05, Reject Null Hypothesis & Accept Alternative Hypothesis.
P-value greater than 0.05, Fail to Reject Null Hypothesis & Reject Alternative Hypothesis.
104
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
Solution
Null hypothesis: There is no relationship between Race and Smoking.
Alternative hypothesis: There is a relationship between Race and Smoking
After importing data next step is to format the data into a frequency count table. This is called
a Contingency Table, we can accomplish this by using the [Link]() function in pandas.
To perform this you should have already imported [Link] package.
The chi2_contingency() method conducts the Chi-square test on a contingency table
(crosstab)
#contingency table
contingency_table=[Link](df_chi[“Race”],df_chi[“Smoking”])
# Observed Values
Observed_Values = contingency_table.values
105
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
# Expected Values
ExpectedValues = chisq_output[3]
print (“chi square output :-\n”chisq_output)
Interpretation: The first value (9.707) is the Chi-square value, followed by the p-value
(0.021), then comes the degrees of freedom (3), and lastly it outputs the expected frequencies
as an array. Since all of the expected frequencies are greater than 5, the chi 2 test results can
be trusted. We can reject the null hypothesis as the p-value is less than 0.05. Thus, the results
indicate that there is a association between different group of Race and Smoking.
106
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
So you have a dataset and you’re about to run some test on it but first, you need to check for
normality.
For Example:
Parametric Test Non-Parametric Test
Paired t-test Wilcoxon Rank sum Test
One way Analysis of variance Anova Krushkal Wallis Test
There are a range of techniques that you can use to check if your data sample deviates from
a Gaussian distribution (Normal distribution). Below are the three tests you might want to
consider to check the normality on test data:
➢ The Shapiro-Wilk test;
➢ The Anderson-Darling test, and;
➢ The Kolmogorov-Smirnov test.
107
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
Shapiro-Wilk Test:
The Shapiro-Wilk test evaluates a data sample and quantifies if a random sample came from
a normal distribution. The Shapiro-Wilk test is believed to be a reliable test of normality,
although there is some suggestion that the test may be suitable for smaller samples of data,
e.g. thousands of observations or fewer.
In [36]: print(df)
Salary age rating
0 100 20 2.3
1 200 21 4.3
2 500 26 5.0
3 300 22 2.3
4 600 23 4.5
5 100 24 3.5
# Assigning x Variable
x=(df[‘salary’])
In [22]: print(x)
0 100
1 200
2 500
3 300
4 600
5 100
0.320363849401474
The example first calculates the test on the data sample, then prints the statistic and
calculated p-value.
#interpret
alpha= 0.05
if p > alpha:
print(‘Sample looks Gaussian (fail to reject HO)’)
else:
print(‘sample does not look Gaussian (reject HO)’)
Anderson-Darling Test:
The test is a modified version of a more sophisticated nonparametric goodness-of-fit
statistical test called the Kolmogorov-Smirnov test. A feature of the Anderson-Darling test
is that it returns a list of critical values rather than a single p-value. This can provide the basis
for a more thorough interpretation of the result.
Checking normality of data using Anderson Darling test for a variable in python
The anderson() SciPy function implements the Anderson-Darling test. It takes as
parameters the data sample and the name of the distribution to test it against. By default, the
test will check against the Gaussian distribution (dist=‘norm’).
# Load require package
import [Link] as stats
This example calculates the statistic on the test data set and prints the critical values.
109
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
In [49]: anderson_results_50
Out[49]:AndersonResult(statistic=0.2166625962964872,critical_values = array
([0.538, 0.613, 0.736,0.858, 1.021]),significance_level=array([15. , 10. , 5.
, 2.5 , 1. ]))
Critical values in a statistical test are a range of pre-defined significance boundaries at which
the H0 can be failed to be rejected if the calculated statistic is less than the critical value.
Rather than just a single p-value, the results give us the p-values for various significance
levels [15. , 10. , 5. , 2.5, 1. ] so if you’re working with boundaries outside of .05 you can see
those results as well.
Here our p-value for .05 is outside the rejection region of 0.736, meaning we cannot reject
the null hypothesis our data comes from a normal distribution.
Interpretation: We can interpret the results by failing to reject the null hypothesis that the
data is normal since our calculated test statistic is less than the critical value at a chosen
significance level. We can see that at each significance level, the test has found that the data
follows a normal distribution
The below example demonstrate the histogram plot on the test problem:
#import required packages for plot
from [Link] imort randn
from matplolib import pyplot
# naming x-axis
[Link](‘x-axis’)
# naming x-axis
[Link](‘y-axis’)
110
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
#history plot
[Link](data)
This is the histogram plot showing the number of observations in each bin. We can see a
Gaussian-like shape to the data, that although is not strongly the familiar bell-shape, is a
rough approximation.
Another popular plot for checking the distribution of a data sample is the quantile-quantile
plot, Q-Q plot, or QQ plot for short. We can develop a QQ plot in Python using the qqplot()
statsmodels function. The function takes the data sample and by default assumes we are
comparing it to a Gaussian distribution. We can draw the standardized line by setting the
‘line‘ argument to ‘s‘.
111
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
Running the example creates the QQ plot showing the scatter plot of points in a diagonal line,
closely fitting the expected diagonal pattern for a sample from a Gaussian distribution. There
are a few small deviations, especially at the bottom of the plot, which is to be expected given
the small data sample.
10.5 References
1. [Link]
2. [Link]
2718f8f9bf2f
3. [Link]
112
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
Chapter 11
ANOVA Procedure: One Way and Two Way
11.1 Introduction
Analysis of Variance (ANOVA) is used when there are more than two populations to compare.
ANOVA provides a means of comparing the variation within each subset or treatment of data
to the variation between the different subsets of data. The between subset variation is a
reflection of the possible differences between the subset averages. The within subset
variation, for each subset, is a reflection of the inherent variation observed when sampling
from the subset repeatedly. ANOVA is the statistical model that you use to predict a
continuous outcome on the basis of one or more categorical predictor variables.
Assumptions:
Analysis of Variance assumes that
1. The observations on Y are independent.
2. Populations being sampled are normal.
3. Populations being sampled have equal variances.
One Way ANOVA or One-Factor ANOVA as a Linear Model. An equivalent way to express the
one-factor model is to say that treatment j came from a population with a common mean (μ)
plus a treatment effect (αj) plus random error (eij):
yij = μ + αj + eij , j = 1, 2, …, c and i = 1, 2, …, n
Random error is assumed to be normally distributed with zero mean and the same variance
for all treatments.
I have used [Link] which has data on feed and weight. Weight is dependent variable
and a type of Feed is factor.
In [1]: import pandas as pd
data=pd.read_csv("C:/User/Admin/Desktop/[Link]")
[Link](5) ## to check first 5 rows of data along with heading
Out [1]: WEIGHT FEED
0 55 'A'
1 49 'A'
2 42 'A'
113
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
3 21 'A'
4 52 'A'
In [4]: ### type choice adds the mean square column in ANOVA
aov_table = [Link].anova_lm(mod,typ=1)
print(aov_table)
df sum_sq mean_sq F PR(>F)
FEED 3.0 26234.95 8744.983333 12.10504 0.000218
Residual 16.0 11558.80 722.425000 NaN NaN
p-value= 0.000218< 0.05 indicates significant difference between four types of feed.
Post hoc analysis Bonferroni is used to answer which feed differs from others.
Method 1: Bonferroni:
In[5]: pair_t = mod.t_test_pairwise('FEED',method='bonferroni')
114
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
pair_t.result_frame
Method 2: Sidak:
In[6]: pair_t = mod.t_test_pairwise('FEED',method='sidak')
pair_t.result_frame
A two-way ANOVA test is a statistical test used to determine the effect of two nominal
predictor (2 Factors) variables on a continuous outcome(dependent) variable. ANOVA
stands for analysis of variance and tests for differences in the effects of independent
variables on a dependent variable.
I have used [Link] which has data on Machine Type, Workmen and Production.
Production is dependent Variable and Machine Type and type of Workmen are two factors.
In [1]: import pandas as pd
data1=pd.read_csv(“C:/User/Admin/Desktop/[Link]”)##File Path
[Link](5) ## First 5 rows
115
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
In [3]: aov_table=[Link].anova_lm(model,typ=1)
print(aov_table)
df sum_sq mean_sq F PR(>F)
C(Workmen) 4.0 201.5 50.375000 8.202171 0.001990
C(MachineType) 3.0 353.8 117.933333 19.202171 0.000071
Residual 12.0 73.7 6.141667 NaN NaN
Both the P value < 0.05 indicates significant difference between five types of workmen and
four types of machine.
We can also do some diagnostics. It is to show the linear model fitted with the OLS method
and get a Quantile-Quantile (QQplot).
Post-hoc Testing:
There are a few different methods of post-hoc testing to find a difference between groups of
factors. I will show how to use Tukey’s HSD. We have to test for difference for each factor
116
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
separately. To Carry out Post Hoc Analysis one has to install statsmodel library and steps are
as follows:
1. Go to search in start button and type Anaconda Prompt which will open the window.
2. Type pip install statsmodels and press Enter key
3. Installation process will begin and it will complete in approximately 2 minutes.
In[5]:mc=[Link](data1[‘Producion’],
data1[‘MachineType’])
mc_results = [Link]()
print(mc_results)
Above table shows Machine Types Aand C, B and C as well as C and D differ significantly.
In [7]:mc=[Link](data1[‘Production’]
,data1[‘Workmen’])
mc_results=[Link]()
print(mc_results)
Above table shows all workmen group does not differ significantly.
11.4 References
1. [Link]
2. [Link]
3. Python Tutorial: [Link]
4. [Link]
117
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
Chapter 12
ANCOVA Procedure
The ANCOVA assumes linear relationship between the dependent variable (DV) and
covariate (CV).
Linearity of regression:
The relationship between the dependent variable and the associated variables must be
linear.
Independence of error terms:
The error terms must not be correlated. The variance-covariance matrix of the error term
must be diagonal.
Homogeneity of regression slopes:
The slopes of regression lines must be equal across all the levels of treatment. The regression
lines must be parallel among all levels.
Normality of error terms:
The distribution of error terms must be normal.
Homogeneity of error variances:
The variances of the error term must be same for different categories or levels of treatments.
We can perform Analysis of Covariance (ANCOVA) and from the p-value we can conclude
whether the means of a dependent variable are same across all levels or categories of
treatment, while statistically controlling the effects of all covariates. We will use ‘pingouin’
package. We need to install this package using pip command pip install pingouin
12.3 Examples
Objective: Our aim is to examine how applicant’s loan amount varies with their education
and income.
Analysis:
In [1] : import numpy as np
import pandas as pd
import [Link] as stats
import [Link] as plt
import statsmodels
import [Link] as sm
from [Link]
import ols
from [Link] import qqplot
from pingouin import ancova
We can see that there are 3 columns in dataset, viz., Education, ApplicantIncome and
LoanAmount.
In [3]: # to view forst 5 observations in our dataset
[Link]()
We are interested to examine how applicants loan amount varies with their education and
income. So, LoanAmount will be dependent variable (DV). Since, Education is a categorical
variable, it will be considered as independent variable (IV) for ANCOVA. Since,
ApplicantIncome is continuous variable, it will be considered as covariate (CV).
In [5]: #to obtain and description of categories or levels in variable 'Gender'
df['Education'].nunique()
Out [5]: 2
In [6]: df['Education'].unique()
Out [6]: array(['Graduate','Not Graduate'], dtype=object)
In [7]: df['Education'].value_counts()
Out [7]: Gradute 344
Not Gradute 88
Name:Education, dtype: int64
There are 2 categories in variable ‘Education’, viz., ‘Graduate’ and ‘Not Graduate’. Out of 432
applicants for whom the observations were recorded, 344 are graduated and 88 are not
graduated.
120
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
From the scatter plot we can assume the linear relationship between ApplicantIncome and
LoanAmount.
Since the observations are collected on different applicants, we can assume that the error
terms will be independent of each other. Now, we have to check whether the assumption
about homogeneity of slopes of regression lines. For this, we will run ANCOVA by including
covariate (CV) as well as interaction between independent variable (IV) and CV. To obtain a
variable which will represent interaction, we will create dummy variable for ‘Education’,
such that 0 will represent ‘Not Graduate’ and 1 will represent ‘Graduate’.
121
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
Now we have included dummy variable in our dataset. We need to calculate variable for
representing interaction term. It can be done multiplying IV by dummy variable.
We have included the variable representing interaction in our dataset. But we need to change
the variable name.
Now we can run the required ANCOVA for checking the homogeneity of regression slopes.
In[14]: #ANCOVA
Ancova(data=df,dv=’LoanAmount’,covar=[‘ApplicantIncome’,’interact’],
between =’Education’)
We can see that the p-value for interact is 0.810027> 0.05. So we can conclude that the
interaction effect of ‘Education’ and ‘ApplicantIncome’ is not significant at 5% level of
significance. So, the slope of regression lines is same across all categories of Education. Thus
we can proceed to perform ANCOVA.
In[15]:ancova(data=df,dv=’LoanAmout’,
covar=’ApplicantIncome’,between=’Education’)
122
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
We can see that the p-value for Applicant Income is 1.090642e-39< 0.05. So we can conclude
that Applicant Income is having significant effect on Loan Amount at 5% level of significance.
But, the p-value for is Education1.751684e-02 < 0.05. So, Education is having significant
effect on LoanAmount at 5% level of significance. Thus, means of Loan Amount are not same
across all levels of Education, while statistically controlling the effect of Applicant Income.
So, it is verified that ANCOVA is a mixture of ANOVA and regression analysis. We will also
check for normality of error term with the help of Q-Q plot.
From the Q-Q plot we can see that many points are lying almost around the line. Only few
points out of 432 observations are far away from the line. So we can assume normality of
error term.
123
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
Now we will also check whether variance of the error term is same for female and male. This
can be done by using Levene’s test.
We can see that the p-value is 0.0015269752598705557< 0.05. So, we can conclude that
variance of the dependent variable and hence that of the error term is not same for graduate
and not graduate. So, the assumption about homogeneity of variance is not satisfied. Thus, it
is not appropriate to perform ANCOVA for this dataset.
Objective: Our aim is to examine how children weight varies with their gender and height.
Analysis:
import numpy as np
import pandas as pd
import [Link] as stats
import [Link] as plt
import statsmodels
import [Link] as sm
from [Link] import ols
from [Link] import qqlot
from pingouin import ancova
In [2]: #Import dataset
df= pd.read_csv(“D:/Gender__Height_Weight.csv”)
print(list(df))
[‘Gender’,’Height’,’Weight’]
We can see that there are 3 columns in dataset, viz., Gender, Height and Weight.
In [3]: # to view first 5 observation in our dataset
[Link]()
124
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
Gender is a categorical variable. Height and Weight are continuous variables. There are 237
observations on each variable, and there is no missing observation.
We are interested to examine how children weight varies with their gender and height. So,
Weight will be dependent variable (DV). Since, Gender is a categorical variable, it will be
considered as independent variable (IV) for ANCOVA. Since, Height is continuous variable, it
will be considered as covariate (CV).
There are 2 categories in variable ‘Gender’, viz., ‘m’ and ‘f’. Here ‘m’ indicates male and ‘f’
indicates female. Out of 237 children for whom the observations were recorded, 126 are
male and 111 are female.
In [8]: #to get description about values in data
[Link](include=’all’)
We want to check whether there is any relationship between Height and Weight and whether
that relationship is linear. This can be done easily by using scatter plot.
125
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
From the scatter plot we can assume the linear relationship between Height and Weight.
Since the observations are collected on different children, we can assume that the error
terms will be independent of each other. Now, we have to check whether the assumption
about homogeneity of slopes of regression lines. For this, we will run ANCOVA by including
covariate (CV) as well as interaction between independent variable (IV) and CV. To obtain a
variable which will represent interaction, we will create dummy variable for ‘Gender’, such
that 0 will represent ‘f’ and 1 will represent ‘m’.
In [10]: #creating dummies
dummy=pd.get_dummies (df[‘Gender’])
df=[Link]([df,dummy[‘m’]],axis=1)
[Link]()
Now we have included dummy variable in our dataset. We need to calculate variable for
representing interaction term. It can be done multiplying IV by dummy variable.
In [11]: #Creating interaction variable
interact=df[‘m’]*df[‘Height’]
df=[Link]([df,interact],axis=1)
print(list(df))
[‘Gemder’,’Height’,’Weight’,’m’,0]
126
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
We have included the variable representing interaction in our dataset. But we need to change
the variable name.
In [12]: df =[Link](colums=(0:’interact’))
print(list(df))
[‘Gender’,’Height’,’Weight’,’m’,’interact’]
Now we can run the required ANCOVA for checking the homogeneity of regression slopes.
In [13]: #ANCOVA
ancova(data=df,dv=’Weight’,cover=[‘Height’,’interact’],between=’Geder’)
We can see that the p-value for interact is 2.762846e-01> 0.05. So we can conclude that the
interaction effect of ‘Height’ and ‘Gender’ is not significant at 5% level of [Link], the
slope of regression lines is same across all categories of Gender. Thus we can proceed to
perform ANCOVA.
We can see that the p-value for Height is 5.444591e-48 < 0.05. So we can conclude that
Height is having significant effect on Weight at 5% level of significance. But, the p-value for
Gender is 3.565750e-01> 0.05. So, Gender is not having significant effect on Weight at 5%
level of significance. Thus, means of weight aresame across all levels of Gender, while
statistically controlling the effect of Height.
In [15]: #Regression and ANCOVA
model_ancova=ols(“weight~Gender+height”,data=df).fit()
print(aov)
So, it is verified that ANCOVA is a mixture of ANOVA and regression analysis. We will also
check for normality of error term with the help of Q-Q plot.
127
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
From the Q-Q plot we can see that many points are lying almost around the line. So we can
assume normality of error term.
Now we will also check whether variance of the error term is same for female and male. This
can be done by using Levene’s test.
We can see that the p-value is 0.42916279362190046 > 0.05. So, we can conclude that
variance of the dependent variable and hence that of the error term is same for female and
male.
References
1. [Link]
2. [Link]
3. [Link]
128
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
Chapter 13
Predictive Analysis of Medical Cost
(Study of Regression Analysis to Random
Forest Regression on Medical Cost)
129
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
In [3]: [Link]()
130
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
Missing Values
In [7]: ###Checking for missing values
[Link]() .sum()
Visualization:
Bar Plots
In [20]: ###catplot or double barplot
##to change the colour - palette=”pink" inside the catplot ()
[Link](x="sex”, kind="count”, palette-"pink", data=data ,
size=5)
131
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
From the above bar graphs we can state that the female count is less than the male count
where the female count is around 660 and male count is around 670. The number of non-
smokers is more than number of smokers i.e. the non-smokers are more than 1000 while the
number of smokers are less than 300. The number of patients is more in south-east region
while in the other regions the numbers of patients are almost equal.
Swarm Plots
In [24]: [Link](x= ‘sex' , y = 'charges' , data = data , hue =
'sex')
132
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
fig (3)
From fig (1) shows that most of the people pay charges between Rs.0 to Rs.15,000
approximately. Some of them are paying charges between Rs.15,000 to Rs.47,000. From fig
(2), one can say that in all four regions majority of the people pay charges upto Rs.15,000
(approximately). Also south-east region has more number of insurers than any other
regions. From fig (3), the smokers pay more charges than the non-smokers. Most of the non-
smokers pay charges less than Rs.20,000. While the insurers in smoker category pay more
than Rs.10,000 and many smokers pay the more than Rs.40,000
Histogram of Charges
In [16]: f= [Link] (figsize=(12, 5))
[Link](data['charges'],kde=0,color='red')
Out [16] : <[Link]._subplots.AxesSubplot at Ox9ed23d0>
133
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
Charges variables positively skewed, i.e., Most of the insurer pay charges less than Rs. 20,000.
In [27]: [Link](data['age'])
Out [27]: <[Link]._subplots.AxesSubplot at 0xb5d8630>
134
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
Here box plots are used to detect the outliers. These are the boxplots of variables Age and
BMI respectively. So, from these box plots we can say that there is no outliers in the variable
Age whereas there are outliers in the variable BMI.
In [30]: [Link](data['children'])
Out [30]: <[Link]._subplots.AxesSubplot at 0xb681510>
These are the box plots for variables Children and Charges depicting the presence of outliers
in variable Charges. Since outliers are present only in two variables i.e. in BMI and Charges
135
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
and therefore calculating lower whisker and upper whisker only for these two variables so
that outliers can be treated.
Inter Quartile Range Score (IQR Score) for the variables BMI and Charges:
For BMI
In [31]: Q1=data[‘bmi’].qaantile(0.25)
Q3=data[‘bmi’].qaantile(0.75)
IQR=Q3-Q1
print(IQR)
8.3975
In [32]: Lower_whisker=Q1-1.5*IQR
Upper_whisker=Q3+1.5*IQR
print(Lower_whisker, Upper_whisker)
13.7 47.290000000000006
For Charges
In [37]: ## Outliers for charges
Q1=data['charges'].quantile(0.25)
Q3=data['charges’].quantile(0.75)
IQR=Q3-Q1
print(IQR)
11899.625365
In [38]: Lower_whisker=Q1-1.5*IQR
Upper_whisker=Q3+1.5*IQR
print(Lower_whisker,Upper_whisker)
-13109.1508975 34489.350562499996
Interpretation
The values which are less than lower whisker and greater than upper whisker are treated as
outliers.
136
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
Note: There are also several methods of detecting outliers such as scatter plot (for continuous
variables), DBSCAN (Density Based Spatial Clustering of Applications with Noise), Cook’s
distance etc.
➢ Z-Score method
While calculating the Z-score we re-scale and center the data and look for data points which
are too far from zero. In most of the cases a threshold of 3 or -3 is used i.e if the Z-score value
is greater than or less than 3 or -3 respectively, that data point will be identified as outliers.
So in this method, the data is refined by deleting the observations which are having outliers.
Note: In this method the rows are directly removed from the data which are having outliers
but the observations cannot be removed as such because until and unless the outliers are not
absurd values we cannot remove them, and hence removing the outliers is an inappropriate
way of dealing with them.
This technique uses the IQR scores calculated earlier to remove outliers. The rule of thumb
is that anything not in the range of Lower whisker = (Q1 - 1.5IQR) and upper whisker = (Q3
+ 1.5 IQR) is an outlier, and can be removed. So in this method, the outliers which are lesser
than lower whisker value are replaced by the lower whisker value and the outliers which are
greater than upper whisker value are replaced by the upper whisker value in the data.
In this analysis, the variable BMI has outliers and they are treated by IQR Score in the given
following way:
137
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
For variable Charges, the outliers can’t be treated by using this method because the outliers
are much larger than the upper whisker value and hence the outliers can’t be a good
imputation for the outliers and thereby outliers can’t be replaced by upper whisker value.
Limitation: So in this analysis without treating outliers for variable charges the analysis has
proceeded and fitted the Multiple Linear Regression Model
Note: The Multiple Linear Regression Model has fitted here but in actual scenario if any of
the assumptions of multiple linear regression does not satisfy then we can’t proceed with
this algorithm. Instead of this algorithm we have to go for non parametric test. Here we have
used Random Forest Regression (non parametric algorithm) for predicting the insurance
charges.
138
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
In [47] : ins_data.columns
Out [47]: Index ( [ 'age' , 'bmi' , 'children' , 'charges' , 'sex_male' ,
'smoker _yes' , 'region_northwest','region_southeast',
'region_southwest'], dtype= ‘object’)
The MLR model is fitted on the train data and has used for the prediction of insurance
charges on the test data.
Standardization of Variables:
We standardize the independent and dependent variables, except the dummy variables, so
that they all get measured on the same scale.
139
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
In [58]:cat_var_xtrain=x_train[[‘sex_male’,smoker_yes’ ,‘region_northwest’
,’region_southest’]]
In [61]: std_xtrain=[Link]([std_num_var_xtrain,cat_var_xtrain],axis=1)
print(std_xtrain)
Note: Since std_xtrain has “NaN” values and it is because std_num_var_xtrain and
cat_var_xtrain both has different indexes therefore changing the index of cat_var_xtrain
In [62]: cat_var_xtrain = cat_var_xtrain.reset_index()
Std_xtrain=[Link]([std_num_var_xtrain,cat_var_xtrain],axis=1)
print(std_xtrain)
140
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
In [63]: std_xtrain=std_xtrain.drop(‘index’,axis=1)
print(std_xtrain)
141
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
In [65]: mean_ytrain=y_train.mean()
std_ytrain=y_train.std()
std_ytrain=(y_train-mean_ytrain)/(std_ytrain)
std_ytrain=std_ytrain.reset_index()
std_ytrain=std_ytrain.drop(‘index , axis=1)
print(std_ytrain)
142
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
In[69]: cat_var_xtest=x_test[[‘sex_male’,’smoker_yes’,’region_northwest’,
’region_southwest’,’region_southest’]]
print(cat_var_xtest)
143
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
In [72]: std_xtest=[Link]([std_num_var_xtest,cat_var_xtest],axis=1)
print(std_xtest)
144
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
Note: Since std_xtest has NaN values and it is because std_num_var_xtest and cat_var_xtest
both has different indexes therefore changing the index of cat_var_xtest
In [74]: std_xtest.columns
std_xtest=std_xtest.drop(‘index’,axis=1)
print(std_xtest)
145
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
Predictive Modeling:
Hypothesis Testing
H0: β1= β2= β3=…….= βp=0 i.e. No variable is significant, against
H1: Not H0, i=1, 2, 3…p, i.e. Atleast one variable is significant
Model Building
Multiple linear regression model between our dependent and independent variables is
Y (charges) = β0+ β1*(age) + β2*(BMI) + β3*(children) + β4*(sex_male) + β5*(smoker_yes) +
β6*(region_northwest) + β7*(region_southeast) + β8*(region_southwest)
146
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
To obtain Residuals
Residuals= [Link] ##Residuals
###Testing the model on test dataset
X = sm.add_constant(std_xtest) #adding a constant
y_test_pred = [Link](X)
Assumptions Validation:
1. Multicollinearity
Multicollinearity exists when two or more of the predictors (independent variables) in a
regression model are moderately or highly correlated. Unfortunately, when it exists, it can
wreak havoc on our analysis and thereby limit the research conclusions we can draw.
Detection
Multicollinearity may be checked multiple ways:
i) Correlation matrix –When computing a matrix of Pearson’s bivariate correlations among
all independent variables, the magnitude of the correlation coefficients should be less
than 0.80.
147
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
ii) Variance Inflation Factor (VIF) – The VIFs of the linear regression indicate the degree
that the variances in the regression estimates are increased due to multicollinearity. VIF
values higher than 5 indicate that multicollinearity is a problem.
Remedies
➢ Drop one of the independent variable which is explained by others
➢ Use Principal Component Regression in case of severe multicollinearity
➢ Use Ridge Regression
In our data we checked the multicollinearity by using VIF. Here we considered VIF threshold
value equals to 5. Consider the model,
Y (charges) = β0+ β1*(age) + β2*(BMI) + β3*(children) + β4*(sex_male) + β5*(smoker_yes) +
β6*(region_northwest) + β7*(region_southeast) + β8*(region_southwest)
Here as we can see none of the values of VIF is greater than or equal to 5. Therefore we can
say that there is no multicollinearity in the data.
2. Linearity
In multiple linear regression we assume that there is linear relationship between the
response variable and predictors.
Detection
One can detect linearity form scatter plot. Another way of testing linearity between the
variables is by using the test called Harvey-Collier multiplier test. The Harvey-Collier test
performs a t-test on the recursive residuals. If the true relationship is not linear but convex
or concave the mean of the recursive residuals should differ from 0 significantly. This means
that a significant result means that you can reject the null hypothesis that the true model is
linear.
148
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
The scatter plot is slightly convex downward which represents non linearity between the
dependent and independent variables.
Note: Here the code sms.linear_harvey_collier() will throw an error of singular matrix and
that is because of absence of multicollinearity in the data. If the variables would have highly
correlated it wouldn’t have thrown the error.
Remedy
If this assumption is not satisfied then use some appropriate transformation. As in our case
we have applied Log transformation on our response variable.
For this data I have tried to implement different transformations but still the data was not
satisfying the linearity assumption. Hence I have proceeded forward without satisfying this
assumption.
3. Homoscedasticity
Detection
Homoscedasticity can be tested statistically by Breush-Pagan test. Consider the hypothesis,
H0: The variance of the residuals is constant, against
H1: The variance of the residuals is not constant.
149
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
In [77]: ###Homoscedasticity
import [Link] as sms
import [Link] as smf
#Breusch-Pagan Test for Heteroscedasticity
bptest=[Link].het_breuschpagan(residuals,[Link])
print(bptest)
(86.48898360885777, 2.3947340156043142e-15, 11.797270173435852,
4.3049684277300757e-16)
Here the p-value is lesser than significance level 0.05 hence we reject H0 and conclude that
the variance of the residuals is not constant.
Remedy
The problem of Heteroscedasticity can be resolved by using some appropriate
transformation on dependent variables. The Box-Cox transformation is the technique to find
out the appropriate transformation for the response variable. Here we tried for both the
remedies but couldn’t work for this data. Hence, I have proceeded for the further
assumptions without solving the problem of heteroscedasticity.
Detection
For univariate case this assumption can be checked by using Shapiro Wilk’s test. For
multivariate this assumption can be checked by Jarque-Bera test.
Here the p-value is lesser than significance level 0.05 hence we reject H0 and conclude that
the residuals do not follow normal distribution.
Remedies
➢ A usual remedy is to use a transformation of the variables to make them closer to
normally distributed.
➢ Generalized linear mixed model
150
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
We tried for the transformation of the variables but couldn’t attain the normality of the
residuals. Further, we didn’t try for the generalized linear mixed model because already
above mentioned assumptions are violated and the remedy didn’t work. So even if this
remedy could have worked to attain the normality of residuals still we couldn’t fit the MLR
model.
5. Autocorrelation
The assumption of uncorrelated and independent error terms for regression models using
time series data is not always appropriate. Usually the errors in the time series data exhibit
serial correlation. i.e. E(εi) and E(εj) is not zero. Such error terms are said to be
autocorrelated.
Detection
i) Plot of residuals versus time
ii) Durbin Watson test
I have used Durbin Watson test for detecting the autocorrelation in the data.
H0: ρ = 0 i.e. there is no autocorrelation present in the data
H1: ρ ≠ 0 i.e. there is autocorrelation present in the data
where, ρ is autocorrelation factor
151
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
These are the codes for model building and model summary by using statsmodels library of
python. Model summary includes Durbin Watson value and therefore there is no need to
impute it by using different codes.
In present study, Durbin Watson value is 2.048 which is approximately equal to 2. Hence
from the interpretation of Durbin Watson test we can say that there is no autocorrelation in
the data.
From the above summary we can see that variables age, BMI, children and region_northwest
are the significant variables. Hence these variables should be included in the model and all
other variables should be excluded.
The adjusted-R2value suggests the proportion of the variance explained by the model. 72.9%
of the total variance is explained by our model.
Interpretation of βi’s:
1-unit increase in X multiplies the expected value of Y by corresponding β value.
Since the assumptions were not satisfied I couldn’t proceed for the further analysis by using
multiple linear regression algorithms and hence I had selected another technique called
Random Forest Regression for the predictive analysis.
152
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
import os
from [Link] import accuracy_score
from [Link] import mean_squared_error
from sklearn.model_selection import train_test_split
from sklearn. ensemble import RandomForestRegressor
#Required to build random forest
For this algorithm, first created dummies for the categorical variables and splitted the data
in train data and test data. After splitting the data into two different data we standardized
the every variable.
Note: The codes for creating dummy variables, splitting the data into train and test and
standardization of the variables are already given above.
##Create_Separate_dateframe_consisting_of_only_independent_variable
x= [Link](columns=('charges'), inplace =False,axis=1)
##Split the data into train end test and then standardize the both the data by
using the above codes
##### Fitting the regression model to the dataset#####
regressor=RandomForestRegressor(n_estimators=220,
max_features=1,
max_depth=1,random_state = 42)
[Link](std_train, std_ytrain)
print(model)
153
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
y_pred = [Link](std_xtest)
test_values =[Link](y_pred)
tv _head=test_ values .head ()
print(test_values)
MSE Interpretation
A larger MSE means that the data values are dispersed widely around its central moment
(mean), and a smaller MSE means otherwise and it is definitely the preferred and/or desired
choice as it shows that data values are dispersed closely to its central moment (mean); which
is usually great.
RMSE Interpretation
The RMSE is the square root of the variance of the residuals. It indicates the absolute fit of
the model to the data–how close the observed data points are to the model’s predicted
values. Whereas R-squared is a relative measure of fit, RMSE is an absolute measure of fit. As
the square root of a variance, RMSE can be interpreted as the standard deviation of the
unexplained variance, and has the useful property of being in the same units as the response
154
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
variable. Lower values of RMSE indicate better fit. RMSE is a good measure of how accurately
the model predicts the response.
Here the MSE and RMSE are 0.808846 and 0.899 which is very small and therefore we can
say that the model is a good fit.
13.3 Conclusion
The patients which are older in age, having high BMI with smoking habit are found to have
high medical charges. The region southeast has maximum number of patients than any other
region because the number of smokers having high BMI are more in that region. The number
of patients covered by the medical health insurance is the aged patients. So eventually the
people who have got the medical insurance are the older in age, have smoking habit, have
high BMI and they mostly belongs to southeast region.
13.4 References
1. [Link]
[Link]/stable/modules/generated/[Link]
2. [Link]
3. [Link]
71824ced454f
155
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
Chapter 14
Factor Analysis
14.1 Introduction
Factor analysis is a technique that is used to reduce a large number of variables into fewer
numbers of factors. This technique extracts maximum common variance from all variables
and puts them into a common score. As an index of all variables, we can use this score for
further analysis.
Factor analysis is part of general linear model (GLM) and this method also assumes several
assumptions:
1. No outlier: Assume that there are no outliers in data.
2. Adequate sample size: The case must be greater than the factor.
3. No perfect multicollinearity: Factor analysis is an interdependency technique. There
should not be perfect multicollinearity between the variables.
4. Homoscedasticity: Since factor analysis is a linear function of measured variables, it
does not require homoscedasticity between the variables.
5. Linearity: Factor analysis is also based on linearity assumption. Non-linear variables
can also be used. After transfer, however, it changes into linear variable.
The factor analysis model can be written algebraically as follows. If you have p variables
𝑋1 , 𝑋2 , … , 𝑋𝑝 measured on a sample of n subjects, then variable 𝑋𝑖 can be written as a linear
combination of m factors F1, F2, . . . , Fm where, as explained above m < p. Thus,
𝑋𝑖 = 𝑎𝑖1 𝐹1 + 𝑎𝑖2 𝐹2 + ⋯ + 𝑎𝑖𝑚 𝐹𝑚 + 𝑒𝑖
where, ‘a’ is are the factor loadings (or scores) for variable 𝑋𝑖 and 𝑒𝑖 is the part of variable
𝑋𝑖 that cannot be ’explained’ by the factors.
Also, we can easily interpret 2D plots and it becomes difficult when we have more
156
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
dimensions. So, dimensionality reduction technique plays very important role by selecting
relevant features without losing much information of the data.
14.2 Abstract
This study aims for learning the technique of Factor Analysis using Python and its application
on identifying the factors to find breast cancer. This case study is based on data “[Link]”
of 569 patients and to find important variables out of 30 variables namely form of
radius_mean, texture_mean, perimeter_mean, area_mean, smoothness_mean,
compactness_mean, concavity_mean, concave points_mean, symmetry_mean,
fractal_dimenion_mean,radius_se, texture_se, perimeter_se, area_se, smoothness_se,
compactness_se, concavity_se, concave points_se, symmetry_se, fractal_dimenion_se,
radius_worst, texture_worst, perimeter_worst, area_worst, smoothness_worst,
compactness_worst, concavity_worst, concave point_worst, symmetry_worst,
fractal_dimenion_worst. Factor Analysis is used to understand the correlation structure of
collected data and identifying the most important factors for identifying breast cancer.
14.3 Analysis
157
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
In [3]: [Link] ().sum () ##to check variable vise whether our data contains any null values
158
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
159
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
Barlett’s test for sphericity tests the null hypothesis that the correlation matrix is an identity
matrix. Small p-value indicate that evidence against the null hypothesis (i.e. the variables
really are correlated). For p-values much larger than 0.05 indicated that there is insufficient
evidence that variables are not correlated, so far factor analysis may not be suitable.
In [5]: from factor_analyzer.factor_analyzer import
calcalate_bartlett_sphericity
chi_square_value,p_value=calculate_bartlett_sphericity(df)
chi_square_value, p_value
Out [5]: (39391.522783630295, 0.0)
Here, p-value is less than 0.05 so now we can proceed for factor analysis.
Let’s try to find Eigen values which would decide appropriate number of factors to be chosen
for our analysis and visualize them.
In [6] : fa=FactorAnalyzer(n_factors=30,rotation=”varimax”)
[Link](df)
ev, v=fa.get_ eigenvalues ()
ev=[Link](list(ev) ,columns=[ 'Eigen_values')
,index=[[Link]])
# Create scree plot using matplotlib
[Link](range(1,df,shape[1]+1),ev)
[Link](range(1,df,shape[1]+1),ev)
[Link]('Scree Plot')
[Link] ('Factors’)
[Link]('Eigenvalue')
[Link](y=1, c='k')
Out [6] : <[Link].Line2D at 0xbf134d0>
We have to select all those factors which have eigen values greater than 1. Hence, number of
factors = 6.
Now we again have to create factor analysis model but selecting n =6 and also estimating
loadings.
160
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
161
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
Let us find out how much these factors are explaining variations in data.
In [9]: output=fa.get_factor_variance()
Output=[Link](list(output),columns=[‘Factor1',’Factor2',
'Factor3','Factor4','Factor5','Factor6'), index=['SS loadings',
'ProportionVar', 'Cumulative Var])
Output
163
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
Other Terminologies:
1. Varimax rotation: Varimax rotation (also called Kaiser – Varimax rotation) maximizes
the sum of the variance of the squared loadings, where ‘loadings’ means correlations
between variables and factors. This usually results in high factors loadings for a smaller
number of variables and low factor loadings for the rest.
2. Eigen values: Eigen values shows variance explained by that particular factor out of total
variance.
3. Factor loading: Factor loading is correlation coefficient for the variable and factor. Factor
loading shows the variance explained by the variable on that particular factor.
14.4 References
1. [Link]
2. [Link]
3. [Link]
[Link]
4. [Link]
5. Dr. Santosh P. Gite (2018), Factoranalysis, Dr. Asha Jindal (ed.) Analysing and visualizing
data with R software, ShailajaPrakashan and Kishinchand Chellaram College, pp181-188.
6. Dr. Asha Jindal (2013), Factorinfluencing infant mortality in Uttar Pradesh, International
Journal of Multidisciplinary Research, Vol I, Issue 10(I), pp 65-70.
164
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
Chapter 15
Cluster Analysis
15.1 Introduction
Cluster analysis or clustering is grouping of cases based on similarities and other numerical
attributes of variables in the sample or population under study. Cluster analysis is useful for
unsupervised machine learning. Unsupervised learning is used to solve problems in machine
learning which have unknown solutions. In this case study, problem is solved using
Agglomerative Hierarchical clustering. Idea behind this method is to form different clusters
using bottom up approach i.e. considering every observation as a cluster then combining
them together until a single cluster is formed starting from the bottom.
For this, a dendrogram is used using the dissimilarities between the observations in the data.
A dendrogram is a diagram similar to tree diagram. Using the dendrogram, different clusters
are then formed by calculating the distances between the observations.
Simplest way to calculate the distance between any two points in two dimensions is
𝑑 = √(𝑋2 − 𝑋1)2 + (𝑌2 − 𝑌1)2
where, d = distance. This method of calculating distance is known as Euclidean method. The
general procedure of Agglomerative Hierarchical clustering is as follows:
1. Step 1: Consider each observation in the data as a single cluster so that in general our
data has let’s says ‘k’ clusters.
2. Step 2: Take two closest observations and make one cluster of them. So now we have ‘k-
1’ clusters.
3. Step 3: Continue with step 2 procedure until we have a single cluster.
165
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
Next part is fitting of clusters to the data provided and visualisation, which we will discuss
in the procedure.
15.2 Procedure
Before importing the dataset it is advised to create a working directory and set it up. It can
be done using the [Link]() command.
X = [Link][:,[1,9]].values
print(X)
166
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
167
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
Understanding the dendrogram and calculating the number of clusters formed is most
important. For that, just see the longest line which is not intersected by a horizontal line i.e.
in this case blue line just above green horizontal line. Similar can be seen in case of blue and
red lines. After that, see in how many parts those intersected horizontal lines are divided.
Here we get a total of five clusters. Next diagram shows how it can be observed.
168
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
print(pred)
AgglomerativeClustering(affinity='euclidean',
compute_full_tree='auto',
connectivity=None,distance_threshold= none,
linkage='ward',memory=none,n_cluster=5,
pooling_func='deprecated')
[1 1 0 0 1 1 2 0 0 1 1 2 2 2 4 4 2 3 3 3 0 2 2 2 1 0 0 3 2 1 2 0]
[Link](X[pred==0,0],X[pred==0,1],s=27, c=’orange’,label=’cluster1’)
[Link](X[pred==1,0],X[pred==1,1],s=27, c=’red’,label=’cluster2’)
[Link](X[pred==2,0],X[pred==2,1],s=27, c=’violet’,label=’cluster3’)
[Link](X[pred==3,0],X[pred==3,1],s=27, c=’indigo’,label=’cluster4’)
[Link](X[pred==4,0],X[pred==4,1],s=27, c=’brown’,label=’cluster5’)
[Link](‘Visualization of Clusters‘)
[Link](‘component 1’)
[Link](‘component 2’)
[Link]()
[Link]()
Interesting part to note is we have plotted multidimensional data in just two dimensions for
simplicity. Further one may note that according to the data, types of cars may be categorized
in two types as performance heavy/ super cars and pocket friendly. Cars who deliver high
end performance are generally having more number of cylinders, greater fuel displacement
capacity in engines to generate more horsepower etc. hence buyers pay a premium price. On
the other hand, budget cars provide more miles per gallon having less fuel displacement
capacity, number of cylinders in the engine etc.
15.3 Acknowledgement
I would like to thank my parents and Dr. Asha Jindal, Associate Professor and Head,
Department of Statistics, K. C. College for her encouragement and support.
169
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
15.4 References
1. [Link]
2. [Link]
3. [Link]
html
4. [Link]
[Link]/stable/modules/generated/[Link]
170
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
Chapter 16
Non-Parametric Test
(Choice and Application using Python)
16.1 Introduction
Processed information has become a key to many decision making systems. Data has become
an inevitable part of all the business models, research and education. Not only educational
Institutions of international repute but business firms like IBM, Microsoft, SAS and many
more has come out with courses and training methods on analysis of data. Data is generated
by many sources, some of the data is result of planned experiments and some are results of
real time processes. Basic types of such data can be of nominal, ordinal and scale type. All
these information/data can further be processed for the purpose of
Fig.1: Research process flowchart from “C. R. Kothari (2009) "Research Methodology:
Methods&Techniques" (Second Revised Edition), New Age International Publishers, New
Delhi.
171
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
Analysis of Data is one of the important steps in the research process. In statistical theory of
inference this procedure is called as testing of hypothesis if the analysis of data is
imperatively for the purpose of testing of hypothesis. This testing of hypothesis process
comprises of following steps:
1. Defining objective and problem statement precisely.
2. Formulate null and alternate hypothesis.
3. Decide on level of significance.
4. Choose an appropriate statistical test.
5. Using an appropriate decision rule, decide whether null hypothesis to be accepted or
rejected.
6. Interpret and conclude the results with respect to problem statement.
Data analysis requires choosing an appropriate statistical test for analysis from parametric
and non-parametric domain. This choice of the test is depending on following criteria:
➢ Type of Hypothesis (Association or Difference type).
➢ Number of dependent and independent variables.
➢ Type of variables (Nominal, Ordinal and Interval or Ratio scale).
➢ Normal Distribution (test of Normality).
The basic conditions for choosing test from parametric domain are
1. The data should be measured on Interval/Ratio scale.
2. Data should satisfy the condition on normality.
If all of the above conditions are satisfied then we use parametric tests. On the contrary a
statistical method is called non-parametric if it makes no assumption on the population
distribution or sample size. This approach is less powerful yet more frequent, more flexible,
more robust, and applicable to quantitative and non-quantitative data.
On choosing appropriate statistical test for analysis one can use Excel, SPSS, MINITAB, SAS,
R-programming and Python programming etc. The packages like SPSS, MINITAB are menu
driven packages. SAS, R and Python are programing languages. The R and Python are the
important free and open source software used in data analytics and business analytics. In
172
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
this chapter we have discussed use of python programming language to apply different non-
parametric tests on different types of data sets and different types of cases.
Statsmodels is a Python module that provides classes and functions for conducting statistical tests.
The statsmodel functions on nonparametric statistics are used to evaluate nonparametric test. Another
platform used for evaluation is SciPy. This is a collection of mathematical algorithms and convenience
functions built on the NumPy extension of Python. SciPy is interactive Python platform for data-
processing.
The purpose of one sample test is to test the significance of difference between sample and
population median.
Sign test:
Basic conditions for applying test: One-sample data and median.
Syntax used:
[Link].sign_test(samp,mu0=test median)
Parameters
samp: One dimensional array of data for which you want to perform the signs test.
mu0: The population median against which data is to be tested.
Returns
1. The signs test returns M = (N(+) - N(-))/2, where N(+) is the number of values above
mu0, N(-) is the number of values below. Values equal to mu0 are discarded.
2. The p-value for M is calculated using the binomial distribution and can be interpreted the
same as for a t-test. The test-statistic is distributed Bin(min(N(+), N(-)), n_trials, where
n_trials equals N(+) + N(-).
Example1:
It is known from the past experience that the median length of fish in a particular polluted
lake was 3.9 inches. During the past two years the lake was cleaned up and the conjecture is
made that now median length is greater than 3.9 inches. A random sample of 10 sunfish
selected from this lake showed lengths as 5.2, 4.1, 5.4, 5.7, 3.0, 6.3, 6.6, 2.8, 1.9, 4.5 inches.
Will you reject the null hypothesis at 10 % level of significance (l.o.s.) on the basis of Sign
Test?
In [1] *Sign test tests the hypothesis type of two sided.
import scipy as sp
import numpy as np
import pandas as pd
import [Link] as smsd
In [2]: # Enter for comparison by using any of the known of method and
entry and choose variable to be tested.
173
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
In [4] : # interpret
alpha = 0.05
if p > alpha:
print('Fail to reject HO')
else:
print('Reject HO')
Fail to reject HO
Interpretation: Since p-value=0.3437> 0.025 indicates one should not reject null
hypothesis.
Note: This test can also be used to test the significance of difference between two paired
observation by using difference data (d=x-y) as one dimensional data as samp and mu0=0.
174
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
➢ Observations between groups are independent. That is, not paired or repeated measures
data.
➢ In order to be a test of medians, the distributions of values for each group need to be of
similar shape and spread; outliers affect the spread. Otherwise the test is a test of
distributions.
Syntax:
[Link] (x, y, use_continuity=True, alternative=None)
or
[Link] (x, y, use_continuity=True, alternative=None)
It computes the Mann-Whitney rank test on samples x and y.
Parameters
1. x, y like Array of samples, should be one-dimensional.
2. use_continuity optional and takes values True or False. Whether a continuity correction
(1/2) should be taken into account. Default is True.
3. alternative {None, ‘two-sided’, ‘less’, ‘greater’}, optional. Defines the type of alternative
hypothesis.
Returns
1. U-value: The Mann-Whitney U statistic, equal to min(U for x, U for y) if alternative is
equal to None (deprecated; exists for backward compatibility), and U for y otherwise.
2. p-value: p-value assuming an asymptotic normal distribution. One-sided or two-sided,
depending on the choice of alternative.
Example 2:
Consider a Phase II clinical trial designed to investigate the effectiveness of a new drug to
reduce symptoms of asthma in children. A total of n=10 participants are randomized to
receive either the new drug or a placebo. Participants are asked to record the number of
episodes of shortness of breath over a 1 week period following receipt of the assigned
treatment. The data is given below.
X 7 5 6 4 12
Y 3 6 4 2 1
Is there a difference in the number of episodes of shortness of breath over a 1 week period
in participants receiving the new drug as compared to those receiving the placebo? By
inspection, it appears that participants receiving the placebo have more episodes of
shortness of breath, but is this statistically significant? Use Mann-Whitney-Wilcoxon test
at 5% l.o.s. to test H0 :Mx = My against H1: Mx<My (use normal approximation.)
In[1]: #comparison of two variables using Mann—Whitney U test
import [Link] as st
from scipy import stats
import [Link] as mannwhitneyu
from scipy import stats
175
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
In[6]: #interpret
alpha= 0.05
if p > alpha:
print ('same distribution (Fail to reject HO)')
else:
print('Different distribution (Reject HO)')
Same distribution (Fail to reject HO)
Interpretation: p-value greater than that of 0.05 at 5% level of significance indicates that
on should not reject null hypothesis.
176
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
Parameters
1. x: One dimensional array such that it is either the first set of data when x and y is given
or one can use differences between two sets of measurements (d=x-y, as x) [in this case
in place of x values difference is entered and in place of y, none is entered.
2. y: (optional), One dimensional array such that either the second set of data (in case ifx,
the first set of data, is specified), or not specified by mentioning none (in case if x is the
set data of differences between two sets (d=x-y, as x) of data.)
3. zero_method: (optional){‘pratt’, ‘wilcox’, ‘zsplit’}
The following options are available (default is ‘wilcox’):
➢ ‘pratt’: Includes zero-differences in the ranking process, but drops the ranks of the zeros.
➢ ‘wilcox’: Discards all zero-differences, the default.
➢ ‘zsplit’: Includes zero-differences in the ranking process and split the zero rank between
positive and negative ones.
4. correction: (Either True or False), optional. If True, apply continuity correction by
adjusting the Wilcoxon rank statistic by 0.5 towards the mean value when computing the
z-statistic. Default is False.
5. alternative: (optional) {“two-sided”, “greater”, “less” if the alternative hypothesis type
is two-sided, greater than type, less than type respectively}. Default is “two-sided”.
Returns
1. statistic: If alternative is “two-sided”, the sum of the ranks of the differences above or
below zero, whichever is smaller. Otherwise the sum of the ranks of the differences above
zero.
2. p-value: The p-value for the test depending on alternative.
Example 3:
Test scores of a group of 15 high – school students before &after a training programme are
as given below :
Score before 63 75 78 84 58 58 70 76 74 88 74 94 99 79 93
Score after 84 86 75 94 50 95 97 98 72 100 101 98 105 84 90
Use appropriate statistical test at to check if the training has any effect on the test scores.
Hypothesis:
H0 :Mx =My ; H1: Mx< My
X: score before training (Median of corresponding distribution is Mx)
Y: score after training (Median of corresponding distribution is My)
177
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
In [4]: #interpret
alpha = 0.05
if p > alpha:
print ('(Fail to reject H0)')
else:
print('(Reject HO)')
(Reject HO)
Interpretation: Since p-value=0.004< 0.01 indicates that one should reject null hypothesis.
Kruskal–Wallis H tests the significance of difference between the medians of more than two
groups, which may have different sizes. This test is commonly called as Non-parametric
ANOVA or Kruskal-Wallis [Link] the result of the Kruskal-Wallis test is significant, then we
go for post-hoc test. Here we will use Dunn’s test to find which pair of the groups are showing
significance when compared pairwise.
178
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
Parameters
1. sample1, sample2, …array_like two or more arrays with the sample measurements can
be given as arguments.
2. nan_policy{‘propagate’, ‘raise’, ‘omit’}, optional
Defines how to handle when input contains nan. The following options are available (default
is ‘propagate’):
➢ ‘propagate’: returns nan
➢ ‘raise’: throws an error
➢ ‘omit’: performs the calculations ignoring nan values
Returns
1. statistic: The Kruskal-Wallis H statistic, corrected for ties.
2. p-value: The p-value for the test using the assumption that H has a chi square
distribution.
Example 4:
Test whether there exists a significance of difference between the scores of three groups
when compared against each other for the following given data set. Use 5% l. o. s. Also use
post-hoc test to find the exact significance.
Group 1 63 75 78 84 58 58 70 76 74 88 74 94 58 79 93
Group 2 84 86 75 94 50 95 97 98 72 100 101 98 105 84 90
Group 3 74 76 65 84 50 85 97 88 72 90 101 98 115 94 90
Hypothesis:
H0: There is no significance of difference between the median of three groups.
H1: At least one pair of groups differs significantly in their median when compared.
In [1]: #Kruskal-Wallis test for comparison of more than two independent
variables
import numpy as np
import pandas as pd
from scipy import stats
from scipy. stats import kruskal
179
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
In[4]: # interpret
alpha = 0.05
if p > alpha:
print(' (Fail to reject H0)')
else:
print('(Reject H0)')
(Reject HO)
Interpretation: Since p-value =0.015< 0.05 indicates one should reject null hypothesis and
conclude that there exists significance of difference between the scores of three group at 5%
l.o.s.
Syntax: [Link](*args)
It computes the Friedman test for repeated measurements
Parameters
1. measurements1, measurements2, measurements3... : Arrays of measurements. All
of the arrays must have the same number of elements. At least 3 sets of measurements
must be given.
Returns
1. statistic :The test statistic, correcting for ties.
2. p-value :The associated p-value assuming that the test statistic has a chi squared
distribution.
Example 5
The study is planned to measure a stress level on four consecutive days of week. 10
individuals were randomly selected and their stress is measured on Day-1 to Day-4 using a
10 point stress scale in which score 10 indicates highest stress. Test the significance of
difference between the stress levels on four days.
Subject Day-1 Day-2 Day-3 Day-4
1 8 7 6 7
180
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
2 5 8 5 6
3 6 5 3 4
4 6 6 7 3
5 8 10 8 6
6 6 5 6 3
7 6 5 2 3
8 9 9 9 6
9 5 4 3 7
10 7 6 6 5
Hypothesis:
H0: There is no significance of difference between the median scores on four days.
H1: There is significance of difference between the median scores on four days.
In [2]: #enter data of Four time points as Dayl, Day2, Day3 and Day4
Dayl=[Link]([8,5,6,6,8,6,6,9,5,7])
Day2=[Link]([7,8,5,6,10,5,5,9,4,6])
Day3=[Link]([6,5,3,7,8,6,2,9,3,6])
Day4=[Link]([7,6,4,3,6,3,3,6,7,5])
In[4]: # interpret
alpha = 0.05
if p > alpha:
print('(Fail to reject H0)')
else:
print('(Reject H0)')
(Reject HO)
181
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
Interpretation: Since p-value =0.047< 0.05 indicates one should reject null hypothesis and
conclude that There is significance of difference between the median scores on four days at
5% l.o.s.
References
182
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
Case Studies
Case study of Polio in Greater Mumbai
17.1 Introduction
The data we posse analysis the polio cases in Greater Mumbai from 1960 to 1975. We
selected this data set because polio has not only been one of the most infectious and
dangerous diseases but was also the corner stone in research in how we fought diseases like
this. Polio research led to the usage of vaccinations in modern medicine to combat viruses.
Poliomyelitis, often called polio, is an infectious disease caused by the poliovirus. Once
known as Infantile Paralysis, the term "poliomyelitis" is used to identify the disease caused
by any of the three types of poliovirus. Poliomyelitis has existed for thousands of years, with
depictions of the disease in ancient art. The virus that causes it was first identified in 1908.
Three serotypes of poliovirus have been identified—poliovirus type 1 (PV1), type 2 (PV2),
and type 3 (PV3). All three are extremely virulent and produce the same disease
symptoms. PV1 is the most commonly encountered form, and the one most closely
associated with paralysis.
Poliomyelitis is a crippling disease with dramatically visible impact on the patient. Over the
last fifty years the disease has been brought under control by the use of oral vaccine. It is of
considerable interest to identify trend, seasonality and other features of data on incidence
of polio.
Through this data we wish to analysis how this kind of deadly disease spreads and how
world events impact the spread of these diseases.
183
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
17.3 Objectives
17.5 Methods
The first step we took was to clean the data before analysis. Since there was data missing for
1 month for Year 1960 and 11 months for Year 1975, we had to exclude while considering
any yearly based calculations. Since all months had 14 data entries, we didn’t have to clean
any data while monthly calculations.
We utilised mathematical statistics techniques like mean, median, mode, Time Series and
Trend Analysis, graph plotting to analysis the data.
184
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
185
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
In [7]: pd.pivot_table(po,values='cases',index=['Month'],
aggfunc=[Link])round()
186
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
[Link]('Number of Cases')
[Link]("total Number of Cases each Year (1960-1975)")
[Link]()
In[11]: #since there is uneven of cases in year 1975, we will consider you
be better indicator
pd.pivot_table(po, values='cases', index=['Year'], aggfunc
=[Link]) . round()
[Link]()
In[14]: po_year=po['Year']
po_cases=po['Cases']
[Link](po_year, po_cases)
z=[Link](po_year,po_cases, 1)
189
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
p=np.poly1d(z)
[Link](po_year,p(po_year),"r--")
[Link]('Number of Cases')
[Link]('Year')
[Link]("Number of cases per year (1960-1975)")
[Link]()
#This graph shows a increasing trendline for years showing that number of cases
has increased per year
In [15]: corr_plot=[Link]()
corr_plot
190
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
191
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
In [19]:def_chi_test(df,alpha):
from scipy import stats
chisq_output=stats.chis2_contingency(contingency_table)
chisq_output
expected_values=chisq_output[3]
chi_squared_stat=(((observed_values-expected_values)**2)/expected_
values).sum().sum()
print(chi_squared_stat)
print(chisq_output)
print(chisq_output[1])
if chisq_output[1]>alpha:
print("we do not reject HO")
else :
print("we do reject HO")
chi_ind_test(po,0.05)
In[20]: # we can not use chisquare result since frequencies are very small
17.7 Conclusion
We observe that polio cases were rising faster than compared to the population. Polio Cases
observed a seasonal trend, seeing sharp increase during rainy seasons and seeing sharp
decrease in summer seasons.
192
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
World events like war cause sharp increase in polio cases. This shows lack of proper health
facilities for emergencies. Similarly, polio outbreak in other countries causes sharp increase
in polio cases in India too. Thus we fail to prevent entry of diseases from other containment
countries. We lack facilities to prevent entry of infected personnel’s Polio cases increased in
period 1960 to 1975.
17.8 Limitations
The data is from a secondary source. It is unclear whether the rise in case is because of
increase in people getting infected with polio or due to better methods to detect polio.
We assume that the polio cases in a month or year where infected in the same month or year.
We assume that there is no gap between when a person was infected and when the infection
was detected. Their might exist a latent period between when a person is infected, when the
infection is detected and when the case has been recorded.
17.9 References
193
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
Chapter 18
Analysis of Air Pollution in New Delhi
18.1 Abstract
There is increasingly growing evidence linking urban air pollution to acute and chronic
illnesses amongst all age groups. Therefore, monitoring of ambient concentrations of various
air pollutants as well as quantification of the dose inhaled becomes quite important,
especially in view of the fact that in many countries, policy decisions for reducing pollutant
concentrations are mainly taken on the basis of their health impacts. The dose when gets
combined with the likely responses, indicates the ultimate health risk (HR). Thus, as an
extension of our earlier studies, HR has been estimated for three pollutants, namely,
suspended particulate matter (SPM), nitrogen dioxide (NO2) and sulfur dioxide (SO2) for
Delhi City in India. For estimation and analyses, three zones have been considered, viz.,
residential, industrial and commercial.
The total population has been divided into three age classes (infants, children and adults)
with different body weights and breathing rates. The exercise takes into account age-specific
breathing rates, body weights for different age categories and occupancy factors for different
zones. Results indicate that health risks due to air pollution in Delhi are highest for children.
For all age categories, health risks due to SO2 (HR_SO2) are the lowest. Hence, HR_SO2 has
been taken as the reference with respect to which HR values due to SPM and NO2 have been
compared. Taking into account all the age categories and their occupancy in different zones,
average HR values for NO2 and SPM turn out to be respectively 22.11 and 16.13 times more
than that for SO2. The present study can be useful in generating public awareness as well as
in averting and mitigating the health risks.
18.2 Data
18.3 Effects
The most important reason for concern over the worsening air pollution in the country is its
effect on the health of individuals. Exposure to particulate matter for a long time can lead to
respiratory and cardiovascular diseases such as asthma, bronchitis, COPD, lung cancer and
heart attack. The Global Burden of Disease Study for 2010, published in 2013, had found that
outdoor air pollution was the fifth-largest killer in India and around 620,000 early deaths
occurred from air pollution-related diseases in 2010. According to a WHO study, 13 of the
20 most-polluted cities in the world are in India; however, the accuracy and methodology of
the WHO study was questioned by the Government of India. India also has one of the highest
number of COPD patients and the highest number of deaths due to COPD.
Over a million Indians die prematurely every year due to air pollution, according to the non-
profit Health Effects Institute. Over two million children—half the children in Delhi—have
abnormalities in their lung function, according to the Delhi Heart and Lung Institute. Over
the past decade air pollution has increased in India significantly. Asthma is the most common
195
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
health problem faced by Indians and it accounts for more than half of the health issues
caused by air pollution.
Ambient air pollution in India is estimated to cause 670,00 deaths annually and particularly
aggravates respiratory and cardiovascular conditions including chronic bronchitis, lung
cancer and asthma. Ambient air pollution is linked to an increase in hospital visits, with a
higher concentration of outdoor pollution particulates resulting in emergency room visit
increases of between 20-25% for a range of conditions associated with higher exposure to
air pollution. Approximately 76% of households in rural India are reliant on solid biomass
for cooking purposes which contributes further to the disease burden of ambient air
pollution experienced by the population of India.
State-Wide Trends:
According to the WHO, India has 14 out of the 15 most polluted cities in the world in terms
of PM 2.5 concentrations.
Air Quality Index (AQI) is a number used to communicate the level of pollution in the air and
it essentially tells you the level of pollution in the air in a given city on a given day. The AQI
of Delhi was placed under the "severe-plus category" when it touched 574, by the System of
Air Quality and Weather Forecasting and Research. In May 2014, the World Health
Organization announced New Delhi as the most polluted city in the world. In November
2016, the Great smog of Delhi was an environmental event which saw New Delhi and
adjoining areas in a dense blanket of smog, which was the worst in 17 years.
A surge on June 14 was caused by dust storms brought on by a combination of extreme heat
and powerful downdraft winds.
Hazardous
Very Unhealthy
Unhealthy
Unhealthy for Sensitive Groups
196
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
Moderate
Good
➢ The average annual SOx and NOx emissions level and periodic violations in industrial
areas of India were significantly and surprisingly lower than the emission and violations
in residential areas of India.
➢ Of the four major Indian cities, air pollution was consistently worse in Delhi, every year
over 5-year period (2004–2018). Kolkata was a close second, followed
by Mumbai. Chennai air pollution was least of the four.
Steps Taken:
➢ The government in Delhi launched an Odd-Even Rule in November, 2017 which is based
on the Odd-Even rationing method: This meant that cars running with number plates
ending in Odd digits could only be driven on certain days of the week, while the even digit
cars could be driven on the remaining days of the week.
➢ The Indian government has committed to a 50% reduction in households using solid fuel
for cooking
➢ Some goals set for future are:
• Clean up the transportation sector by introducing 1,000 electric public transport buses
to its 5, 50-string fee.
• Identify effective ways to inform the public about air pollution data.
• Launch new citizen science programs to better document exposures.
• Reduce Carbon Emissions: "According to Inter-governmental Panel on Climate Change,
to limit warming well below 2 degree Celsius, CO2 emissions should decline by about 20
per cent by 2030 and reach net zero around 2075; to limit warming below 1.5 degree
Celsius, CO2 emissions should decline by 50 per cent by 2030 and reach net zero by
around 2050.
18.4 Analysis
We use “dataset (3) (1).csv”. We began by importing the libraries we are going to need.
197
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
[Link]()
It returns range, column, number of non-null objects of each column, datatype and memory
usage.
198
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
[Link]()
It results in a number of non null values in each column.
[Link]()
Generate descriptive statistics that summarize the central tendency, dispersion, and shape
of a dataset’s distribution, excluding NaN values.
[Link]
It returns a number of rows and columns in a dataset.
[Link]().sum()
It returns a number of null values in each column.
199
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
200
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
The columns such as so2, no2, rspm, spm, pm2_5 are the ones which contribute much to our
analysis. So, we need to remove null from those columns to avoid inaccuracy in the
prediction. We use the Imputer from [Link] to fill the missing values in every
column with the mean.
201
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
In [15]: # checking to see if the dataset has any null values left over
and the format
print ([Link]().sum())
so2 status:
A collection of estimates of past and future anthropogenic global so2 emissions. The cofala
et al. estimates are for sensitivity studies on so2 emission policies. Those most at risk of
developing problems if they are exposed to so2 are people with asthama or similar
conditions.
202
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
The plot shows the states with highest so2 levels in ascending order. We can see that
Uttaranchal has the highest so2 concentration. Nagaland has the least concentrations of so2
among the states.
In [18]: [Link]()
cols=['so2', 'no2', 'rspm', 'spm', 'pm2_5']
[Link](df[cols], size = 2.5)
[Link]()
203
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
The relationship between pm2_5 and any other feature is simply because pm2_5 has tons of
null values. So2 and no2 values are highly concentrated near to the origin. spm and rspm
share somewhat linear relationship, rest all features are not entirely related.
Correlation matrix:
In [19]: corrmat=[Link]()
f, ax [Link](figsize= (15,10))
[Link](corrmat,vmax = 1,square = True, annot = True)
204
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
It is clear from the correlation matrix that we have some correlation between spm and rspm,
which supports our scatter plot analysis.
By applying linear regression we can take multiple X’s and predict the corresponding Y
values.
print_model = [Link]()
print(print_model)
In the graph, the dots are the true data and the line is linear model. The plot shows that the
data is highly positively skewed.
206
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
In [29]: x=(df['no2'])
print(x)
In [31]: print(stat)
0.7809625864028931
In [32] print(p)
0.0
In [33]: alpha=O.05
if p>alpha:
print('sample looks Guass±an (fail to reject H0)')
else:
print('sample does not look Guass±an (reject H0)')
The p-value returned is less than 0.05 and finds that the data is not likely drawn from a
Gaussian distribution (normal distribution).
207
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
The histogram plot shows the number of observations in each bin. We cannot see a Gaussian-
like shape to the data.
In [37]: qqplot(x,line='s')
[Link]()
The QQ plot shows the scatter plot of points is not in a diagonal line, closely fitting the pattern
for a sample from a positively skewed distribution.
208
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
There has been a gradual increase of so2 concentration in Bihar from 1987 to 1999. The
presence of so2has been high from 1980 to 2000 in some states but has decreased in the new
country (from 2000).
209
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
We have created a new dataframe containing the NO2, SO2, rspm, and spm data regarding
state ‘Andhra Pradesh’ only and group it by ‘year’.
In [26]: [Link](year_wise_AP['so2],'-sc',markersize = 3)
[Link](year_wise_AP['so2],'-or', markersize = 3)
[Link]()
210
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
In [27]: [Link](year_wise_AP['rspm],'-ob',markersize = 3)
[Link](year_wise_AP['spm],'-om', markersize = 3)
[Link]()
This gave an alarming signal that the value spm in Andhra Pradesh is hiking. It’s 220 µg/m3
for the past 6 years (2010–2015).
Result:
Based on the various data visualization and analysis done it can be evaluated that most of
the pollutants vary within acceptable limits and do not have significant correlation amongst
other pollutants. However PM10 value is quite high for most of the months. As these readings
are for the Kadubeesanahalli sensor the reason for this excess PM10 level might be due to
high vehicular density in the vicinity. Along with that, other factors such as wind speed, road
repairs, building construction that result in high dust particles might also have some impact
on the variation in pollutant levels.
211
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
18.5 Conclusion
Based on data study and research carried out for the air pollutant dataset collected by PAQS
sensor device it can be inferred that the pollutant levels of some harmful particulate matters
such as PM10 are quite high in the air. One of the key sources for this seems to be the high
vehicle density in the location where this data was collected. As the pollutants are mostly
varying in a constant manner throughout the day the approximate pollutant levels can be
predicted for the next day for a particular temperature and relative humidity factor. Also
based on the observation it can be predicted that the pollutant levels vary based on the
vehicle density i.e. during peak hours it is quite high. Also, air pollutants are influenced by
the seasonal changes. In monsoon, the pollutant levels are low when compared to winter and
spring seasons where it is high and again in summer the pollutant levels go down.
18.6 References
1. [Link]
2. [Link]
3. Training Programme notes conducted by Pravesh Tiwari
212
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
Chapter 19
Analyzing Happiness Development Index using
Python
Mr. Satvik Tandon, Mr. Alpesh Rathod and Mr. Gaurav Jadhav
DBT- Star College Status Scheme Researchers, Department of Statistics
19.1 Abstract
The data I have is of HDI for years 2015, 2016 and 2017. The data consists of 6 indicators. I
have done analysis on the date using Python Software. We have analyze the data to find the
best country in each category, find the country with biggest gain, mean changes, correlation
between various factors and created a model to predict Happiness Score and find on what
factors does Happiness Score depend on. In this report I am going to share my findings and
give interpretations. Furthermore, I have added variable Ranks for each year to analyze how
many countries people are happy or not.
The data is for Happiness Index for years 2015, 2016 and 2017 for 158, 157 and 155
countries respectively. The variables in this dataset are:
1. Country – The country for which the values belong.
2. Region – The region to which the country it belongs to.
3. Happiness Score – The happiness Score the country had.
4. Economy (GDP per Capita) – Economy score of the country.
5. Family – The Family score of the country.
6. Health (Life Expectancy) – The health score of the country.
213
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
We have also added the variable ‘Ranks’ to the dataset. Ranks is a categorical variable telling
whether people of a country are happy, okay or sad according to the happiness score.
19.3 Objectives
19.4 Methods
We utilized statistical techniques like linear regression modeling, measures of central
tendency, graphical illustrations, statistical tests like chi square and z proportion test, etc.
We created our own model using ordinary least square and tested by dividing our data into
2 parts. Python concepts like modeling, statistical testing, graphical representations,
refactoring, creating and calling functions, etc.
19.5 Analysis
Importing libraries:
We are using “[Link]”, “[Link]” and “[Link]”.
We began by importing the libraries we are going to need.
214
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
Importing Dataset:
Then we are going to load the datafiles. Since we have ranks given for year 2015 and 2016
in columns 2, we will declare those columns as index columns for respective datasets.
In [2]: [Link]("C:/Users/Ädmin/Desktop")
y2015 = pd.read_csv("C:/Users/Admin/Desktcp/[Link]", index col =2)
y2016 = pd.read_csv("C:/Users/Admin/Desktop/[Link]", index col =2)
y2017 = pd.read_csv("C:/Users/Admin/Desktop/[Link]")
Using info () to find brief summary of the data. We will start by seeing a brief overview of
our data using formula [Link]().
215
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
However, each data set has unequal number of countries in it. We will find out those
countries which are available in all data set.
In [5]: # =================================================================
#The below code check vhether a country which exists in 2015 also exists in
#2016 and 2017
#The above code thus checks whether a country in all three database
# =========================================================================
same_countries = 0
#same countries will store number of similiar countries
same_countries_list = [ ]
# same_countries_list contains names of similiar countries
for x in [Link](y2015['Country']) :
if x in [Link](y2016['Country']):
if x in [Link](y2017['Country']) :
same_countries = same_countries+l
same_countries [Link] (x)
print (same_countries)
print (same_countries_list
216
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
There are 146 countries similar in all dataset. The list of those countries is given above.
Because of uneven number of countries and the data belonging to three different years, I will
not combine the dataset into one for analysis but rather keep it separately while conducting
analysis. Without knowing how the countries affect the dataset and analysis, it wouldn’t be
right to discard the data points. So, to maintain fair analysis the dataset is kept separated.
The above function will take the three datasets as input and then print the best country in
various categories for each year. By creating a function not only does the memory required
decrease but it also makes executing the same set of codes for three different countries much
easier. This also allows us to add new datasets without having to perform cumbersome code
additions. We will call the function using command best survey (DataFrame).
217
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
max_n_change_country = ""
# Stores the name of the country Vith maximum positive change
max_n_change_score = 0
# Stores the most positive change in score
if [Link][x, 0] == [Link][y,0]:
# Checks the data is for the sane country
change_scores.append([Link][y,2] — [Link][x,2]
# Store the changed value
if ([Link][y,2] - [Link][x, 2]) > max_p_change_score:
max_p_change_score =[Link][y,2] - [Link][x, 2]
# This variable is to calculate the biggest improvement
max_p_change_country = [Link][y,O]
elif ([Link][y,2]—[Link][x, 2] < max_n_change score:
max_n_change_score = [Link][y,2] -[Link][x,2]
# This variable is to calculate the biggest downfall
max_n_change_country = [Link][y,0]
In [19]:print("2016:")
[Link](y2016['Region'],columns = 'Country',dropna = True)
In [20]:print ("2017:")
[Link](y2017 ['Region'],columns = 'Country',dropna = True)
219
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
We will divide the countries into different categories (ranks) according to happiness score.
This will allow also performing better analysis and understanding which country’s citizens
are happy, okay or sad. The code used for this is:
220
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
In [21]: def_dividing_category(df) :
# we will divide the scores into different categories and add those categories
into each database
# More than 7 indicates Happy, between 7 and 5 indicates Okay and less than 5
indicates Sad
Checking how many people are Happy, Okay and Sad for each year:
We will now see how many countries fall in each category.
In [23]: print("2015:")
[Link](y2015['Ranks], columns = 'count')
In [25]: print("2016:")
[Link](y2016['Ranks], columns = 'count')
In [26]: print("2017:")
[Link](y2017['Ranks], columns = 'count')
By looking at the results we can see that the number of happy, okay and sad nations have not
changed over time. Its laregly been the same. We will check this later through Hypothesis
testing.
Checking how many people are Happy, Okay and Sad in each Region for every year:
Next we will look at how many happy, okay and sad countries exist in each region. We will
also graph this to better understand the results.
221
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
In [28]:print("2015:")
[Link](y2015['Region'],y2015['Ranks']
In [29]:print("2016:")
[Link](y2016['Region'],y2016['Ranks']
In [30]:print("2017:")
[Link](y2017['Region'],y2017['Ranks']
By looking at this data we can see that certain region has no happy countries and certain
regions have no sad countries. This can be better understood through bar plot graphs.
We will plot average happiness score for each region for all year. The codes used for this are:
In [31]: def region_rank_plot(df,year):
[Link]( Region' , data = df, hue = 'Ranks')
222
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
[Link](rotation = 90)
[Link]('Region')
[Link]('Number of Nations in each Category')
[Link]("Region and Ranks ({})".format(year))
223
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
For 2015, 2016 and 2017, it is observed that happy countries are seen only in Western
Europe, North America, Australia and New Zealand, Middle East and Latin America. All of
which have either developed nation or nation with a lot of wealth. Most happy countries are
in Western Europe. On the other hand, majority of sad countries are present in Sub-Sahara
Africa. The countries in Sub-Sahara Africa are under developed countries and the area also
prone to civil wars. This has caused these nations to be unhappy. The proportion of nation
in this region is sad. Regions with developing countries like Central and Eastern Europe,
Latin America and Western Europe sees most people being okay, i.e., neither happy nor sad.
North America, Australia and New Zealand only have happy countries. But we must also
remember that these regions have very few nations. For 2016 and 2017, Western Europe no
longer has any sad countries in it.
Testing whether Proportion of Happy, Okay and Sad people have changed:
We will conduct a hypothesis test to understand whether the proportion of happy, sad and
okay families has changed or not. Level of Significance is 1%. The code for this is:
224
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
2015 to 2016
H01: Proportion of Happy Countries has remained unchanged, against
H11: Proportion of Happy Countries has changed.
H02: Proportion of Sad Countries has remained unchanged, against
H12: Proportion of Sad Countries has changed.
H03: Proportion of Okay Countries has remained unchanged, against
H13: Proportion of Okay Countries has changed.
Conclusion: We can thus conclude from the test that proportion of happy countries, sad
countries and okay countries have not changed from 2015 to 2016.
2016 to 2017
H01: Proportion of Happy Countries has remained unchanged, against
H11: Proportion of Happy Countries has changed.
H02: Proportion of Sad Countries has remained unchanged, against
H12: Proportion of Sad Countries has changed.
H03: Proportion of Okay Countries has remained unchanged, against
H13: Proportion of Okay Countries has changed.
225
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
we do not reject HO
Conclusion: We can thus conclude from the test that proportion of happy countries, sad
countries and okay countries have not changed from 2016 to 2017.
2015 to 2017
H01: Proportion of Happy Countries has remained unchanged, against
H11: Proportion of Happy Countries has changed.
H02: Proportion of Sad Countries has remained unchanged, against
H12: Proportion of Sad Countries has changed.
H03: Proportion of Okay Countries has remained unchanged, against
H13: Proportion of Okay Countries has changed.
In [43]: proportion_test(g15, (g15+a15+b15), g17/(g17+b17+a17))
we do not reject HO
Conclusion: We can thus conclude from the test that proportion of happy countries, sad
countries and okay countries have not changed from 2015 to 2017.
A combined dataset was created. This was done using the formula
In [46]: combined_dataset = [Link]()
combined_dataset =combined_dataset.append(y2016, sort = False)
combined_dataset =combined_dataset.append(y2017, sort = False)
Testing whether Happiness is dependent on the Region in which the Country belongs
to:
Since countries in the same region have political, geographically and cultural similarity, so
logically speaking happiness of country should depend the region in which the country
belongs. Country in the same regions should have similar amount of happiness. We will
check this using Chi Square test.
H0: Happiness of people in a country is independent of the Region the country belongs to.
H1: Happiness of people in a country is not independent of the Region the country belongs
to.
Level of significance: 1%
Decision Criteria: If p- value is less than level of signifiance or chi square calculated is more
than chi square tabulated, we will reject H0.
226
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
print(chi_squared—stat)
print(chisq_output)
print(chisq_output[1])
if chisq_output[1]>alpha:
print("We do not reject HO")
else :
print("We do reject HO")
In[54]:chi_ind_test(combined_dataset,alpha = 0.01)
Conclusion: We can thus conclude that happiness of people in a country is dependent on the
Region the country belongs to.
Limitations: Since some of the expected frequencies is less than 5, we cannot fully trust the
chi square results.
Correlation Plotting:
We will check the correlation between the models through pairwise scatter plot and
correlation plot of the combined dataset using matlibplot and seaborn libraries.
Plotting Scatter Plot of different Factors
227
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
[Link]("Scores")
[Link]("pairwise Scatter plot")
[Link](loc='upper left')
228
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
229
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
Its clear from the above model that happiness of country is highly correlated to Economic
and Health conditions and weakly correlated to Generosity of people in the nation and the
people’s trust on the government.
Predictive Modeling:
Dividing the dataset into Test and Train
The following code will create a copy of data frame of Combined Dataset in a randomized
order. The new dataset is divided into 2 parts train and test. 70% of data points are in train
and 30% in test.
In[55]: # We Will store a randomized order in a new dataframe
comb = combined_dataset.sample(frac=l).reset_index(drop=True)
# I have divided it into 2 parts
# 70% in test and 30% in train
train = comb [O: 329]
test = comb [329:]
230
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
print_model = [Link]()
print(print_model)
The model accuracy is given by Adjusted R Squared. The accuracy of the model is given as
98.1%. Variables for which P > |t| is more than 0.05 are not significant and are rejected.
Happiness is not dependent on them. Since Trust has P value 0.145, Trust does not have a
significant effect on Happiness. Happiness is dependent on Economy, Family, Health,
Freedom and Generosity.
We will create a new model after not considering Trust has a dependent variable.
In [57]: # We first import the libraries needed
from sklearn import datasets, linear_model
# Our dependent variable is Happiness Score
231
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
print_model = [Link]()
print(print_model)
The accuracy of the model is given as 98.1%. Variables for which P > |t| is more than 0.05 are
not significant and are rejected. Happiness is not dependent on them. Happiness is
dependent on Economy, Family, Health, Freedom and Generosity.
Since this model has a similar accuracy to the previous model and variables in this model are
significant variables, we will accept this model.
232
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
predictions = [Link](x_test)
[Link](y_test, predictions)
[Link]("True Values")
[Link]("Predictions")
[Link]("Pridiction V/S True Values")
alpha = 0.05
if pval > alpha:
print("Error terms follow a noraml distribution")
else :
print ( "Error terms do not follow a normal distribution")
233
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
19.6 Conclusion
We were able to obtain best countries in all of the six categories for all the three years.
Switzerland, Denmark and Norway were the happiest country in 2015, 2016 and 2017
respectively. Other results can be seen on page 6.
From year 2015 to 2016, Algeria saw the biggest improvement whereas Liberia saw the
biggest downfall; from year 2016 to 2017, Bulgaria saw the biggest improvement whereas
Venezuela saw the biggest downfall and for year 2015 to 2017, Latvia saw the biggest
downfall and Venezuela saw the biggest downfall.
Venezuela saw the biggest downfall and most change in score, this is because from 2016,
Venezuela's economy collapsed and the country saw huge riots. Currently Venezuela is in an
economic crisis. This led to such a huge downfall in its score.
At an average 15 nations are happy, 83 are okay and 60 are sad. Western Europe is the
happiest region and Sub Sahara Region is the saddest.
Happiness of a country is dependent on the region in which the country belongs to. This may
be because countries in the same region tend to have similar political, social and economical
conditions.
The proportion of happy, sad and okay countries has not changed in the 3 year time period.
Happiness is highly correlated to Economy and Health and weakly correlated to Generosity.
We were also able to construct a model for predicting happiness score:
𝐻𝑎𝑝𝑝𝑖𝑛𝑒𝑠𝑠 𝑆𝑐𝑜𝑟𝑒 = 1.1196 × 𝐸𝑐𝑜𝑛𝑜𝑚𝑦 + 1.6687 × 𝐹𝑎𝑚𝑖𝑙𝑦 + 1.6127 × 𝐻𝑒𝑎𝑙𝑡ℎ
+2.8593 × 𝐹𝑟𝑒𝑒𝑑𝑜𝑚 + 1.8650 × 𝐺𝑒𝑛𝑒𝑟𝑜𝑠𝑖𝑡𝑦
19.7 Limitation
The data we have is for only 3 years. Such a short period will not provide adequate answers.
A long period would have given us a better idea. While frequency testing some of the
frequencies was less than 5, because of this the results we obtained cannot be fully tested.
Only 146 countries data are available in all 3 dataset. This leads to inadequate results will
finding the countries with biggest change since data for some countries are missing to do
proper comparison.
The dataset is for a preference survey. Preference surveys do not always provide the right
results. A country's economic conditions may be bad but through propaganda people might
be influenced to give better results. Thus, propaganda might have influenced the survey
results.
234
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
The data is secondary and thus we have no idea how this survey was conducted and data
collected. The dataset does not consider difference in happiness and results due to gender,
religion, sexuality, etc. A particular group may be happier compared to another. It does not
consider cultural impact, entertainment impact, etc. on happiness.
19.8 Acknowledgment
We acknowledge our Principal Dr. Hemalata Bagla of K. C. College for encouraging Project
Work and Research. We sincerely acknowledge Dr. Asha Jindal, Head, Department of
Statistics, K. C. College for organizing the Python Software Schem Training with DBT- Star
College Status Scheme Fund in which we could acquire the skills to complete this project. We
would like to especially acknowledge Mr. Pravesh Tiwari for teaching us Python Software
effectively in short span of time.
19.9 References
1. [Link]
80b61beca4b6
2. [Link]
visualizations-f1c49c816f07
3. Training Programme material provided by Pravesh Tiwari Sir
235
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
Chapter 20
Analysis of Diabetes
20.1 Introduction
Diabetes is a very common metabolic disease. Usually onset of type 2 diabetes happens in
middle age and sometimes in old age. But nowadays incidences of this disease are reported
in children as well. There are several factors for developing diabetes like genetic
susceptibility, body weight, food habit and sedentary lifestyle. Undiagnosed diabetes may
result in very high blood sugar level referred as hyperglycemia which can lead to
complication like diabetic retinopathy, nephropathy, neuropathy, cardiac stroke and foot
ulcer. So, early detection of diabetes is very important to improve quality of life of patients
and enhancement of their life expectancy
In today’s competitive world talented people are the most worthwhile treasure for the
company and at the same time burdensome to hold down such valuable resources in
organization. During last year’s, large investments were put into tools and information
systems to manage performance, hiring, compliance and employees’ development in order
to enhance its capabilities.
The learning process starts with the gathering of data by different means, from various
resources. Then the next step is to prepare the data, that is pre-process it in order to fix the
data related issues and to reduce the dimensionality of the space by removing the irrelevant
data (or selecting the data of interest). Since the amount of data that is being used for
learning is large, it is difficult for the system to make decisions, so algorithms are designed
using some logic, probability, statistics, control theory etc. to analyse the data and retrieve
the knowledge from the past experiences. Next step is testing the model to calculate the
accuracy and performance of the system. And finally optimization of the
system, i.e. improvising the model by using new rules or data set. The techniques of machine
learning are used for classification, prediction and pattern recognition. Machine learning can
be applied in various areas like: search engine, web page ranking, email filtering, face tagging
and recognizing, related advertisements, character recognition, gaming, robotics, disease
prediction and traffic management The biggest struggles in achieving better utilization of
data resources and information systems are inefficient use of the data, asking wrong
236
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
1. The goal of the case study is to find out which are the most influential factors leading to
diabetes.
2. Which person will get diabetes next?
20.4 Methodology
1. Visualization: - The first step is to visualize and perform univariate analysis to explore
data to find useful insights.
2. Model: - Next step is to model the data in order to confirm or reject our hypothesis that
certain variables are significant in determining person diabetes.
3. Actionable Insights: - The final step is to review and build onto our analysis by drawing
new insights or further enhancing existing insights.
237
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
In [4]: [Link]()
From the structure of the data we can see that sample size of the data is 768 and there are 9
variables and also whether a variable is categorical or continuous.
SkinThickness 0
Insulin 0
BNI 0
DiabetesPedigreeFunction 0
Age 0
Outcome 0
dtype: int64
In [8]: [Link]().value_counts()
Out [8]: Fales 768
dtype: int64
In [10]:#unvivarite analyasis
[Link](figsize=(10,6))
[Link](diab['Pregancies'],kde=False,bins=50)
[Link]('Pregancies per Person on Pima People',fontsize=18)
[Link](fontsize=13)
[Link](fontsize=13)
[Link]('Pregnancies'fontsize=15)
[Link]('Number of people',fontsize=15)
[Link]()
239
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
Predictive Modeling:
Logistic Regression
Logistic Regression is a classification algorithm. It is used to predict a binary outcome (1 / 0,
Yes / No, True / False) given a set of independent variables. In simple words, it predicts the
probability of occurrence of an event by fitting data to a logit function. Logistic regression is
241
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
used to describe data and to explain the relationship between one dependent binary variable
and one or more nominal, ordinal, interval or ratio-level independent variables.
The logistic regression model
ln[p/(1-p)] = a + BX + e or
[p/(1-p)] = exp(a + BX + e)
where,
ln is the natural logarithm, logexp, where exp=2.71828…
p is the probability that the event Y occurs, p(Y=1)
p/(1-p) is the "odds ratio"
ln[p/(1-p)] is the log odds ratio, or "logit"
All other components of the model are the same.
The logistic regression model is simply a non-linear transformation of the linear regression.
The "logistic" distribution is an S-shaped distribution function which is similar to the
standard-normal distribution (which results in a probit regression model) but easier to work
with in most applications (the probabilities are easier to calculate). The logit distribution
constrains the estimated probabilities to lie between 0 and 1.
For instance, the estimated probability is:
p = 1/[1 + exp(-a - BX)]
With this functional form:
if you let a + BX =0, then p = .50
as a + BX gets really big, p approaches 1
as a + BX gets really small, p approaches 0.
Data splitting
Separating data into training and testing sets is an important part of evaluating data mining
models. Typically, when you separate a data set into a training set and testing set, most of
the data is used for training, and a smaller portion of the data is used for testing. Analysis
Services randomly samples the data to help ensure that the testing and training sets are
similar. By using similar data for training and testing, you can minimize the effects of data
discrepancies and better understand the characteristics of the model.
After a model has been processed by using the training set, you test the model by making
predictions against the test set. Because the data in the testing set already contains known
values for the attribute that you want to predict, it is easy to determine whether the model's
guesses are correct.
Dependent Variable for Modeling: (Whether the person is diabetic or not in future)
Independent Variables: Pregnancies, Glucose, BloodPressure, Insulin, BMI,
DiabetesPedigreeFunction, Age
242
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
In [17]: x = [Link](['Outcome'],axis=1)
y=diab['Outcome']
In [39]: print(x_train.columns[rfe.support_])
x_train_rfe = x_train[x_train.columns[rfe.support_]]
x_train_rfe = sm.add_constant(x_train_rfe)
Index(['Pregnancies','Glucose','BMI','DiabetesPedigreePunction'],dty
pe='object'
243
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
Test Statistic:
χ2 = L1 – L2 which follows Chi-square distribution with k df.
L1 = –2 log L with only constant term L2 = –2 log L with k variables and constant term
Reject H0 for large value of χ2 or Reject H0 if p-value < 0.05
Since p-value is less than 0.05 we reject H0 and conclude that the variable is making impact
on dependent variable.
244
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
logit_roc_auc = roc_auc_score(y_test,
logreg.predict_proba(x_test_rfe)[:,1])
[Link](),
[Link](fpr, tpr, label= 'Logistic Regression (area = %0.2f)'
%logit_roc_auc)
[Link]([0,1],[0,1],'r--')
[Link]([0.0, 1.0])
[Link]([0.0,1.05])
[Link]('False Positive Rate')
[Link]('True Positive Rate')
[Link]('Receiver operating charateristic')
[Link](loc="lower right")
#[Link]('Log_ROC')
[Link]()
Area inside the curve which indicates accuracy of the model is 74%.
20.7 References
1. [Link]
2. [Link]
3. Training Programme notes conducted by Pravesh Tiwari
245
ISBN 978-93-80788-93-7
Analyzing and Visualizing Data using free open-source software – Python Programming with case studies
ISBN 978-93-80788-93-7
View publication stats