San Jose State University Department of Applied Data Science
**DATA 200 Computational Programming for Data Analytics**
Spring 2024 Instructor: Ron Mak
"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 3.17 Intro to Data Science: Measures of Central Tendency—Mean, Median and Mode "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### **Measures of central tendency** in descriptive statistics:\n",
"- **mean**: the *average* of a set of values\n",
"- **median**: the *middle value* of a set of **sorted** values\n",
"- **mode**: the *most frequently occuring* value in a set of values\n",
"\n",
"#### Each is a single value that represents a \"central\" value of the set of values, one that is most \"typical\" of the values."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"grades = [85, 93, 45, 89, 85]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(f'mean = {sum(grades)/len(grades)}')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Recall the `min()` and `max()` functions:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(f'min = {min(grades)}')\n",
"print(f'max = {max(grades)}')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### The statistics module of the Standard Python Library has functions to calculate the mean, median, and mode of a list of values."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import statistics"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(f'mean = {statistics.mean(grades)}')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(f'sorted grades = {sorted(grades)}')\n",
"print(f'median = {statistics.median(grades)}')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### What if there are an even number of values in the list? Then the median is the average of the two middle values."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"even_list = [86, 14, 55, 92, 75, 80]\n",
"\n",
"print(f'sorted grades = {sorted(even_list)}')\n",
"print(f'median = {statistics.median(even_list)}')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(f'mode = {statistics.mode(grades)}')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"##########################################################################\n",
"# (C) Copyright 2019 by Deitel & Associates, Inc. and #\n",
"# Pearson Education, Inc. All Rights Reserved. #\n",
"# #\n",
"# DISCLAIMER: The authors and publisher of this book have used their #\n",
"# best efforts in preparing the book. These efforts include the #\n",
"# development, research, and testing of the theories and programs #\n",
"# to determine their effectiveness. The authors and publisher make #\n",
"# no warranty of any kind, expressed or implied, with regard to these #\n",
"# programs or to the documentation contained in these books. The authors #\n",
"# and publisher shall not be liable in any event for incidental or #\n",
"# consequential damages in connection with, or arising out of, the #\n",
"# furnishing, performance, or use of these programs. #\n",
"##########################################################################\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Additional material (C) Copyright 2023 by Ronald Mak"
]
}
],
"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.11.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}