This is a brief overview of statistics in python. In data science we always inspect our data using descriptive statistics and descriptive plots. Such statistics can be used of course for visualisations or dashboards as well. The statistical analysis can be done with a number of tests, depending on the characteristics of the data and the research question to be answered. This notebook refers to the most important ones
Practical: Descriptive statistics
Graphical: Descriptive plots
Analytical: Statistical analysis
#import librariesimport numpy as npimport pandas as pd#import scipy #import statsmodels
Descriptive statistics
Let us create some data for demonstration purpose. We will put the data in a pandas dataframe since pandas has some nice numpy methods built ins, like mean(), sum(), max(),min() etc. It can even deliver the descriptive statistics at once with describe()
#series of values with weightsx =[8.0,1,2.5,4,28.0]w =[0.1,0.2,0.3,0.25,0.15]X = pd.DataFrame({'measurement':x,'weights':w})
We can also use the built in plots for our explatory data analyses. Like boxplot(), hist(), plot.kde() or just plot(). Seaborn has some nice plots as well
png
png
png
png
png
Analytical statistics
Normality check with Shapiro-Wilk Test
It is good practice to check for normality. The Shapiro-Wilk Test is a good test for checking normality
#check distribution of data
X['measurement'].hist()
#check distribution for normality
X['measurement'].plot.kde()
#check normality with qqplot
from statsmodels.graphics.gofplots import qqplot
from matplotlib import pyplot
qqplot(X['measurement'], line='s')
pyplot.show()
#plot to see time effect
X['measurement'].plot()
from scipy.stats import shapiro
# normality test
stat, p = shapiro(X['measurement'])
print('Statistics=%.3f, p=%.3f' % (stat, p))
# interpret
alpha = 0.05
if p > alpha:
print('Sample looks Gaussian (fail to reject H0)')
else:
print('Sample does not look Gaussian (reject H0)')
Statistics=0.754, p=0.032
Sample does not look Gaussian (reject H0)