Python For Data Analysis
Python For Data Analysis
Descriptive statistics
Inferential statistics
2
Python Libraries for Data Science
Many popular Python toolboxes/libraries:
• NumPy
• SciPy
• Pandas
• SciKit-Learn
• TensorFlow
• Keras
• PyTorch
Visualization libraries
• matplotlib
• Seaborn
3
Python Libraries for Data Science
NumPy:
introduces objects for multidimensional arrays and matrices, as well as
functions that allow to easily perform advanced mathematical and statistical
operations on those objects
Link: http://www.numpy.org/
4
Python Libraries for Data Science
SciPy:
collection of algorithms for linear algebra, differential equations, numerical
integration, optimization, statistics and more
built on NumPy
Link: https://www.scipy.org/scipylib/
5
Python Libraries for Data Science
Pandas:
adds data structures and tools designed to work with table-like data (similar
to Series and Data Frames in R)
Link: http://pandas.pydata.org/
6
Python Libraries for Data Science
SciKit-Learn:
provides machine learning algorithms: classification, regression, clustering,
model validation etc.
Link: http://scikit-learn.org/
7
TensorFlow
Features:
• Better computational graph visualizations
• Reduces error by 50 to 60 percent in neural machine learning
• Parallel computing to execute complex models
• Seamless library management backed by Google
• Quicker updates and frequent new releases to provide you with the latest features
• TensorFlow is particularly useful for the following applications:
• Speech and image recognition
• Text-based applications
• Time-series analysis
• Video detection
• Link : https://www.tensorflow.org
8
Keras
• Features:
• Keras provides a vast prelabeled datasets which can be used to directly import and load.
• It contains various implemented layers and parameters that can be used for
construction, configuration, training, and evaluation of neural networks
• Applications:
• One of the most significant applications of Keras are the deep learning models that are
available with their pretrained weights. You can use these models directly to make
predictions or extract its features without creating or training your own new model.
• Link https://keras.io
9
Python Libraries for Data Science
matplotlib:
python 2D plotting library which produces publication quality figures in a
variety of hardcopy formats
10
Python Libraries for Data Science
Seaborn:
based on matplotlib
Link: https://seaborn.pydata.org/
11
Start Jupyter notebook
# On the Shared Computing Cluster
[scc1 ~] jupyter notebook
12
Loading Python Libraries
In [ ]: #Import Python Libraries
import numpy as np
import scipy as sp
import pandas as pd
import matplotlib as mpl
import seaborn as sns
13
Reading data using pandas
In [ ]: #Read csv file
df = pd.read_csv("http://rcs.bu.edu/examples/python/data_analysis/Salaries.csv")
Note: The above command has many optional arguments to fine-tune the data import process.
pd.read_excel('myfile.xlsx',sheet_name='Sheet1', index_col=None,
na_values=['NA'])
pd.read_stata('myfile.dta')
pd.read_sas('myfile.sas7bdat')
pd.read_hdf('myfile.h5','df')
14
Exploring data frames
In [3]: #List first 5 records
df.head()
Out[3]:
15
Hands-on exercises
Can you guess how to view the last few records; Hint:
16
Data Frame data types
Pandas Type Native Python Type Description
object string The most general dtype. Will be
assigned to your column if column
has mixed types (numbers and
strings).
datetime64, timedelta[ns] N/A (but see the datetime module Values meant to hold time data.
in Python’s standard library) Look into these for time series
experiments.
17
Data Frame data types
In [4]: #Check a particular column type
df['salary'].dtype
Out[4]: dtype('int64')
df.attribute description
dtypes list the types of the columns
columns list the column names
axes list the row labels and column names
ndim number of dimensions
19
Hands-on exercises
20
Data Frames methods
Unlike attributes, python methods have parenthesis.
All attributes and methods can be listed with a dir() function: dir(df)
df.method() description
head( [n] ), tail( [n] ) first/last n rows
What are the mean values of the first 50 records in the dataset? Hint: use
head() method to subset the first 50 records and then calculate the mean
22
Selecting a column in a Data Frame
Method 1: Subset the data frame using column name:
df['sex']
Note: there is an attribute rank for pandas data frames, so to select a column with a name
"rank" we should use method 1.
23
Hands-on exercises
Find how many values in the salary column (use count method);
sal2
24
Data Frames groupby method
Using "group by" method we can:
• Split the data into groups based on some criteria
• Calculate statistics (or apply a function) to each group
• Similar to dplyr() function in R
In [ ]: #Group data using rank
df_rank = df.groupby(['rank'])
In [ ]: #Calculate mean value for each numeric column per each group
df_rank.mean()
25
Data Frames groupby method
Once groupby object is create we can calculate various statistics for each group:
In [ ]: #Calculate mean salary for each professor rank:
df.groupby('rank')[['salary']].mean()
Note: If single brackets are used to specify the column (e.g. salary), then the output is Pandas Series object.
When double brackets are used the output is a Data Frame
26
Data Frames groupby method
27
Data Frame: filtering
To subset the data we can apply Boolean indexing. This indexing is commonly
known as a filter. For example if we want to subset the rows in which the salary
value is greater than $120K:
In [ ]: #Calculate mean salary for each professor rank:
df_sub = df[ df['salary'] > 120000 ]
29
Data Frames: Slicing
When selecting one column, it is possible to use single set of brackets, but the
resulting object will be a Series (not a DataFrame):
In [ ]: #Select column salary:
df['salary']
When we need to select more than one column and/or make the output to be a
DataFrame, we should use double brackets:
In [ ]: #Select column salary:
df[['rank','salary']]
30
Data Frames: Selecting rows
If we need to select a range of rows, we can specify the range using ":"
Notice that the first row has a position 0, and the last value in the range is omitted:
So for 0:10 range the first 10 rows are returned with the positions starting with 0
and ending with 9
31
Data Frames: method loc
If we need to select a range of rows, using their labels we can use method loc:
Out[ ]:
32
Data Frames: method iloc
If we need to select a range of rows and/or columns, using their positions we can
use method iloc:
In [ ]: #Select rows by their labels:
df_sub.iloc[10:20,[0, 3, 4, 5]]
Out[ ]:
33
Data Frames: method iloc (summary)
df.iloc[0] # First row of a data frame
df.iloc[i] #(i+1)th row
df.iloc[-1] # Last row
34
Data Frames: Sorting
We can sort the data by a value in the column. By default the sorting will occur in
ascending order and a new data frame is return.
In [ ]: # Create a new data frame from the original sorted by the column Salary
df_sorted = df.sort_values( by ='service')
df_sorted.head()
Out[ ]:
35
Data Frames: Sorting
Out[ ]:
36
Missing Values
Missing values are marked as NaN
In [ ]: # Read a dataset with missing values
flights = pd.read_csv("http://rcs.bu.edu/examples/python/data_analysis/flights.csv")
Out[ ]:
37
Missing Values
There are a number of methods to deal with missing values in the data frame:
df.method() description
dropna() Drop missing observations
39
Aggregation Functions in Pandas
Aggregation - computing a summary statistic about each group, i.e.
• compute group sums or means
• compute group sizes/counts
min, max
count, sum, prod
mean, median, mode, mad
std, var
40
Aggregation Functions in Pandas
agg() method are useful when multiple statistics are computed per column:
In [ ]: flights[['dep_delay','arr_delay']].agg(['min','mean','max'])
Out[ ]:
41
Basic Descriptive Statistics
df.method() description
describe Basic statistics (count, mean, std, min, quantiles, max)
kurt kurtosis
42
Graphics to explore the data
Seaborn package is built on matplotlib but provides high level
interface for drawing attractive statistical graphics, similar to ggplot2
library in R. It specifically targets statistical data visualization
In [ ]: %matplotlib inline
43
Graphics
description
distplot histogram
barplot estimate of central tendency for a numeric variable
violinplot similar to boxplot, also shows the probability density of the
data
jointplot Scatterplot
regplot Regression plot
pairplot Pairplot
boxplot boxplot
swarmplot categorical scatterplot
factorplot General categorical plot
44
Basic statistical Analysis
statsmodel and scikit-learn - both have a number of function for statistical analysis
The first one is mostly used for regular analysis using R style formulas, while scikit-learn is
more tailored for Machine Learning.
statsmodels:
• linear regressions
• ANOVA tests
• hypothesis testings
• many more ...
scikit-learn:
• kmeans
• support vector machines
• random forests
• many more ...
55
The “R” Language
• An evolution of the “S” language developed at Bell labs for EDA.
• Idea was to allow interactive exploration and visualization of data.
• The preferred language for statisticians, used by many other data
scientists.
• Features:
• Probably the most comprehensive collection of statistical models and
distributions.
• CRAN: a very large resource of open source statistical models.
• Dot plot
58
Chart types
• Jitter plot
• Noise added to the y-axis to spread the points
59
Chart types
• Error bars: usually based on confidence intervals (CI). 95% CI means 95%
of points are in the range,
so 2.5% of points are above or below the bar.
• Not necessarily symmetric:
60
Chart types
• Box-and-whisker plot : a graphical form of 5-number summary (Tukey)
61
Chart types
• Histogram
62
Chart types
• Kernel density estimate
63
Chart types
• Histogram and Kernel Density Estimates
• Histogram
• Proper selection of bin width is important
• Outliers should be discarded
• KDE (like a smooth histogram)
• Kernel function
• Box, Epanechnikov, Gaussian
• Kernel bandwidth
64
Chart types
• Cumulative distribution function
• Integral of the histogram – simpler to build than KDE (don’t need
smoothing)
65
Chart types
• Two variables
• Bar chart
• Scatter plot
• Line plot
• Log-log plot
66
Chart types
• Bar plot: one variable is discrete
67
Chart types
• Scatter plot
68
Chart types
• Line plot
69
Chart types
• Log-log plot: Very useful for power law data
Frequency of
words in tweets
slope ~ -1
71
Chart types
• Stacked plot: stack variable is discrete:
72
Chart types
• Parallel coordinate plot: one discrete variable, an arbitrary number of
other variables:
73
Normal Distributions, Mean, Variance
The mean of a set of values is just the average of the values.
Variance a measure of the width of a distribution. Specifically, the variance is the mean squared
deviation of samples from the sample mean:
𝑛
1 2
𝑉𝑎𝑟 ( 𝑋 )= ∑ ( 𝑋 𝑖 − 𝑋 )
´
𝑛 𝑖=1
The standard deviation is the square root of variance.
The normal distribution is completed characterized by mean and variance.
mean
Standard deviation
Central Limit Theorem
The distribution of the sum (or mean) of a set of n identically-distributed random variables Xi
approaches a normal distribution as n .
The common parametric statistical tests, like t-test and ANOVA assume normally-distributed data,
but depend on sample mean and variance measures of the data.
They typically work reasonably well for data that are not normally distributed as long as the
samples are not too small.
Correcting distributions
Many statistical tools, including mean and variance, t-test, ANOVA etc. assume data are
normally distributed.
Very often this is not true. The box-and-whisker plot is a good clue
Whenever its asymmetric, the data cannot be normal. The histogram gives even more
information
Correcting distributions
In many cases these distribution can be corrected before any other processing.
Examples:
• X satisfies a log-normal distribution, Y=log(X) has a normal dist.
• X poisson with mean k and sdev. sqrt(k). Then sqrt(X) is approximately normally
distributed with sdev 1.
Distributions
Some other important distributions:
• Poisson: the distribution of counts that occur at a certain “rate”.
• Observed frequency of a given term in a corpus.
• Number of visits to a web site in a fixed time interval.
• Number of web site clicks in an hour.
• Exponential: the interval between two such events.
• Zipf/Pareto/Yule distributions: govern the frequencies of different terms in a
document, or web site visits.
• Binomial/Multinomial: The number of counts of events (e.g. die tosses = 6) out of n
trials.
• You should understand the distribution of your data before applying any model.
Hypothesis Testing
• We want to prove a hypothesis HA, but its hard so we try to disprove a null
hypothesis H0.
• A test statistic is some measurement we can make on the data which is likely to be
big under HA but small under H0.
• We chose a test statistic whose distribution we know if H0 is true: e.g.
• Two samples a and b, normally distributed, from A and B.
• H0 hypothesis that mean(A) = mean(B), test statistic is:
s = mean(a) – mean(b).
• s has mean zero and is normally distributed under H0.
• But its “large” if the two means are different.
Hypothesis Testing – contd.
• s = mean(a) – mean(b) is our test statistic,
H0 the hypothesis that mean(A)=mean(B)
• We reject if Pr(x > s | H0 ) < p
• p is a suitable “small” probability, say 0.05.
When the p value is less than 5% (p < .05), we reject the null
hypothesis
Hypothesis Testing
You use the single-sample test for one group of individuals in two
conditions. Just subtract the two measurements for each person,
and use the difference for the single sample t-test.
This is called a within-subjects design.
T-statistic and T-distribution
• We use the t-statistic from the last slide to test whether the mean of our sample
could be zero.
• If the underlying population has mean zero, the t-distribution should be distributed
like this:
where:
Prob(X) Count(X)
X=0 0.3 10
X=1 0.7 50
Where Prob(X) is part of a null hypothesis about the data (e.g. that a coin is fair).
The CHI-squared statistic lets you test whether an observation is consistent with the data:
Oi is an observed count, and Ei is the expected value of that count. It has a chi-squared
distribution, whose p-values you compute to do the test.
Fisher’s exact test
In case we only have counts under different conditions
Count1(X) Count2(X)
X=0 a b
X=1 c d
Y
Primary School High School College Grad degree
The table shows the mean values of a response variable (e.g. avg number of
Facebook posts per day) in each group.
We would like to know in a single test whether the response variable depends on
Y, at some particular significance such as 0.05.
ANOVA
In ANOVA we compute a single statistic (an F-statistic) that compares variance between
groups with variance within each group.
VARbetween
F
VAR within
The higher the F-value is, the less probable is the null hypothesis that the samples all
come from the same population.
We can look up the F-statistic value in a cumulative F-distribution (similar to the other
statistics) to get the p-value.
ANOVA tests can be much more complicated, with multiple dependent variables,
hierarchies of variables, correlated measurements etc.
Closing Words
All the tests so far are parametric tests that assume the data are normally distributed,
and that the samples are independent of each other and all have the same
distribution (IID).
They may be arbitrarily inaccurate is those assumptions are not met. Always make sure
your data satisfies the assumptions of the test you’re using. e.g. watch out for:
• Outliers – will corrupt many tests that use variance estimates.
• Correlated values as samples, e.g. if you repeated measurements on the same subject.
• Skewed distributions – give invalid results.
Non-parametric tests
These tests make no assumption about the distribution of the input data,
and can be used on very general datasets:
• K-S test
• Permutation tests
It can also be used to compare distributions of data values in a large data pipeline: Most
errors will distort the distribution of a data parameter and a K-S test can detect this.
Non-parametric tests
Permutation tests
Bootstrap confidence intervals
• We wont discuss these in detail, but its important to know that non-parametric tests
using one of the above methods exist for many forms of hypothesis.
• They make no assumptions about the distribution of the data, but in many cases are
just as sensitive as parametric tests.
• They use computational cycles to simulate sample data, to derive p-value estimates
approximately, and accuracy improves with the amount of computational work done.