Module 1 (introduction) (First 4 chapters) completed

pull/789/head
uv012 4 days ago
parent 0d9af55c68
commit bf84b17f93

@ -0,0 +1,17 @@
# Introduction to Data Science
![data in action](images/data.jpg)
> Photo by <a href="https://unsplash.com/@dawson2406?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Stephen Dawson</a> on <a href="https://unsplash.com/s/photos/data?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Unsplash</a>
In these lessons, you will discover how Data Science is defined and learn about ethical considerations that must be considered by a data scientist. You will also learn how data is defined and learn a bit about statistics and probability, the core academic domains of Data Science.
### Topics
1. [Defining Data Science](01-defining-data-science/README.md)
2. [Data Science Ethics](02-ethics/README.md)
3. [Defining Data](03-defining-data/README.md)
4. [Introduction to Statistics and Probability](04-stats-and-probability/README.md)
### Credits
These lessons were written with ❤️ by [Nitya Narasimhan](https://twitter.com/nitya) and [Dmitry Soshnikov](https://twitter.com/shwars).

@ -0,0 +1,165 @@
# Defining Data Science
| ![ Sketchnote by [(@sketchthedocs)](https://sketchthedocs.dev) ](../../sketchnotes/01-Definitions.png) |
| :----------------------------------------------------------------------------------------------------: |
| Defining Data Science - _Sketchnote by [@nitya](https://twitter.com/nitya)_ |
---
[![Defining Data Science Video](images/video-def-ds.png)](https://youtu.be/beZ7Mb_oz9I)
## [Pre-lecture quiz](https://ff-quizzes.netlify.app/en/ds/quiz/0)
## What is Data?
In our everyday life, we are constantly surrounded by data. The text you are reading now is data. The list of phone numbers of your friends in your smartphone is data, as well as the current time displayed on your watch. As human beings, we naturally operate with data by counting the money we have or by writing letters to our friends.
However, data became much more critical with the creation of computers. The primary role of computers is to perform computations, but they need data to operate on. Thus, we need to understand how computers store and process data.
With the emergence of the Internet, the role of computers as data handling devices increased. If you think about it, we now use computers more and more for data processing and communication, rather than actual computations. When we write an e-mail to a friend or search for some information on the Internet - we are essentially creating, storing, transmitting, and manipulating data.
> Can you remember the last time you have used computers to actually compute something?
## What is Data Science?
In [Wikipedia](https://en.wikipedia.org/wiki/Data_science), **Data Science** is defined as *a scientific field that uses scientific methods to extract knowledge and insights from structured and unstructured data, and apply knowledge and actionable insights from data across a broad range of application domains*.
This definition highlights the following important aspects of data science:
* The main goal of data science is to **extract knowledge** from data, in other words - to **understand** data, find some hidden relationships and build a **model**.
* Data science uses **scientific methods**, such as probability and statistics. In fact, when the term *data science* was first introduced, some people argued that data science was just a new fancy name for statistics. Nowadays it has become evident that the field is much broader.
* Obtained knowledge should be applied to produce some **actionable insights**, i.e. practical insights that you can apply to real business situations.
* We should be able to operate on both **structured** and **unstructured** data. We will come back to discuss different types of data later in the course.
* **Application domain** is an important concept, and data scientists often need at least some degree of expertise in the problem domain, for example: finance, medicine, marketing, etc.
> Another important aspect of Data Science is that it studies how data can be gathered, stored and operated upon using computers. While statistics gives us mathematical foundations, data science applies mathematical concepts to actually draw insights from data.
One of the ways (attributed to [Jim Gray](https://en.wikipedia.org/wiki/Jim_Gray_(computer_scientist))) to look at the data science is to consider it to be a separate paradigm of science:
* **Empirical**, in which we rely mostly on observations and results of experiments
* **Theoretical**, where new concepts emerge from existing scientific knowledge
* **Computational**, where we discover new principles based on some computational experiments
* **Data-Driven**, based on discovering relationships and patterns in the data
## Other Related Fields
Since data is pervasive, data science itself is also a broad field, touching many other disciplines.
<dl>
<dt>Databases</dt>
<dd>
A critical consideration is <b>how to store</b> the data, i.e. how to structure it in a way that allows faster processing. There are different types of databases that store structured and unstructured data, which <a href="../../2-Working-With-Data/README.md">we will consider in our course</a>.
</dd>
<dt>Big Data</dt>
<dd>
Often we need to store and process very large quantities of data with a relatively simple structure. There are special approaches and tools to store that data in a distributed manner on a computer cluster, and process it efficiently.
</dd>
<dt>Machine Learning</dt>
<dd>
One way to understand data is to <b>build a model</b> that will be able to predict a desired outcome. Developing models from data is called <b>machine learning</b>. You may want to have a look at our <a href="https://aka.ms/ml-beginners">Machine Learning for Beginners</a> Curriculum to learn more about it.
</dd>
<dt>Artificial Intelligence</dt>
<dd>
An area of machine learning known as artificial intelligence (AI) also relies on data, and it involves building high complexity models that mimic human thought processes. AI methods often allow us to turn unstructured data (e.g. natural language) into structured insights.
</dd>
<dt>Visualization</dt>
<dd>
Vast amounts of data are incomprehensible for a human being, but once we create useful visualizations using that data, we can make more sense of the data, and draw some conclusions. Thus, it is important to know many ways to visualize information - something that we will cover in <a href="../../3-Data-Visualization/README.md">Section 3</a> of our course. Related fields also include <b>Infographics</b>, and <b>Human-Computer Interaction</b> in general.
</dd>
</dl>
## Types of Data
As we have already mentioned, data is everywhere. We just need to capture it in the right way! It is useful to distinguish between **structured** and **unstructured** data. The former is typically represented in some well-structured form, often as a table or number of tables, while the latter is just a collection of files. Sometimes we can also talk about **semi-structured** data, that have some sort of a structure that may vary greatly.
| Structured | Semi-structured | Unstructured |
| ---------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------- | --------------------------------------- |
| List of people with their phone numbers | Wikipedia pages with links | Text of Encyclopedia Britannica |
| Temperature in all rooms of a building at every minute for the last 20 years | Collection of scientific papers in JSON format with authors, data of publication, and abstract | File share with corporate documents |
| Data for age and gender of all people entering the building | Internet pages | Raw video feed from surveillance camera |
## Where to get Data
There are many possible sources of data, and it will be impossible to list all of them! However, let's mention some of the typical places where you can get data:
* **Structured**
- **Internet of Things** (IoT), including data from different sensors, such as temperature or pressure sensors, provides a lot of useful data. For example, if an office building is equipped with IoT sensors, we can automatically control heating and lighting in order to minimize costs.
- **Surveys** that we ask users to complete after a purchase, or after visiting a web site.
- **Analysis of behavior** can, for example, help us understand how deeply a user goes into a site, and what is the typical reason for leaving the site.
* **Unstructured**
- **Texts** can be a rich source of insights, such as an overall **sentiment score**, or extracting keywords and semantic meaning.
- **Images** or **Video**. A video from a surveillance camera can be used to estimate traffic on the road, and inform people about potential traffic jams.
- Web server **Logs** can be used to understand which pages of our site are most often visited, and for how long.
* Semi-structured
- **Social Network** graphs can be great sources of data about user personalities and potential effectiveness in spreading information around.
- When we have a bunch of photographs from a party, we can try to extract **Group Dynamics** data by building a graph of people taking pictures with each other.
By knowing different possible sources of data, you can try to think about different scenarios where data science techniques can be applied to know the situation better, and to improve business processes.
## What you can do with Data
In Data Science, we focus on the following steps of data journey:
<dl>
<dt>1) Data Acquisition</dt>
<dd>
The first step is to collect the data. While in many cases it can be a straightforward process, like data coming to a database from a web application, sometimes we need to use special techniques. For example, data from IoT sensors can be overwhelming, and it is a good practice to use buffering endpoints such as IoT Hub to collect all the data before further processing.
</dd>
<dt>2) Data Storage</dt>
<dd>
Storing data can be challenging, especially if we are talking about big data. When deciding how to store data, it makes sense to anticipate the way you would like to query the data in the future. There are several ways data can be stored:
<ul>
<li>A relational database stores a collection of tables, and uses a special language called SQL to query them. Typically, tables are organized into different groups called schemas. In many cases we need to convert the data from original form to fit the schema.</li>
<li><a href="https://en.wikipedia.org/wiki/NoSQL">A NoSQL</a> database, such as <a href="https://azure.microsoft.com/services/cosmos-db/?WT.mc_id=academic-77958-bethanycheum">CosmosDB</a>, does not enforce schemas on data, and allows storing more complex data, for example, hierarchical JSON documents or graphs. However, NoSQL databases do not have the rich querying capabilities of SQL, and cannot enforce referential integrity, i.e. rules on how the data is structured in tables and governing the relationships between tables.</li>
<li><a href="https://en.wikipedia.org/wiki/Data_lake">Data Lake</a> storage is used for large collections of data in raw, unstructured form. Data lakes are often used with big data, where all data cannot fit on one machine, and has to be stored and processed by a cluster of servers. <a href="https://en.wikipedia.org/wiki/Apache_Parquet">Parquet</a> is the data format that is often used in conjunction with big data.</li>
</ul>
</dd>
<dt>3) Data Processing</dt>
<dd>
This is the most exciting part of the data journey, which involves converting the data from its original form into a form that can be used for visualization/model training. When dealing with unstructured data such as text or images, we may need to use some AI techniques to extract <b>features</b> from the data, thus converting it to structured form.
</dd>
<dt>4) Visualization / Human Insights</dt>
<dd>
Oftentimes, in order to understand the data, we need to visualize it. Having many different visualization techniques in our toolbox, we can find the right view to make an insight. Often, a data scientist needs to "play with data", visualizing it many times and looking for some relationships. Also, we may use statistical techniques to test a hypotheses or prove a correlation between different pieces of data.
</dd>
<dt>5) Training a predictive model</dt>
<dd>
Because the ultimate goal of data science is to be able to make decisions based on data, we may want to use the techniques of <a href="http://github.com/microsoft/ml-for-beginners">Machine Learning</a> to build a predictive model. We can then use this to make predictions using new data sets with similar structures.
</dd>
</dl>
Of course, depending on the actual data, some steps might be missing (e.g., when we already have the data in the database, or when we do not need model training), or some steps might be repeated several times (such as data processing).
## Digitalization and Digital Transformation
In the last decade, many businesses started to understand the importance of data when making business decisions. To apply data science principles to running a business, one first needs to collect some data, i.e. translate business processes into digital form. This is known as **digitalization**. Applying data science techniques to this data to guide decisions can lead to significant increases in productivity (or even business pivot), called **digital transformation**.
Let's consider an example. Suppose we have a data science course (like this one) which we deliver online to students, and we want to use data science to improve it. How can we do it?
We can start by asking "What can be digitized?" The simplest way would be to measure the time it takes each student to complete each module, and to measure the obtained knowledge by giving a multiple-choice test at the end of each module. By averaging time-to-complete across all students, we can find out which modules cause the most difficulties for students, and work on simplifying them.
> You may argue that this approach is not ideal, because modules can be of different lengths. It is probably more fair to divide the time by the length of the module (in number of characters), and compare those values instead.
When we start analyzing results of multiple-choice tests, we can try to determine which concepts that students have difficulty understanding, and use that information to improve the content. To do that, we need to design tests in such a way that each question maps to a certain concept or chunk of knowledge.
If we want to get even more complicated, we can plot the time taken for each module against the age category of students. We might find out that for some age categories it takes an inappropriately long time to complete the module, or that students drop out before completing it. This can help us provide age recommendations for the module, and minimize people's dissatisfaction from wrong expectations.
## 🚀 Challenge
In this challenge, we will try to find concepts relevant to the field of Data Science by looking at texts. We will take a Wikipedia article on Data Science, download and process the text, and then build a word cloud like this one:
![Word Cloud for Data Science](images/ds_wordcloud.png)
Visit [`notebook.ipynb`](./notebook.ipynb ':ignore') to read through the code. You can also run the code, and see how it performs all data transformations in real time.
> If you do not know how to run code in a Jupyter Notebook, have a look at [this article](https://soshnikov.com/education/how-to-execute-notebooks-from-github/).
## [Post-lecture quiz](https://ff-quizzes.netlify.app/en/ds/quiz/1)
## Assignments
* **Task 1**: Modify the code above to find out related concepts for the fields of **Big Data** and **Machine Learning**
* **Task 2**: [Think About Data Science Scenarios](assignment.md)
## Credits
This lesson has been authored with ♥️ by [Dmitry Soshnikov](http://soshnikov.com)

@ -0,0 +1,31 @@
# Assignment: Data Science Scenarios
In this first assignment, we ask you to think about some real-life process or problem in different problem domains, and how you can improve it using the Data Science process. Think about the following:
1. Which data can you collect?
1. How would you collect it?
1. How would you store the data? How large the data is likely to be?
1. Which insights you might be able to get from this data? Which decisions we would be able to take based on the data?
Try to think about 3 different problems/processes and describe each of the points above for each problem domain.
Here are some of the problem domains and problems that can get you started thinking:
1. How can you use data to improve education process for children in schools?
1. How can you use data to control vaccination during the pandemic?
1. How can you use data to make sure you are being productive at work?
## Instructions
Fill in the following table (substitute suggested problem domains for your own ones if needed):
| Problem Domain | Problem | Which data to collect | How to store the data | Which insights/decisions we can make |
|----------------|---------|-----------------------|-----------------------|--------------------------------------|
| Education | | | | |
| Vaccination | | | | |
| Productivity | | | | |
## Rubric
Exemplary | Adequate | Needs Improvement
--- | --- | -- |
One was able to identify reasonable data sources, ways of storing data and possible decisions/insights for all problem domains | Some of the aspects of the solution are not detailed, data storage is not discussed, at least 2 problem domains are described | Only parts of the data solution are described, only one problem domain is considered.

@ -0,0 +1,48 @@
Not to be confused with Information science or Computer science.
The existence of Comet NEOWISE (here depicted as a series of red dots) was discovered by analyzing astronomical survey data acquired by a space telescope, the Wide-field Infrared Survey Explorer.
Data science is an interdisciplinary academic field[1] that uses statistics, scientific computing, scientific methods, processing, scientific visualization, algorithms, coding (like Python, SQL, and R), and systems to extract or extrapolate knowledge from potentially noisy, structured, or unstructured data.[2] A data scientist is a professional who creates programming code and combines it with statistical knowledge to summarize data.[3]
Scope of data science
Data science plays a critical role in modern decision-making by enabling organizations to extract actionable insights from large and complex datasets.[4] Data science also integrates domain knowledge from the underlying application domain (e.g., natural sciences, information technology, and medicine).[5] Data science is multifaceted and can be described as a science, a research paradigm, a research method, a discipline, a workflow, and a profession.[6]
Data science is "a concept to unify statistics, data analysis, informatics, and their related methods" to "understand and analyze actual phenomena" with data.[7] It uses techniques and theories drawn from many fields within the context of mathematics, statistics, computer science, information science, and domain knowledge.[8] However, data science is distinct from computer science and information science. Turing Award winner Jim Gray imagined data science as a "fourth paradigm" of science (empirical, theoretical, computational, and now data-driven) and asserted that "everything about science is changing because of the impact of information technology" and the data deluge.[9][10]
Data science is often described as a multidisciplinary and rapidly evolving field because it draws on techniques from diverse areas, such as computer science, statistics, information science, and other subject-specific disciplines. Some researchers argue that the combination of the different fields resembles how information science was decades ago.[11] These similarities help clarify how data science gradually became its own field of study.[11]
Foundations
Data science is an interdisciplinary field[12] focused on extracting knowledge from typically large data sets and applying the knowledge from that data to solve problems in other application domains. The field encompasses preparing data for analysis, formulating data science problems, analyzing data, and summarizing these findings. As such, it incorporates skills from computer science, mathematics, data visualization, graphic design, communication, and business.[13]
Vasant Dhar writes that statistics emphasizes quantitative data and description. In contrast, data science deals with quantitative and qualitative data (e.g., from images, text, sensors, transactions, customer information, etc.) and emphasizes prediction and action.[14] Andrew Gelman of Columbia University has described statistics as a non-essential part of data science.[15] Stanford professor David Donoho writes that data science is not distinguished from statistics by the size of datasets or use of computing and that many graduate programs misleadingly advertise their analytics and statistics training as the essence of a data-science program. He describes data science as an applied field growing out of traditional statistics.[16]
Etymology
Early usage
In 1962, John Tukey described a field he called "data analysis", which resembles modern data science.[16] In 1985, in a lecture given to the Chinese Academy of Sciences in Beijing, C. F. Jeff Wu used the term "data science" for the first time as an alternative name for statistics.[17] Later, attendees at a 1992 statistics symposium at the University of Montpellier II acknowledged the emergence of a new discipline focused on data of various origins and forms, combining established concepts and principles of statistics and data analysis with computing.[18][19]
The term "data science" has been traced back to 1974, when Peter Naur proposed it as an alternative name to computer science. In his 1974 book Concise Survey of Computer Methods, Peter Naur proposed using the term data science rather than computer science to reflect the growing emphasis on data-driven methods[20][8] In 1996, the International Federation of Classification Societies became the first conference to specifically feature data science as a topic.[8] However, the definition was still in flux. After the 1985 lecture at the Chinese Academy of Sciences in Beijing, in 1997 C. F. Jeff Wu again suggested that statistics should be renamed data science. He reasoned that a new name would help statistics shed inaccurate stereotypes, such as being synonymous with accounting or limited to describing data.[21] In 1998, Hayashi Chikio argued for data science as a new, interdisciplinary concept, with three aspects: data design, collection, and analysis.[19]
Modern usage
In 2012, technologists Thomas H. Davenport and DJ Patil declared "Data Scientist: The Sexiest Job of the 21st Century",[22] a catchphrase that was picked up even by major-city newspapers like the New York Times[23] and the Boston Globe.[24] A decade later, they reaffirmed it, stating that "the job is more in demand than ever with employers".[25]
The modern conception of data science as an independent discipline is sometimes attributed to William S. Cleveland.[26] In 2014, the American Statistical Association's Section on Statistical Learning and Data Mining changed its name to the Section on Statistical Learning and Data Science, reflecting the ascendant popularity of data science.[27]
The professional title of "data scientist" has been attributed to DJ Patil and Jeff Hammerbacher in 2008.[28] Though it was used by the National Science Board in their 2005 report "Long-Lived Digital Data Collections: Enabling Research and Education in the 21st Century", it referred broadly to any key role in managing a digital data collection.[29]
Data science and data analysis
summary statistics and scatterplots showing the Datasaurus dozen data set
Example of exploratory data analysis using the Datasaurus dozen data set
Data analysis typically involves working with structured datasets to answer specific questions or solve specific problems. This can involve tasks such as data cleaning and data visualization to summarize data and develop hypotheses about relationships between variables. Data analysts typically use statistical methods to test these hypotheses and draw conclusions from the data.[30]
Data science involves working with larger datasets that often require advanced computational and statistical methods to analyze. Data scientists often work with unstructured data such as text or images and use machine learning algorithms to build predictive models. Data science often uses statistical analysis, data preprocessing, and supervised learning.[31][32]
Cloud computing for data science
A cloud-based architecture for enabling big data analytics. Data flows from various sources, such as personal computers, laptops, and smart phones, through cloud services for processing and analysis, finally leading to various big data applications.
Cloud computing can offer access to large amounts of computational power and storage.[33] In big data, where volumes of information are continually generated and processed, these platforms can be used to handle complex and resource-intensive analytical tasks.[34]
Some distributed computing frameworks are designed to handle big data workloads. These frameworks can enable data scientists to process and analyze large datasets in parallel, which can reduce processing times.[35]
Ethical consideration in data science
Data science involves collecting, processing, and analyzing data which often includes personal and sensitive information. Ethical concerns include potential privacy violations, bias perpetuation, and negative societal impacts.[36][37]
Machine learning models can amplify existing biases present in training data, leading to discriminatory or unfair outcomes.[38][39]

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

@ -0,0 +1,6 @@
{
"cells": [],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 5
}

@ -0,0 +1,48 @@
Not to be confused with Information science or Computer science.
The existence of Comet NEOWISE (here depicted as a series of red dots) was discovered by analyzing astronomical survey data acquired by a space telescope, the Wide-field Infrared Survey Explorer.
Data science is an interdisciplinary academic field[1] that uses statistics, scientific computing, scientific methods, processing, scientific visualization, algorithms, coding (like Python, SQL, and R), and systems to extract or extrapolate knowledge from potentially noisy, structured, or unstructured data.[2] A data scientist is a professional who creates programming code and combines it with statistical knowledge to summarize data.[3]
Scope of data science
Data science plays a critical role in modern decision-making by enabling organizations to extract actionable insights from large and complex datasets.[4] Data science also integrates domain knowledge from the underlying application domain (e.g., natural sciences, information technology, and medicine).[5] Data science is multifaceted and can be described as a science, a research paradigm, a research method, a discipline, a workflow, and a profession.[6]
Data science is "a concept to unify statistics, data analysis, informatics, and their related methods" to "understand and analyze actual phenomena" with data.[7] It uses techniques and theories drawn from many fields within the context of mathematics, statistics, computer science, information science, and domain knowledge.[8] However, data science is distinct from computer science and information science. Turing Award winner Jim Gray imagined data science as a "fourth paradigm" of science (empirical, theoretical, computational, and now data-driven) and asserted that "everything about science is changing because of the impact of information technology" and the data deluge.[9][10]
Data science is often described as a multidisciplinary and rapidly evolving field because it draws on techniques from diverse areas, such as computer science, statistics, information science, and other subject-specific disciplines. Some researchers argue that the combination of the different fields resembles how information science was decades ago.[11] These similarities help clarify how data science gradually became its own field of study.[11]
Foundations
Data science is an interdisciplinary field[12] focused on extracting knowledge from typically large data sets and applying the knowledge from that data to solve problems in other application domains. The field encompasses preparing data for analysis, formulating data science problems, analyzing data, and summarizing these findings. As such, it incorporates skills from computer science, mathematics, data visualization, graphic design, communication, and business.[13]
Vasant Dhar writes that statistics emphasizes quantitative data and description. In contrast, data science deals with quantitative and qualitative data (e.g., from images, text, sensors, transactions, customer information, etc.) and emphasizes prediction and action.[14] Andrew Gelman of Columbia University has described statistics as a non-essential part of data science.[15] Stanford professor David Donoho writes that data science is not distinguished from statistics by the size of datasets or use of computing and that many graduate programs misleadingly advertise their analytics and statistics training as the essence of a data-science program. He describes data science as an applied field growing out of traditional statistics.[16]
Etymology
Early usage
In 1962, John Tukey described a field he called "data analysis", which resembles modern data science.[16] In 1985, in a lecture given to the Chinese Academy of Sciences in Beijing, C. F. Jeff Wu used the term "data science" for the first time as an alternative name for statistics.[17] Later, attendees at a 1992 statistics symposium at the University of Montpellier II acknowledged the emergence of a new discipline focused on data of various origins and forms, combining established concepts and principles of statistics and data analysis with computing.[18][19]
The term "data science" has been traced back to 1974, when Peter Naur proposed it as an alternative name to computer science. In his 1974 book Concise Survey of Computer Methods, Peter Naur proposed using the term data science rather than computer science to reflect the growing emphasis on data-driven methods[20][8] In 1996, the International Federation of Classification Societies became the first conference to specifically feature data science as a topic.[8] However, the definition was still in flux. After the 1985 lecture at the Chinese Academy of Sciences in Beijing, in 1997 C. F. Jeff Wu again suggested that statistics should be renamed data science. He reasoned that a new name would help statistics shed inaccurate stereotypes, such as being synonymous with accounting or limited to describing data.[21] In 1998, Hayashi Chikio argued for data science as a new, interdisciplinary concept, with three aspects: data design, collection, and analysis.[19]
Modern usage
In 2012, technologists Thomas H. Davenport and DJ Patil declared "Data Scientist: The Sexiest Job of the 21st Century",[22] a catchphrase that was picked up even by major-city newspapers like the New York Times[23] and the Boston Globe.[24] A decade later, they reaffirmed it, stating that "the job is more in demand than ever with employers".[25]
The modern conception of data science as an independent discipline is sometimes attributed to William S. Cleveland.[26] In 2014, the American Statistical Association's Section on Statistical Learning and Data Mining changed its name to the Section on Statistical Learning and Data Science, reflecting the ascendant popularity of data science.[27]
The professional title of "data scientist" has been attributed to DJ Patil and Jeff Hammerbacher in 2008.[28] Though it was used by the National Science Board in their 2005 report "Long-Lived Digital Data Collections: Enabling Research and Education in the 21st Century", it referred broadly to any key role in managing a digital data collection.[29]
Data science and data analysis
summary statistics and scatterplots showing the Datasaurus dozen data set
Example of exploratory data analysis using the Datasaurus dozen data set
Data analysis typically involves working with structured datasets to answer specific questions or solve specific problems. This can involve tasks such as data cleaning and data visualization to summarize data and develop hypotheses about relationships between variables. Data analysts typically use statistical methods to test these hypotheses and draw conclusions from the data.[30]
Data science involves working with larger datasets that often require advanced computational and statistical methods to analyze. Data scientists often work with unstructured data such as text or images and use machine learning algorithms to build predictive models. Data science often uses statistical analysis, data preprocessing, and supervised learning.[31][32]
Cloud computing for data science
A cloud-based architecture for enabling big data analytics. Data flows from various sources, such as personal computers, laptops, and smart phones, through cloud services for processing and analysis, finally leading to various big data applications.
Cloud computing can offer access to large amounts of computational power and storage.[33] In big data, where volumes of information are continually generated and processed, these platforms can be used to handle complex and resource-intensive analytical tasks.[34]
Some distributed computing frameworks are designed to handle big data workloads. These frameworks can enable data scientists to process and analyze large datasets in parallel, which can reduce processing times.[35]
Ethical consideration in data science
Data science involves collecting, processing, and analyzing data which often includes personal and sensitive information. Ethical concerns include potential privacy violations, bias perpetuation, and negative societal impacts.[36][37]
Machine learning models can amplify existing biases present in training data, leading to discriminatory or unfair outcomes.[38][39]

Binary file not shown.

After

Width:  |  Height:  |  Size: 236 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 103 KiB

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

@ -0,0 +1,33 @@
# Assignment: Data Science Scenarios
In this first assignment, we ask you to think about some real-life process or problem in different problem domains, and how you can improve it using the Data Science process. Think about the following:
1. Which data can you collect?
1. How would you collect it?
1. How would you store the data? How large the data is likely to be?
1. Which insights you might be able to get from this data? Which decisions we would be able to take based on the data?
Try to think about 3 different problems/processes and describe each of the points above for each problem domain.
Here are some of the problem domains and problems that can get you started thinking:
1. How can you use data to improve education process for children in schools?
1. How can you use data to control vaccination during the pandemic?
1. How can you use data to make sure you are being productive at work?
## Instructions
Fill in the following table (substitute suggested problem domains for your own ones if needed):
| Problem Domain | Problem | Which data to collect | How to store the data | Which insights/decisions we can make |
|----------------|---------|-----------------------|-----------------------|--------------------------------------|
| Education | In university, we typically have low attendance to lectures, and we have the hypothesis that students who attend lectures on average to better during exams. We want to stimulate attendance and test the hypothesis. | We can track attendance through pictures taken by the security camera in class, or by tracking bluetooth/wifi addresses of student mobile phones in class. Exam data is already available in the university database. | In case we track security camera images - we need to store a few (5-10) photographs during class (unstructured data), and then use AI to identify faces of students (convert data to structured form). | We can compute average attendance data for each student, and see if there is any correlation with exam grades. We will talk more about correlation in [probability and statistics](../../04-stats-and-probability/README.md) section. In order to stimulate student attendance, we can publish the weekly attendance rating on school portal, and draw prizes among those with highest attendance. |
| Vaccination | | | | |
| Productivity | | | | |
> *We provide just one answer as an example, so that you can get an idea of what is expected in this assignment.*
## Rubric
Exemplary | Adequate | Needs Improvement
--- | --- | -- |
One was able to identify reasonable data sources, ways of storing data and possible decisions/insights for all problem domains | Some of the aspects of the solution are not detailed, data storage is not discussed, at least 2 problem domains are described | Only parts of the data solution are described, only one problem domain is considered.

@ -0,0 +1,636 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "860a77fe-e073-47ec-858f-3c81e52a4167",
"metadata": {},
"source": [
"# To test the hypothesis that the First_Baseman are older than Second_Baseman"
]
},
{
"cell_type": "markdown",
"id": "89e2a25b-42d4-4abe-9ea3-7b359c1e0092",
"metadata": {},
"source": [
"## 1. Imort the dataset"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "08752c0b-23b2-4e06-afb1-e9f5aed67c5b",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "b41e6fe7-0cc3-46e5-95df-7fe063d8d06d",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Adam_Donachie</th>\n",
" <th>BAL</th>\n",
" <th>Catcher</th>\n",
" <th>74</th>\n",
" <th>180</th>\n",
" <th>22.99</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Paul_Bako</td>\n",
" <td>BAL</td>\n",
" <td>Catcher</td>\n",
" <td>74</td>\n",
" <td>215.0</td>\n",
" <td>34.69</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Ramon_Hernandez</td>\n",
" <td>BAL</td>\n",
" <td>Catcher</td>\n",
" <td>72</td>\n",
" <td>210.0</td>\n",
" <td>30.78</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Kevin_Millar</td>\n",
" <td>BAL</td>\n",
" <td>First_Baseman</td>\n",
" <td>72</td>\n",
" <td>210.0</td>\n",
" <td>35.43</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>Chris_Gomez</td>\n",
" <td>BAL</td>\n",
" <td>First_Baseman</td>\n",
" <td>73</td>\n",
" <td>188.0</td>\n",
" <td>35.71</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>Brian_Roberts</td>\n",
" <td>BAL</td>\n",
" <td>Second_Baseman</td>\n",
" <td>69</td>\n",
" <td>176.0</td>\n",
" <td>29.39</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1028</th>\n",
" <td>Brad_Thompson</td>\n",
" <td>STL</td>\n",
" <td>Relief_Pitcher</td>\n",
" <td>73</td>\n",
" <td>190.0</td>\n",
" <td>25.08</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1029</th>\n",
" <td>Tyler_Johnson</td>\n",
" <td>STL</td>\n",
" <td>Relief_Pitcher</td>\n",
" <td>74</td>\n",
" <td>180.0</td>\n",
" <td>25.73</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1030</th>\n",
" <td>Chris_Narveson</td>\n",
" <td>STL</td>\n",
" <td>Relief_Pitcher</td>\n",
" <td>75</td>\n",
" <td>205.0</td>\n",
" <td>25.19</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1031</th>\n",
" <td>Randy_Keisler</td>\n",
" <td>STL</td>\n",
" <td>Relief_Pitcher</td>\n",
" <td>75</td>\n",
" <td>190.0</td>\n",
" <td>31.01</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1032</th>\n",
" <td>Josh_Kinney</td>\n",
" <td>STL</td>\n",
" <td>Relief_Pitcher</td>\n",
" <td>73</td>\n",
" <td>195.0</td>\n",
" <td>27.92</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>1033 rows × 6 columns</p>\n",
"</div>"
],
"text/plain": [
" Adam_Donachie BAL Catcher 74 180 22.99\n",
"0 Paul_Bako BAL Catcher 74 215.0 34.69\n",
"1 Ramon_Hernandez BAL Catcher 72 210.0 30.78\n",
"2 Kevin_Millar BAL First_Baseman 72 210.0 35.43\n",
"3 Chris_Gomez BAL First_Baseman 73 188.0 35.71\n",
"4 Brian_Roberts BAL Second_Baseman 69 176.0 29.39\n",
"... ... ... ... .. ... ...\n",
"1028 Brad_Thompson STL Relief_Pitcher 73 190.0 25.08\n",
"1029 Tyler_Johnson STL Relief_Pitcher 74 180.0 25.73\n",
"1030 Chris_Narveson STL Relief_Pitcher 75 205.0 25.19\n",
"1031 Randy_Keisler STL Relief_Pitcher 75 190.0 31.01\n",
"1032 Josh_Kinney STL Relief_Pitcher 73 195.0 27.92\n",
"\n",
"[1033 rows x 6 columns]"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df = pd.read_csv('../../data/SOCR_MLB.tsv', sep = '\\t')\n",
"df"
]
},
{
"cell_type": "markdown",
"id": "0ef9578e-b899-4141-abf2-c2706b8bf1b8",
"metadata": {},
"source": [
"### Now, we'll rename the columns."
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "beb226b3-a940-4a4b-9ea8-04d83451c0e9",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Name</th>\n",
" <th>Team</th>\n",
" <th>Role</th>\n",
" <th>Height</th>\n",
" <th>Weight</th>\n",
" <th>Age</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Paul_Bako</td>\n",
" <td>BAL</td>\n",
" <td>Catcher</td>\n",
" <td>74</td>\n",
" <td>215.0</td>\n",
" <td>34.69</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Ramon_Hernandez</td>\n",
" <td>BAL</td>\n",
" <td>Catcher</td>\n",
" <td>72</td>\n",
" <td>210.0</td>\n",
" <td>30.78</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Kevin_Millar</td>\n",
" <td>BAL</td>\n",
" <td>First_Baseman</td>\n",
" <td>72</td>\n",
" <td>210.0</td>\n",
" <td>35.43</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>Chris_Gomez</td>\n",
" <td>BAL</td>\n",
" <td>First_Baseman</td>\n",
" <td>73</td>\n",
" <td>188.0</td>\n",
" <td>35.71</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>Brian_Roberts</td>\n",
" <td>BAL</td>\n",
" <td>Second_Baseman</td>\n",
" <td>69</td>\n",
" <td>176.0</td>\n",
" <td>29.39</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1028</th>\n",
" <td>Brad_Thompson</td>\n",
" <td>STL</td>\n",
" <td>Relief_Pitcher</td>\n",
" <td>73</td>\n",
" <td>190.0</td>\n",
" <td>25.08</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1029</th>\n",
" <td>Tyler_Johnson</td>\n",
" <td>STL</td>\n",
" <td>Relief_Pitcher</td>\n",
" <td>74</td>\n",
" <td>180.0</td>\n",
" <td>25.73</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1030</th>\n",
" <td>Chris_Narveson</td>\n",
" <td>STL</td>\n",
" <td>Relief_Pitcher</td>\n",
" <td>75</td>\n",
" <td>205.0</td>\n",
" <td>25.19</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1031</th>\n",
" <td>Randy_Keisler</td>\n",
" <td>STL</td>\n",
" <td>Relief_Pitcher</td>\n",
" <td>75</td>\n",
" <td>190.0</td>\n",
" <td>31.01</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1032</th>\n",
" <td>Josh_Kinney</td>\n",
" <td>STL</td>\n",
" <td>Relief_Pitcher</td>\n",
" <td>73</td>\n",
" <td>195.0</td>\n",
" <td>27.92</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>1033 rows × 6 columns</p>\n",
"</div>"
],
"text/plain": [
" Name Team Role Height Weight Age\n",
"0 Paul_Bako BAL Catcher 74 215.0 34.69\n",
"1 Ramon_Hernandez BAL Catcher 72 210.0 30.78\n",
"2 Kevin_Millar BAL First_Baseman 72 210.0 35.43\n",
"3 Chris_Gomez BAL First_Baseman 73 188.0 35.71\n",
"4 Brian_Roberts BAL Second_Baseman 69 176.0 29.39\n",
"... ... ... ... ... ... ...\n",
"1028 Brad_Thompson STL Relief_Pitcher 73 190.0 25.08\n",
"1029 Tyler_Johnson STL Relief_Pitcher 74 180.0 25.73\n",
"1030 Chris_Narveson STL Relief_Pitcher 75 205.0 25.19\n",
"1031 Randy_Keisler STL Relief_Pitcher 75 190.0 31.01\n",
"1032 Josh_Kinney STL Relief_Pitcher 73 195.0 27.92\n",
"\n",
"[1033 rows x 6 columns]"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.columns = ['Name', 'Team', 'Role', 'Height', 'Weight', 'Age']\n",
"df"
]
},
{
"cell_type": "markdown",
"id": "2319f4b4-39c1-464c-ae9d-06a24ab33a07",
"metadata": {},
"source": [
"## 2. Calculate mean confidence interval"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "4d45e822-f539-4753-8d1a-02bb16d15b9b",
"metadata": {},
"outputs": [],
"source": [
"import scipy.stats"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "2d2a016b-a738-4f41-bdf6-8c25b03302a1",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "f7e1836d-b54f-4510-bf3f-80d843585e38",
"metadata": {},
"outputs": [],
"source": [
"def mean_confidence_interval(data, confidence=0.95):\n",
" a = 1.0 * data\n",
" n = len(a)\n",
" m, se = np.mean(a), scipy.stats.sem(a)\n",
" h = scipy.stats.t.ppf((1+confidence)/2, n-1) * se\n",
" return m, h"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "9186a1d0-18ad-4d6d-badd-bc3d0200f6a2",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Mean_Weight</th>\n",
" <th>Mean_Height</th>\n",
" <th>Mean_Age</th>\n",
" </tr>\n",
" <tr>\n",
" <th>Role</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>Catcher</th>\n",
" <td>204.653333</td>\n",
" <td>72.706667</td>\n",
" <td>29.651333</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Designated_Hitter</th>\n",
" <td>220.888889</td>\n",
" <td>74.222222</td>\n",
" <td>30.389444</td>\n",
" </tr>\n",
" <tr>\n",
" <th>First_Baseman</th>\n",
" <td>213.109091</td>\n",
" <td>74.000000</td>\n",
" <td>29.474727</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Outfielder</th>\n",
" <td>199.113402</td>\n",
" <td>73.010309</td>\n",
" <td>28.953454</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Relief_Pitcher</th>\n",
" <td>203.517460</td>\n",
" <td>74.374603</td>\n",
" <td>28.539810</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Second_Baseman</th>\n",
" <td>184.344828</td>\n",
" <td>71.362069</td>\n",
" <td>29.026897</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Shortstop</th>\n",
" <td>182.923077</td>\n",
" <td>71.903846</td>\n",
" <td>28.402885</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Starting_Pitcher</th>\n",
" <td>205.163636</td>\n",
" <td>74.719457</td>\n",
" <td>28.236109</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Third_Baseman</th>\n",
" <td>200.955556</td>\n",
" <td>73.044444</td>\n",
" <td>28.691111</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Mean_Weight Mean_Height Mean_Age\n",
"Role \n",
"Catcher 204.653333 72.706667 29.651333\n",
"Designated_Hitter 220.888889 74.222222 30.389444\n",
"First_Baseman 213.109091 74.000000 29.474727\n",
"Outfielder 199.113402 73.010309 28.953454\n",
"Relief_Pitcher 203.517460 74.374603 28.539810\n",
"Second_Baseman 184.344828 71.362069 29.026897\n",
"Shortstop 182.923077 71.903846 28.402885\n",
"Starting_Pitcher 205.163636 74.719457 28.236109\n",
"Third_Baseman 200.955556 73.044444 28.691111"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.groupby('Role').agg(\n",
" Mean_Weight = ('Weight', 'mean'),\n",
" Mean_Height = ('Height', 'mean'),\n",
" Mean_Age = ('Age', 'mean')\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "76788dff-f408-4f3a-b1bc-3d0de5ce69b9",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"p: 0.85, 1st_Baseman_Height: 28.56..30.39, 2nd_Baseman_Height: 28.18..29.87\n",
"p: 0.90, 1st_Baseman_Height: 28.42..30.53, 2nd_Baseman_Height: 28.06..29.99\n",
"p: 0.95, 1st_Baseman_Height: 28.22..30.73, 2nd_Baseman_Height: 27.87..30.18\n"
]
}
],
"source": [
"for p in [0.85, 0.9, 0.95]:\n",
" m1, h1 = mean_confidence_interval(df.loc[df['Role'] == 'First_Baseman', ['Age']], p)\n",
" m2, h2 = mean_confidence_interval(df.loc[df['Role'] == 'Second_Baseman', ['Age']], p)\n",
" print(f\"p: {p:.2f}, 1st_Baseman_Height: {m1 - h1[0]:.2f}..{m1 + h1[0]:.2f}, 2nd_Baseman_Height: {m2 - h2[0]:.2f}..{m2 + h2[0]:.2f}\")"
]
},
{
"cell_type": "markdown",
"id": "cbcd7c63-0b28-47ca-a335-d00dd23ed048",
"metadata": {},
"source": [
"## 3. Student's t-test"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "1c91f652-705a-45af-9100-b85ae7fb4145",
"metadata": {},
"outputs": [],
"source": [
"from scipy.stats import ttest_ind"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "7f587af7-4a4e-4744-88d1-2d285ac699f3",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"t-val: 0.53, p-val: 0.60\n"
]
}
],
"source": [
"tval, pval = ttest_ind(df.loc[df['Role'] == 'First_Baseman', ['Age']], df.loc[df['Role'] == 'Second_Baseman', ['Age']], equal_var=False)\n",
"print(f\"t-val: {tval[0]:.2f}, p-val: {pval[0]:.2f}\")"
]
},
{
"cell_type": "markdown",
"id": "a325bcce-2cc3-4e09-a5c6-d2fd573be0e3",
"metadata": {},
"source": [
"> Since, p-val = 0.60 (greater than 0.05). It fails to reject the Null Hypothesis (H0)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9d86266a-510f-41f9-97a4-740796d8dc27",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.13.4"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

@ -0,0 +1,591 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "fb546a97-c009-46af-91bb-99b5962302db",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "778e4798-ab37-4e4b-9844-fc8f44a31f34",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Adam_Donachie</th>\n",
" <th>BAL</th>\n",
" <th>Catcher</th>\n",
" <th>74</th>\n",
" <th>180</th>\n",
" <th>22.99</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Paul_Bako</td>\n",
" <td>BAL</td>\n",
" <td>Catcher</td>\n",
" <td>74</td>\n",
" <td>215.0</td>\n",
" <td>34.69</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Ramon_Hernandez</td>\n",
" <td>BAL</td>\n",
" <td>Catcher</td>\n",
" <td>72</td>\n",
" <td>210.0</td>\n",
" <td>30.78</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Kevin_Millar</td>\n",
" <td>BAL</td>\n",
" <td>First_Baseman</td>\n",
" <td>72</td>\n",
" <td>210.0</td>\n",
" <td>35.43</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>Chris_Gomez</td>\n",
" <td>BAL</td>\n",
" <td>First_Baseman</td>\n",
" <td>73</td>\n",
" <td>188.0</td>\n",
" <td>35.71</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>Brian_Roberts</td>\n",
" <td>BAL</td>\n",
" <td>Second_Baseman</td>\n",
" <td>69</td>\n",
" <td>176.0</td>\n",
" <td>29.39</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1028</th>\n",
" <td>Brad_Thompson</td>\n",
" <td>STL</td>\n",
" <td>Relief_Pitcher</td>\n",
" <td>73</td>\n",
" <td>190.0</td>\n",
" <td>25.08</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1029</th>\n",
" <td>Tyler_Johnson</td>\n",
" <td>STL</td>\n",
" <td>Relief_Pitcher</td>\n",
" <td>74</td>\n",
" <td>180.0</td>\n",
" <td>25.73</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1030</th>\n",
" <td>Chris_Narveson</td>\n",
" <td>STL</td>\n",
" <td>Relief_Pitcher</td>\n",
" <td>75</td>\n",
" <td>205.0</td>\n",
" <td>25.19</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1031</th>\n",
" <td>Randy_Keisler</td>\n",
" <td>STL</td>\n",
" <td>Relief_Pitcher</td>\n",
" <td>75</td>\n",
" <td>190.0</td>\n",
" <td>31.01</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1032</th>\n",
" <td>Josh_Kinney</td>\n",
" <td>STL</td>\n",
" <td>Relief_Pitcher</td>\n",
" <td>73</td>\n",
" <td>195.0</td>\n",
" <td>27.92</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>1033 rows × 6 columns</p>\n",
"</div>"
],
"text/plain": [
" Adam_Donachie BAL Catcher 74 180 22.99\n",
"0 Paul_Bako BAL Catcher 74 215.0 34.69\n",
"1 Ramon_Hernandez BAL Catcher 72 210.0 30.78\n",
"2 Kevin_Millar BAL First_Baseman 72 210.0 35.43\n",
"3 Chris_Gomez BAL First_Baseman 73 188.0 35.71\n",
"4 Brian_Roberts BAL Second_Baseman 69 176.0 29.39\n",
"... ... ... ... .. ... ...\n",
"1028 Brad_Thompson STL Relief_Pitcher 73 190.0 25.08\n",
"1029 Tyler_Johnson STL Relief_Pitcher 74 180.0 25.73\n",
"1030 Chris_Narveson STL Relief_Pitcher 75 205.0 25.19\n",
"1031 Randy_Keisler STL Relief_Pitcher 75 190.0 31.01\n",
"1032 Josh_Kinney STL Relief_Pitcher 73 195.0 27.92\n",
"\n",
"[1033 rows x 6 columns]"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df = pd.read_table('../../data/SOCR_MLB.tsv')\n",
"df"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "b4f7df78-72e7-423c-8884-88ec32f5df89",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Name</th>\n",
" <th>Team</th>\n",
" <th>Role</th>\n",
" <th>Weight</th>\n",
" <th>Height</th>\n",
" <th>Age</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Paul_Bako</td>\n",
" <td>BAL</td>\n",
" <td>Catcher</td>\n",
" <td>74</td>\n",
" <td>215.0</td>\n",
" <td>34.69</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Ramon_Hernandez</td>\n",
" <td>BAL</td>\n",
" <td>Catcher</td>\n",
" <td>72</td>\n",
" <td>210.0</td>\n",
" <td>30.78</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Kevin_Millar</td>\n",
" <td>BAL</td>\n",
" <td>First_Baseman</td>\n",
" <td>72</td>\n",
" <td>210.0</td>\n",
" <td>35.43</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>Chris_Gomez</td>\n",
" <td>BAL</td>\n",
" <td>First_Baseman</td>\n",
" <td>73</td>\n",
" <td>188.0</td>\n",
" <td>35.71</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>Brian_Roberts</td>\n",
" <td>BAL</td>\n",
" <td>Second_Baseman</td>\n",
" <td>69</td>\n",
" <td>176.0</td>\n",
" <td>29.39</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1028</th>\n",
" <td>Brad_Thompson</td>\n",
" <td>STL</td>\n",
" <td>Relief_Pitcher</td>\n",
" <td>73</td>\n",
" <td>190.0</td>\n",
" <td>25.08</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1029</th>\n",
" <td>Tyler_Johnson</td>\n",
" <td>STL</td>\n",
" <td>Relief_Pitcher</td>\n",
" <td>74</td>\n",
" <td>180.0</td>\n",
" <td>25.73</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1030</th>\n",
" <td>Chris_Narveson</td>\n",
" <td>STL</td>\n",
" <td>Relief_Pitcher</td>\n",
" <td>75</td>\n",
" <td>205.0</td>\n",
" <td>25.19</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1031</th>\n",
" <td>Randy_Keisler</td>\n",
" <td>STL</td>\n",
" <td>Relief_Pitcher</td>\n",
" <td>75</td>\n",
" <td>190.0</td>\n",
" <td>31.01</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1032</th>\n",
" <td>Josh_Kinney</td>\n",
" <td>STL</td>\n",
" <td>Relief_Pitcher</td>\n",
" <td>73</td>\n",
" <td>195.0</td>\n",
" <td>27.92</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>1033 rows × 6 columns</p>\n",
"</div>"
],
"text/plain": [
" Name Team Role Weight Height Age\n",
"0 Paul_Bako BAL Catcher 74 215.0 34.69\n",
"1 Ramon_Hernandez BAL Catcher 72 210.0 30.78\n",
"2 Kevin_Millar BAL First_Baseman 72 210.0 35.43\n",
"3 Chris_Gomez BAL First_Baseman 73 188.0 35.71\n",
"4 Brian_Roberts BAL Second_Baseman 69 176.0 29.39\n",
"... ... ... ... ... ... ...\n",
"1028 Brad_Thompson STL Relief_Pitcher 73 190.0 25.08\n",
"1029 Tyler_Johnson STL Relief_Pitcher 74 180.0 25.73\n",
"1030 Chris_Narveson STL Relief_Pitcher 75 205.0 25.19\n",
"1031 Randy_Keisler STL Relief_Pitcher 75 190.0 31.01\n",
"1032 Josh_Kinney STL Relief_Pitcher 73 195.0 27.92\n",
"\n",
"[1033 rows x 6 columns]"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.columns = ['Name', 'Team', 'Role', 'Weight', 'Height', 'Age']\n",
"df"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "5028c04b-71b3-4632-92f8-cb405fd34722",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import scipy.stats"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "b60fb4ab-f0fb-489f-b431-075b53bbb687",
"metadata": {},
"outputs": [],
"source": [
"def mean_confidence_interval(data, confidence=0.95):\n",
" a = 1.0 * data\n",
" n = len(a)\n",
" m, se = np.mean(a), scipy.stats.sem(a)\n",
" h = se * scipy.stats.t.ppf((1+confidence)/2, n-1)\n",
" return m, h"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "17da783d-f98e-40d3-be32-bcc29ce3cd0d",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Mean_Height</th>\n",
" <th>Count</th>\n",
" </tr>\n",
" <tr>\n",
" <th>Role</th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>Catcher</th>\n",
" <td>204.653333</td>\n",
" <td>75</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Designated_Hitter</th>\n",
" <td>220.888889</td>\n",
" <td>18</td>\n",
" </tr>\n",
" <tr>\n",
" <th>First_Baseman</th>\n",
" <td>213.109091</td>\n",
" <td>55</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Outfielder</th>\n",
" <td>199.113402</td>\n",
" <td>194</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Relief_Pitcher</th>\n",
" <td>203.517460</td>\n",
" <td>315</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Second_Baseman</th>\n",
" <td>184.344828</td>\n",
" <td>58</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Shortstop</th>\n",
" <td>182.923077</td>\n",
" <td>52</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Starting_Pitcher</th>\n",
" <td>205.163636</td>\n",
" <td>221</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Third_Baseman</th>\n",
" <td>200.955556</td>\n",
" <td>45</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Mean_Height Count\n",
"Role \n",
"Catcher 204.653333 75\n",
"Designated_Hitter 220.888889 18\n",
"First_Baseman 213.109091 55\n",
"Outfielder 199.113402 194\n",
"Relief_Pitcher 203.517460 315\n",
"Second_Baseman 184.344828 58\n",
"Shortstop 182.923077 52\n",
"Starting_Pitcher 205.163636 221\n",
"Third_Baseman 200.955556 45"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.groupby('Role').agg(\n",
" Mean_Height=('Height', 'mean'),\n",
" Count=('Age', 'count')\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "e4fb81c4-069c-4aa7-bd07-5a4e3f450fb9",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"p: 0.85, 1st_Baseman_Height: 209.36..216.86, 3rd_Baseman_Height: 196.99..204.92\n",
"p: 0.9, 1st_Baseman_Height: 208.82..217.40, 3rd_Baseman_Height: 196.41..205.50\n",
"p: 0.95, 1st_Baseman_Height: 207.97..218.25, 3rd_Baseman_Height: 195.51..206.41\n"
]
}
],
"source": [
"for p in [0.85, 0.9, 0.95]:\n",
" m1, h1 = mean_confidence_interval(df.loc[df['Role'] == 'First_Baseman', ['Height']], p)\n",
" m2, h2 = mean_confidence_interval(df.loc[df['Role'] == 'Third_Baseman', ['Height']], p)\n",
" print(f\"p: {p}, 1st_Baseman_Height: {m1-h1[0]:.2f}..{m1+h1[0]:.2f}, 3rd_Baseman_Height: {m2-h2[0]:.2f}..{m2+h2[0]:.2f}\")"
]
},
{
"cell_type": "markdown",
"id": "09b8fbc9-59e7-480e-b9ed-9b2dbe93247d",
"metadata": {},
"source": [
"# To test the hypothesis that first basemen are taller than third basemen"
]
},
{
"cell_type": "markdown",
"id": "e5f74ec7-415f-4a0e-941c-a7b1e6b900a4",
"metadata": {},
"source": [
"## We'll do the Student's t-test"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "53194c25-9470-4d73-ab6c-19dfe8b75da9",
"metadata": {},
"outputs": [],
"source": [
"from scipy.stats import ttest_ind"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "deaaaeb2-79d9-43e1-bf34-22b5befac82c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"t_val: 3.26, p-value: 0.00\n"
]
}
],
"source": [
"tval, pval = ttest_ind(df.loc[df['Role'] == 'First_Baseman', 'Height'], df.loc[df['Role'] == 'Third_Baseman', 'Height'], equal_var=False)\n",
"print(f\"t_val: {tval:.2f}, p-value: {pval:.2f}\")"
]
},
{
"cell_type": "markdown",
"id": "69b6a712-21bc-4f52-8a30-4ff4d5fdecf7",
"metadata": {},
"source": [
"> Since p-value is zero, we failed to reject the Alternate Hypothesis (H1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "754515cc-894f-4dfa-8a97-c29383c005d3",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.13.4"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

@ -0,0 +1,568 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Introduction to Probability and Statistics\n",
"In this notebook, we will play around with some of the concepts we have previously discussed. Many concepts from probability and statistics are well-represented in major libraries for data processing in Python, such as `numpy` and `pandas`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import pandas as pd\n",
"import random\n",
"import matplotlib.pyplot as plt"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Random Variables and Distributions\n",
"Let's start with drawing a sample of 30 values from a uniform distribution from 0 to 9. We will also compute mean and variance."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"sample = [ random.randint(0,10) for _ in range(30) ]\n",
"print(f\"Sample: {sample}\")\n",
"print(f\"Mean = {np.mean(sample)}\")\n",
"print(f\"Variance = {np.var(sample)}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To visually estimate how many different values are there in the sample, we can plot the **histogram**:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"plt.hist(sample)\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Analyzing Real Data\n",
"\n",
"Mean and variance are very important when analyzing real-world data. Let's load the data about baseball players from [SOCR MLB Height/Weight Data](http://wiki.stat.ucla.edu/socr/index.php/SOCR_Data_MLB_HeightsWeights)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"df = pd.read_csv(\"../../data/SOCR_MLB.tsv\",sep='\\t', header=None, names=['Name','Team','Role','Weight','Height','Age'])\n",
"df\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"> We are using a package called [**Pandas**](https://pandas.pydata.org/) here for data analysis. We will talk more about Pandas and working with data in Python later in this course.\n",
"\n",
"Let's compute average values for age, height and weight:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"df[['Age','Height','Weight']].mean()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now let's focus on height, and compute standard deviation and variance: "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(list(df['Height'])[:20])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"mean = df['Height'].mean()\n",
"var = df['Height'].var()\n",
"std = df['Height'].std()\n",
"print(f\"Mean = {mean}\\nVariance = {var}\\nStandard Deviation = {std}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In addition to mean, it makes sense to look at the median value and quartiles. They can be visualized using a **box plot**:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"plt.figure(figsize=(10,2))\n",
"plt.boxplot(df['Height'].ffill(), vert=False, showmeans=True)\n",
"plt.grid(color='gray', linestyle='dotted')\n",
"plt.tight_layout()\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can also make box plots of subsets of our dataset, for example, grouped by player role."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"df.boxplot(column='Height', by='Role', figsize=(10,8))\n",
"plt.xticks(rotation='vertical')\n",
"plt.tight_layout()\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"> **Note**: This diagram suggests, that on average, the heights of first basemen are higher than heights of second basemen. Later we will learn how we can test this hypothesis more formally, and how to demonstrate that our data is statistically significant to show that. \n",
"\n",
"Age, height and weight are all continuous random variables. What do you think their distribution is? A good way to find out is to plot the histogram of values: "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"df['Weight'].hist(bins=15, figsize=(10,6))\n",
"plt.suptitle('Weight distribution of MLB Players')\n",
"plt.xlabel('Weight')\n",
"plt.ylabel('Count')\n",
"plt.tight_layout()\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Normal Distribution\n",
"\n",
"Let's create an artificial sample of weights that follows a normal distribution with the same mean and variance as our real data:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"generated = np.random.normal(mean, std, 1000)\n",
"generated[:20]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"plt.figure(figsize=(10,6))\n",
"plt.hist(generated, bins=15)\n",
"plt.tight_layout()\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"plt.figure(figsize=(10,6))\n",
"plt.hist(np.random.normal(0,1,50000), bins=300)\n",
"plt.tight_layout()\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Since most values in real life are normally distributed, we should not use a uniform random number generator to generate sample data. Here is what happens if we try to generate weights with a uniform distribution (generated by `np.random.rand`):"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"wrong_sample = np.random.rand(1000)*2*std+mean-std\n",
"plt.figure(figsize=(10,6))\n",
"plt.hist(wrong_sample)\n",
"plt.tight_layout()\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Confidence Intervals\n",
"\n",
"Let's now calculate confidence intervals for the weights and heights of baseball players. We will use the code [from this stackoverflow discussion](https://stackoverflow.com/questions/15033511/compute-a-confidence-interval-from-sample-data):"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import scipy.stats\n",
"\n",
"def mean_confidence_interval(data, confidence=0.95):\n",
" a = 1.0 * np.array(data)\n",
" n = len(a)\n",
" m, se = np.mean(a), scipy.stats.sem(a)\n",
" h = se * scipy.stats.t.ppf((1 + confidence) / 2., n-1)\n",
" return m, h\n",
"\n",
"for p in [0.85, 0.9, 0.95]:\n",
" m, h = mean_confidence_interval(df['Weight'].fillna(method='pad'),p)\n",
" print(f\"p={p:.2f}, mean = {m:.2f} ± {h:.2f}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Hypothesis Testing\n",
"\n",
"Let's explore different roles in our baseball players dataset:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"df.groupby('Role').agg({ 'Weight' : 'mean', 'Height' : 'mean', 'Age' : 'count'}).rename(columns={ 'Age' : 'Count'})"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's test the hypothesis that First Basemen are taller than Second Basemen. The simplest way to do this is to test the confidence intervals:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for p in [0.85,0.9,0.95]:\n",
" m1, h1 = mean_confidence_interval(df.loc[df['Role']=='First_Baseman',['Height']],p)\n",
" m2, h2 = mean_confidence_interval(df.loc[df['Role']=='Second_Baseman',['Height']],p)\n",
" print(f'Conf={p:.2f}, 1st basemen height: {m1-h1[0]:.2f}..{m1+h1[0]:.2f}, 2nd basemen height: {m2-h2[0]:.2f}..{m2+h2[0]:.2f}')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can see that the intervals do not overlap.\n",
"\n",
"A statistically more correct way to prove the hypothesis is to use a **Student t-test**:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from scipy.stats import ttest_ind\n",
"\n",
"tval, pval = ttest_ind(df.loc[df['Role']=='First_Baseman',['Height']], df.loc[df['Role']=='Second_Baseman',['Height']],equal_var=False)\n",
"print(f\"T-value = {tval[0]:.2f}\\nP-value: {pval[0]}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The two values returned by the `ttest_ind` function are:\n",
"* p-value can be considered as the probability of two distributions having the same mean. In our case, it is very low, meaning that there is strong evidence supporting that first basemen are taller.\n",
"* t-value is the intermediate value of normalized mean difference that is used in the t-test, and it is compared against a threshold value for a given confidence value."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Simulating a Normal Distribution with the Central Limit Theorem\n",
"\n",
"The pseudo-random generator in Python is designed to give us a uniform distribution. If we want to create a generator for normal distribution, we can use the central limit theorem. To get a normally distributed value we will just compute a mean of a uniform-generated sample."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def normal_random(sample_size=100):\n",
" sample = [random.uniform(0,1) for _ in range(sample_size) ]\n",
" return sum(sample)/sample_size\n",
"\n",
"sample = [normal_random() for _ in range(100)]\n",
"plt.figure(figsize=(10,6))\n",
"plt.hist(sample)\n",
"plt.tight_layout()\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Correlation and Evil Baseball Corp\n",
"\n",
"Correlation allows us to find relations between data sequences. In our toy example, let's pretend there is an evil baseball corporation that pays its players according to their height - the taller the player is, the more money he/she gets. Suppose there is a base salary of $1000, and an additional bonus from $0 to $100, depending on height. We will take the real players from MLB, and compute their imaginary salaries:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"heights = df['Height'].fillna(method='pad')\n",
"salaries = 1000+(heights-heights.min())/(heights.max()-heights.mean())*100\n",
"print(list(zip(heights, salaries))[:10])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's now compute covariance and correlation of those sequences. `np.cov` will give us a so-called **covariance matrix**, which is an extension of covariance to multiple variables. The element $M_{ij}$ of the covariance matrix $M$ is a correlation between input variables $X_i$ and $X_j$, and diagonal values $M_{ii}$ is the variance of $X_{i}$. Similarly, `np.corrcoef` will give us the **correlation matrix**."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(f\"Covariance matrix:\\n{np.cov(heights, salaries)}\")\n",
"print(f\"Covariance = {np.cov(heights, salaries)[0,1]}\")\n",
"print(f\"Correlation = {np.corrcoef(heights, salaries)[0,1]}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A correlation equal to 1 means that there is a strong **linear relation** between two variables. We can visually see the linear relation by plotting one value against the other:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"plt.figure(figsize=(10,6))\n",
"plt.scatter(heights,salaries)\n",
"plt.tight_layout()\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's see what happens if the relation is not linear. Suppose that our corporation decided to hide the obvious linear dependency between heights and salaries, and introduced some non-linearity into the formula, such as `sin`:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"salaries = 1000+np.sin((heights-heights.min())/(heights.max()-heights.mean()))*100\n",
"print(f\"Correlation = {np.corrcoef(heights, salaries)[0,1]}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In this case, the correlation is slightly smaller, but it is still quite high. Now, to make the relation even less obvious, we might want to add some extra randomness by adding some random variable to the salary. Let's see what happens:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"salaries = 1000+np.sin((heights-heights.min())/(heights.max()-heights.mean()))*100+np.random.random(size=len(heights))*20-10\n",
"print(f\"Correlation = {np.corrcoef(heights, salaries)[0,1]}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"plt.figure(figsize=(10,6))\n",
"plt.scatter(heights, salaries)\n",
"plt.tight_layout()\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"> Can you guess why the dots line up into vertical lines like this?\n",
"\n",
"We have observed the correlation between an artificially engineered concept like salary and the observed variable *height*. Let's also see if the two observed variables, such as height and weight, correlate too:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"np.corrcoef(df['Height'].ffill(),df['Weight'])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Unfortunately, we did not get any results - only some strange `nan` values. This is due to the fact that some of the values in our series are undefined, represented as `nan`, which causes the result of the operation to be undefined as well. By looking at the matrix we can see that `Weight` is the problematic column, because self-correlation between `Height` values has been computed.\n",
"\n",
"> This example shows the importance of **data preparation** and **cleaning**. Without proper data we cannot compute anything.\n",
"\n",
"Let's use `fillna` method to fill the missing values, and compute the correlation: "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"np.corrcoef(df['Height'].fillna(method='pad'), df['Weight'])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"There is indeed a correlation, but not such a strong one as in our artificial example. Indeed, if we look at the scatter plot of one value against the other, the relation would be much less obvious:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"plt.figure(figsize=(10,6))\n",
"plt.scatter(df['Weight'],df['Height'])\n",
"plt.xlabel('Weight')\n",
"plt.ylabel('Height')\n",
"plt.tight_layout()\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Conclusion\n",
"\n",
"In this notebook we have learnt how to perform basic operations on data to compute statistical functions. We now know how to use a sound apparatus of math and statistics in order to prove some hypotheses, and how to compute confidence intervals for arbitrary variables given a data sample. "
]
}
],
"metadata": {
"interpreter": {
"hash": "86193a1ab0ba47eac1c69c1756090baa3b420b3eea7d4aafab8b85f8b312f0c5"
},
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}

File diff suppressed because one or more lines are too long

@ -0,0 +1,636 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "860a77fe-e073-47ec-858f-3c81e52a4167",
"metadata": {},
"source": [
"# To test the hypothesis that the First_Baseman are older than Second_Baseman"
]
},
{
"cell_type": "markdown",
"id": "89e2a25b-42d4-4abe-9ea3-7b359c1e0092",
"metadata": {},
"source": [
"## 1. Imort the dataset"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "08752c0b-23b2-4e06-afb1-e9f5aed67c5b",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "b41e6fe7-0cc3-46e5-95df-7fe063d8d06d",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Adam_Donachie</th>\n",
" <th>BAL</th>\n",
" <th>Catcher</th>\n",
" <th>74</th>\n",
" <th>180</th>\n",
" <th>22.99</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Paul_Bako</td>\n",
" <td>BAL</td>\n",
" <td>Catcher</td>\n",
" <td>74</td>\n",
" <td>215.0</td>\n",
" <td>34.69</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Ramon_Hernandez</td>\n",
" <td>BAL</td>\n",
" <td>Catcher</td>\n",
" <td>72</td>\n",
" <td>210.0</td>\n",
" <td>30.78</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Kevin_Millar</td>\n",
" <td>BAL</td>\n",
" <td>First_Baseman</td>\n",
" <td>72</td>\n",
" <td>210.0</td>\n",
" <td>35.43</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>Chris_Gomez</td>\n",
" <td>BAL</td>\n",
" <td>First_Baseman</td>\n",
" <td>73</td>\n",
" <td>188.0</td>\n",
" <td>35.71</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>Brian_Roberts</td>\n",
" <td>BAL</td>\n",
" <td>Second_Baseman</td>\n",
" <td>69</td>\n",
" <td>176.0</td>\n",
" <td>29.39</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1028</th>\n",
" <td>Brad_Thompson</td>\n",
" <td>STL</td>\n",
" <td>Relief_Pitcher</td>\n",
" <td>73</td>\n",
" <td>190.0</td>\n",
" <td>25.08</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1029</th>\n",
" <td>Tyler_Johnson</td>\n",
" <td>STL</td>\n",
" <td>Relief_Pitcher</td>\n",
" <td>74</td>\n",
" <td>180.0</td>\n",
" <td>25.73</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1030</th>\n",
" <td>Chris_Narveson</td>\n",
" <td>STL</td>\n",
" <td>Relief_Pitcher</td>\n",
" <td>75</td>\n",
" <td>205.0</td>\n",
" <td>25.19</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1031</th>\n",
" <td>Randy_Keisler</td>\n",
" <td>STL</td>\n",
" <td>Relief_Pitcher</td>\n",
" <td>75</td>\n",
" <td>190.0</td>\n",
" <td>31.01</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1032</th>\n",
" <td>Josh_Kinney</td>\n",
" <td>STL</td>\n",
" <td>Relief_Pitcher</td>\n",
" <td>73</td>\n",
" <td>195.0</td>\n",
" <td>27.92</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>1033 rows × 6 columns</p>\n",
"</div>"
],
"text/plain": [
" Adam_Donachie BAL Catcher 74 180 22.99\n",
"0 Paul_Bako BAL Catcher 74 215.0 34.69\n",
"1 Ramon_Hernandez BAL Catcher 72 210.0 30.78\n",
"2 Kevin_Millar BAL First_Baseman 72 210.0 35.43\n",
"3 Chris_Gomez BAL First_Baseman 73 188.0 35.71\n",
"4 Brian_Roberts BAL Second_Baseman 69 176.0 29.39\n",
"... ... ... ... .. ... ...\n",
"1028 Brad_Thompson STL Relief_Pitcher 73 190.0 25.08\n",
"1029 Tyler_Johnson STL Relief_Pitcher 74 180.0 25.73\n",
"1030 Chris_Narveson STL Relief_Pitcher 75 205.0 25.19\n",
"1031 Randy_Keisler STL Relief_Pitcher 75 190.0 31.01\n",
"1032 Josh_Kinney STL Relief_Pitcher 73 195.0 27.92\n",
"\n",
"[1033 rows x 6 columns]"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df = pd.read_csv('../../data/SOCR_MLB.tsv', sep = '\\t')\n",
"df"
]
},
{
"cell_type": "markdown",
"id": "0ef9578e-b899-4141-abf2-c2706b8bf1b8",
"metadata": {},
"source": [
"### Now, we'll rename the columns."
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "beb226b3-a940-4a4b-9ea8-04d83451c0e9",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Name</th>\n",
" <th>Team</th>\n",
" <th>Role</th>\n",
" <th>Height</th>\n",
" <th>Weight</th>\n",
" <th>Age</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Paul_Bako</td>\n",
" <td>BAL</td>\n",
" <td>Catcher</td>\n",
" <td>74</td>\n",
" <td>215.0</td>\n",
" <td>34.69</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Ramon_Hernandez</td>\n",
" <td>BAL</td>\n",
" <td>Catcher</td>\n",
" <td>72</td>\n",
" <td>210.0</td>\n",
" <td>30.78</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Kevin_Millar</td>\n",
" <td>BAL</td>\n",
" <td>First_Baseman</td>\n",
" <td>72</td>\n",
" <td>210.0</td>\n",
" <td>35.43</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>Chris_Gomez</td>\n",
" <td>BAL</td>\n",
" <td>First_Baseman</td>\n",
" <td>73</td>\n",
" <td>188.0</td>\n",
" <td>35.71</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>Brian_Roberts</td>\n",
" <td>BAL</td>\n",
" <td>Second_Baseman</td>\n",
" <td>69</td>\n",
" <td>176.0</td>\n",
" <td>29.39</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1028</th>\n",
" <td>Brad_Thompson</td>\n",
" <td>STL</td>\n",
" <td>Relief_Pitcher</td>\n",
" <td>73</td>\n",
" <td>190.0</td>\n",
" <td>25.08</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1029</th>\n",
" <td>Tyler_Johnson</td>\n",
" <td>STL</td>\n",
" <td>Relief_Pitcher</td>\n",
" <td>74</td>\n",
" <td>180.0</td>\n",
" <td>25.73</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1030</th>\n",
" <td>Chris_Narveson</td>\n",
" <td>STL</td>\n",
" <td>Relief_Pitcher</td>\n",
" <td>75</td>\n",
" <td>205.0</td>\n",
" <td>25.19</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1031</th>\n",
" <td>Randy_Keisler</td>\n",
" <td>STL</td>\n",
" <td>Relief_Pitcher</td>\n",
" <td>75</td>\n",
" <td>190.0</td>\n",
" <td>31.01</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1032</th>\n",
" <td>Josh_Kinney</td>\n",
" <td>STL</td>\n",
" <td>Relief_Pitcher</td>\n",
" <td>73</td>\n",
" <td>195.0</td>\n",
" <td>27.92</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>1033 rows × 6 columns</p>\n",
"</div>"
],
"text/plain": [
" Name Team Role Height Weight Age\n",
"0 Paul_Bako BAL Catcher 74 215.0 34.69\n",
"1 Ramon_Hernandez BAL Catcher 72 210.0 30.78\n",
"2 Kevin_Millar BAL First_Baseman 72 210.0 35.43\n",
"3 Chris_Gomez BAL First_Baseman 73 188.0 35.71\n",
"4 Brian_Roberts BAL Second_Baseman 69 176.0 29.39\n",
"... ... ... ... ... ... ...\n",
"1028 Brad_Thompson STL Relief_Pitcher 73 190.0 25.08\n",
"1029 Tyler_Johnson STL Relief_Pitcher 74 180.0 25.73\n",
"1030 Chris_Narveson STL Relief_Pitcher 75 205.0 25.19\n",
"1031 Randy_Keisler STL Relief_Pitcher 75 190.0 31.01\n",
"1032 Josh_Kinney STL Relief_Pitcher 73 195.0 27.92\n",
"\n",
"[1033 rows x 6 columns]"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.columns = ['Name', 'Team', 'Role', 'Height', 'Weight', 'Age']\n",
"df"
]
},
{
"cell_type": "markdown",
"id": "2319f4b4-39c1-464c-ae9d-06a24ab33a07",
"metadata": {},
"source": [
"## 2. Calculate mean confidence interval"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "4d45e822-f539-4753-8d1a-02bb16d15b9b",
"metadata": {},
"outputs": [],
"source": [
"import scipy.stats"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "2d2a016b-a738-4f41-bdf6-8c25b03302a1",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "f7e1836d-b54f-4510-bf3f-80d843585e38",
"metadata": {},
"outputs": [],
"source": [
"def mean_confidence_interval(data, confidence=0.95):\n",
" a = 1.0 * data\n",
" n = len(a)\n",
" m, se = np.mean(a), scipy.stats.sem(a)\n",
" h = scipy.stats.t.ppf((1+confidence)/2, n-1) * se\n",
" return m, h"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "9186a1d0-18ad-4d6d-badd-bc3d0200f6a2",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Mean_Weight</th>\n",
" <th>Mean_Height</th>\n",
" <th>Mean_Age</th>\n",
" </tr>\n",
" <tr>\n",
" <th>Role</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>Catcher</th>\n",
" <td>204.653333</td>\n",
" <td>72.706667</td>\n",
" <td>29.651333</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Designated_Hitter</th>\n",
" <td>220.888889</td>\n",
" <td>74.222222</td>\n",
" <td>30.389444</td>\n",
" </tr>\n",
" <tr>\n",
" <th>First_Baseman</th>\n",
" <td>213.109091</td>\n",
" <td>74.000000</td>\n",
" <td>29.474727</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Outfielder</th>\n",
" <td>199.113402</td>\n",
" <td>73.010309</td>\n",
" <td>28.953454</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Relief_Pitcher</th>\n",
" <td>203.517460</td>\n",
" <td>74.374603</td>\n",
" <td>28.539810</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Second_Baseman</th>\n",
" <td>184.344828</td>\n",
" <td>71.362069</td>\n",
" <td>29.026897</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Shortstop</th>\n",
" <td>182.923077</td>\n",
" <td>71.903846</td>\n",
" <td>28.402885</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Starting_Pitcher</th>\n",
" <td>205.163636</td>\n",
" <td>74.719457</td>\n",
" <td>28.236109</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Third_Baseman</th>\n",
" <td>200.955556</td>\n",
" <td>73.044444</td>\n",
" <td>28.691111</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Mean_Weight Mean_Height Mean_Age\n",
"Role \n",
"Catcher 204.653333 72.706667 29.651333\n",
"Designated_Hitter 220.888889 74.222222 30.389444\n",
"First_Baseman 213.109091 74.000000 29.474727\n",
"Outfielder 199.113402 73.010309 28.953454\n",
"Relief_Pitcher 203.517460 74.374603 28.539810\n",
"Second_Baseman 184.344828 71.362069 29.026897\n",
"Shortstop 182.923077 71.903846 28.402885\n",
"Starting_Pitcher 205.163636 74.719457 28.236109\n",
"Third_Baseman 200.955556 73.044444 28.691111"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.groupby('Role').agg(\n",
" Mean_Weight = ('Weight', 'mean'),\n",
" Mean_Height = ('Height', 'mean'),\n",
" Mean_Age = ('Age', 'mean')\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "76788dff-f408-4f3a-b1bc-3d0de5ce69b9",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"p: 0.85, 1st_Baseman_Height: 28.56..30.39, 2nd_Baseman_Height: 28.18..29.87\n",
"p: 0.90, 1st_Baseman_Height: 28.42..30.53, 2nd_Baseman_Height: 28.06..29.99\n",
"p: 0.95, 1st_Baseman_Height: 28.22..30.73, 2nd_Baseman_Height: 27.87..30.18\n"
]
}
],
"source": [
"for p in [0.85, 0.9, 0.95]:\n",
" m1, h1 = mean_confidence_interval(df.loc[df['Role'] == 'First_Baseman', ['Age']], p)\n",
" m2, h2 = mean_confidence_interval(df.loc[df['Role'] == 'Second_Baseman', ['Age']], p)\n",
" print(f\"p: {p:.2f}, 1st_Baseman_Height: {m1 - h1[0]:.2f}..{m1 + h1[0]:.2f}, 2nd_Baseman_Height: {m2 - h2[0]:.2f}..{m2 + h2[0]:.2f}\")"
]
},
{
"cell_type": "markdown",
"id": "cbcd7c63-0b28-47ca-a335-d00dd23ed048",
"metadata": {},
"source": [
"## 3. Student's t-test"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "1c91f652-705a-45af-9100-b85ae7fb4145",
"metadata": {},
"outputs": [],
"source": [
"from scipy.stats import ttest_ind"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "7f587af7-4a4e-4744-88d1-2d285ac699f3",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"t-val: 0.53, p-val: 0.60\n"
]
}
],
"source": [
"tval, pval = ttest_ind(df.loc[df['Role'] == 'First_Baseman', ['Age']], df.loc[df['Role'] == 'Second_Baseman', ['Age']], equal_var=False)\n",
"print(f\"t-val: {tval[0]:.2f}, p-val: {pval[0]:.2f}\")"
]
},
{
"cell_type": "markdown",
"id": "a325bcce-2cc3-4e09-a5c6-d2fd573be0e3",
"metadata": {},
"source": [
"> Since, p-val = 0.60 (greater than 0.05). It fails to reject the Null Hypothesis (H0)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9d86266a-510f-41f9-97a4-740796d8dc27",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.13.4"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

@ -0,0 +1,591 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "fb546a97-c009-46af-91bb-99b5962302db",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "778e4798-ab37-4e4b-9844-fc8f44a31f34",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Adam_Donachie</th>\n",
" <th>BAL</th>\n",
" <th>Catcher</th>\n",
" <th>74</th>\n",
" <th>180</th>\n",
" <th>22.99</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Paul_Bako</td>\n",
" <td>BAL</td>\n",
" <td>Catcher</td>\n",
" <td>74</td>\n",
" <td>215.0</td>\n",
" <td>34.69</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Ramon_Hernandez</td>\n",
" <td>BAL</td>\n",
" <td>Catcher</td>\n",
" <td>72</td>\n",
" <td>210.0</td>\n",
" <td>30.78</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Kevin_Millar</td>\n",
" <td>BAL</td>\n",
" <td>First_Baseman</td>\n",
" <td>72</td>\n",
" <td>210.0</td>\n",
" <td>35.43</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>Chris_Gomez</td>\n",
" <td>BAL</td>\n",
" <td>First_Baseman</td>\n",
" <td>73</td>\n",
" <td>188.0</td>\n",
" <td>35.71</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>Brian_Roberts</td>\n",
" <td>BAL</td>\n",
" <td>Second_Baseman</td>\n",
" <td>69</td>\n",
" <td>176.0</td>\n",
" <td>29.39</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1028</th>\n",
" <td>Brad_Thompson</td>\n",
" <td>STL</td>\n",
" <td>Relief_Pitcher</td>\n",
" <td>73</td>\n",
" <td>190.0</td>\n",
" <td>25.08</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1029</th>\n",
" <td>Tyler_Johnson</td>\n",
" <td>STL</td>\n",
" <td>Relief_Pitcher</td>\n",
" <td>74</td>\n",
" <td>180.0</td>\n",
" <td>25.73</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1030</th>\n",
" <td>Chris_Narveson</td>\n",
" <td>STL</td>\n",
" <td>Relief_Pitcher</td>\n",
" <td>75</td>\n",
" <td>205.0</td>\n",
" <td>25.19</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1031</th>\n",
" <td>Randy_Keisler</td>\n",
" <td>STL</td>\n",
" <td>Relief_Pitcher</td>\n",
" <td>75</td>\n",
" <td>190.0</td>\n",
" <td>31.01</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1032</th>\n",
" <td>Josh_Kinney</td>\n",
" <td>STL</td>\n",
" <td>Relief_Pitcher</td>\n",
" <td>73</td>\n",
" <td>195.0</td>\n",
" <td>27.92</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>1033 rows × 6 columns</p>\n",
"</div>"
],
"text/plain": [
" Adam_Donachie BAL Catcher 74 180 22.99\n",
"0 Paul_Bako BAL Catcher 74 215.0 34.69\n",
"1 Ramon_Hernandez BAL Catcher 72 210.0 30.78\n",
"2 Kevin_Millar BAL First_Baseman 72 210.0 35.43\n",
"3 Chris_Gomez BAL First_Baseman 73 188.0 35.71\n",
"4 Brian_Roberts BAL Second_Baseman 69 176.0 29.39\n",
"... ... ... ... .. ... ...\n",
"1028 Brad_Thompson STL Relief_Pitcher 73 190.0 25.08\n",
"1029 Tyler_Johnson STL Relief_Pitcher 74 180.0 25.73\n",
"1030 Chris_Narveson STL Relief_Pitcher 75 205.0 25.19\n",
"1031 Randy_Keisler STL Relief_Pitcher 75 190.0 31.01\n",
"1032 Josh_Kinney STL Relief_Pitcher 73 195.0 27.92\n",
"\n",
"[1033 rows x 6 columns]"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df = pd.read_table('../../data/SOCR_MLB.tsv')\n",
"df"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "b4f7df78-72e7-423c-8884-88ec32f5df89",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Name</th>\n",
" <th>Team</th>\n",
" <th>Role</th>\n",
" <th>Weight</th>\n",
" <th>Height</th>\n",
" <th>Age</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Paul_Bako</td>\n",
" <td>BAL</td>\n",
" <td>Catcher</td>\n",
" <td>74</td>\n",
" <td>215.0</td>\n",
" <td>34.69</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Ramon_Hernandez</td>\n",
" <td>BAL</td>\n",
" <td>Catcher</td>\n",
" <td>72</td>\n",
" <td>210.0</td>\n",
" <td>30.78</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Kevin_Millar</td>\n",
" <td>BAL</td>\n",
" <td>First_Baseman</td>\n",
" <td>72</td>\n",
" <td>210.0</td>\n",
" <td>35.43</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>Chris_Gomez</td>\n",
" <td>BAL</td>\n",
" <td>First_Baseman</td>\n",
" <td>73</td>\n",
" <td>188.0</td>\n",
" <td>35.71</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>Brian_Roberts</td>\n",
" <td>BAL</td>\n",
" <td>Second_Baseman</td>\n",
" <td>69</td>\n",
" <td>176.0</td>\n",
" <td>29.39</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1028</th>\n",
" <td>Brad_Thompson</td>\n",
" <td>STL</td>\n",
" <td>Relief_Pitcher</td>\n",
" <td>73</td>\n",
" <td>190.0</td>\n",
" <td>25.08</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1029</th>\n",
" <td>Tyler_Johnson</td>\n",
" <td>STL</td>\n",
" <td>Relief_Pitcher</td>\n",
" <td>74</td>\n",
" <td>180.0</td>\n",
" <td>25.73</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1030</th>\n",
" <td>Chris_Narveson</td>\n",
" <td>STL</td>\n",
" <td>Relief_Pitcher</td>\n",
" <td>75</td>\n",
" <td>205.0</td>\n",
" <td>25.19</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1031</th>\n",
" <td>Randy_Keisler</td>\n",
" <td>STL</td>\n",
" <td>Relief_Pitcher</td>\n",
" <td>75</td>\n",
" <td>190.0</td>\n",
" <td>31.01</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1032</th>\n",
" <td>Josh_Kinney</td>\n",
" <td>STL</td>\n",
" <td>Relief_Pitcher</td>\n",
" <td>73</td>\n",
" <td>195.0</td>\n",
" <td>27.92</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>1033 rows × 6 columns</p>\n",
"</div>"
],
"text/plain": [
" Name Team Role Weight Height Age\n",
"0 Paul_Bako BAL Catcher 74 215.0 34.69\n",
"1 Ramon_Hernandez BAL Catcher 72 210.0 30.78\n",
"2 Kevin_Millar BAL First_Baseman 72 210.0 35.43\n",
"3 Chris_Gomez BAL First_Baseman 73 188.0 35.71\n",
"4 Brian_Roberts BAL Second_Baseman 69 176.0 29.39\n",
"... ... ... ... ... ... ...\n",
"1028 Brad_Thompson STL Relief_Pitcher 73 190.0 25.08\n",
"1029 Tyler_Johnson STL Relief_Pitcher 74 180.0 25.73\n",
"1030 Chris_Narveson STL Relief_Pitcher 75 205.0 25.19\n",
"1031 Randy_Keisler STL Relief_Pitcher 75 190.0 31.01\n",
"1032 Josh_Kinney STL Relief_Pitcher 73 195.0 27.92\n",
"\n",
"[1033 rows x 6 columns]"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.columns = ['Name', 'Team', 'Role', 'Weight', 'Height', 'Age']\n",
"df"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "5028c04b-71b3-4632-92f8-cb405fd34722",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import scipy.stats"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "b60fb4ab-f0fb-489f-b431-075b53bbb687",
"metadata": {},
"outputs": [],
"source": [
"def mean_confidence_interval(data, confidence=0.95):\n",
" a = 1.0 * data\n",
" n = len(a)\n",
" m, se = np.mean(a), scipy.stats.sem(a)\n",
" h = se * scipy.stats.t.ppf((1+confidence)/2, n-1)\n",
" return m, h"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "17da783d-f98e-40d3-be32-bcc29ce3cd0d",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Mean_Height</th>\n",
" <th>Count</th>\n",
" </tr>\n",
" <tr>\n",
" <th>Role</th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>Catcher</th>\n",
" <td>204.653333</td>\n",
" <td>75</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Designated_Hitter</th>\n",
" <td>220.888889</td>\n",
" <td>18</td>\n",
" </tr>\n",
" <tr>\n",
" <th>First_Baseman</th>\n",
" <td>213.109091</td>\n",
" <td>55</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Outfielder</th>\n",
" <td>199.113402</td>\n",
" <td>194</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Relief_Pitcher</th>\n",
" <td>203.517460</td>\n",
" <td>315</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Second_Baseman</th>\n",
" <td>184.344828</td>\n",
" <td>58</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Shortstop</th>\n",
" <td>182.923077</td>\n",
" <td>52</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Starting_Pitcher</th>\n",
" <td>205.163636</td>\n",
" <td>221</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Third_Baseman</th>\n",
" <td>200.955556</td>\n",
" <td>45</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Mean_Height Count\n",
"Role \n",
"Catcher 204.653333 75\n",
"Designated_Hitter 220.888889 18\n",
"First_Baseman 213.109091 55\n",
"Outfielder 199.113402 194\n",
"Relief_Pitcher 203.517460 315\n",
"Second_Baseman 184.344828 58\n",
"Shortstop 182.923077 52\n",
"Starting_Pitcher 205.163636 221\n",
"Third_Baseman 200.955556 45"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.groupby('Role').agg(\n",
" Mean_Height=('Height', 'mean'),\n",
" Count=('Age', 'count')\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "e4fb81c4-069c-4aa7-bd07-5a4e3f450fb9",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"p: 0.85, 1st_Baseman_Height: 209.36..216.86, 3rd_Baseman_Height: 196.99..204.92\n",
"p: 0.9, 1st_Baseman_Height: 208.82..217.40, 3rd_Baseman_Height: 196.41..205.50\n",
"p: 0.95, 1st_Baseman_Height: 207.97..218.25, 3rd_Baseman_Height: 195.51..206.41\n"
]
}
],
"source": [
"for p in [0.85, 0.9, 0.95]:\n",
" m1, h1 = mean_confidence_interval(df.loc[df['Role'] == 'First_Baseman', ['Height']], p)\n",
" m2, h2 = mean_confidence_interval(df.loc[df['Role'] == 'Third_Baseman', ['Height']], p)\n",
" print(f\"p: {p}, 1st_Baseman_Height: {m1-h1[0]:.2f}..{m1+h1[0]:.2f}, 3rd_Baseman_Height: {m2-h2[0]:.2f}..{m2+h2[0]:.2f}\")"
]
},
{
"cell_type": "markdown",
"id": "09b8fbc9-59e7-480e-b9ed-9b2dbe93247d",
"metadata": {},
"source": [
"# To test the hypothesis that first basemen are taller than third basemen"
]
},
{
"cell_type": "markdown",
"id": "e5f74ec7-415f-4a0e-941c-a7b1e6b900a4",
"metadata": {},
"source": [
"## We'll do the Student's t-test"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "53194c25-9470-4d73-ab6c-19dfe8b75da9",
"metadata": {},
"outputs": [],
"source": [
"from scipy.stats import ttest_ind"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "deaaaeb2-79d9-43e1-bf34-22b5befac82c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"t_val: 3.26, p-value: 0.00\n"
]
}
],
"source": [
"tval, pval = ttest_ind(df.loc[df['Role'] == 'First_Baseman', 'Height'], df.loc[df['Role'] == 'Third_Baseman', 'Height'], equal_var=False)\n",
"print(f\"t_val: {tval:.2f}, p-value: {pval:.2f}\")"
]
},
{
"cell_type": "markdown",
"id": "69b6a712-21bc-4f52-8a30-4ff4d5fdecf7",
"metadata": {},
"source": [
"> Since p-value is zero, we failed to reject the Alternate Hypothesis (H1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "754515cc-894f-4dfa-8a97-c29383c005d3",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.13.4"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load Diff

@ -0,0 +1,87 @@
"""
Hello World - Data Science Style!
This is your very first data science program. It introduces you to the basic
concepts of working with data in Python.
What you'll learn:
- How to create a simple dataset
- How to display data
- How to work with Python lists and dictionaries
- Basic data manipulation
Prerequisites: Just Python installed on your computer!
"""
# Let's start with the classic "Hello, World!" but with a data science twist
print("=" * 50)
print("Hello, World of Data Science!")
print("=" * 50)
print()
# In data science, we work with data. Let's create our first simple dataset.
# We'll use a list to store information about students and their test scores.
# A list is a collection of items in Python, written with square brackets []
students = ["Alice", "Bob", "Charlie", "Diana", "Eve"]
scores = [85, 92, 78, 95, 88]
print("Our Dataset:")
print("-" * 50)
print("Students:", students)
print("Scores:", scores)
print()
# Now let's do something useful with this data!
# We can find basic statistics about the scores
# Find the highest score
highest_score = max(scores)
print(f"📊 Highest score: {highest_score}")
# Find the lowest score
lowest_score = min(scores)
print(f"📊 Lowest score: {lowest_score}")
# Calculate the average score
# sum() adds all numbers together, len() tells us how many items we have
average_score = sum(scores) / len(scores)
print(f"📊 Average score: {average_score:.2f}") # .2f means show 2 decimal places
print()
# Let's find who got the highest score
# We use index() to find where the highest_score is in our list
top_student_index = scores.index(highest_score)
top_student = students[top_student_index]
print(f"🏆 Top student: {top_student} with a score of {highest_score}")
print()
# Now let's organize this data in a more structured way
# We'll use a dictionary - it pairs keys (student names) with values (scores)
print("Student Scores (organized as key-value pairs):")
print("-" * 50)
# Create a dictionary by pairing students with their scores
student_scores = {}
for i in range(len(students)):
student_scores[students[i]] = scores[i]
# Display each student and their score
for student, score in student_scores.items():
# Add a special marker for the top student
marker = "" if student == top_student else " "
print(f"{marker} {student}: {score} points")
print()
print("=" * 50)
print("Congratulations! You've completed your first data science program!")
print("=" * 50)
# What did we just do?
# 1. Created a simple dataset (student names and scores)
# 2. Performed basic analysis (max, min, average)
# 3. Found insights (who is the top student)
# 4. Organized the data in a useful structure (dictionary)
#
# These are the fundamental building blocks of data science!
# Next, you'll learn to work with real datasets using powerful libraries.

@ -0,0 +1,128 @@
"""
Loading and Exploring Data
In real data science projects, you'll work with data stored in files.
This example shows you how to load data from a CSV file and explore it.
What you'll learn:
- How to load data from a CSV file
- How to view basic information about your dataset
- How to display the first/last rows
- How to get summary statistics
Prerequisites: pandas library (install with: pip install pandas)
"""
# Import the pandas library - it's the most popular tool for working with data in Python
# We give it the short name 'pd' so we can type less
import pandas as pd
print("=" * 70)
print("Welcome to Data Loading and Exploration!")
print("=" * 70)
print()
# Step 1: Load data from a CSV file
# CSV stands for "Comma-Separated Values" - a common format for storing data
# We'll use the birds dataset that comes with this repository
print("📂 Loading data from birds.csv...")
print()
# Load the data into a DataFrame (think of it as a smart spreadsheet)
# A DataFrame is pandas' main data structure - it organizes data in rows and columns
data = pd.read_csv('../data/birds.csv')
print("✅ Data loaded successfully!")
print()
# Step 2: Get basic information about the dataset
print("-" * 70)
print("BASIC DATASET INFORMATION")
print("-" * 70)
# How many rows and columns do we have?
num_rows, num_columns = data.shape
print(f"📊 Dataset size: {num_rows} rows × {num_columns} columns")
print()
# What are the column names?
print("📋 Column names:")
for i, column in enumerate(data.columns, 1):
print(f" {i}. {column}")
print()
# Step 3: Look at the first few rows of data
# This gives us a quick preview of what the data looks like
print("-" * 70)
print("FIRST 5 ROWS OF DATA (Preview)")
print("-" * 70)
print(data.head()) # head() shows the first 5 rows by default
print()
# Step 4: Look at the last few rows
print("-" * 70)
print("LAST 3 ROWS OF DATA")
print("-" * 70)
print(data.tail(3)) # tail(3) shows the last 3 rows
print()
# Step 5: Get information about data types
print("-" * 70)
print("DATA TYPES AND NON-NULL COUNTS")
print("-" * 70)
print(data.info()) # Shows column names, data types, and count of non-null values
print()
# Step 6: Get statistical summary
print("-" * 70)
print("STATISTICAL SUMMARY (for numerical columns)")
print("-" * 70)
# describe() gives us statistics like mean, std, min, max, etc.
print(data.describe())
print()
# Step 7: Check for missing values
print("-" * 70)
print("MISSING VALUES CHECK")
print("-" * 70)
missing_values = data.isnull().sum()
print("Number of missing values per column:")
print(missing_values)
print()
if missing_values.sum() == 0:
print("✅ Great! No missing values found.")
else:
print("⚠️ Some columns have missing values. You may need to handle them.")
print()
# Step 8: Get unique values in a column
print("-" * 70)
print("SAMPLE: UNIQUE VALUES")
print("-" * 70)
# Let's see what unique values exist in the first column
first_column = data.columns[0]
unique_count = data[first_column].nunique()
print(f"The column '{first_column}' has {unique_count} unique value(s)")
print()
# Summary
print("=" * 70)
print("SUMMARY")
print("=" * 70)
print("You've learned how to:")
print(" ✓ Load data from a CSV file using pandas")
print(" ✓ Check the size and shape of your dataset")
print(" ✓ View the first and last rows")
print(" ✓ Understand data types")
print(" ✓ Get statistical summaries")
print(" ✓ Check for missing values")
print()
print("Next step: Try loading other CSV files from the data/ folder!")
print("=" * 70)
# Pro Tips:
# - Always explore your data before analyzing it
# - Check for missing values and understand why they might be missing
# - Look at the data types to ensure they make sense
# - Use head() and tail() to spot any obvious issues with your data
Loading…
Cancel
Save