Skip to content

Instantly share code, notes, and snippets.

@nihal-singh
Created August 26, 2020 08:57
Show Gist options
  • Save nihal-singh/f91b55af6cfdb55142c83e5090c33f58 to your computer and use it in GitHub Desktop.
Save nihal-singh/f91b55af6cfdb55142c83e5090c33f58 to your computer and use it in GitHub Desktop.
Created on Skills Network Labs
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a href=\"https://cognitiveclass.ai/\">\n",
" <img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/Ad/CCLog.png\" width=\"200\" align=\"center\">\n",
"</a>\n",
"\n",
"\n",
"\n",
"<h1 align=center><font size = 5><b>A </b>pplication <b>P</b>rogramming <b>I</b>nterface</font> (API)</h1>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"An API lets two pieces of software talk to each other. Just like a function, you don’t have to know how the API works only its inputs and outputs. An essential type of API is a REST API that allows you to access resources via the internet. In this lab, we will review the Pandas Library in the context of an API, we will also review a basic REST API "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"alert alert-block alert-info\" style=\"margin-top: 20px\">\n",
" <a href=\"https://cocl.us/topNotebooksPython101Coursera\">\n",
" <img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/Ad/TopAd.png\" width=\"750\" align=\"center\">\n",
" </a>\n",
"</div>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Table of Contents\n",
"<div class=\"alert alert-block alert-info\" style=\"margin-top: 20px\">\n",
"<li><a href=\"#ref0\">Pandas is an API</a></li>\n",
"<li><a href=\"#ref1\">REST APIs Basics </a></li>\n",
"<li><a href=\"#ref2\">Quiz on Tuples</a></li>\n",
"\n",
"<p></p>\n",
"Estimated Time Needed: <strong>15 min</strong>\n",
"</div>\n",
"\n",
"<hr>"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Requirement already satisfied: nba_api in /home/jupyterlab/conda/envs/python/lib/python3.6/site-packages (1.1.9)\n",
"Requirement already satisfied: requests in /home/jupyterlab/conda/envs/python/lib/python3.6/site-packages (from nba_api) (2.24.0)\n",
"Requirement already satisfied: idna<3,>=2.5 in /home/jupyterlab/conda/envs/python/lib/python3.6/site-packages (from requests->nba_api) (2.10)\n",
"Requirement already satisfied: certifi>=2017.4.17 in /home/jupyterlab/conda/envs/python/lib/python3.6/site-packages (from requests->nba_api) (2020.6.20)\n",
"Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /home/jupyterlab/conda/envs/python/lib/python3.6/site-packages (from requests->nba_api) (1.25.10)\n",
"Requirement already satisfied: chardet<4,>=3.0.2 in /home/jupyterlab/conda/envs/python/lib/python3.6/site-packages (from requests->nba_api) (3.0.4)\n"
]
}
],
"source": [
"!pip install nba_api"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2 id=\"PandasAPI\">Pandas is an API </h2>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You will use this function in the lab:"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [],
"source": [
"def one_dict(list_dict):\n",
" keys=list_dict[0].keys()\n",
" out_dict={key:[] for key in keys}\n",
" out_dict\n",
" for dict_ in list_dict:\n",
" for key, value in dict_.items():\n",
" out_dict[key].append(value)\n",
" return out_dict "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2 id=\"PandasAPI\">Pandas is an API </h2>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Pandas is actually set of software components , much of witch is not even written in Python.\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [],
"source": [
"import pandas as pd\n",
"import matplotlib.pyplot as plt"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You create a dictionary, this is just data."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [],
"source": [
"dict_={'a':[11,21,31],'b':[12,22,32]}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"When you create a Pandas object with the Dataframe constructor in API lingo, this is an \"instance\". The data in the dictionary is passed along to the pandas API. You then use the dataframe to communicate with the API."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"data": {
"text/plain": [
"pandas.core.frame.DataFrame"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df=pd.DataFrame(dict_)\n",
"type(df)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src = \"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/Chapter%206/images/pandas_api.png\" width = 800, align = \"center\" alt=\"logistic regression block diagram\" />"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"When you call the method head the dataframe communicates with the API displaying the first few rows of the dataframe.\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"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>a</th>\n",
" <th>b</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>11</td>\n",
" <td>12</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>21</td>\n",
" <td>22</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>31</td>\n",
" <td>32</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" a b\n",
"0 11 12\n",
"1 21 22\n",
"2 31 32"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"When you call the method mean,the API will calculate the mean and return the value."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"data": {
"text/plain": [
"a 21.0\n",
"b 22.0\n",
"dtype: float64"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.mean()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2 id=\"ref1\">REST APIs</h2>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<p>Rest API’s function by sending a <b>request</b>, the request is communicated via HTTP message. The HTTP message usually contains a JSON file. This contains instructions for what operation we would like the service or <b>resource</b> to perform. In a similar manner, API returns a <b>response</b>, via an HTTP message, this response is usually contained within a JSON.</p>\n",
"<p>In this lab, we will use the <a href=https://pypi.org/project/nba-api/>NBA API</a> to determine how well the Golden State Warriors performed against the Toronto Raptors. We will use the API do the determined number of points the Golden State Warriors won or lost by for each game. So if the value is three, the Golden State Warriors won by three points. Similarly it the Golden State Warriors lost by two points the result will be negative two. The API is reltivly will handle a lot of the details such a Endpoints and Authentication </p>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In the nba api to make a request for a specific team, it's quite simple, we don't require a JSON all we require is an id. This information is stored locally in the API we import the module teams "
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"from nba_api.stats.static import teams\n",
"import matplotlib.pyplot as plt"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#https://pypi.org/project/nba-api/"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The method <code>get_teams()</code> returns a list of dictionaries the dictionary key id has a unique identifier for each team as a value "
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"nba_teams = teams.get_teams()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The dictionary key id has a unique identifier for each team as a value, let's look at the first three elements of the list:"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'id': 1610612737,\n",
" 'full_name': 'Atlanta Hawks',\n",
" 'abbreviation': 'ATL',\n",
" 'nickname': 'Hawks',\n",
" 'city': 'Atlanta',\n",
" 'state': 'Atlanta',\n",
" 'year_founded': 1949},\n",
" {'id': 1610612738,\n",
" 'full_name': 'Boston Celtics',\n",
" 'abbreviation': 'BOS',\n",
" 'nickname': 'Celtics',\n",
" 'city': 'Boston',\n",
" 'state': 'Massachusetts',\n",
" 'year_founded': 1946},\n",
" {'id': 1610612739,\n",
" 'full_name': 'Cleveland Cavaliers',\n",
" 'abbreviation': 'CLE',\n",
" 'nickname': 'Cavaliers',\n",
" 'city': 'Cleveland',\n",
" 'state': 'Ohio',\n",
" 'year_founded': 1970}]"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nba_teams[0:3]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To make things easier, we can convert the dictionary to a table. First, we use the function <code>one dict</code>, to create a dictionary. We use the common keys for each team as the keys, the value is a list; each element of the list corresponds to the values for each team.\n",
"We then convert the dictionary to a dataframe, each row contains the information for a different team."
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"dict_keys(['id', 'full_name', 'abbreviation', 'nickname', 'city', 'state', 'year_founded'])\n",
"{'id': [], 'full_name': [], 'abbreviation': [], 'nickname': [], 'city': [], 'state': [], 'year_founded': []}\n"
]
},
{
"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>id</th>\n",
" <th>full_name</th>\n",
" <th>abbreviation</th>\n",
" <th>nickname</th>\n",
" <th>city</th>\n",
" <th>state</th>\n",
" <th>year_founded</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1610612737</td>\n",
" <td>Atlanta Hawks</td>\n",
" <td>ATL</td>\n",
" <td>Hawks</td>\n",
" <td>Atlanta</td>\n",
" <td>Atlanta</td>\n",
" <td>1949</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>1610612738</td>\n",
" <td>Boston Celtics</td>\n",
" <td>BOS</td>\n",
" <td>Celtics</td>\n",
" <td>Boston</td>\n",
" <td>Massachusetts</td>\n",
" <td>1946</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>1610612739</td>\n",
" <td>Cleveland Cavaliers</td>\n",
" <td>CLE</td>\n",
" <td>Cavaliers</td>\n",
" <td>Cleveland</td>\n",
" <td>Ohio</td>\n",
" <td>1970</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>1610612740</td>\n",
" <td>New Orleans Pelicans</td>\n",
" <td>NOP</td>\n",
" <td>Pelicans</td>\n",
" <td>New Orleans</td>\n",
" <td>Louisiana</td>\n",
" <td>2002</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>1610612741</td>\n",
" <td>Chicago Bulls</td>\n",
" <td>CHI</td>\n",
" <td>Bulls</td>\n",
" <td>Chicago</td>\n",
" <td>Illinois</td>\n",
" <td>1966</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" id full_name abbreviation nickname city \\\n",
"0 1610612737 Atlanta Hawks ATL Hawks Atlanta \n",
"1 1610612738 Boston Celtics BOS Celtics Boston \n",
"2 1610612739 Cleveland Cavaliers CLE Cavaliers Cleveland \n",
"3 1610612740 New Orleans Pelicans NOP Pelicans New Orleans \n",
"4 1610612741 Chicago Bulls CHI Bulls Chicago \n",
"\n",
" state year_founded \n",
"0 Atlanta 1949 \n",
"1 Massachusetts 1946 \n",
"2 Ohio 1970 \n",
"3 Louisiana 2002 \n",
"4 Illinois 1966 "
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
" \n",
"dict_nba_team=one_dict_(nba_teams)\n",
"df_teams=pd.DataFrame(dict_nba_team)\n",
"df_teams.head()\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Will use the team's nickname to find the unique id, we can see the row that contains the warriors by using the column nickname as follows:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [],
"source": [
"df_warriors=df_teams[df_teams['nickname']=='Warriors']\n",
"df_warriors"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"we can use the following line of code to access the first column of the dataframe:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [],
"source": [
"id_warriors=df_warriors[['id']].values[0][0]\n",
"#we now have an integer that can be used to request the Warriors information \n",
"id_warriors"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The function \"League Game Finder \" will make an API call, its in the module <code>stats.endpoints</code> "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from nba_api.stats.endpoints import leaguegamefinder"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The parameter <code>team_id_nullable</code> is the unique ID for the warriors. Under the hood, the NBA API is making a HTTP request. \n",
"The information requested is provided and is transmitted via an HTTP response this is assigned to the object <code>gamefinder</code>."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [],
"source": [
"# Since https://stats.nba.com does lot allow api calls from Cloud IPs and Skills Network Labs uses a Cloud IP.\n",
"# The following code is comment out, you can run it on jupyter labs on your own computer.\n",
"# gamefinder = leaguegamefinder.LeagueGameFinder(team_id_nullable=id_warriors)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"we can see the json file by running the following line of code. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Since https://stats.nba.com does lot allow api calls from Cloud IPs and Skills Network Labs uses a Cloud IP.\n",
"# The following code is comment out, you can run it on jupyter labs on your own computer.\n",
"# gamefinder.get_json()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The game finder object has a method <code>get_data_frames()</code>, that returns a dataframe. If we view the dataframe, we can see it contains information about all the games the Warriors played. The <code>PLUS_MINUS</code> column contains information on the score, if the value is negative the Warriors lost by that many points, if the value is positive, the warriors one by that amount of points. The column <code>MATCHUP </code>had the team the Warriors were playing, GSW stands for golden state and TOR means Toronto Raptors; <code>vs</code> signifies it was a home game and the <code>@ </code>symbol means an away game."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Since https://stats.nba.com does lot allow api calls from Cloud IPs and Skills Network Labs uses a Cloud IP.\n",
"# The following code is comment out, you can run it on jupyter labs on your own computer.\n",
"# games = gamefinder.get_data_frames()[0]\n",
"# games.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"you can download the dataframe from the API call for Golden State and run the rest like a video."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"! wget https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/Chapter%205/Labs/Golden_State.pkl"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"file_name = \"Golden_State.pkl\"\n",
"games = pd.read_pickle(file_name)\n",
"games.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can create two dataframes, one for the games that the Warriors faced the raptors at home and the second for away games."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [],
"source": [
"games_home=games [games ['MATCHUP']=='GSW vs. TOR']\n",
"games_away=games [games ['MATCHUP']=='GSW @ TOR']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can calculate the mean for the column <code>PLUS_MINUS</code> for the dataframes <code>games_home</code> and <code> games_away</code>:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [],
"source": [
"games_home.mean()['PLUS_MINUS']"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [],
"source": [
"games_away.mean()['PLUS_MINUS']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can plot out the <code>PLUS MINUS</code> column for for the dataframes <code>games_home</code> and <code> games_away</code>.\n",
"We see the warriors played better at home."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [],
"source": [
"fig, ax = plt.subplots()\n",
"\n",
"games_away.plot(x='GAME_DATE',y='PLUS_MINUS', ax=ax)\n",
"games_home.plot(x='GAME_DATE',y='PLUS_MINUS', ax=ax)\n",
"ax.legend([\"away\", \"home\"])\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" <a href=\"http://cocl.us/NotebooksPython101bottom\"><img src = \"https://ibm.box.com/shared/static/irypdxea2q4th88zu1o1tsd06dya10go.png\" width = 750, align = \"center\"></a>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### About the Authors: \n",
"\n",
" [Joseph Santarcangelo]( https://www.linkedin.com/in/joseph-s-50398b136/) has a PhD in Electrical Engineering, his research focused on using machine learning, signal processing, and computer vision to determine how videos impact human cognition. Joseph has been working for IBM since he completed his PhD.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Copyright &copy; 2017 [cognitiveclass.ai](https:cognitiveclass.ai). This notebook and its source code are released under the terms of the [MIT License](cognitiveclass.ai)."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python",
"language": "python",
"name": "conda-env-python-py"
},
"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.6.11"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment