Ss Project With Python
Ss Project With Python
MID-TERM EXAMINATION
REPORT
SEMESTER: III
PROGRAM & SPECIALIZATION: BCA (DS)
ACADEMIC YEAR: 2022-23
Statistic in python
"";"Gender";"FSIQ";"VIQ";"PIQ";"Weight";"Height";"MRI_Count"
"1";"Female";133;132;124;"118";"64.5";816932
"2";"Male";140;150;124;".";"72.5";1001121
"3";"Male";139;123;150;"143";"73.3";1038437
"4";"Male";133;129;128;"172";"68.8";965353
"5";"Female";137;132;134;"147";"65.0";951545
We will store and manipulate this data in a pandas.DataFrame, from the pandas module. It is the Python equivalent
of the spreadsheet table. It is different from a 2D numpy array as it has named columns, can contain a mixture of
different data types by column, and has elaborate selection and pivotal mechanisms.
Reading from a CSV file: Using the above CSV file that gives observations of brain size and weight and IQ
(Willerman et al. 1991), the data are a mixture of numerical and categorical values:
>>> data
Unnamed: 0 Gender FSIQ VIQ PIQ Weight Height MRI_Count
...
Missing values
The weight of the second individual is missing in the CSV file. If we don’t specify the missing value (NA = not
available) marker, we will not be able to do statistical analysis.
Creating from arrays: A pandas.DataFrame can also be seen as a dictionary of 1D ‘series’, eg arrays or lists. If
we have 3 numpy arrays:
t sin cos
0 -6.000000 0.279415 0.960170
...
Other inputs: pandas can input data from SQL, excel files, or other formats. See the pandas documentation.
Manipulating data
data is a pandas.DataFrame, that resembles R’s dataframe:
(40, 8)
0 Female
1 Male
2 Male
3 Male
4 Female
...
>>> # Simpler selector
109.45
Note
For a quick view on a large dataframe, use its describe method: pandas.DataFrame.describe().
('Female', 109.45)
('Male', 115.25)
groupby_gender is a powerful object that exposes many operations on the resulting group of dataframes:
>>>>>> groupby_gender.mean()
Use tab-completion on groupby_gender to find more. Other common grouping functions are median, count (useful for
checking to see the amount of missing values in different subsets) or sum. Groupby evaluation is lazy, no work is done
until an aggregation function is applied.
Exercise
• What is the mean value for VIQ for the full population?
• How many males/females were included in this study?
• Hint use ‘tab completion’ to find out the methods that can be called, instead of ‘mean’ in the above
example.
• What is the average value of MRI counts expressed in log units, for males and females?
Note
Plotting data
Pandas comes with some plotting tools (pandas.tools.plotting, using matplotlib behind the scene) to
display statistics of the data in dataframes:
Scatter matrices:
Two populations
Exercise
Plot the scatter matrix for males only, and for females only. Do you think that the 2 sub-populations correspond
to gender?
Hypothesis testing: comparing two groups
For simple statistical tests, we will use the scipy.stats sub-module of scipy:
See also
Scipy is a vast library. For a quick summary to the whole library, see the scipy chapter.
scipy.stats.ttest_1samp() tests if the population mean of data is likely to be equal to a given value
(technically if observations are drawn from a Gaussian distributions of given population mean). It returns the T
statistic, and the p-value (see the function’s help):
>>>>>> stats.ttest_1samp(data['VIQ'], 0)
Ttest_1sampResult(statistic=30.088099970..., pvalue=1.32891964...e-28)
With a p-value of 10^-28 we can claim that the population mean for the IQ (VIQ measure) is not 0.
Ttest_indResult(statistic=-0.77261617232..., pvalue=0.4445287677858...)