{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"tags": [
"remove-input"
]
},
"outputs": [],
"source": [
"from datascience import *\n",
"path_data = '../../../assets/data/'\n",
"import matplotlib\n",
"matplotlib.use('Agg')\n",
"%matplotlib inline\n",
"import matplotlib.pyplot as plots\n",
"plots.style.use('fivethirtyeight')\n",
"import numpy as np"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Classifying by One Variable\n",
"\n",
"Data scientists often need to classify individuals into groups according to shared features, and then identify some characteristics of the groups. For example, in the example using Galton's data on heights, we saw that it was useful to classify families according to the parents' midparent heights, and then find the average height of the children in each group.\n",
"\n",
"This section is about classifying individuals into categories that are not numerical. We begin by recalling the basic use of `group`. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Counting the Number in Each Category\n",
"The `group` method with a single argument counts the number of rows for each category in a column. The result contains one row per unique value in the grouped column.\n",
"\n",
"Here is a small table of data on ice cream cones. The `group` method can be used to list the distinct flavors and provide the counts of each flavor."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
" \n",
" \n",
" Flavor | Price | \n",
"
\n",
" \n",
" \n",
" \n",
" strawberry | 3.55 | \n",
"
\n",
" \n",
" chocolate | 4.75 | \n",
"
\n",
" \n",
" chocolate | 6.55 | \n",
"
\n",
" \n",
" strawberry | 5.25 | \n",
"
\n",
" \n",
" chocolate | 5.25 | \n",
"
\n",
" \n",
"
"
],
"text/plain": [
"Flavor | Price\n",
"strawberry | 3.55\n",
"chocolate | 4.75\n",
"chocolate | 6.55\n",
"strawberry | 5.25\n",
"chocolate | 5.25"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cones = Table().with_columns(\n",
" 'Flavor', make_array('strawberry', 'chocolate', 'chocolate', 'strawberry', 'chocolate'),\n",
" 'Price', make_array(3.55, 4.75, 6.55, 5.25, 5.25)\n",
")\n",
"cones"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" \n",
" \n",
" Flavor | count | \n",
"
\n",
" \n",
" \n",
" \n",
" chocolate | 3 | \n",
"
\n",
" \n",
" strawberry | 2 | \n",
"
\n",
" \n",
"
"
],
"text/plain": [
"Flavor | count\n",
"chocolate | 3\n",
"strawberry | 2"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cones.group('Flavor')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"There are two distinct categories, chocolate and strawberry. The call to `group` creates a table of counts in each category. The column is called `count` by default, and contains the number of rows in each category.\n",
"\n",
"Notice that this can all be worked out from just the `Flavor` column. The `Price` column has not been used.\n",
"\n",
"But what if we wanted the total price of the cones of each different flavor? That's where the second argument of `group` comes in."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Finding a Characteristic of Each Category\n",
"The optional second argument of `group` names the function that will be used to aggregate values in other columns for all of those rows. For instance, `sum` will sum up the prices in all rows that match each category. This result also contains one row per unique value in the grouped column, but it has the same number of columns as the original table.\n",
"\n",
"To find the total price of each flavor, we call `group` again, with `Flavor` as its first argument as before. But this time there is a second argument: the function name `sum`."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" \n",
" \n",
" Flavor | Price sum | \n",
"
\n",
" \n",
" \n",
" \n",
" chocolate | 16.55 | \n",
"
\n",
" \n",
" strawberry | 8.8 | \n",
"
\n",
" \n",
"
"
],
"text/plain": [
"Flavor | Price sum\n",
"chocolate | 16.55\n",
"strawberry | 8.8"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cones.group('Flavor', sum)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To create this new table, `group` has calculated the sum of the `Price` entries in all the rows corresponding to each distinct flavor. The prices in the three `chocolate` rows add up to $\\$16.55$ (you can assume that price is being measured in dollars). The prices in the two `strawberry` rows have a total of $\\$8.80$.\n",
"\n",
"The label of the newly created \"sum\" column is `Price sum`, which is created by taking the label of the column being summed, and appending the word `sum`. \n",
"\n",
"Because `group` finds the `sum` of all columns other than the one with the categories, there is no need to specify that it has to `sum` the prices.\n",
"\n",
"To see in more detail what `group` is doing, notice that you could have figured out the total prices yourself, not only by mental arithmetic but also using code. For example, to find the total price of all the chocolate cones, you could start by creating a new table consisting of only the chocolate cones, and then accessing the column of prices:"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([4.75, 6.55, 5.25])"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cones.where('Flavor', are.equal_to('chocolate')).column('Price')"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"16.55"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sum(cones.where('Flavor', are.equal_to('chocolate')).column('Price'))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This is what `group` is doing for each distinct value in `Flavor`."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" \n",
" \n",
" Flavor | Array of All the Prices | Sum of the Array | \n",
"
\n",
" \n",
" \n",
" \n",
" chocolate | [4.75 6.55 5.25] | 16.55 | \n",
"
\n",
" \n",
" strawberry | [3.55 5.25] | 8.8 | \n",
"
\n",
" \n",
"
"
],
"text/plain": [
"Flavor | Array of All the Prices | Sum of the Array\n",
"chocolate | [4.75 6.55 5.25] | 16.55\n",
"strawberry | [3.55 5.25] | 8.8"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# For each distinct value in `Flavor, access all the rows\n",
"# and create an array of `Price`\n",
"\n",
"cones_choc = cones.where('Flavor', are.equal_to('chocolate')).column('Price')\n",
"cones_strawb = cones.where('Flavor', are.equal_to('strawberry')).column('Price')\n",
"\n",
"# Display the arrays in a table\n",
"\n",
"grouped_cones = Table().with_columns(\n",
" 'Flavor', make_array('chocolate', 'strawberry'),\n",
" 'Array of All the Prices', make_array(cones_choc, cones_strawb)\n",
")\n",
"\n",
"# Append a column with the sum of the `Price` values in each array\n",
"\n",
"price_totals = grouped_cones.with_column(\n",
" 'Sum of the Array', make_array(sum(cones_choc), sum(cones_strawb))\n",
")\n",
"price_totals"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can replace `sum` by any other functions that work on arrays. For example, you could use `max` to find the largest price in each category:"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" \n",
" \n",
" Flavor | Price max | \n",
"
\n",
" \n",
" \n",
" \n",
" chocolate | 6.55 | \n",
"
\n",
" \n",
" strawberry | 5.25 | \n",
"
\n",
" \n",
"
"
],
"text/plain": [
"Flavor | Price max\n",
"chocolate | 6.55\n",
"strawberry | 5.25"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cones.group('Flavor', max)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Once again, `group` creates arrays of the prices in each `Flavor` category. But now it finds the `max` of each array:"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" \n",
" \n",
" Flavor | Array of All the Prices | Max of the Array | \n",
"
\n",
" \n",
" \n",
" \n",
" chocolate | [4.75 6.55 5.25] | 6.55 | \n",
"
\n",
" \n",
" strawberry | [3.55 5.25] | 5.25 | \n",
"
\n",
" \n",
"
"
],
"text/plain": [
"Flavor | Array of All the Prices | Max of the Array\n",
"chocolate | [4.75 6.55 5.25] | 6.55\n",
"strawberry | [3.55 5.25] | 5.25"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"price_maxes = grouped_cones.with_column(\n",
" 'Max of the Array', make_array(max(cones_choc), max(cones_strawb))\n",
")\n",
"price_maxes"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Indeed, the original call to `group` with just one argument has the same effect as using `len` as the function and then cleaning up the table."
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" \n",
" \n",
" Flavor | Array of All the Prices | Length of the Array | \n",
"
\n",
" \n",
" \n",
" \n",
" chocolate | [4.75 6.55 5.25] | 3 | \n",
"
\n",
" \n",
" strawberry | [3.55 5.25] | 2 | \n",
"
\n",
" \n",
"
"
],
"text/plain": [
"Flavor | Array of All the Prices | Length of the Array\n",
"chocolate | [4.75 6.55 5.25] | 3\n",
"strawberry | [3.55 5.25] | 2"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"lengths = grouped_cones.with_column(\n",
" 'Length of the Array', make_array(len(cones_choc), len(cones_strawb))\n",
")\n",
"lengths"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example: NBA Salaries\n",
"The table `nba` contains data on the 2015-2016 players in the National Basketball Association. We have examined these data earlier. Recall that salaries are measured in millions of dollars."
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" \n",
" \n",
" PLAYER | POSITION | TEAM | SALARY | \n",
"
\n",
" \n",
" \n",
" \n",
" Paul Millsap | PF | Atlanta Hawks | 18.6717 | \n",
"
\n",
" \n",
" Al Horford | C | Atlanta Hawks | 12 | \n",
"
\n",
" \n",
" Tiago Splitter | C | Atlanta Hawks | 9.75625 | \n",
"
\n",
" \n",
" Jeff Teague | PG | Atlanta Hawks | 8 | \n",
"
\n",
" \n",
" Kyle Korver | SG | Atlanta Hawks | 5.74648 | \n",
"
\n",
" \n",
" Thabo Sefolosha | SF | Atlanta Hawks | 4 | \n",
"
\n",
" \n",
" Mike Scott | PF | Atlanta Hawks | 3.33333 | \n",
"
\n",
" \n",
" Kent Bazemore | SF | Atlanta Hawks | 2 | \n",
"
\n",
" \n",
" Dennis Schroder | PG | Atlanta Hawks | 1.7634 | \n",
"
\n",
" \n",
" Tim Hardaway Jr. | SG | Atlanta Hawks | 1.30452 | \n",
"
\n",
" \n",
"
\n",
"... (407 rows omitted)
"
],
"text/plain": [
"PLAYER | POSITION | TEAM | SALARY\n",
"Paul Millsap | PF | Atlanta Hawks | 18.6717\n",
"Al Horford | C | Atlanta Hawks | 12\n",
"Tiago Splitter | C | Atlanta Hawks | 9.75625\n",
"Jeff Teague | PG | Atlanta Hawks | 8\n",
"Kyle Korver | SG | Atlanta Hawks | 5.74648\n",
"Thabo Sefolosha | SF | Atlanta Hawks | 4\n",
"Mike Scott | PF | Atlanta Hawks | 3.33333\n",
"Kent Bazemore | SF | Atlanta Hawks | 2\n",
"Dennis Schroder | PG | Atlanta Hawks | 1.7634\n",
"Tim Hardaway Jr. | SG | Atlanta Hawks | 1.30452\n",
"... (407 rows omitted)"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nba1 = Table.read_table(path_data + 'nba_salaries.csv')\n",
"nba = nba1.relabeled(\"'15-'16 SALARY\", 'SALARY')\n",
"nba"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**1.** How much money did each team pay for its players' salaries?\n",
"\n",
"The only columns involved are `TEAM` and `SALARY`. We have to `group` the rows by `TEAM` and then `sum` the salaries of the groups. "
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" \n",
" \n",
" TEAM | SALARY sum | \n",
"
\n",
" \n",
" \n",
" \n",
" Atlanta Hawks | 69.5731 | \n",
"
\n",
" \n",
" Boston Celtics | 50.2855 | \n",
"
\n",
" \n",
" Brooklyn Nets | 57.307 | \n",
"
\n",
" \n",
" Charlotte Hornets | 84.1024 | \n",
"
\n",
" \n",
" Chicago Bulls | 78.8209 | \n",
"
\n",
" \n",
" Cleveland Cavaliers | 102.312 | \n",
"
\n",
" \n",
" Dallas Mavericks | 65.7626 | \n",
"
\n",
" \n",
" Denver Nuggets | 62.4294 | \n",
"
\n",
" \n",
" Detroit Pistons | 42.2118 | \n",
"
\n",
" \n",
" Golden State Warriors | 94.0851 | \n",
"
\n",
" \n",
"
\n",
"... (20 rows omitted)
"
],
"text/plain": [
"TEAM | SALARY sum\n",
"Atlanta Hawks | 69.5731\n",
"Boston Celtics | 50.2855\n",
"Brooklyn Nets | 57.307\n",
"Charlotte Hornets | 84.1024\n",
"Chicago Bulls | 78.8209\n",
"Cleveland Cavaliers | 102.312\n",
"Dallas Mavericks | 65.7626\n",
"Denver Nuggets | 62.4294\n",
"Detroit Pistons | 42.2118\n",
"Golden State Warriors | 94.0851\n",
"... (20 rows omitted)"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"teams_and_money = nba.select('TEAM', 'SALARY')\n",
"teams_and_money.group('TEAM', sum)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**2.** How many NBA players were there in each of the five positions?\n",
"\n",
"We have to classify by `POSITION`, and count. This can be done with just one argument to group:"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" \n",
" \n",
" POSITION | count | \n",
"
\n",
" \n",
" \n",
" \n",
" C | 69 | \n",
"
\n",
" \n",
" PF | 85 | \n",
"
\n",
" \n",
" PG | 85 | \n",
"
\n",
" \n",
" SF | 82 | \n",
"
\n",
" \n",
" SG | 96 | \n",
"
\n",
" \n",
"
"
],
"text/plain": [
"POSITION | count\n",
"C | 69\n",
"PF | 85\n",
"PG | 85\n",
"SF | 82\n",
"SG | 96"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nba.group('POSITION')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**3.** What was the average salary of the players at each of the five positions?\n",
"\n",
"This time, we have to group by `POSITION` and take the mean of the salaries. For clarity, we will work with a table of just the positions and the salaries."
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" \n",
" \n",
" POSITION | SALARY mean | \n",
"
\n",
" \n",
" \n",
" \n",
" C | 6.08291 | \n",
"
\n",
" \n",
" PF | 4.95134 | \n",
"
\n",
" \n",
" PG | 5.16549 | \n",
"
\n",
" \n",
" SF | 5.53267 | \n",
"
\n",
" \n",
" SG | 3.9882 | \n",
"
\n",
" \n",
"
"
],
"text/plain": [
"POSITION | SALARY mean\n",
"C | 6.08291\n",
"PF | 4.95134\n",
"PG | 5.16549\n",
"SF | 5.53267\n",
"SG | 3.9882"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"positions_and_money = nba.select('POSITION', 'SALARY')\n",
"positions_and_money.group('POSITION', np.mean)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Center was the most highly paid position, at an average of over 6 million dollars.\n",
"\n",
"If we had not selected the two columns as our first step, `group` would not attempt to \"average\" the categorical columns in `nba`. (It is impossible to average two strings like \"Atlanta Hawks\" and \"Boston Celtics\".) It performs arithmetic only on numerical columns and leaves the rest blank."
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" \n",
" \n",
" POSITION | PLAYER mean | TEAM mean | SALARY mean | \n",
"
\n",
" \n",
" \n",
" \n",
" C | | | 6.08291 | \n",
"
\n",
" \n",
" PF | | | 4.95134 | \n",
"
\n",
" \n",
" PG | | | 5.16549 | \n",
"
\n",
" \n",
" SF | | | 5.53267 | \n",
"
\n",
" \n",
" SG | | | 3.9882 | \n",
"
\n",
" \n",
"
"
],
"text/plain": [
"POSITION | PLAYER mean | TEAM mean | SALARY mean\n",
"C | | | 6.08291\n",
"PF | | | 4.95134\n",
"PG | | | 5.16549\n",
"SF | | | 5.53267\n",
"SG | | | 3.9882"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nba.group('POSITION', np.mean)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.7.8"
}
},
"nbformat": 4,
"nbformat_minor": 1
}