├── README.md
├── Certificate.pdf
├── Week 1
├── week1.pdf
├── 50YearsDataScience.pdf
└── Week1.ipynb
├── Week 2
├── week2.pdf
├── Patreon-Case-Study.pdf
└── Assignment2.ipynb
├── Week 3
└── week3.pdf
├── Week 4
├── week4.pdf
├── Assignment4.ipynb
└── Week4.ipynb
├── .gitattributes
└── .gitignore
/README.md:
--------------------------------------------------------------------------------
1 | # Introduction-to-Data-Science-in-Python
2 |
3 | Coursera course by University of Michigan
4 |
--------------------------------------------------------------------------------
/Certificate.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bondeanikets/Introduction-to-Data-Science-in-Python/master/Certificate.pdf
--------------------------------------------------------------------------------
/Week 1/week1.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bondeanikets/Introduction-to-Data-Science-in-Python/master/Week 1/week1.pdf
--------------------------------------------------------------------------------
/Week 2/week2.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bondeanikets/Introduction-to-Data-Science-in-Python/master/Week 2/week2.pdf
--------------------------------------------------------------------------------
/Week 3/week3.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bondeanikets/Introduction-to-Data-Science-in-Python/master/Week 3/week3.pdf
--------------------------------------------------------------------------------
/Week 4/week4.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bondeanikets/Introduction-to-Data-Science-in-Python/master/Week 4/week4.pdf
--------------------------------------------------------------------------------
/Week 1/50YearsDataScience.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bondeanikets/Introduction-to-Data-Science-in-Python/master/Week 1/50YearsDataScience.pdf
--------------------------------------------------------------------------------
/Week 2/Patreon-Case-Study.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bondeanikets/Introduction-to-Data-Science-in-Python/master/Week 2/Patreon-Case-Study.pdf
--------------------------------------------------------------------------------
/.gitattributes:
--------------------------------------------------------------------------------
1 | # Auto detect text files and perform LF normalization
2 | * text=auto
3 |
4 | # Custom for Visual Studio
5 | *.cs diff=csharp
6 |
7 | # Standard to msysgit
8 | *.doc diff=astextplain
9 | *.DOC diff=astextplain
10 | *.docx diff=astextplain
11 | *.DOCX diff=astextplain
12 | *.dot diff=astextplain
13 | *.DOT diff=astextplain
14 | *.pdf diff=astextplain
15 | *.PDF diff=astextplain
16 | *.rtf diff=astextplain
17 | *.RTF diff=astextplain
18 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Windows image file caches
2 | Thumbs.db
3 | ehthumbs.db
4 |
5 | # Folder config file
6 | Desktop.ini
7 |
8 | # Recycle Bin used on file shares
9 | $RECYCLE.BIN/
10 |
11 | # Windows Installer files
12 | *.cab
13 | *.msi
14 | *.msm
15 | *.msp
16 |
17 | # Windows shortcuts
18 | *.lnk
19 |
20 | # =========================
21 | # Operating System Files
22 | # =========================
23 |
24 | # OSX
25 | # =========================
26 |
27 | .DS_Store
28 | .AppleDouble
29 | .LSOverride
30 |
31 | # Thumbnails
32 | ._*
33 |
34 | # Files that might appear in the root of a volume
35 | .DocumentRevisions-V100
36 | .fseventsd
37 | .Spotlight-V100
38 | .TemporaryItems
39 | .Trashes
40 | .VolumeIcon.icns
41 |
42 | # Directories potentially created on remote AFP share
43 | .AppleDB
44 | .AppleDesktop
45 | Network Trash Folder
46 | Temporary Items
47 | .apdisk
48 |
--------------------------------------------------------------------------------
/Week 4/Assignment4.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "---\n",
8 | "\n",
9 | "_You are currently looking at **version 1.0** of this notebook. To download notebooks and datafiles, as well as get help on Jupyter notebooks in the Coursera platform, visit the [Jupyter Notebook FAQ](https://www.coursera.org/learn/python-data-analysis/resources/0dhYG) course resource._\n",
10 | "\n",
11 | "---"
12 | ]
13 | },
14 | {
15 | "cell_type": "code",
16 | "execution_count": 1,
17 | "metadata": {
18 | "collapsed": true
19 | },
20 | "outputs": [],
21 | "source": [
22 | "import pandas as pd\n",
23 | "import numpy as np\n",
24 | "from scipy.stats import ttest_ind"
25 | ]
26 | },
27 | {
28 | "cell_type": "markdown",
29 | "metadata": {},
30 | "source": [
31 | "# Assignment 4 - Hypothesis Testing\n",
32 | "This assignment requires more individual learning than previous assignments - you are encouraged to check out the [pandas documentation](http://pandas.pydata.org/pandas-docs/stable/) to find functions or methods you might not have used yet, or ask questions on [Stack Overflow](http://stackoverflow.com/) and tag them as pandas and python related. And of course, the discussion forums are open for interaction with your peers and the course staff.\n",
33 | "\n",
34 | "Definitions:\n",
35 | "* A _quarter_ is a specific three month period, Q1 is January through March, Q2 is April through June, Q3 is July through September, Q4 is October through December.\n",
36 | "* A _recession_ is defined as starting with two consecutive quarters of GDP decline, and ending with two consecutive quarters of GDP growth.\n",
37 | "* A _recession bottom_ is the quarter within a recession which had the lowest GDP.\n",
38 | "* A _university town_ is a city which has a high percentage of university students compared to the total population of the city.\n",
39 | "\n",
40 | "**Hypothesis**: University towns have their mean housing prices less effected by recessions. Run a t-test to compare the ratio of the mean price of houses in university towns the quarter before the recession starts compared to the recession bottom. (`price_ratio=quarter_before_recession/recession_bottom`)\n",
41 | "\n",
42 | "The following data files are available for this assignment:\n",
43 | "* From the [Zillow research data site](http://www.zillow.com/research/data/) there is housing data for the United States. In particular the datafile for [all homes at a city level](http://files.zillowstatic.com/research/public/City/City_Zhvi_AllHomes.csv), ```City_Zhvi_AllHomes.csv```, has median home sale prices at a fine grained level.\n",
44 | "* From the Wikipedia page on college towns is a list of [university towns in the United States](https://en.wikipedia.org/wiki/List_of_college_towns#College_towns_in_the_United_States) which has been copy and pasted into the file ```university_towns.txt```.\n",
45 | "* From Bureau of Economic Analysis, US Department of Commerce, the [GDP over time](http://www.bea.gov/national/index.htm#gdp) of the United States in current dollars (use the chained value in 2009 dollars), in quarterly intervals, in the file ```gdplev.xls```. For this assignment, only look at GDP data from the first quarter of 2000 onward.\n",
46 | "\n",
47 | "Each function in this assignment below is worth 10%, with the exception of ```run_ttest()```, which is worth 50%."
48 | ]
49 | },
50 | {
51 | "cell_type": "code",
52 | "execution_count": 2,
53 | "metadata": {
54 | "collapsed": false
55 | },
56 | "outputs": [],
57 | "source": [
58 | "# Use this dictionary to map state names to two letter acronyms\n",
59 | "states = {'OH': 'Ohio', 'KY': 'Kentucky', 'AS': 'American Samoa', 'NV': 'Nevada', 'WY': 'Wyoming', 'NA': 'National', 'AL': 'Alabama', 'MD': 'Maryland', 'AK': 'Alaska', 'UT': 'Utah', 'OR': 'Oregon', 'MT': 'Montana', 'IL': 'Illinois', 'TN': 'Tennessee', 'DC': 'District of Columbia', 'VT': 'Vermont', 'ID': 'Idaho', 'AR': 'Arkansas', 'ME': 'Maine', 'WA': 'Washington', 'HI': 'Hawaii', 'WI': 'Wisconsin', 'MI': 'Michigan', 'IN': 'Indiana', 'NJ': 'New Jersey', 'AZ': 'Arizona', 'GU': 'Guam', 'MS': 'Mississippi', 'PR': 'Puerto Rico', 'NC': 'North Carolina', 'TX': 'Texas', 'SD': 'South Dakota', 'MP': 'Northern Mariana Islands', 'IA': 'Iowa', 'MO': 'Missouri', 'CT': 'Connecticut', 'WV': 'West Virginia', 'SC': 'South Carolina', 'LA': 'Louisiana', 'KS': 'Kansas', 'NY': 'New York', 'NE': 'Nebraska', 'OK': 'Oklahoma', 'FL': 'Florida', 'CA': 'California', 'CO': 'Colorado', 'PA': 'Pennsylvania', 'DE': 'Delaware', 'NM': 'New Mexico', 'RI': 'Rhode Island', 'MN': 'Minnesota', 'VI': 'Virgin Islands', 'NH': 'New Hampshire', 'MA': 'Massachusetts', 'GA': 'Georgia', 'ND': 'North Dakota', 'VA': 'Virginia'}"
60 | ]
61 | },
62 | {
63 | "cell_type": "code",
64 | "execution_count": 3,
65 | "metadata": {
66 | "collapsed": false
67 | },
68 | "outputs": [],
69 | "source": [
70 | "def get_list_of_university_towns():\n",
71 | " '''Returns a DataFrame of towns and the states they are in from the \n",
72 | " university_towns.txt list. The format of the DataFrame should be:\n",
73 | " DataFrame( [ [\"Michigan\", \"Ann Arbor\"], [\"Michigan\", \"Yipsilanti\"] ], \n",
74 | " columns=[\"State\", \"RegionName\"] )\n",
75 | " \n",
76 | " The following cleaning needs to be done:\n",
77 | "\n",
78 | " 1. For \"State\", removing characters from \"[\" to the end.\n",
79 | " 2. For \"RegionName\", when applicable, removing every character from \" (\" to the end.\n",
80 | " 3. Depending on how you read the data, you may need to remove newline character '\\n'. '''\n",
81 | " data = []\n",
82 | " state = None\n",
83 | " state_towns = []\n",
84 | " with open('university_towns.txt') as file:\n",
85 | " for line in file:\n",
86 | " thisLine = line[:-1]\n",
87 | " if thisLine[-6:] == '[edit]':\n",
88 | " state = thisLine[:-6]\n",
89 | " continue\n",
90 | " if '(' in line:\n",
91 | " town = thisLine[:thisLine.index('(')-1]\n",
92 | " state_towns.append([state,town])\n",
93 | " else:\n",
94 | " town = thisLine\n",
95 | " state_towns.append([state,town])\n",
96 | " data.append(thisLine)\n",
97 | " df = pd.DataFrame(state_towns,columns = ['State','RegionName'])\n",
98 | " return df"
99 | ]
100 | },
101 | {
102 | "cell_type": "code",
103 | "execution_count": 4,
104 | "metadata": {
105 | "collapsed": false
106 | },
107 | "outputs": [
108 | {
109 | "data": {
110 | "text/plain": [
111 | "'2008q3'"
112 | ]
113 | },
114 | "execution_count": 4,
115 | "metadata": {},
116 | "output_type": "execute_result"
117 | }
118 | ],
119 | "source": [
120 | "def get_recession_start():\n",
121 | " '''Returns the year and quarter of the recession start time as a \n",
122 | " string value in a format such as 2005q3'''\n",
123 | " gdplev = pd.ExcelFile('gdplev.xls')\n",
124 | " gdplev = gdplev.parse(\"Sheet1\", skiprows=219)\n",
125 | " gdplev = gdplev[['1999q4', 9926.1]]\n",
126 | " gdplev.columns = ['Quarter','GDP']\n",
127 | " for i in range(2, len(gdplev)):\n",
128 | " if (gdplev.iloc[i-2][1] > gdplev.iloc[i-1][1]) and (gdplev.iloc[i-1][1] > gdplev.iloc[i][1]):\n",
129 | " return gdplev.iloc[i-2][0]\n",
130 | "get_recession_start()"
131 | ]
132 | },
133 | {
134 | "cell_type": "code",
135 | "execution_count": 5,
136 | "metadata": {
137 | "collapsed": false
138 | },
139 | "outputs": [],
140 | "source": [
141 | "def get_recession_end():\n",
142 | " '''Returns the year and quarter of the recession end time as a \n",
143 | " string value in a format such as 2005q3'''\n",
144 | " gdplev = pd.ExcelFile('gdplev.xls')\n",
145 | " gdplev = gdplev.parse(\"Sheet1\", skiprows=219)\n",
146 | " gdplev = gdplev[['1999q4', 9926.1]]\n",
147 | " gdplev.columns = ['Quarter','GDP']\n",
148 | " start = get_recession_start()\n",
149 | " start_index = gdplev[gdplev['Quarter'] == start].index.tolist()[0]\n",
150 | " gdplev=gdplev.iloc[start_index:]\n",
151 | " for i in range(2, len(gdplev)):\n",
152 | " if (gdplev.iloc[i-2][1] < gdplev.iloc[i-1][1]) and (gdplev.iloc[i-1][1] < gdplev.iloc[i][1]):\n",
153 | " return gdplev.iloc[i][0]"
154 | ]
155 | },
156 | {
157 | "cell_type": "code",
158 | "execution_count": 6,
159 | "metadata": {
160 | "collapsed": false
161 | },
162 | "outputs": [],
163 | "source": [
164 | "def get_recession_bottom():\n",
165 | " '''Returns the year and quarter of the recession bottom time as a \n",
166 | " string value in a format such as 2005q3'''\n",
167 | " gdplev = pd.ExcelFile('gdplev.xls')\n",
168 | " gdplev = gdplev.parse(\"Sheet1\", skiprows=219)\n",
169 | " gdplev = gdplev[['1999q4', 9926.1]]\n",
170 | " gdplev.columns = ['Quarter','GDP']\n",
171 | " start = get_recession_start()\n",
172 | " start_index = gdplev[gdplev['Quarter'] == start].index.tolist()[0]\n",
173 | " end = get_recession_end()\n",
174 | " end_index = gdplev[gdplev['Quarter'] == end].index.tolist()[0]\n",
175 | " gdplev=gdplev.iloc[start_index:end_index+1]\n",
176 | " bottom = gdplev['GDP'].min()\n",
177 | " bottom_index = gdplev[gdplev['GDP'] == bottom].index.tolist()[0]-start_index\n",
178 | " return gdplev.iloc[bottom_index]['Quarter']"
179 | ]
180 | },
181 | {
182 | "cell_type": "code",
183 | "execution_count": 7,
184 | "metadata": {
185 | "collapsed": false
186 | },
187 | "outputs": [],
188 | "source": [
189 | "def new_col_names():\n",
190 | " #generating the new coloumns names \n",
191 | " years = list(range(2000,2017))\n",
192 | " quars = ['q1','q2','q3','q4']\n",
193 | " quar_years = []\n",
194 | " for i in years:\n",
195 | " for x in quars:\n",
196 | " quar_years.append((str(i)+x))\n",
197 | " return quar_years[:67]\n",
198 | "def convert_housing_data_to_quarters():\n",
199 | " '''Converts the housing data to quarters and returns it as mean \n",
200 | " values in a dataframe. This dataframe should be a dataframe with\n",
201 | " columns for 2000q1 through 2016q3, and should have a multi-index\n",
202 | " in the shape of [\"State\",\"RegionName\"].\n",
203 | " \n",
204 | " Note: Quarters are defined in the assignment description, they are\n",
205 | " not arbitrary three month periods.\n",
206 | " \n",
207 | " The resulting dataframe should have 67 columns, and 10,730 rows.\n",
208 | " '''\n",
209 | " data = pd.read_csv('City_Zhvi_AllHomes.csv')\n",
210 | " data.drop(['Metro','CountyName','RegionID','SizeRank'],axis=1,inplace=1)\n",
211 | " data['State'] = data['State'].map(states)\n",
212 | " data.set_index(['State','RegionName'],inplace=True)\n",
213 | " col = list(data.columns)\n",
214 | " col = col[0:45]\n",
215 | " data.drop(col,axis=1,inplace=1)\n",
216 | "\n",
217 | " #qs is the quarters of the year\n",
218 | " qs = [list(data.columns)[x:x+3] for x in range(0, len(list(data.columns)), 3)]\n",
219 | " \n",
220 | " # new columns\n",
221 | " column_names = new_col_names()\n",
222 | " for col,q in zip(column_names,qs):\n",
223 | " data[col] = data[q].mean(axis=1)\n",
224 | " \n",
225 | " data = data[column_names]\n",
226 | " return data"
227 | ]
228 | },
229 | {
230 | "cell_type": "code",
231 | "execution_count": 8,
232 | "metadata": {
233 | "collapsed": false
234 | },
235 | "outputs": [
236 | {
237 | "data": {
238 | "text/plain": [
239 | "(True, 0.00036641601595526971, 'university town')"
240 | ]
241 | },
242 | "execution_count": 8,
243 | "metadata": {},
244 | "output_type": "execute_result"
245 | }
246 | ],
247 | "source": [
248 | "def run_ttest():\n",
249 | " '''First creates new data showing the decline or growth of housing prices\n",
250 | " between the recession start and the recession bottom. Then runs a ttest\n",
251 | " comparing the university town values to the non-university towns values, \n",
252 | " return whether the alternative hypothesis (that the two groups are the same)\n",
253 | " is true or not as well as the p-value of the confidence. \n",
254 | " \n",
255 | " Return the tuple (different, p, better) \n",
256 | " where \n",
257 | " \n",
258 | " different=True if the t-test is True at a p<0.01 (we reject the null hypothesis), \n",
259 | " different=False if otherwise (we cannot reject the null hypothesis). \n",
260 | " \n",
261 | " The variable p should\n",
262 | " be equal to the exact p value returned from scipy.stats.ttest_ind(). The\n",
263 | " value for better should be either \"university town\" or \"non-university town\"\n",
264 | " depending on which has a lower mean price ratio (which is equivilent to a\n",
265 | " reduced market loss).'''\n",
266 | " data = convert_housing_data_to_quarters().copy()\n",
267 | " data = data.loc[:,'2008q3':'2009q2']\n",
268 | " data = data.reset_index()\n",
269 | " def price_ratio(row):\n",
270 | " return (row['2008q3'] - row['2009q2'])/row['2008q3']\n",
271 | " \n",
272 | " data['up&down'] = data.apply(price_ratio,axis=1)\n",
273 | " #uni data \n",
274 | " \n",
275 | " uni_town = get_list_of_university_towns()['RegionName']\n",
276 | " uni_town = set(uni_town)\n",
277 | "\n",
278 | " def is_uni_town(row):\n",
279 | " #check if the town is a university towns or not.\n",
280 | " if row['RegionName'] in uni_town:\n",
281 | " return 1\n",
282 | " else:\n",
283 | " return 0\n",
284 | " data['is_uni'] = data.apply(is_uni_town,axis=1)\n",
285 | " \n",
286 | " \n",
287 | " not_uni = data[data['is_uni']==0].loc[:,'up&down'].dropna()\n",
288 | " is_uni = data[data['is_uni']==1].loc[:,'up&down'].dropna()\n",
289 | " def better():\n",
290 | " if not_uni.mean() < is_uni.mean():\n",
291 | " return 'non-university town'\n",
292 | " else:\n",
293 | " return 'university town'\n",
294 | " p_val = list(ttest_ind(not_uni, is_uni))[1]\n",
295 | " result = (True,p_val,better())\n",
296 | " return result"
297 | ]
298 | },
299 | {
300 | "cell_type": "code",
301 | "execution_count": null,
302 | "metadata": {
303 | "collapsed": true
304 | },
305 | "outputs": [],
306 | "source": []
307 | }
308 | ],
309 | "metadata": {
310 | "coursera": {
311 | "course_slug": "python-data-analysis",
312 | "graded_item_id": "Il9Fx",
313 | "launcher_item_id": "TeDW0",
314 | "part_id": "WGlun"
315 | },
316 | "kernelspec": {
317 | "display_name": "Python 3",
318 | "language": "python",
319 | "name": "python3"
320 | },
321 | "language_info": {
322 | "codemirror_mode": {
323 | "name": "ipython",
324 | "version": 3
325 | },
326 | "file_extension": ".py",
327 | "mimetype": "text/x-python",
328 | "name": "python",
329 | "nbconvert_exporter": "python",
330 | "pygments_lexer": "ipython3",
331 | "version": "3.5.2"
332 | }
333 | },
334 | "nbformat": 4,
335 | "nbformat_minor": 0
336 | }
337 |
--------------------------------------------------------------------------------
/Week 2/Assignment2.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "---\n",
8 | "\n",
9 | "_You are currently looking at **version 1.1** of this notebook. To download notebooks and datafiles, as well as get help on Jupyter notebooks in the Coursera platform, visit the [Jupyter Notebook FAQ](https://www.coursera.org/learn/python-data-analysis/resources/0dhYG) course resource._\n",
10 | "\n",
11 | "---"
12 | ]
13 | },
14 | {
15 | "cell_type": "markdown",
16 | "metadata": {},
17 | "source": [
18 | "# Assignment 2 - Pandas Introduction\n",
19 | "All questions are weighted the same in this assignment.\n",
20 | "## Part 1\n",
21 | "The following code loads the olympics dataset (olympics.csv), which was derrived from the Wikipedia entry on [All Time Olympic Games Medals](https://en.wikipedia.org/wiki/All-time_Olympic_Games_medal_table), and does some basic data cleaning. \n",
22 | "\n",
23 | "The columns are organized as # of Summer games, Summer medals, # of Winter games, Winter medals, total # number of games, total # of medals. Use this dataset to answer the questions below."
24 | ]
25 | },
26 | {
27 | "cell_type": "code",
28 | "execution_count": 17,
29 | "metadata": {
30 | "collapsed": false,
31 | "nbgrader": {
32 | "grade": false,
33 | "grade_id": "1",
34 | "locked": false,
35 | "solution": false
36 | }
37 | },
38 | "outputs": [
39 | {
40 | "data": {
41 | "text/html": [
42 | "
\n",
43 | "
\n",
44 | " \n",
45 | " \n",
46 | " | \n",
47 | " # Summer | \n",
48 | " Gold | \n",
49 | " Silver | \n",
50 | " Bronze | \n",
51 | " Total | \n",
52 | " # Winter | \n",
53 | " Gold.1 | \n",
54 | " Silver.1 | \n",
55 | " Bronze.1 | \n",
56 | " Total.1 | \n",
57 | " # Games | \n",
58 | " Gold.2 | \n",
59 | " Silver.2 | \n",
60 | " Bronze.2 | \n",
61 | " Combined total | \n",
62 | " ID | \n",
63 | "
\n",
64 | " \n",
65 | " \n",
66 | " \n",
67 | " | Afghanistan | \n",
68 | " 13 | \n",
69 | " 0 | \n",
70 | " 0 | \n",
71 | " 2 | \n",
72 | " 2 | \n",
73 | " 0 | \n",
74 | " 0 | \n",
75 | " 0 | \n",
76 | " 0 | \n",
77 | " 0 | \n",
78 | " 13 | \n",
79 | " 0 | \n",
80 | " 0 | \n",
81 | " 2 | \n",
82 | " 2 | \n",
83 | " AFG | \n",
84 | "
\n",
85 | " \n",
86 | " | Algeria | \n",
87 | " 12 | \n",
88 | " 5 | \n",
89 | " 2 | \n",
90 | " 8 | \n",
91 | " 15 | \n",
92 | " 3 | \n",
93 | " 0 | \n",
94 | " 0 | \n",
95 | " 0 | \n",
96 | " 0 | \n",
97 | " 15 | \n",
98 | " 5 | \n",
99 | " 2 | \n",
100 | " 8 | \n",
101 | " 15 | \n",
102 | " ALG | \n",
103 | "
\n",
104 | " \n",
105 | " | Argentina | \n",
106 | " 23 | \n",
107 | " 18 | \n",
108 | " 24 | \n",
109 | " 28 | \n",
110 | " 70 | \n",
111 | " 18 | \n",
112 | " 0 | \n",
113 | " 0 | \n",
114 | " 0 | \n",
115 | " 0 | \n",
116 | " 41 | \n",
117 | " 18 | \n",
118 | " 24 | \n",
119 | " 28 | \n",
120 | " 70 | \n",
121 | " ARG | \n",
122 | "
\n",
123 | " \n",
124 | " | Armenia | \n",
125 | " 5 | \n",
126 | " 1 | \n",
127 | " 2 | \n",
128 | " 9 | \n",
129 | " 12 | \n",
130 | " 6 | \n",
131 | " 0 | \n",
132 | " 0 | \n",
133 | " 0 | \n",
134 | " 0 | \n",
135 | " 11 | \n",
136 | " 1 | \n",
137 | " 2 | \n",
138 | " 9 | \n",
139 | " 12 | \n",
140 | " ARM | \n",
141 | "
\n",
142 | " \n",
143 | " | Australasia | \n",
144 | " 2 | \n",
145 | " 3 | \n",
146 | " 4 | \n",
147 | " 5 | \n",
148 | " 12 | \n",
149 | " 0 | \n",
150 | " 0 | \n",
151 | " 0 | \n",
152 | " 0 | \n",
153 | " 0 | \n",
154 | " 2 | \n",
155 | " 3 | \n",
156 | " 4 | \n",
157 | " 5 | \n",
158 | " 12 | \n",
159 | " ANZ | \n",
160 | "
\n",
161 | " \n",
162 | "
\n",
163 | "
"
164 | ],
165 | "text/plain": [
166 | " # Summer Gold Silver Bronze Total # Winter Gold.1 \\\n",
167 | "Afghanistan 13 0 0 2 2 0 0 \n",
168 | "Algeria 12 5 2 8 15 3 0 \n",
169 | "Argentina 23 18 24 28 70 18 0 \n",
170 | "Armenia 5 1 2 9 12 6 0 \n",
171 | "Australasia 2 3 4 5 12 0 0 \n",
172 | "\n",
173 | " Silver.1 Bronze.1 Total.1 # Games Gold.2 Silver.2 Bronze.2 \\\n",
174 | "Afghanistan 0 0 0 13 0 0 2 \n",
175 | "Algeria 0 0 0 15 5 2 8 \n",
176 | "Argentina 0 0 0 41 18 24 28 \n",
177 | "Armenia 0 0 0 11 1 2 9 \n",
178 | "Australasia 0 0 0 2 3 4 5 \n",
179 | "\n",
180 | " Combined total ID \n",
181 | "Afghanistan 2 AFG \n",
182 | "Algeria 15 ALG \n",
183 | "Argentina 70 ARG \n",
184 | "Armenia 12 ARM \n",
185 | "Australasia 12 ANZ "
186 | ]
187 | },
188 | "execution_count": 17,
189 | "metadata": {},
190 | "output_type": "execute_result"
191 | }
192 | ],
193 | "source": [
194 | "import pandas as pd\n",
195 | "\n",
196 | "df = pd.read_csv('olympics.csv', index_col=0, skiprows=1)\n",
197 | "\n",
198 | "for col in df.columns:\n",
199 | " if col[:2]=='01':\n",
200 | " df.rename(columns={col:'Gold'+col[4:]}, inplace=True)\n",
201 | " if col[:2]=='02':\n",
202 | " df.rename(columns={col:'Silver'+col[4:]}, inplace=True)\n",
203 | " if col[:2]=='03':\n",
204 | " df.rename(columns={col:'Bronze'+col[4:]}, inplace=True)\n",
205 | " if col[:1]=='№':\n",
206 | " df.rename(columns={col:'#'+col[1:]}, inplace=True)\n",
207 | "\n",
208 | "names_ids = df.index.str.split('\\s\\(') # split the index by '('\n",
209 | "\n",
210 | "df.index = names_ids.str[0] # the [0] element is the country name (new index) \n",
211 | "df['ID'] = names_ids.str[1].str[:3] # the [1] element is the abbreviation or ID (take first 3 characters from that)\n",
212 | "\n",
213 | "df = df.drop('Totals')\n",
214 | "df.head()"
215 | ]
216 | },
217 | {
218 | "cell_type": "markdown",
219 | "metadata": {},
220 | "source": [
221 | "### Question 0 (Example)\n",
222 | "\n",
223 | "What is the first country in df?\n",
224 | "\n",
225 | "*This function should return a Series.*"
226 | ]
227 | },
228 | {
229 | "cell_type": "code",
230 | "execution_count": 10,
231 | "metadata": {
232 | "collapsed": false
233 | },
234 | "outputs": [
235 | {
236 | "data": {
237 | "text/plain": [
238 | "# Summer 13\n",
239 | "Gold 0\n",
240 | "Silver 0\n",
241 | "Bronze 2\n",
242 | "Total 2\n",
243 | "# Winter 0\n",
244 | "Gold.1 0\n",
245 | "Silver.1 0\n",
246 | "Bronze.1 0\n",
247 | "Total.1 0\n",
248 | "# Games 13\n",
249 | "Gold.2 0\n",
250 | "Silver.2 0\n",
251 | "Bronze.2 2\n",
252 | "Combined total 2\n",
253 | "ID AFG\n",
254 | "Name: Afghanistan, dtype: object"
255 | ]
256 | },
257 | "execution_count": 10,
258 | "metadata": {},
259 | "output_type": "execute_result"
260 | }
261 | ],
262 | "source": [
263 | "# You should write your whole answer within the function provided. The autograder will call\n",
264 | "# this function and compare the return value against the correct solution value\n",
265 | "def answer_zero():\n",
266 | " # This function returns the row for Afghanistan, which is a Series object. The assignment\n",
267 | " # question description will tell you the general format the autograder is expecting\n",
268 | " return df.iloc[0]\n",
269 | "\n",
270 | "# You can examine what your function returns by calling it in the cell. If you have questions\n",
271 | "# about the assignment formats, check out the discussion forums for any FAQs\n",
272 | "answer_zero() "
273 | ]
274 | },
275 | {
276 | "cell_type": "markdown",
277 | "metadata": {},
278 | "source": [
279 | "### Question 1\n",
280 | "Which country has won the most gold medals in summer games?\n",
281 | "\n",
282 | "*This function should return a single string value.*"
283 | ]
284 | },
285 | {
286 | "cell_type": "code",
287 | "execution_count": 12,
288 | "metadata": {
289 | "collapsed": false,
290 | "nbgrader": {
291 | "grade": false,
292 | "locked": false,
293 | "solution": false
294 | }
295 | },
296 | "outputs": [],
297 | "source": [
298 | "def answer_one():\n",
299 | " return df['Gold'].argmax()"
300 | ]
301 | },
302 | {
303 | "cell_type": "markdown",
304 | "metadata": {},
305 | "source": [
306 | "### Question 2\n",
307 | "Which country had the biggest difference between their summer and winter gold medal counts?\n",
308 | "\n",
309 | "*This function should return a single string value.*"
310 | ]
311 | },
312 | {
313 | "cell_type": "code",
314 | "execution_count": 92,
315 | "metadata": {
316 | "collapsed": false
317 | },
318 | "outputs": [],
319 | "source": [
320 | "def answer_two():\n",
321 | " return (df['Gold']-df['Gold.1']).argmax()"
322 | ]
323 | },
324 | {
325 | "cell_type": "markdown",
326 | "metadata": {},
327 | "source": [
328 | "### Question 3\n",
329 | "Which country has the biggest difference between their summer gold medal counts and winter gold medal counts relative to their total gold medal count? \n",
330 | "\n",
331 | "$$\\frac{Summer~Gold - Winter~Gold}{Total~Gold}$$\n",
332 | "\n",
333 | "Only include countries that have won at least 1 gold in both summer and winter.\n",
334 | "\n",
335 | "*This function should return a single string value.*"
336 | ]
337 | },
338 | {
339 | "cell_type": "code",
340 | "execution_count": 25,
341 | "metadata": {
342 | "collapsed": false
343 | },
344 | "outputs": [],
345 | "source": [
346 | "def answer_three():\n",
347 | " copy_df = df.copy()\n",
348 | " copy_df = copy_df[(copy_df['Gold']>0) & (copy_df['Gold.1']>0)]\n",
349 | " return ((copy_df['Gold']-copy_df['Gold.1'])/copy_df['Gold.2']).argmax()"
350 | ]
351 | },
352 | {
353 | "cell_type": "markdown",
354 | "metadata": {},
355 | "source": [
356 | "### Question 4\n",
357 | "Write a function that creates a Series called \"Points\" which is a weighted value where each gold medal (`Gold.2`) counts for 3 points, silver medals (`Silver.2`) for 2 points, and bronze medals (`Bronze.2`) for 1 point. The function should return only the column (a Series object) which you created.\n",
358 | "\n",
359 | "*This function should return a Series named `Points` of length 146*"
360 | ]
361 | },
362 | {
363 | "cell_type": "code",
364 | "execution_count": 28,
365 | "metadata": {
366 | "collapsed": false
367 | },
368 | "outputs": [],
369 | "source": [
370 | "def answer_four():\n",
371 | " df['Points'] = df['Gold.2']*3 + df['Silver.2']*2 + df['Bronze.2']\n",
372 | " return df['Points']"
373 | ]
374 | },
375 | {
376 | "cell_type": "markdown",
377 | "metadata": {},
378 | "source": [
379 | "## Part 2\n",
380 | "For the next set of questions, we will be using census data from the [United States Census Bureau](http://www.census.gov/popest/data/counties/totals/2015/CO-EST2015-alldata.html). Counties are political and geographic subdivisions of states in the United States. This dataset contains population data for counties and states in the US from 2010 to 2015. [See this document](http://www.census.gov/popest/data/counties/totals/2015/files/CO-EST2015-alldata.pdf) for a description of the variable names.\n",
381 | "\n",
382 | "The census dataset (census.csv) should be loaded as census_df. Answer questions using this as appropriate.\n",
383 | "\n",
384 | "### Question 5\n",
385 | "Which state has the most counties in it? (hint: consider the sumlevel key carefully! You'll need this for future questions too...)\n",
386 | "\n",
387 | "*This function should return a single string value.*"
388 | ]
389 | },
390 | {
391 | "cell_type": "code",
392 | "execution_count": 29,
393 | "metadata": {
394 | "collapsed": false
395 | },
396 | "outputs": [
397 | {
398 | "data": {
399 | "text/html": [
400 | "\n",
401 | "
\n",
402 | " \n",
403 | " \n",
404 | " | \n",
405 | " SUMLEV | \n",
406 | " REGION | \n",
407 | " DIVISION | \n",
408 | " STATE | \n",
409 | " COUNTY | \n",
410 | " STNAME | \n",
411 | " CTYNAME | \n",
412 | " CENSUS2010POP | \n",
413 | " ESTIMATESBASE2010 | \n",
414 | " POPESTIMATE2010 | \n",
415 | " ... | \n",
416 | " RDOMESTICMIG2011 | \n",
417 | " RDOMESTICMIG2012 | \n",
418 | " RDOMESTICMIG2013 | \n",
419 | " RDOMESTICMIG2014 | \n",
420 | " RDOMESTICMIG2015 | \n",
421 | " RNETMIG2011 | \n",
422 | " RNETMIG2012 | \n",
423 | " RNETMIG2013 | \n",
424 | " RNETMIG2014 | \n",
425 | " RNETMIG2015 | \n",
426 | "
\n",
427 | " \n",
428 | " \n",
429 | " \n",
430 | " | 0 | \n",
431 | " 40 | \n",
432 | " 3 | \n",
433 | " 6 | \n",
434 | " 1 | \n",
435 | " 0 | \n",
436 | " Alabama | \n",
437 | " Alabama | \n",
438 | " 4779736 | \n",
439 | " 4780127 | \n",
440 | " 4785161 | \n",
441 | " ... | \n",
442 | " 0.002295 | \n",
443 | " -0.193196 | \n",
444 | " 0.381066 | \n",
445 | " 0.582002 | \n",
446 | " -0.467369 | \n",
447 | " 1.030015 | \n",
448 | " 0.826644 | \n",
449 | " 1.383282 | \n",
450 | " 1.724718 | \n",
451 | " 0.712594 | \n",
452 | "
\n",
453 | " \n",
454 | " | 1 | \n",
455 | " 50 | \n",
456 | " 3 | \n",
457 | " 6 | \n",
458 | " 1 | \n",
459 | " 1 | \n",
460 | " Alabama | \n",
461 | " Autauga County | \n",
462 | " 54571 | \n",
463 | " 54571 | \n",
464 | " 54660 | \n",
465 | " ... | \n",
466 | " 7.242091 | \n",
467 | " -2.915927 | \n",
468 | " -3.012349 | \n",
469 | " 2.265971 | \n",
470 | " -2.530799 | \n",
471 | " 7.606016 | \n",
472 | " -2.626146 | \n",
473 | " -2.722002 | \n",
474 | " 2.592270 | \n",
475 | " -2.187333 | \n",
476 | "
\n",
477 | " \n",
478 | " | 2 | \n",
479 | " 50 | \n",
480 | " 3 | \n",
481 | " 6 | \n",
482 | " 1 | \n",
483 | " 3 | \n",
484 | " Alabama | \n",
485 | " Baldwin County | \n",
486 | " 182265 | \n",
487 | " 182265 | \n",
488 | " 183193 | \n",
489 | " ... | \n",
490 | " 14.832960 | \n",
491 | " 17.647293 | \n",
492 | " 21.845705 | \n",
493 | " 19.243287 | \n",
494 | " 17.197872 | \n",
495 | " 15.844176 | \n",
496 | " 18.559627 | \n",
497 | " 22.727626 | \n",
498 | " 20.317142 | \n",
499 | " 18.293499 | \n",
500 | "
\n",
501 | " \n",
502 | " | 3 | \n",
503 | " 50 | \n",
504 | " 3 | \n",
505 | " 6 | \n",
506 | " 1 | \n",
507 | " 5 | \n",
508 | " Alabama | \n",
509 | " Barbour County | \n",
510 | " 27457 | \n",
511 | " 27457 | \n",
512 | " 27341 | \n",
513 | " ... | \n",
514 | " -4.728132 | \n",
515 | " -2.500690 | \n",
516 | " -7.056824 | \n",
517 | " -3.904217 | \n",
518 | " -10.543299 | \n",
519 | " -4.874741 | \n",
520 | " -2.758113 | \n",
521 | " -7.167664 | \n",
522 | " -3.978583 | \n",
523 | " -10.543299 | \n",
524 | "
\n",
525 | " \n",
526 | " | 4 | \n",
527 | " 50 | \n",
528 | " 3 | \n",
529 | " 6 | \n",
530 | " 1 | \n",
531 | " 7 | \n",
532 | " Alabama | \n",
533 | " Bibb County | \n",
534 | " 22915 | \n",
535 | " 22919 | \n",
536 | " 22861 | \n",
537 | " ... | \n",
538 | " -5.527043 | \n",
539 | " -5.068871 | \n",
540 | " -6.201001 | \n",
541 | " -0.177537 | \n",
542 | " 0.177258 | \n",
543 | " -5.088389 | \n",
544 | " -4.363636 | \n",
545 | " -5.403729 | \n",
546 | " 0.754533 | \n",
547 | " 1.107861 | \n",
548 | "
\n",
549 | " \n",
550 | "
\n",
551 | "
5 rows × 100 columns
\n",
552 | "
"
553 | ],
554 | "text/plain": [
555 | " SUMLEV REGION DIVISION STATE COUNTY STNAME CTYNAME \\\n",
556 | "0 40 3 6 1 0 Alabama Alabama \n",
557 | "1 50 3 6 1 1 Alabama Autauga County \n",
558 | "2 50 3 6 1 3 Alabama Baldwin County \n",
559 | "3 50 3 6 1 5 Alabama Barbour County \n",
560 | "4 50 3 6 1 7 Alabama Bibb County \n",
561 | "\n",
562 | " CENSUS2010POP ESTIMATESBASE2010 POPESTIMATE2010 ... \\\n",
563 | "0 4779736 4780127 4785161 ... \n",
564 | "1 54571 54571 54660 ... \n",
565 | "2 182265 182265 183193 ... \n",
566 | "3 27457 27457 27341 ... \n",
567 | "4 22915 22919 22861 ... \n",
568 | "\n",
569 | " RDOMESTICMIG2011 RDOMESTICMIG2012 RDOMESTICMIG2013 RDOMESTICMIG2014 \\\n",
570 | "0 0.002295 -0.193196 0.381066 0.582002 \n",
571 | "1 7.242091 -2.915927 -3.012349 2.265971 \n",
572 | "2 14.832960 17.647293 21.845705 19.243287 \n",
573 | "3 -4.728132 -2.500690 -7.056824 -3.904217 \n",
574 | "4 -5.527043 -5.068871 -6.201001 -0.177537 \n",
575 | "\n",
576 | " RDOMESTICMIG2015 RNETMIG2011 RNETMIG2012 RNETMIG2013 RNETMIG2014 \\\n",
577 | "0 -0.467369 1.030015 0.826644 1.383282 1.724718 \n",
578 | "1 -2.530799 7.606016 -2.626146 -2.722002 2.592270 \n",
579 | "2 17.197872 15.844176 18.559627 22.727626 20.317142 \n",
580 | "3 -10.543299 -4.874741 -2.758113 -7.167664 -3.978583 \n",
581 | "4 0.177258 -5.088389 -4.363636 -5.403729 0.754533 \n",
582 | "\n",
583 | " RNETMIG2015 \n",
584 | "0 0.712594 \n",
585 | "1 -2.187333 \n",
586 | "2 18.293499 \n",
587 | "3 -10.543299 \n",
588 | "4 1.107861 \n",
589 | "\n",
590 | "[5 rows x 100 columns]"
591 | ]
592 | },
593 | "execution_count": 29,
594 | "metadata": {},
595 | "output_type": "execute_result"
596 | }
597 | ],
598 | "source": [
599 | "census_df = pd.read_csv('census.csv')\n",
600 | "census_df.head()"
601 | ]
602 | },
603 | {
604 | "cell_type": "code",
605 | "execution_count": 38,
606 | "metadata": {
607 | "collapsed": false
608 | },
609 | "outputs": [],
610 | "source": [
611 | "def answer_five():\n",
612 | " return census_df['STNAME'].value_counts().argmax()"
613 | ]
614 | },
615 | {
616 | "cell_type": "markdown",
617 | "metadata": {},
618 | "source": [
619 | "### Question 6\n",
620 | "Only looking at the three most populous counties for each state, what are the three most populous states (in order of highest population to lowest population)? Use `CENSUS2010POP`.\n",
621 | "\n",
622 | "*This function should return a list of string values.*"
623 | ]
624 | },
625 | {
626 | "cell_type": "code",
627 | "execution_count": 139,
628 | "metadata": {
629 | "collapsed": false
630 | },
631 | "outputs": [
632 | {
633 | "data": {
634 | "text/plain": [
635 | "['California', 'Texas', 'Illinois']"
636 | ]
637 | },
638 | "execution_count": 139,
639 | "metadata": {},
640 | "output_type": "execute_result"
641 | }
642 | ],
643 | "source": [
644 | "def answer_six():\n",
645 | " copy_df = census_df.copy()\n",
646 | " copy_df = copy_df.groupby(['STNAME'])\n",
647 | " states_pop = pd.DataFrame(columns=['pop'])\n",
648 | " for i, c in copy_df:\n",
649 | " states_pop.loc[i] = [c.sort_values(by='CENSUS2010POP', ascending=False)[1:4]['CENSUS2010POP'].sum()]\n",
650 | " top3 = states_pop.nlargest(3,'pop')\n",
651 | " return list(top3.index)"
652 | ]
653 | },
654 | {
655 | "cell_type": "markdown",
656 | "metadata": {},
657 | "source": [
658 | "### Question 7\n",
659 | "Which county has had the largest absolute change in population within the period 2010-2015? (Hint: population values are stored in columns POPESTIMATE2010 through POPESTIMATE2015, you need to consider all six columns.)\n",
660 | "\n",
661 | "e.g. If County Population in the 5 year period is 100, 120, 80, 105, 100, 130, then its largest change in the period would be |130-80| = 50.\n",
662 | "\n",
663 | "*This function should return a single string value.*"
664 | ]
665 | },
666 | {
667 | "cell_type": "code",
668 | "execution_count": 150,
669 | "metadata": {
670 | "collapsed": false
671 | },
672 | "outputs": [
673 | {
674 | "data": {
675 | "text/plain": [
676 | "'Harris County'"
677 | ]
678 | },
679 | "execution_count": 150,
680 | "metadata": {},
681 | "output_type": "execute_result"
682 | }
683 | ],
684 | "source": [
685 | "def answer_seven():\n",
686 | " pop = census_df[['STNAME','CTYNAME','POPESTIMATE2015','POPESTIMATE2014','POPESTIMATE2013','POPESTIMATE2012','POPESTIMATE2011','POPESTIMATE2010']]\n",
687 | " pop = pop[pop['STNAME']!=pop['CTYNAME']]\n",
688 | " index = (pop.max(axis=1)-pop.min(axis=1)).argmax()\n",
689 | " return census_df.loc[index]['CTYNAME']"
690 | ]
691 | },
692 | {
693 | "cell_type": "markdown",
694 | "metadata": {},
695 | "source": [
696 | "### Question 8\n",
697 | "In this datafile, the United States is broken up into four regions using the \"REGION\" column. \n",
698 | "\n",
699 | "Create a query that finds the counties that belong to regions 1 or 2, whose name starts with 'Washington', and whose POPESTIMATE2015 was greater than their POPESTIMATE 2014.\n",
700 | "\n",
701 | "*This function should return a 5x2 DataFrame with the columns = ['STNAME', 'CTYNAME'] and the same index ID as the census_df (sorted ascending by index).*"
702 | ]
703 | },
704 | {
705 | "cell_type": "code",
706 | "execution_count": 163,
707 | "metadata": {
708 | "collapsed": false
709 | },
710 | "outputs": [
711 | {
712 | "data": {
713 | "text/html": [
714 | "\n",
715 | "
\n",
716 | " \n",
717 | " \n",
718 | " | \n",
719 | " STNAME | \n",
720 | " CTYNAME | \n",
721 | "
\n",
722 | " \n",
723 | " \n",
724 | " \n",
725 | " | 896 | \n",
726 | " Iowa | \n",
727 | " Washington County | \n",
728 | "
\n",
729 | " \n",
730 | " | 1419 | \n",
731 | " Minnesota | \n",
732 | " Washington County | \n",
733 | "
\n",
734 | " \n",
735 | " | 2345 | \n",
736 | " Pennsylvania | \n",
737 | " Washington County | \n",
738 | "
\n",
739 | " \n",
740 | " | 2355 | \n",
741 | " Rhode Island | \n",
742 | " Washington County | \n",
743 | "
\n",
744 | " \n",
745 | " | 3163 | \n",
746 | " Wisconsin | \n",
747 | " Washington County | \n",
748 | "
\n",
749 | " \n",
750 | "
\n",
751 | "
"
752 | ],
753 | "text/plain": [
754 | " STNAME CTYNAME\n",
755 | "896 Iowa Washington County\n",
756 | "1419 Minnesota Washington County\n",
757 | "2345 Pennsylvania Washington County\n",
758 | "2355 Rhode Island Washington County\n",
759 | "3163 Wisconsin Washington County"
760 | ]
761 | },
762 | "execution_count": 163,
763 | "metadata": {},
764 | "output_type": "execute_result"
765 | }
766 | ],
767 | "source": [
768 | "def answer_eight():\n",
769 | " return census_df[(census_df['REGION']<3 ) & (census_df['CTYNAME'] == 'Washington County') & (census_df['POPESTIMATE2015']>census_df['POPESTIMATE2014'])][['STNAME','CTYNAME']]"
770 | ]
771 | },
772 | {
773 | "cell_type": "code",
774 | "execution_count": null,
775 | "metadata": {
776 | "collapsed": true
777 | },
778 | "outputs": [],
779 | "source": []
780 | }
781 | ],
782 | "metadata": {
783 | "anaconda-cloud": {},
784 | "coursera": {
785 | "course_slug": "python-data-analysis",
786 | "graded_item_id": "tHmgx",
787 | "launcher_item_id": "Um6Bz",
788 | "part_id": "OQsnr"
789 | },
790 | "kernelspec": {
791 | "display_name": "Python 3",
792 | "language": "python",
793 | "name": "python3"
794 | },
795 | "language_info": {
796 | "codemirror_mode": {
797 | "name": "ipython",
798 | "version": 3
799 | },
800 | "file_extension": ".py",
801 | "mimetype": "text/x-python",
802 | "name": "python",
803 | "nbconvert_exporter": "python",
804 | "pygments_lexer": "ipython3",
805 | "version": "3.5.2"
806 | }
807 | },
808 | "nbformat": 4,
809 | "nbformat_minor": 0
810 | }
811 |
--------------------------------------------------------------------------------
/Week 4/Week4.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "---\n",
8 | "\n",
9 | "_You are currently looking at **version 1.0** of this notebook. To download notebooks and datafiles, as well as get help on Jupyter notebooks in the Coursera platform, visit the [Jupyter Notebook FAQ](https://www.coursera.org/learn/python-data-analysis/resources/0dhYG) course resource._\n",
10 | "\n",
11 | "---"
12 | ]
13 | },
14 | {
15 | "cell_type": "markdown",
16 | "metadata": {},
17 | "source": [
18 | "# Distributions in Pandas"
19 | ]
20 | },
21 | {
22 | "cell_type": "code",
23 | "execution_count": 1,
24 | "metadata": {
25 | "collapsed": false
26 | },
27 | "outputs": [],
28 | "source": [
29 | "import pandas as pd\n",
30 | "import numpy as np"
31 | ]
32 | },
33 | {
34 | "cell_type": "code",
35 | "execution_count": 23,
36 | "metadata": {
37 | "collapsed": false
38 | },
39 | "outputs": [
40 | {
41 | "data": {
42 | "text/plain": [
43 | "0"
44 | ]
45 | },
46 | "execution_count": 23,
47 | "metadata": {},
48 | "output_type": "execute_result"
49 | }
50 | ],
51 | "source": [
52 | "np.random.binomial(1, 0.5)"
53 | ]
54 | },
55 | {
56 | "cell_type": "code",
57 | "execution_count": 13,
58 | "metadata": {
59 | "collapsed": false
60 | },
61 | "outputs": [
62 | {
63 | "data": {
64 | "text/plain": [
65 | "0.499456"
66 | ]
67 | },
68 | "execution_count": 13,
69 | "metadata": {},
70 | "output_type": "execute_result"
71 | }
72 | ],
73 | "source": [
74 | "np.random.binomial(1000000, 0.5)/1000000"
75 | ]
76 | },
77 | {
78 | "cell_type": "code",
79 | "execution_count": 27,
80 | "metadata": {
81 | "collapsed": false
82 | },
83 | "outputs": [
84 | {
85 | "data": {
86 | "text/plain": [
87 | "9"
88 | ]
89 | },
90 | "execution_count": 27,
91 | "metadata": {},
92 | "output_type": "execute_result"
93 | }
94 | ],
95 | "source": [
96 | "chance_of_tornado = 0.01/100\n",
97 | "np.random.binomial(100000, chance_of_tornado)"
98 | ]
99 | },
100 | {
101 | "cell_type": "code",
102 | "execution_count": 30,
103 | "metadata": {
104 | "collapsed": false
105 | },
106 | "outputs": [
107 | {
108 | "name": "stdout",
109 | "output_type": "stream",
110 | "text": [
111 | "118 tornadoes back to back in 2739.72602739726 years\n"
112 | ]
113 | }
114 | ],
115 | "source": [
116 | "chance_of_tornado = 0.01\n",
117 | "\n",
118 | "tornado_events = np.random.binomial(1, chance_of_tornado, 1000000)\n",
119 | " \n",
120 | "two_days_in_a_row = 0\n",
121 | "for j in range(1,len(tornado_events)-1):\n",
122 | " if tornado_events[j]==1 and tornado_events[j-1]==1:\n",
123 | " two_days_in_a_row+=1\n",
124 | "\n",
125 | "print('{} tornadoes back to back in {} years'.format(two_days_in_a_row, 1000000/365))"
126 | ]
127 | },
128 | {
129 | "cell_type": "code",
130 | "execution_count": 55,
131 | "metadata": {
132 | "collapsed": false
133 | },
134 | "outputs": [
135 | {
136 | "data": {
137 | "text/plain": [
138 | "0.6295388722617103"
139 | ]
140 | },
141 | "execution_count": 55,
142 | "metadata": {},
143 | "output_type": "execute_result"
144 | }
145 | ],
146 | "source": [
147 | "np.random.uniform(0, 1)"
148 | ]
149 | },
150 | {
151 | "cell_type": "code",
152 | "execution_count": 64,
153 | "metadata": {
154 | "collapsed": false
155 | },
156 | "outputs": [
157 | {
158 | "data": {
159 | "text/plain": [
160 | "-1.2469247211946008"
161 | ]
162 | },
163 | "execution_count": 64,
164 | "metadata": {},
165 | "output_type": "execute_result"
166 | }
167 | ],
168 | "source": [
169 | "np.random.normal(0.75)"
170 | ]
171 | },
172 | {
173 | "cell_type": "markdown",
174 | "metadata": {},
175 | "source": [
176 | "Formula for standard deviation\n",
177 | "$$\\sqrt{\\frac{1}{N} \\sum_{i=1}^N (x_i - \\overline{x})^2}$$"
178 | ]
179 | },
180 | {
181 | "cell_type": "code",
182 | "execution_count": 70,
183 | "metadata": {
184 | "collapsed": false
185 | },
186 | "outputs": [
187 | {
188 | "data": {
189 | "text/plain": [
190 | "1.0065258567421238"
191 | ]
192 | },
193 | "execution_count": 70,
194 | "metadata": {},
195 | "output_type": "execute_result"
196 | }
197 | ],
198 | "source": [
199 | "distribution = np.random.normal(0.75,size=10000)\n",
200 | "\n",
201 | "np.sqrt(np.sum((np.mean(distribution)-distribution)**2)/len(distribution))"
202 | ]
203 | },
204 | {
205 | "cell_type": "code",
206 | "execution_count": 75,
207 | "metadata": {
208 | "collapsed": false,
209 | "scrolled": true
210 | },
211 | "outputs": [
212 | {
213 | "data": {
214 | "text/plain": [
215 | "1.0065258567421238"
216 | ]
217 | },
218 | "execution_count": 75,
219 | "metadata": {},
220 | "output_type": "execute_result"
221 | }
222 | ],
223 | "source": [
224 | "np.std(distribution)"
225 | ]
226 | },
227 | {
228 | "cell_type": "code",
229 | "execution_count": 78,
230 | "metadata": {
231 | "collapsed": false
232 | },
233 | "outputs": [
234 | {
235 | "data": {
236 | "text/plain": [
237 | "-0.013249564329728791"
238 | ]
239 | },
240 | "execution_count": 78,
241 | "metadata": {},
242 | "output_type": "execute_result"
243 | }
244 | ],
245 | "source": [
246 | "import scipy.stats as stats\n",
247 | "stats.kurtosis(distribution)"
248 | ]
249 | },
250 | {
251 | "cell_type": "code",
252 | "execution_count": 79,
253 | "metadata": {
254 | "collapsed": false
255 | },
256 | "outputs": [
257 | {
258 | "data": {
259 | "text/plain": [
260 | "-0.0048870324877872545"
261 | ]
262 | },
263 | "execution_count": 79,
264 | "metadata": {},
265 | "output_type": "execute_result"
266 | }
267 | ],
268 | "source": [
269 | "stats.skew(distribution)"
270 | ]
271 | },
272 | {
273 | "cell_type": "code",
274 | "execution_count": 82,
275 | "metadata": {
276 | "collapsed": false
277 | },
278 | "outputs": [
279 | {
280 | "data": {
281 | "text/plain": [
282 | "1.9889311393293112"
283 | ]
284 | },
285 | "execution_count": 82,
286 | "metadata": {},
287 | "output_type": "execute_result"
288 | }
289 | ],
290 | "source": [
291 | "chi_squared_df2 = np.random.chisquare(2, size=100000)\n",
292 | "stats.skew(chi_squared_df2)"
293 | ]
294 | },
295 | {
296 | "cell_type": "code",
297 | "execution_count": 88,
298 | "metadata": {
299 | "collapsed": false
300 | },
301 | "outputs": [
302 | {
303 | "data": {
304 | "text/plain": [
305 | "1.3322980185207178"
306 | ]
307 | },
308 | "execution_count": 88,
309 | "metadata": {},
310 | "output_type": "execute_result"
311 | }
312 | ],
313 | "source": [
314 | "chi_squared_df5 = np.random.chisquare(5, size=10000)\n",
315 | "stats.skew(chi_squared_df5)"
316 | ]
317 | },
318 | {
319 | "cell_type": "code",
320 | "execution_count": 85,
321 | "metadata": {
322 | "collapsed": false
323 | },
324 | "outputs": [
325 | {
326 | "data": {
327 | "text/plain": [
328 | ""
329 | ]
330 | },
331 | "execution_count": 85,
332 | "metadata": {},
333 | "output_type": "execute_result"
334 | },
335 | {
336 | "data": {
337 | "image/png": "iVBORw0KGgoAAAANSUhEUgAAAh8AAAFkCAYAAACAUFlOAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAAPYQAAD2EBqD+naQAAIABJREFUeJzs3Xl8VdW5//HPc5IQEqYwBmUKgyAiIoSKijJIX0BxQhzT\nSwVsLaWt9Yf2YsWJQQvFISgqBaTigLEKRepVIUpBaaXYAiJXAaFacCgoU0AwDMnz++Oc5CYh54SE\nZAfk+/Z1XpK9nrP32iuE8806a+9j7o6IiIhIUELV3QERERE5tSh8iIiISKAUPkRERCRQCh8iIiIS\nKIUPERERCZTCh4iIiARK4UNEREQCpfAhIiIigVL4EBERkUApfIiIiEigyhU+zOxnZrbWzHIij3fN\nbGCJmglm9qWZHTCzN82sXYn2RDN7wsx2mNk+M5tnZk1K1NQ3s7mRY+w2s6fMrFaJmhZm9pqZ7Tez\nbWY2xcwUpkRERE5w5X2x/gy4A+gGpAN/ARaaWUcAM7sD+CXwU+A8YD+w2MxqFNnHVOBS4GqgF3A6\nML/EcV4AOgL9IrW9gBkFjZGQ8ToQD5wPDAOGAxPKeT4iIiISMDveD5Yzs53Ar939aTP7EnjQ3TMj\nbXWB7cAwd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DOuzkPjOyS9I6m1jQFKRtJM0l1UZfYOAZtx9pqZm4fJ\nt0salnRx0Q3qMheRRo0OgLM2AePqVsF5q0HSJEkPkdbP2tjOnPV954PGG9DV30PdNgGPAAuAJcBM\n4EtJU4psVI+ZQXqjc/YmZi2wGJgHPAncDnysZtscl0SuwyvAhogYm4flrDVRp27gvJ1G0vWS/gb+\nAYaB+yLiR9qYs55eZMz+PxFRuXb/NklbgF+AB4GRYlplZVA1dPu9pO+AHcBc4ItCGtVdhoHrgFuK\nbkiPqVk3562m7cAsYBrwAPC2pNva+R+UYeTjbDagsyoRcRD4CSj17PkJ2k/aTNzZa0FE7CK9jkuf\nPUmvA/cAcyPit4pfOWsNNKjbaZw3iIh/I2JnRHwdEU+TvqSxlDbmrO87HxFxHNgKzB87l4fT5gMb\ni2pXr5F0AenF2PCFa6fkN7H9jM/eVNLMe2fvDEm6AriEkmcvf4AuAu6IiNHK3zlr9TWqW53rnbfT\nTQLObWfOyvLYxRvQTZCkFcBHpEctlwPPAceB94psV7fJc2AGSHcDAFdJmgUciIg9pGfMz0j6GdgN\nPA/sBVYX0Nyu0Khm+RgCPiC9yQ0AL5JG3VrexrtXSRomff1zIXBY0tid58GIOJb/7axVaVa3nEXn\nrYKkF0jzYEaBC4FB0jyYu/Il7clZRJTiIK0psBs4CnwF3Fh0m7r5IHUy9uZ6jQKrgJlFt6vbjvyi\nPEl6tFd5vFVxzbPAPuAI6Q1toOh2d2vNgMnAJ6QPgmPATuAN4LKi211wzWrV6wSwuOo6Z20CdXPe\natbszVyHo7ku64F5Vde0nLNSrPNhZmZm3aPv53yYmZlZd3Hnw8zMzDrKnQ8zMzPrKHc+zMzMrKPc\n+TAzM7OOcufDzMzMOsqdDzMzM+sodz7MzMyso9z5MDMzs45y58PMzMw6yp0PMzMz66j/ANrRqHdO\nOq2fAAAAAElFTkSuQmCC\n",
338 | "text/plain": [
339 | ""
340 | ]
341 | },
342 | "metadata": {},
343 | "output_type": "display_data"
344 | }
345 | ],
346 | "source": [
347 | "%matplotlib inline\n",
348 | "import matplotlib\n",
349 | "import matplotlib.pyplot as plt\n",
350 | "\n",
351 | "output = plt.hist([chi_squared_df2,chi_squared_df5], bins=50, histtype='step', \n",
352 | " label=['2 degrees of freedom','5 degrees of freedom'])\n",
353 | "plt.legend(loc='upper right')\n"
354 | ]
355 | },
356 | {
357 | "cell_type": "markdown",
358 | "metadata": {},
359 | "source": [
360 | "# Hypothesis Testing"
361 | ]
362 | },
363 | {
364 | "cell_type": "code",
365 | "execution_count": 40,
366 | "metadata": {
367 | "collapsed": false
368 | },
369 | "outputs": [],
370 | "source": [
371 | "df = pd.read_csv('grades.csv')"
372 | ]
373 | },
374 | {
375 | "cell_type": "code",
376 | "execution_count": 41,
377 | "metadata": {
378 | "collapsed": false
379 | },
380 | "outputs": [
381 | {
382 | "data": {
383 | "text/html": [
384 | "\n",
385 | "
\n",
386 | " \n",
387 | " \n",
388 | " | \n",
389 | " student_id | \n",
390 | " assignment1_grade | \n",
391 | " assignment1_submission | \n",
392 | " assignment2_grade | \n",
393 | " assignment2_submission | \n",
394 | " assignment3_grade | \n",
395 | " assignment3_submission | \n",
396 | " assignment4_grade | \n",
397 | " assignment4_submission | \n",
398 | " assignment5_grade | \n",
399 | " assignment5_submission | \n",
400 | " assignment6_grade | \n",
401 | " assignment6_submission | \n",
402 | "
\n",
403 | " \n",
404 | " \n",
405 | " \n",
406 | " | 0 | \n",
407 | " B73F2C11-70F0-E37D-8B10-1D20AFED50B1 | \n",
408 | " 92.733946 | \n",
409 | " 2015-11-02 06:55:34.282000000 | \n",
410 | " 83.030552 | \n",
411 | " 2015-11-09 02:22:58.938000000 | \n",
412 | " 67.164441 | \n",
413 | " 2015-11-12 08:58:33.998000000 | \n",
414 | " 53.011553 | \n",
415 | " 2015-11-16 01:21:24.663000000 | \n",
416 | " 47.710398 | \n",
417 | " 2015-11-20 13:24:59.692000000 | \n",
418 | " 38.168318 | \n",
419 | " 2015-11-22 18:31:15.934000000 | \n",
420 | "
\n",
421 | " \n",
422 | " | 1 | \n",
423 | " 98A0FAE0-A19A-13D2-4BB5-CFBFD94031D1 | \n",
424 | " 86.790821 | \n",
425 | " 2015-11-29 14:57:44.429000000 | \n",
426 | " 86.290821 | \n",
427 | " 2015-12-06 17:41:18.449000000 | \n",
428 | " 69.772657 | \n",
429 | " 2015-12-10 08:54:55.904000000 | \n",
430 | " 55.098125 | \n",
431 | " 2015-12-13 17:32:30.941000000 | \n",
432 | " 49.588313 | \n",
433 | " 2015-12-19 23:26:39.285000000 | \n",
434 | " 44.629482 | \n",
435 | " 2015-12-21 17:07:24.275000000 | \n",
436 | "
\n",
437 | " \n",
438 | " | 2 | \n",
439 | " D0F62040-CEB0-904C-F563-2F8620916C4E | \n",
440 | " 85.512541 | \n",
441 | " 2016-01-09 05:36:02.389000000 | \n",
442 | " 85.512541 | \n",
443 | " 2016-01-09 06:39:44.416000000 | \n",
444 | " 68.410033 | \n",
445 | " 2016-01-15 20:22:45.882000000 | \n",
446 | " 54.728026 | \n",
447 | " 2016-01-11 12:41:50.749000000 | \n",
448 | " 49.255224 | \n",
449 | " 2016-01-11 17:31:12.489000000 | \n",
450 | " 44.329701 | \n",
451 | " 2016-01-17 16:24:42.765000000 | \n",
452 | "
\n",
453 | " \n",
454 | " | 3 | \n",
455 | " FFDF2B2C-F514-EF7F-6538-A6A53518E9DC | \n",
456 | " 86.030665 | \n",
457 | " 2016-04-30 06:50:39.801000000 | \n",
458 | " 68.824532 | \n",
459 | " 2016-04-30 17:20:38.727000000 | \n",
460 | " 61.942079 | \n",
461 | " 2016-05-12 07:47:16.326000000 | \n",
462 | " 49.553663 | \n",
463 | " 2016-05-07 16:09:20.485000000 | \n",
464 | " 49.553663 | \n",
465 | " 2016-05-24 12:51:18.016000000 | \n",
466 | " 44.598297 | \n",
467 | " 2016-05-26 08:09:12.058000000 | \n",
468 | "
\n",
469 | " \n",
470 | " | 4 | \n",
471 | " 5ECBEEB6-F1CE-80AE-3164-E45E99473FB4 | \n",
472 | " 64.813800 | \n",
473 | " 2015-12-13 17:06:10.750000000 | \n",
474 | " 51.491040 | \n",
475 | " 2015-12-14 12:25:12.056000000 | \n",
476 | " 41.932832 | \n",
477 | " 2015-12-29 14:25:22.594000000 | \n",
478 | " 36.929549 | \n",
479 | " 2015-12-28 01:29:55.901000000 | \n",
480 | " 33.236594 | \n",
481 | " 2015-12-29 14:46:06.628000000 | \n",
482 | " 33.236594 | \n",
483 | " 2016-01-05 01:06:59.546000000 | \n",
484 | "
\n",
485 | " \n",
486 | "
\n",
487 | "
"
488 | ],
489 | "text/plain": [
490 | " student_id assignment1_grade \\\n",
491 | "0 B73F2C11-70F0-E37D-8B10-1D20AFED50B1 92.733946 \n",
492 | "1 98A0FAE0-A19A-13D2-4BB5-CFBFD94031D1 86.790821 \n",
493 | "2 D0F62040-CEB0-904C-F563-2F8620916C4E 85.512541 \n",
494 | "3 FFDF2B2C-F514-EF7F-6538-A6A53518E9DC 86.030665 \n",
495 | "4 5ECBEEB6-F1CE-80AE-3164-E45E99473FB4 64.813800 \n",
496 | "\n",
497 | " assignment1_submission assignment2_grade \\\n",
498 | "0 2015-11-02 06:55:34.282000000 83.030552 \n",
499 | "1 2015-11-29 14:57:44.429000000 86.290821 \n",
500 | "2 2016-01-09 05:36:02.389000000 85.512541 \n",
501 | "3 2016-04-30 06:50:39.801000000 68.824532 \n",
502 | "4 2015-12-13 17:06:10.750000000 51.491040 \n",
503 | "\n",
504 | " assignment2_submission assignment3_grade \\\n",
505 | "0 2015-11-09 02:22:58.938000000 67.164441 \n",
506 | "1 2015-12-06 17:41:18.449000000 69.772657 \n",
507 | "2 2016-01-09 06:39:44.416000000 68.410033 \n",
508 | "3 2016-04-30 17:20:38.727000000 61.942079 \n",
509 | "4 2015-12-14 12:25:12.056000000 41.932832 \n",
510 | "\n",
511 | " assignment3_submission assignment4_grade \\\n",
512 | "0 2015-11-12 08:58:33.998000000 53.011553 \n",
513 | "1 2015-12-10 08:54:55.904000000 55.098125 \n",
514 | "2 2016-01-15 20:22:45.882000000 54.728026 \n",
515 | "3 2016-05-12 07:47:16.326000000 49.553663 \n",
516 | "4 2015-12-29 14:25:22.594000000 36.929549 \n",
517 | "\n",
518 | " assignment4_submission assignment5_grade \\\n",
519 | "0 2015-11-16 01:21:24.663000000 47.710398 \n",
520 | "1 2015-12-13 17:32:30.941000000 49.588313 \n",
521 | "2 2016-01-11 12:41:50.749000000 49.255224 \n",
522 | "3 2016-05-07 16:09:20.485000000 49.553663 \n",
523 | "4 2015-12-28 01:29:55.901000000 33.236594 \n",
524 | "\n",
525 | " assignment5_submission assignment6_grade \\\n",
526 | "0 2015-11-20 13:24:59.692000000 38.168318 \n",
527 | "1 2015-12-19 23:26:39.285000000 44.629482 \n",
528 | "2 2016-01-11 17:31:12.489000000 44.329701 \n",
529 | "3 2016-05-24 12:51:18.016000000 44.598297 \n",
530 | "4 2015-12-29 14:46:06.628000000 33.236594 \n",
531 | "\n",
532 | " assignment6_submission \n",
533 | "0 2015-11-22 18:31:15.934000000 \n",
534 | "1 2015-12-21 17:07:24.275000000 \n",
535 | "2 2016-01-17 16:24:42.765000000 \n",
536 | "3 2016-05-26 08:09:12.058000000 \n",
537 | "4 2016-01-05 01:06:59.546000000 "
538 | ]
539 | },
540 | "execution_count": 41,
541 | "metadata": {},
542 | "output_type": "execute_result"
543 | }
544 | ],
545 | "source": [
546 | "df.head()"
547 | ]
548 | },
549 | {
550 | "cell_type": "code",
551 | "execution_count": 42,
552 | "metadata": {
553 | "collapsed": false
554 | },
555 | "outputs": [
556 | {
557 | "data": {
558 | "text/plain": [
559 | "2315"
560 | ]
561 | },
562 | "execution_count": 42,
563 | "metadata": {},
564 | "output_type": "execute_result"
565 | }
566 | ],
567 | "source": [
568 | "len(df)"
569 | ]
570 | },
571 | {
572 | "cell_type": "code",
573 | "execution_count": 90,
574 | "metadata": {
575 | "collapsed": false
576 | },
577 | "outputs": [],
578 | "source": [
579 | "early = df[df['assignment1_submission'] <= '2015-12-31']\n",
580 | "late = df[df['assignment1_submission'] > '2015-12-31']"
581 | ]
582 | },
583 | {
584 | "cell_type": "code",
585 | "execution_count": 91,
586 | "metadata": {
587 | "collapsed": false
588 | },
589 | "outputs": [
590 | {
591 | "data": {
592 | "text/plain": [
593 | "assignment1_grade 74.972741\n",
594 | "assignment2_grade 67.252190\n",
595 | "assignment3_grade 61.129050\n",
596 | "assignment4_grade 54.157620\n",
597 | "assignment5_grade 48.634643\n",
598 | "assignment6_grade 43.838980\n",
599 | "dtype: float64"
600 | ]
601 | },
602 | "execution_count": 91,
603 | "metadata": {},
604 | "output_type": "execute_result"
605 | }
606 | ],
607 | "source": [
608 | "early.mean()"
609 | ]
610 | },
611 | {
612 | "cell_type": "code",
613 | "execution_count": 92,
614 | "metadata": {
615 | "collapsed": false
616 | },
617 | "outputs": [
618 | {
619 | "data": {
620 | "text/plain": [
621 | "assignment1_grade 74.017429\n",
622 | "assignment2_grade 66.370822\n",
623 | "assignment3_grade 60.023244\n",
624 | "assignment4_grade 54.058138\n",
625 | "assignment5_grade 48.599402\n",
626 | "assignment6_grade 43.844384\n",
627 | "dtype: float64"
628 | ]
629 | },
630 | "execution_count": 92,
631 | "metadata": {},
632 | "output_type": "execute_result"
633 | }
634 | ],
635 | "source": [
636 | "late.mean()"
637 | ]
638 | },
639 | {
640 | "cell_type": "code",
641 | "execution_count": 93,
642 | "metadata": {
643 | "collapsed": false
644 | },
645 | "outputs": [],
646 | "source": [
647 | "from scipy import stats\n",
648 | "stats.ttest_ind?"
649 | ]
650 | },
651 | {
652 | "cell_type": "code",
653 | "execution_count": 94,
654 | "metadata": {
655 | "collapsed": false
656 | },
657 | "outputs": [
658 | {
659 | "data": {
660 | "text/plain": [
661 | "Ttest_indResult(statistic=1.400549944897566, pvalue=0.16148283016060577)"
662 | ]
663 | },
664 | "execution_count": 94,
665 | "metadata": {},
666 | "output_type": "execute_result"
667 | }
668 | ],
669 | "source": [
670 | "stats.ttest_ind(early['assignment1_grade'], late['assignment1_grade'])"
671 | ]
672 | },
673 | {
674 | "cell_type": "code",
675 | "execution_count": 95,
676 | "metadata": {
677 | "collapsed": false
678 | },
679 | "outputs": [
680 | {
681 | "data": {
682 | "text/plain": [
683 | "Ttest_indResult(statistic=1.3239868220912567, pvalue=0.18563824610067967)"
684 | ]
685 | },
686 | "execution_count": 95,
687 | "metadata": {},
688 | "output_type": "execute_result"
689 | }
690 | ],
691 | "source": [
692 | "stats.ttest_ind(early['assignment2_grade'], late['assignment2_grade'])"
693 | ]
694 | },
695 | {
696 | "cell_type": "code",
697 | "execution_count": 98,
698 | "metadata": {
699 | "collapsed": false
700 | },
701 | "outputs": [
702 | {
703 | "data": {
704 | "text/plain": [
705 | "Ttest_indResult(statistic=1.7116160037010733, pvalue=0.087101516341556676)"
706 | ]
707 | },
708 | "execution_count": 98,
709 | "metadata": {},
710 | "output_type": "execute_result"
711 | }
712 | ],
713 | "source": [
714 | "stats.ttest_ind(early['assignment3_grade'], late['assignment3_grade'])"
715 | ]
716 | },
717 | {
718 | "cell_type": "code",
719 | "execution_count": null,
720 | "metadata": {
721 | "collapsed": true
722 | },
723 | "outputs": [],
724 | "source": []
725 | }
726 | ],
727 | "metadata": {
728 | "kernelspec": {
729 | "display_name": "Python 3",
730 | "language": "python",
731 | "name": "python3"
732 | },
733 | "language_info": {
734 | "codemirror_mode": {
735 | "name": "ipython",
736 | "version": 3
737 | },
738 | "file_extension": ".py",
739 | "mimetype": "text/x-python",
740 | "name": "python",
741 | "nbconvert_exporter": "python",
742 | "pygments_lexer": "ipython3",
743 | "version": "3.5.2"
744 | }
745 | },
746 | "nbformat": 4,
747 | "nbformat_minor": 0
748 | }
749 |
--------------------------------------------------------------------------------
/Week 1/Week1.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "---\n",
8 | "\n",
9 | "_You are currently looking at **version 1.1** of this notebook. To download notebooks and datafiles, as well as get help on Jupyter notebooks in the Coursera platform, visit the [Jupyter Notebook FAQ](https://www.coursera.org/learn/python-data-analysis/resources/0dhYG) course resource._\n",
10 | "\n",
11 | "---"
12 | ]
13 | },
14 | {
15 | "cell_type": "markdown",
16 | "metadata": {},
17 | "source": [
18 | "# The Python Programming Language: Functions"
19 | ]
20 | },
21 | {
22 | "cell_type": "code",
23 | "execution_count": 11,
24 | "metadata": {
25 | "collapsed": false
26 | },
27 | "outputs": [
28 | {
29 | "data": {
30 | "text/plain": [
31 | "3"
32 | ]
33 | },
34 | "execution_count": 11,
35 | "metadata": {},
36 | "output_type": "execute_result"
37 | }
38 | ],
39 | "source": [
40 | "x = 1\n",
41 | "y = 2\n",
42 | "x + y"
43 | ]
44 | },
45 | {
46 | "cell_type": "code",
47 | "execution_count": 12,
48 | "metadata": {
49 | "collapsed": false
50 | },
51 | "outputs": [
52 | {
53 | "data": {
54 | "text/plain": [
55 | "1"
56 | ]
57 | },
58 | "execution_count": 12,
59 | "metadata": {},
60 | "output_type": "execute_result"
61 | }
62 | ],
63 | "source": [
64 | "x"
65 | ]
66 | },
67 | {
68 | "cell_type": "markdown",
69 | "metadata": {},
70 | "source": [
71 | "
\n",
72 | "`add_numbers` is a function that takes two numbers and adds them together."
73 | ]
74 | },
75 | {
76 | "cell_type": "code",
77 | "execution_count": null,
78 | "metadata": {
79 | "collapsed": false
80 | },
81 | "outputs": [],
82 | "source": [
83 | "def add_numbers(x, y):\n",
84 | " return x + y\n",
85 | "\n",
86 | "add_numbers(1, 2)"
87 | ]
88 | },
89 | {
90 | "cell_type": "markdown",
91 | "metadata": {},
92 | "source": [
93 | "
\n",
94 | "`add_numbers` updated to take an optional 3rd parameter. Using `print` allows printing of multiple expressions within a single cell."
95 | ]
96 | },
97 | {
98 | "cell_type": "code",
99 | "execution_count": null,
100 | "metadata": {
101 | "collapsed": false
102 | },
103 | "outputs": [],
104 | "source": [
105 | "def add_numbers(x,y,z=None):\n",
106 | " if (z==None):\n",
107 | " return x+y\n",
108 | " else:\n",
109 | " return x+y+z\n",
110 | "\n",
111 | "print(add_numbers(1, 2))\n",
112 | "print(add_numbers(1, 2, 3))"
113 | ]
114 | },
115 | {
116 | "cell_type": "markdown",
117 | "metadata": {},
118 | "source": [
119 | "
\n",
120 | "`add_numbers` updated to take an optional flag parameter."
121 | ]
122 | },
123 | {
124 | "cell_type": "code",
125 | "execution_count": null,
126 | "metadata": {
127 | "collapsed": false
128 | },
129 | "outputs": [],
130 | "source": [
131 | "def add_numbers(x, y, z=None, flag=False):\n",
132 | " if (flag):\n",
133 | " print('Flag is true!')\n",
134 | " if (z==None):\n",
135 | " return x + y\n",
136 | " else:\n",
137 | " return x + y + z\n",
138 | " \n",
139 | "print(add_numbers(1, 2, flag=True))"
140 | ]
141 | },
142 | {
143 | "cell_type": "markdown",
144 | "metadata": {},
145 | "source": [
146 | "
\n",
147 | "Assign function `add_numbers` to variable `a`."
148 | ]
149 | },
150 | {
151 | "cell_type": "code",
152 | "execution_count": 14,
153 | "metadata": {
154 | "collapsed": false
155 | },
156 | "outputs": [
157 | {
158 | "data": {
159 | "text/plain": [
160 | "3"
161 | ]
162 | },
163 | "execution_count": 14,
164 | "metadata": {},
165 | "output_type": "execute_result"
166 | }
167 | ],
168 | "source": [
169 | "def add_numbers(x,y):\n",
170 | " return x+y\n",
171 | "\n",
172 | "a = add_numbers\n",
173 | "a(1,2)"
174 | ]
175 | },
176 | {
177 | "cell_type": "markdown",
178 | "metadata": {},
179 | "source": [
180 | "
\n",
181 | "# The Python Programming Language: Types and Sequences"
182 | ]
183 | },
184 | {
185 | "cell_type": "markdown",
186 | "metadata": {},
187 | "source": [
188 | "
\n",
189 | "Use `type` to return the object's type."
190 | ]
191 | },
192 | {
193 | "cell_type": "code",
194 | "execution_count": null,
195 | "metadata": {
196 | "collapsed": false
197 | },
198 | "outputs": [],
199 | "source": [
200 | "type('This is a string')"
201 | ]
202 | },
203 | {
204 | "cell_type": "code",
205 | "execution_count": null,
206 | "metadata": {
207 | "collapsed": false
208 | },
209 | "outputs": [],
210 | "source": [
211 | "type(None)"
212 | ]
213 | },
214 | {
215 | "cell_type": "code",
216 | "execution_count": null,
217 | "metadata": {
218 | "collapsed": false
219 | },
220 | "outputs": [],
221 | "source": [
222 | "type(1)"
223 | ]
224 | },
225 | {
226 | "cell_type": "code",
227 | "execution_count": null,
228 | "metadata": {
229 | "collapsed": false
230 | },
231 | "outputs": [],
232 | "source": [
233 | "type(1.0)"
234 | ]
235 | },
236 | {
237 | "cell_type": "code",
238 | "execution_count": 15,
239 | "metadata": {
240 | "collapsed": false
241 | },
242 | "outputs": [
243 | {
244 | "data": {
245 | "text/plain": [
246 | "function"
247 | ]
248 | },
249 | "execution_count": 15,
250 | "metadata": {},
251 | "output_type": "execute_result"
252 | }
253 | ],
254 | "source": [
255 | "type(add_numbers)"
256 | ]
257 | },
258 | {
259 | "cell_type": "markdown",
260 | "metadata": {},
261 | "source": [
262 | "
\n",
263 | "Tuples are an immutable data structure (cannot be altered)."
264 | ]
265 | },
266 | {
267 | "cell_type": "code",
268 | "execution_count": 16,
269 | "metadata": {
270 | "collapsed": false
271 | },
272 | "outputs": [
273 | {
274 | "data": {
275 | "text/plain": [
276 | "tuple"
277 | ]
278 | },
279 | "execution_count": 16,
280 | "metadata": {},
281 | "output_type": "execute_result"
282 | }
283 | ],
284 | "source": [
285 | "x = (1, 'a', 2, 'b')\n",
286 | "type(x)"
287 | ]
288 | },
289 | {
290 | "cell_type": "markdown",
291 | "metadata": {},
292 | "source": [
293 | "
\n",
294 | "Lists are a mutable data structure."
295 | ]
296 | },
297 | {
298 | "cell_type": "code",
299 | "execution_count": null,
300 | "metadata": {
301 | "collapsed": false
302 | },
303 | "outputs": [],
304 | "source": [
305 | "x = [1, 'a', 2, 'b']\n",
306 | "type(x)"
307 | ]
308 | },
309 | {
310 | "cell_type": "markdown",
311 | "metadata": {},
312 | "source": [
313 | "
\n",
314 | "Use `append` to append an object to a list."
315 | ]
316 | },
317 | {
318 | "cell_type": "code",
319 | "execution_count": null,
320 | "metadata": {
321 | "collapsed": false
322 | },
323 | "outputs": [],
324 | "source": [
325 | "x.append(3.3)\n",
326 | "print(x)"
327 | ]
328 | },
329 | {
330 | "cell_type": "markdown",
331 | "metadata": {},
332 | "source": [
333 | "
\n",
334 | "This is an example of how to loop through each item in the list."
335 | ]
336 | },
337 | {
338 | "cell_type": "code",
339 | "execution_count": 17,
340 | "metadata": {
341 | "collapsed": false
342 | },
343 | "outputs": [
344 | {
345 | "name": "stdout",
346 | "output_type": "stream",
347 | "text": [
348 | "1\n",
349 | "a\n",
350 | "2\n",
351 | "b\n"
352 | ]
353 | }
354 | ],
355 | "source": [
356 | "for item in x:\n",
357 | " print(item)"
358 | ]
359 | },
360 | {
361 | "cell_type": "markdown",
362 | "metadata": {},
363 | "source": [
364 | "
\n",
365 | "Or using the indexing operator:"
366 | ]
367 | },
368 | {
369 | "cell_type": "code",
370 | "execution_count": null,
371 | "metadata": {
372 | "collapsed": false
373 | },
374 | "outputs": [],
375 | "source": [
376 | "i=0\n",
377 | "while( i != len(x) ):\n",
378 | " print(x[i])\n",
379 | " i = i + 1"
380 | ]
381 | },
382 | {
383 | "cell_type": "markdown",
384 | "metadata": {},
385 | "source": [
386 | "
\n",
387 | "Use `+` to concatenate lists."
388 | ]
389 | },
390 | {
391 | "cell_type": "code",
392 | "execution_count": null,
393 | "metadata": {
394 | "collapsed": false
395 | },
396 | "outputs": [],
397 | "source": [
398 | "[1,2] + [3,4]"
399 | ]
400 | },
401 | {
402 | "cell_type": "markdown",
403 | "metadata": {},
404 | "source": [
405 | "
\n",
406 | "Use `*` to repeat lists."
407 | ]
408 | },
409 | {
410 | "cell_type": "code",
411 | "execution_count": null,
412 | "metadata": {
413 | "collapsed": false
414 | },
415 | "outputs": [],
416 | "source": [
417 | "[1]*3"
418 | ]
419 | },
420 | {
421 | "cell_type": "markdown",
422 | "metadata": {},
423 | "source": [
424 | "
\n",
425 | "Use the `in` operator to check if something is inside a list."
426 | ]
427 | },
428 | {
429 | "cell_type": "code",
430 | "execution_count": 18,
431 | "metadata": {
432 | "collapsed": false
433 | },
434 | "outputs": [
435 | {
436 | "data": {
437 | "text/plain": [
438 | "True"
439 | ]
440 | },
441 | "execution_count": 18,
442 | "metadata": {},
443 | "output_type": "execute_result"
444 | }
445 | ],
446 | "source": [
447 | "1 in [1, 2, 3]"
448 | ]
449 | },
450 | {
451 | "cell_type": "markdown",
452 | "metadata": {},
453 | "source": [
454 | "
\n",
455 | "Now let's look at strings. Use bracket notation to slice a string."
456 | ]
457 | },
458 | {
459 | "cell_type": "code",
460 | "execution_count": 22,
461 | "metadata": {
462 | "collapsed": false
463 | },
464 | "outputs": [
465 | {
466 | "name": "stdout",
467 | "output_type": "stream",
468 | "text": [
469 | "T\n",
470 | "i si\n",
471 | "Th\n"
472 | ]
473 | }
474 | ],
475 | "source": [
476 | "x = 'This is a string'\n",
477 | "print(x[0]) #first character\n",
478 | "print(x[5:1:-1]) #first character, but we have explicitly set the end character\n",
479 | "print(x[0:2]) #first two characters\n"
480 | ]
481 | },
482 | {
483 | "cell_type": "markdown",
484 | "metadata": {},
485 | "source": [
486 | "
\n",
487 | "This will return the last element of the string."
488 | ]
489 | },
490 | {
491 | "cell_type": "code",
492 | "execution_count": null,
493 | "metadata": {
494 | "collapsed": false
495 | },
496 | "outputs": [],
497 | "source": [
498 | "x[-1]"
499 | ]
500 | },
501 | {
502 | "cell_type": "markdown",
503 | "metadata": {},
504 | "source": [
505 | "
\n",
506 | "This will return the slice starting from the 4th element from the end and stopping before the 2nd element from the end."
507 | ]
508 | },
509 | {
510 | "cell_type": "code",
511 | "execution_count": 20,
512 | "metadata": {
513 | "collapsed": false
514 | },
515 | "outputs": [
516 | {
517 | "data": {
518 | "text/plain": [
519 | "'ri'"
520 | ]
521 | },
522 | "execution_count": 20,
523 | "metadata": {},
524 | "output_type": "execute_result"
525 | }
526 | ],
527 | "source": [
528 | "x[-4:-2]"
529 | ]
530 | },
531 | {
532 | "cell_type": "markdown",
533 | "metadata": {},
534 | "source": [
535 | "
\n",
536 | "This is a slice from the beginning of the string and stopping before the 3rd element."
537 | ]
538 | },
539 | {
540 | "cell_type": "code",
541 | "execution_count": null,
542 | "metadata": {
543 | "collapsed": false
544 | },
545 | "outputs": [],
546 | "source": [
547 | "x[:3]"
548 | ]
549 | },
550 | {
551 | "cell_type": "markdown",
552 | "metadata": {},
553 | "source": [
554 | "
\n",
555 | "And this is a slice starting from the 3rd element of the string and going all the way to the end."
556 | ]
557 | },
558 | {
559 | "cell_type": "code",
560 | "execution_count": null,
561 | "metadata": {
562 | "collapsed": false
563 | },
564 | "outputs": [],
565 | "source": [
566 | "x[3:]"
567 | ]
568 | },
569 | {
570 | "cell_type": "code",
571 | "execution_count": null,
572 | "metadata": {
573 | "collapsed": false
574 | },
575 | "outputs": [],
576 | "source": [
577 | "firstname = 'Christopher'\n",
578 | "lastname = 'Brooks'\n",
579 | "\n",
580 | "print(firstname + ' ' + lastname)\n",
581 | "print(firstname*3)\n",
582 | "print('Chris' in firstname)\n"
583 | ]
584 | },
585 | {
586 | "cell_type": "markdown",
587 | "metadata": {},
588 | "source": [
589 | "
\n",
590 | "`split` returns a list of all the words in a string, or a list split on a specific character."
591 | ]
592 | },
593 | {
594 | "cell_type": "code",
595 | "execution_count": 23,
596 | "metadata": {
597 | "collapsed": false
598 | },
599 | "outputs": [
600 | {
601 | "name": "stdout",
602 | "output_type": "stream",
603 | "text": [
604 | "Christopher\n",
605 | "Brooks\n"
606 | ]
607 | }
608 | ],
609 | "source": [
610 | "firstname = 'Christopher Arthur Hansen Brooks'.split(' ')[0] # [0] selects the first element of the list\n",
611 | "lastname = 'Christopher Arthur Hansen Brooks'.split(' ')[-1] # [-1] selects the last element of the list\n",
612 | "print(firstname)\n",
613 | "print(lastname)"
614 | ]
615 | },
616 | {
617 | "cell_type": "markdown",
618 | "metadata": {},
619 | "source": [
620 | "
\n",
621 | "Make sure you convert objects to strings before concatenating."
622 | ]
623 | },
624 | {
625 | "cell_type": "code",
626 | "execution_count": 24,
627 | "metadata": {
628 | "collapsed": false
629 | },
630 | "outputs": [
631 | {
632 | "ename": "TypeError",
633 | "evalue": "Can't convert 'int' object to str implicitly",
634 | "output_type": "error",
635 | "traceback": [
636 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
637 | "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
638 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;34m'Chris'\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
639 | "\u001b[0;31mTypeError\u001b[0m: Can't convert 'int' object to str implicitly"
640 | ]
641 | }
642 | ],
643 | "source": [
644 | "'Chris' + 2"
645 | ]
646 | },
647 | {
648 | "cell_type": "code",
649 | "execution_count": 25,
650 | "metadata": {
651 | "collapsed": false
652 | },
653 | "outputs": [
654 | {
655 | "data": {
656 | "text/plain": [
657 | "'Chris2'"
658 | ]
659 | },
660 | "execution_count": 25,
661 | "metadata": {},
662 | "output_type": "execute_result"
663 | }
664 | ],
665 | "source": [
666 | "'Chris' + str(2)"
667 | ]
668 | },
669 | {
670 | "cell_type": "markdown",
671 | "metadata": {},
672 | "source": [
673 | "
\n",
674 | "Dictionaries associate keys with values."
675 | ]
676 | },
677 | {
678 | "cell_type": "code",
679 | "execution_count": null,
680 | "metadata": {
681 | "collapsed": false
682 | },
683 | "outputs": [],
684 | "source": [
685 | "x = {'Christopher Brooks': 'brooksch@umich.edu', 'Bill Gates': 'billg@microsoft.com'}\n",
686 | "x['Christopher Brooks'] # Retrieve a value by using the indexing operator\n"
687 | ]
688 | },
689 | {
690 | "cell_type": "code",
691 | "execution_count": null,
692 | "metadata": {
693 | "collapsed": true
694 | },
695 | "outputs": [],
696 | "source": [
697 | "x['Kevyn Collins-Thompson'] = None\n",
698 | "x['Kevyn Collins-Thompson']"
699 | ]
700 | },
701 | {
702 | "cell_type": "markdown",
703 | "metadata": {},
704 | "source": [
705 | "
\n",
706 | "Iterate over all of the keys:"
707 | ]
708 | },
709 | {
710 | "cell_type": "code",
711 | "execution_count": null,
712 | "metadata": {
713 | "collapsed": false,
714 | "scrolled": true
715 | },
716 | "outputs": [],
717 | "source": [
718 | "for name in x:\n",
719 | " print(x[name])"
720 | ]
721 | },
722 | {
723 | "cell_type": "markdown",
724 | "metadata": {},
725 | "source": [
726 | "
\n",
727 | "Iterate over all of the values:"
728 | ]
729 | },
730 | {
731 | "cell_type": "code",
732 | "execution_count": null,
733 | "metadata": {
734 | "collapsed": false
735 | },
736 | "outputs": [],
737 | "source": [
738 | "for email in x.values():\n",
739 | " print(email)"
740 | ]
741 | },
742 | {
743 | "cell_type": "markdown",
744 | "metadata": {},
745 | "source": [
746 | "
\n",
747 | "Iterate over all of the items in the list:"
748 | ]
749 | },
750 | {
751 | "cell_type": "code",
752 | "execution_count": null,
753 | "metadata": {
754 | "collapsed": false
755 | },
756 | "outputs": [],
757 | "source": [
758 | "for name, email in x.items():\n",
759 | " print(name)\n",
760 | " print(email)"
761 | ]
762 | },
763 | {
764 | "cell_type": "markdown",
765 | "metadata": {},
766 | "source": [
767 | "
\n",
768 | "You can unpack a sequence into different variables:"
769 | ]
770 | },
771 | {
772 | "cell_type": "code",
773 | "execution_count": null,
774 | "metadata": {
775 | "collapsed": true
776 | },
777 | "outputs": [],
778 | "source": [
779 | "x = ('Christopher', 'Brooks', 'brooksch@umich.edu')\n",
780 | "fname, lname, email = x"
781 | ]
782 | },
783 | {
784 | "cell_type": "code",
785 | "execution_count": null,
786 | "metadata": {
787 | "collapsed": false
788 | },
789 | "outputs": [],
790 | "source": [
791 | "fname"
792 | ]
793 | },
794 | {
795 | "cell_type": "code",
796 | "execution_count": null,
797 | "metadata": {
798 | "collapsed": false
799 | },
800 | "outputs": [],
801 | "source": [
802 | "lname"
803 | ]
804 | },
805 | {
806 | "cell_type": "markdown",
807 | "metadata": {},
808 | "source": [
809 | "
\n",
810 | "Make sure the number of values you are unpacking matches the number of variables being assigned."
811 | ]
812 | },
813 | {
814 | "cell_type": "code",
815 | "execution_count": 29,
816 | "metadata": {
817 | "collapsed": false
818 | },
819 | "outputs": [
820 | {
821 | "ename": "ValueError",
822 | "evalue": "not enough values to unpack (expected 3, got 2)",
823 | "output_type": "error",
824 | "traceback": [
825 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
826 | "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
827 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mx\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;34m'Christopher'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'Brooks'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'brooksch@umich.edu'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'Ann Arbor'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mfname\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlname\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0memail\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
828 | "\u001b[0;31mValueError\u001b[0m: not enough values to unpack (expected 3, got 2)"
829 | ]
830 | }
831 | ],
832 | "source": [
833 | "x = ('Christopher', 'Brooks', 'brooksch@umich.edu', 'Ann Arbor')\n",
834 | "fname, lname, email = x"
835 | ]
836 | },
837 | {
838 | "cell_type": "markdown",
839 | "metadata": {},
840 | "source": [
841 | "
\n",
842 | "# The Python Programming Language: More on Strings"
843 | ]
844 | },
845 | {
846 | "cell_type": "code",
847 | "execution_count": null,
848 | "metadata": {
849 | "collapsed": false
850 | },
851 | "outputs": [],
852 | "source": [
853 | "print('Chris' + 2)"
854 | ]
855 | },
856 | {
857 | "cell_type": "code",
858 | "execution_count": null,
859 | "metadata": {
860 | "collapsed": false
861 | },
862 | "outputs": [],
863 | "source": [
864 | "print('Chris' + str(2))"
865 | ]
866 | },
867 | {
868 | "cell_type": "markdown",
869 | "metadata": {},
870 | "source": [
871 | "
\n",
872 | "Python has a built in method for convenient string formatting."
873 | ]
874 | },
875 | {
876 | "cell_type": "code",
877 | "execution_count": 30,
878 | "metadata": {
879 | "collapsed": false
880 | },
881 | "outputs": [
882 | {
883 | "name": "stdout",
884 | "output_type": "stream",
885 | "text": [
886 | "Chris bought 4 item(s) at a price of 3.24 each for a total of 12.96\n"
887 | ]
888 | }
889 | ],
890 | "source": [
891 | "sales_record = {\n",
892 | "'price': 3.24,\n",
893 | "'num_items': 4,\n",
894 | "'person': 'Chris'}\n",
895 | "\n",
896 | "sales_statement = '{} bought {} item(s) at a price of {} each for a total of {}'\n",
897 | "\n",
898 | "print(sales_statement.format(sales_record['person'],\n",
899 | " sales_record['num_items'],\n",
900 | " sales_record['price'],\n",
901 | " sales_record['num_items']*sales_record['price']))\n"
902 | ]
903 | },
904 | {
905 | "cell_type": "markdown",
906 | "metadata": {},
907 | "source": [
908 | "
\n",
909 | "# Reading and Writing CSV files"
910 | ]
911 | },
912 | {
913 | "cell_type": "markdown",
914 | "metadata": {},
915 | "source": [
916 | "
\n",
917 | "Let's import our datafile mpg.csv, which contains fuel economy data for 234 cars.\n",
918 | "\n",
919 | "* mpg : miles per gallon\n",
920 | "* class : car classification\n",
921 | "* cty : city mpg\n",
922 | "* cyl : # of cylinders\n",
923 | "* displ : engine displacement in liters\n",
924 | "* drv : f = front-wheel drive, r = rear wheel drive, 4 = 4wd\n",
925 | "* fl : fuel (e = ethanol E85, d = diesel, r = regular, p = premium, c = CNG)\n",
926 | "* hwy : highway mpg\n",
927 | "* manufacturer : automobile manufacturer\n",
928 | "* model : model of car\n",
929 | "* trans : type of transmission\n",
930 | "* year : model year"
931 | ]
932 | },
933 | {
934 | "cell_type": "code",
935 | "execution_count": 31,
936 | "metadata": {
937 | "collapsed": false,
938 | "scrolled": true
939 | },
940 | "outputs": [
941 | {
942 | "data": {
943 | "text/plain": [
944 | "[{'': '1',\n",
945 | " 'class': 'compact',\n",
946 | " 'cty': '18',\n",
947 | " 'cyl': '4',\n",
948 | " 'displ': '1.8',\n",
949 | " 'drv': 'f',\n",
950 | " 'fl': 'p',\n",
951 | " 'hwy': '29',\n",
952 | " 'manufacturer': 'audi',\n",
953 | " 'model': 'a4',\n",
954 | " 'trans': 'auto(l5)',\n",
955 | " 'year': '1999'},\n",
956 | " {'': '2',\n",
957 | " 'class': 'compact',\n",
958 | " 'cty': '21',\n",
959 | " 'cyl': '4',\n",
960 | " 'displ': '1.8',\n",
961 | " 'drv': 'f',\n",
962 | " 'fl': 'p',\n",
963 | " 'hwy': '29',\n",
964 | " 'manufacturer': 'audi',\n",
965 | " 'model': 'a4',\n",
966 | " 'trans': 'manual(m5)',\n",
967 | " 'year': '1999'},\n",
968 | " {'': '3',\n",
969 | " 'class': 'compact',\n",
970 | " 'cty': '20',\n",
971 | " 'cyl': '4',\n",
972 | " 'displ': '2',\n",
973 | " 'drv': 'f',\n",
974 | " 'fl': 'p',\n",
975 | " 'hwy': '31',\n",
976 | " 'manufacturer': 'audi',\n",
977 | " 'model': 'a4',\n",
978 | " 'trans': 'manual(m6)',\n",
979 | " 'year': '2008'}]"
980 | ]
981 | },
982 | "execution_count": 31,
983 | "metadata": {},
984 | "output_type": "execute_result"
985 | }
986 | ],
987 | "source": [
988 | "import csv\n",
989 | "\n",
990 | "%precision 2\n",
991 | "\n",
992 | "with open('mpg.csv') as csvfile:\n",
993 | " mpg = list(csv.DictReader(csvfile))\n",
994 | " \n",
995 | "mpg[:3] # The first three dictionaries in our list."
996 | ]
997 | },
998 | {
999 | "cell_type": "markdown",
1000 | "metadata": {},
1001 | "source": [
1002 | "
\n",
1003 | "`csv.Dictreader` has read in each row of our csv file as a dictionary. `len` shows that our list is comprised of 234 dictionaries."
1004 | ]
1005 | },
1006 | {
1007 | "cell_type": "code",
1008 | "execution_count": null,
1009 | "metadata": {
1010 | "collapsed": false
1011 | },
1012 | "outputs": [],
1013 | "source": [
1014 | "len(mpg)"
1015 | ]
1016 | },
1017 | {
1018 | "cell_type": "markdown",
1019 | "metadata": {},
1020 | "source": [
1021 | "
\n",
1022 | "`keys` gives us the column names of our csv."
1023 | ]
1024 | },
1025 | {
1026 | "cell_type": "code",
1027 | "execution_count": null,
1028 | "metadata": {
1029 | "collapsed": false
1030 | },
1031 | "outputs": [],
1032 | "source": [
1033 | "mpg[0].keys()"
1034 | ]
1035 | },
1036 | {
1037 | "cell_type": "markdown",
1038 | "metadata": {},
1039 | "source": [
1040 | "
\n",
1041 | "This is how to find the average cty fuel economy across all cars. All values in the dictionaries are strings, so we need to convert to float."
1042 | ]
1043 | },
1044 | {
1045 | "cell_type": "code",
1046 | "execution_count": null,
1047 | "metadata": {
1048 | "collapsed": false
1049 | },
1050 | "outputs": [],
1051 | "source": [
1052 | "sum(float(d['cty']) for d in mpg) / len(mpg)"
1053 | ]
1054 | },
1055 | {
1056 | "cell_type": "markdown",
1057 | "metadata": {},
1058 | "source": [
1059 | "
\n",
1060 | "Similarly this is how to find the average hwy fuel economy across all cars."
1061 | ]
1062 | },
1063 | {
1064 | "cell_type": "code",
1065 | "execution_count": null,
1066 | "metadata": {
1067 | "collapsed": false
1068 | },
1069 | "outputs": [],
1070 | "source": [
1071 | "sum(float(d['hwy']) for d in mpg) / len(mpg)"
1072 | ]
1073 | },
1074 | {
1075 | "cell_type": "markdown",
1076 | "metadata": {},
1077 | "source": [
1078 | "
\n",
1079 | "Use `set` to return the unique values for the number of cylinders the cars in our dataset have."
1080 | ]
1081 | },
1082 | {
1083 | "cell_type": "code",
1084 | "execution_count": null,
1085 | "metadata": {
1086 | "collapsed": false
1087 | },
1088 | "outputs": [],
1089 | "source": [
1090 | "cylinders = set(d['cyl'] for d in mpg)\n",
1091 | "cylinders"
1092 | ]
1093 | },
1094 | {
1095 | "cell_type": "markdown",
1096 | "metadata": {},
1097 | "source": [
1098 | "
\n",
1099 | "Here's a more complex example where we are grouping the cars by number of cylinder, and finding the average cty mpg for each group."
1100 | ]
1101 | },
1102 | {
1103 | "cell_type": "code",
1104 | "execution_count": null,
1105 | "metadata": {
1106 | "collapsed": false
1107 | },
1108 | "outputs": [],
1109 | "source": [
1110 | "CtyMpgByCyl = []\n",
1111 | "\n",
1112 | "for c in cylinders: # iterate over all the cylinder levels\n",
1113 | " summpg = 0\n",
1114 | " cyltypecount = 0\n",
1115 | " for d in mpg: # iterate over all dictionaries\n",
1116 | " if d['cyl'] == c: # if the cylinder level type matches,\n",
1117 | " summpg += float(d['cty']) # add the cty mpg\n",
1118 | " cyltypecount += 1 # increment the count\n",
1119 | " CtyMpgByCyl.append((c, summpg / cyltypecount)) # append the tuple ('cylinder', 'avg mpg')\n",
1120 | "\n",
1121 | "CtyMpgByCyl.sort(key=lambda x: x[0])\n",
1122 | "CtyMpgByCyl"
1123 | ]
1124 | },
1125 | {
1126 | "cell_type": "markdown",
1127 | "metadata": {},
1128 | "source": [
1129 | "
\n",
1130 | "Use `set` to return the unique values for the class types in our dataset."
1131 | ]
1132 | },
1133 | {
1134 | "cell_type": "code",
1135 | "execution_count": null,
1136 | "metadata": {
1137 | "collapsed": false
1138 | },
1139 | "outputs": [],
1140 | "source": [
1141 | "vehicleclass = set(d['class'] for d in mpg) # what are the class types\n",
1142 | "vehicleclass"
1143 | ]
1144 | },
1145 | {
1146 | "cell_type": "markdown",
1147 | "metadata": {},
1148 | "source": [
1149 | "
\n",
1150 | "And here's an example of how to find the average hwy mpg for each class of vehicle in our dataset."
1151 | ]
1152 | },
1153 | {
1154 | "cell_type": "code",
1155 | "execution_count": null,
1156 | "metadata": {
1157 | "collapsed": false
1158 | },
1159 | "outputs": [],
1160 | "source": [
1161 | "HwyMpgByClass = []\n",
1162 | "\n",
1163 | "for t in vehicleclass: # iterate over all the vehicle classes\n",
1164 | " summpg = 0\n",
1165 | " vclasscount = 0\n",
1166 | " for d in mpg: # iterate over all dictionaries\n",
1167 | " if d['class'] == t: # if the cylinder amount type matches,\n",
1168 | " summpg += float(d['hwy']) # add the hwy mpg\n",
1169 | " vclasscount += 1 # increment the count\n",
1170 | " HwyMpgByClass.append((t, summpg / vclasscount)) # append the tuple ('class', 'avg mpg')\n",
1171 | "\n",
1172 | "HwyMpgByClass.sort(key=lambda x: x[1])\n",
1173 | "HwyMpgByClass"
1174 | ]
1175 | },
1176 | {
1177 | "cell_type": "markdown",
1178 | "metadata": {},
1179 | "source": [
1180 | "
\n",
1181 | "# The Python Programming Language: Dates and Times"
1182 | ]
1183 | },
1184 | {
1185 | "cell_type": "code",
1186 | "execution_count": 32,
1187 | "metadata": {
1188 | "collapsed": true
1189 | },
1190 | "outputs": [],
1191 | "source": [
1192 | "import datetime as dt\n",
1193 | "import time as tm"
1194 | ]
1195 | },
1196 | {
1197 | "cell_type": "markdown",
1198 | "metadata": {},
1199 | "source": [
1200 | "
\n",
1201 | "`time` returns the current time in seconds since the Epoch. (January 1st, 1970)"
1202 | ]
1203 | },
1204 | {
1205 | "cell_type": "code",
1206 | "execution_count": 33,
1207 | "metadata": {
1208 | "collapsed": false
1209 | },
1210 | "outputs": [
1211 | {
1212 | "data": {
1213 | "text/plain": [
1214 | "1480155062.40"
1215 | ]
1216 | },
1217 | "execution_count": 33,
1218 | "metadata": {},
1219 | "output_type": "execute_result"
1220 | }
1221 | ],
1222 | "source": [
1223 | "tm.time()"
1224 | ]
1225 | },
1226 | {
1227 | "cell_type": "markdown",
1228 | "metadata": {},
1229 | "source": [
1230 | "
\n",
1231 | "Convert the timestamp to datetime."
1232 | ]
1233 | },
1234 | {
1235 | "cell_type": "code",
1236 | "execution_count": 34,
1237 | "metadata": {
1238 | "collapsed": false
1239 | },
1240 | "outputs": [
1241 | {
1242 | "data": {
1243 | "text/plain": [
1244 | "datetime.datetime(2016, 11, 26, 10, 11, 6, 536817)"
1245 | ]
1246 | },
1247 | "execution_count": 34,
1248 | "metadata": {},
1249 | "output_type": "execute_result"
1250 | }
1251 | ],
1252 | "source": [
1253 | "dtnow = dt.datetime.fromtimestamp(tm.time())\n",
1254 | "dtnow"
1255 | ]
1256 | },
1257 | {
1258 | "cell_type": "markdown",
1259 | "metadata": {},
1260 | "source": [
1261 | "
\n",
1262 | "Handy datetime attributes:"
1263 | ]
1264 | },
1265 | {
1266 | "cell_type": "code",
1267 | "execution_count": 35,
1268 | "metadata": {
1269 | "collapsed": false
1270 | },
1271 | "outputs": [
1272 | {
1273 | "data": {
1274 | "text/plain": [
1275 | "(2016, 11, 26, 10, 11, 6)"
1276 | ]
1277 | },
1278 | "execution_count": 35,
1279 | "metadata": {},
1280 | "output_type": "execute_result"
1281 | }
1282 | ],
1283 | "source": [
1284 | "dtnow.year, dtnow.month, dtnow.day, dtnow.hour, dtnow.minute, dtnow.second # get year, month, day, etc.from a datetime"
1285 | ]
1286 | },
1287 | {
1288 | "cell_type": "markdown",
1289 | "metadata": {},
1290 | "source": [
1291 | "
\n",
1292 | "`timedelta` is a duration expressing the difference between two dates."
1293 | ]
1294 | },
1295 | {
1296 | "cell_type": "code",
1297 | "execution_count": 36,
1298 | "metadata": {
1299 | "collapsed": false
1300 | },
1301 | "outputs": [
1302 | {
1303 | "data": {
1304 | "text/plain": [
1305 | "datetime.timedelta(100)"
1306 | ]
1307 | },
1308 | "execution_count": 36,
1309 | "metadata": {},
1310 | "output_type": "execute_result"
1311 | }
1312 | ],
1313 | "source": [
1314 | "delta = dt.timedelta(days = 100) # create a timedelta of 100 days\n",
1315 | "delta"
1316 | ]
1317 | },
1318 | {
1319 | "cell_type": "markdown",
1320 | "metadata": {},
1321 | "source": [
1322 | "
\n",
1323 | "`date.today` returns the current local date."
1324 | ]
1325 | },
1326 | {
1327 | "cell_type": "code",
1328 | "execution_count": 37,
1329 | "metadata": {
1330 | "collapsed": true
1331 | },
1332 | "outputs": [],
1333 | "source": [
1334 | "today = dt.date.today()"
1335 | ]
1336 | },
1337 | {
1338 | "cell_type": "code",
1339 | "execution_count": 38,
1340 | "metadata": {
1341 | "collapsed": false
1342 | },
1343 | "outputs": [
1344 | {
1345 | "data": {
1346 | "text/plain": [
1347 | "datetime.date(2016, 8, 18)"
1348 | ]
1349 | },
1350 | "execution_count": 38,
1351 | "metadata": {},
1352 | "output_type": "execute_result"
1353 | }
1354 | ],
1355 | "source": [
1356 | "today - delta # the date 100 days ago"
1357 | ]
1358 | },
1359 | {
1360 | "cell_type": "code",
1361 | "execution_count": 39,
1362 | "metadata": {
1363 | "collapsed": false
1364 | },
1365 | "outputs": [
1366 | {
1367 | "data": {
1368 | "text/plain": [
1369 | "True"
1370 | ]
1371 | },
1372 | "execution_count": 39,
1373 | "metadata": {},
1374 | "output_type": "execute_result"
1375 | }
1376 | ],
1377 | "source": [
1378 | "today > today-delta # compare dates"
1379 | ]
1380 | },
1381 | {
1382 | "cell_type": "markdown",
1383 | "metadata": {},
1384 | "source": [
1385 | "
\n",
1386 | "# The Python Programming Language: Objects and map()"
1387 | ]
1388 | },
1389 | {
1390 | "cell_type": "markdown",
1391 | "metadata": {},
1392 | "source": [
1393 | "
\n",
1394 | "An example of a class in python:"
1395 | ]
1396 | },
1397 | {
1398 | "cell_type": "code",
1399 | "execution_count": 40,
1400 | "metadata": {
1401 | "collapsed": true
1402 | },
1403 | "outputs": [],
1404 | "source": [
1405 | "class Person:\n",
1406 | " department = 'School of Information' #a class variable\n",
1407 | "\n",
1408 | " def set_name(self, new_name): #a method\n",
1409 | " self.name = new_name\n",
1410 | " def set_location(self, new_location):\n",
1411 | " self.location = new_location"
1412 | ]
1413 | },
1414 | {
1415 | "cell_type": "code",
1416 | "execution_count": 41,
1417 | "metadata": {
1418 | "collapsed": false
1419 | },
1420 | "outputs": [
1421 | {
1422 | "name": "stdout",
1423 | "output_type": "stream",
1424 | "text": [
1425 | "Christopher Brooks live in Ann Arbor, MI, USA and works in the department School of Information\n"
1426 | ]
1427 | }
1428 | ],
1429 | "source": [
1430 | "person = Person()\n",
1431 | "person.set_name('Christopher Brooks')\n",
1432 | "person.set_location('Ann Arbor, MI, USA')\n",
1433 | "print('{} live in {} and works in the department {}'.format(person.name, person.location, person.department))"
1434 | ]
1435 | },
1436 | {
1437 | "cell_type": "markdown",
1438 | "metadata": {},
1439 | "source": [
1440 | "
\n",
1441 | "Here's an example of mapping the `min` function between two lists."
1442 | ]
1443 | },
1444 | {
1445 | "cell_type": "code",
1446 | "execution_count": 46,
1447 | "metadata": {
1448 | "collapsed": false
1449 | },
1450 | "outputs": [
1451 | {
1452 | "data": {
1453 | "text/plain": [
1454 | "[9.00, 11.00, 12.34, 1.34]"
1455 | ]
1456 | },
1457 | "execution_count": 46,
1458 | "metadata": {},
1459 | "output_type": "execute_result"
1460 | }
1461 | ],
1462 | "source": [
1463 | "store1 = [10.00, 11.00, 12.34, 1.34]\n",
1464 | "store2 = [9.00, 11.10, 12.34, 2.01]\n",
1465 | "cheapest = map(min, store1, store2)\n",
1466 | "list(cheapest)"
1467 | ]
1468 | },
1469 | {
1470 | "cell_type": "markdown",
1471 | "metadata": {},
1472 | "source": [
1473 | "
\n",
1474 | "Now let's iterate through the map object to see the values."
1475 | ]
1476 | },
1477 | {
1478 | "cell_type": "code",
1479 | "execution_count": 45,
1480 | "metadata": {
1481 | "collapsed": false,
1482 | "scrolled": true
1483 | },
1484 | "outputs": [
1485 | {
1486 | "name": "stdout",
1487 | "output_type": "stream",
1488 | "text": [
1489 | "9.0\n",
1490 | "11.0\n",
1491 | "12.34\n",
1492 | "1.34\n"
1493 | ]
1494 | }
1495 | ],
1496 | "source": [
1497 | "for item in cheapest:\n",
1498 | " print(item)"
1499 | ]
1500 | },
1501 | {
1502 | "cell_type": "markdown",
1503 | "metadata": {},
1504 | "source": [
1505 | "
\n",
1506 | "# The Python Programming Language: Lambda and List Comprehensions"
1507 | ]
1508 | },
1509 | {
1510 | "cell_type": "markdown",
1511 | "metadata": {},
1512 | "source": [
1513 | "
\n",
1514 | "Here's an example of lambda that takes in three parameters and adds the first two."
1515 | ]
1516 | },
1517 | {
1518 | "cell_type": "code",
1519 | "execution_count": 47,
1520 | "metadata": {
1521 | "collapsed": true
1522 | },
1523 | "outputs": [],
1524 | "source": [
1525 | "my_function = lambda a, b, c : a + b"
1526 | ]
1527 | },
1528 | {
1529 | "cell_type": "code",
1530 | "execution_count": 48,
1531 | "metadata": {
1532 | "collapsed": false
1533 | },
1534 | "outputs": [
1535 | {
1536 | "data": {
1537 | "text/plain": [
1538 | "3"
1539 | ]
1540 | },
1541 | "execution_count": 48,
1542 | "metadata": {},
1543 | "output_type": "execute_result"
1544 | }
1545 | ],
1546 | "source": [
1547 | "my_function(1, 2, 3)"
1548 | ]
1549 | },
1550 | {
1551 | "cell_type": "markdown",
1552 | "metadata": {},
1553 | "source": [
1554 | "
\n",
1555 | "Let's iterate from 0 to 999 and return the even numbers."
1556 | ]
1557 | },
1558 | {
1559 | "cell_type": "code",
1560 | "execution_count": 49,
1561 | "metadata": {
1562 | "collapsed": false
1563 | },
1564 | "outputs": [
1565 | {
1566 | "data": {
1567 | "text/plain": [
1568 | "[0,\n",
1569 | " 2,\n",
1570 | " 4,\n",
1571 | " 6,\n",
1572 | " 8,\n",
1573 | " 10,\n",
1574 | " 12,\n",
1575 | " 14,\n",
1576 | " 16,\n",
1577 | " 18,\n",
1578 | " 20,\n",
1579 | " 22,\n",
1580 | " 24,\n",
1581 | " 26,\n",
1582 | " 28,\n",
1583 | " 30,\n",
1584 | " 32,\n",
1585 | " 34,\n",
1586 | " 36,\n",
1587 | " 38,\n",
1588 | " 40,\n",
1589 | " 42,\n",
1590 | " 44,\n",
1591 | " 46,\n",
1592 | " 48,\n",
1593 | " 50,\n",
1594 | " 52,\n",
1595 | " 54,\n",
1596 | " 56,\n",
1597 | " 58,\n",
1598 | " 60,\n",
1599 | " 62,\n",
1600 | " 64,\n",
1601 | " 66,\n",
1602 | " 68,\n",
1603 | " 70,\n",
1604 | " 72,\n",
1605 | " 74,\n",
1606 | " 76,\n",
1607 | " 78,\n",
1608 | " 80,\n",
1609 | " 82,\n",
1610 | " 84,\n",
1611 | " 86,\n",
1612 | " 88,\n",
1613 | " 90,\n",
1614 | " 92,\n",
1615 | " 94,\n",
1616 | " 96,\n",
1617 | " 98,\n",
1618 | " 100,\n",
1619 | " 102,\n",
1620 | " 104,\n",
1621 | " 106,\n",
1622 | " 108,\n",
1623 | " 110,\n",
1624 | " 112,\n",
1625 | " 114,\n",
1626 | " 116,\n",
1627 | " 118,\n",
1628 | " 120,\n",
1629 | " 122,\n",
1630 | " 124,\n",
1631 | " 126,\n",
1632 | " 128,\n",
1633 | " 130,\n",
1634 | " 132,\n",
1635 | " 134,\n",
1636 | " 136,\n",
1637 | " 138,\n",
1638 | " 140,\n",
1639 | " 142,\n",
1640 | " 144,\n",
1641 | " 146,\n",
1642 | " 148,\n",
1643 | " 150,\n",
1644 | " 152,\n",
1645 | " 154,\n",
1646 | " 156,\n",
1647 | " 158,\n",
1648 | " 160,\n",
1649 | " 162,\n",
1650 | " 164,\n",
1651 | " 166,\n",
1652 | " 168,\n",
1653 | " 170,\n",
1654 | " 172,\n",
1655 | " 174,\n",
1656 | " 176,\n",
1657 | " 178,\n",
1658 | " 180,\n",
1659 | " 182,\n",
1660 | " 184,\n",
1661 | " 186,\n",
1662 | " 188,\n",
1663 | " 190,\n",
1664 | " 192,\n",
1665 | " 194,\n",
1666 | " 196,\n",
1667 | " 198,\n",
1668 | " 200,\n",
1669 | " 202,\n",
1670 | " 204,\n",
1671 | " 206,\n",
1672 | " 208,\n",
1673 | " 210,\n",
1674 | " 212,\n",
1675 | " 214,\n",
1676 | " 216,\n",
1677 | " 218,\n",
1678 | " 220,\n",
1679 | " 222,\n",
1680 | " 224,\n",
1681 | " 226,\n",
1682 | " 228,\n",
1683 | " 230,\n",
1684 | " 232,\n",
1685 | " 234,\n",
1686 | " 236,\n",
1687 | " 238,\n",
1688 | " 240,\n",
1689 | " 242,\n",
1690 | " 244,\n",
1691 | " 246,\n",
1692 | " 248,\n",
1693 | " 250,\n",
1694 | " 252,\n",
1695 | " 254,\n",
1696 | " 256,\n",
1697 | " 258,\n",
1698 | " 260,\n",
1699 | " 262,\n",
1700 | " 264,\n",
1701 | " 266,\n",
1702 | " 268,\n",
1703 | " 270,\n",
1704 | " 272,\n",
1705 | " 274,\n",
1706 | " 276,\n",
1707 | " 278,\n",
1708 | " 280,\n",
1709 | " 282,\n",
1710 | " 284,\n",
1711 | " 286,\n",
1712 | " 288,\n",
1713 | " 290,\n",
1714 | " 292,\n",
1715 | " 294,\n",
1716 | " 296,\n",
1717 | " 298,\n",
1718 | " 300,\n",
1719 | " 302,\n",
1720 | " 304,\n",
1721 | " 306,\n",
1722 | " 308,\n",
1723 | " 310,\n",
1724 | " 312,\n",
1725 | " 314,\n",
1726 | " 316,\n",
1727 | " 318,\n",
1728 | " 320,\n",
1729 | " 322,\n",
1730 | " 324,\n",
1731 | " 326,\n",
1732 | " 328,\n",
1733 | " 330,\n",
1734 | " 332,\n",
1735 | " 334,\n",
1736 | " 336,\n",
1737 | " 338,\n",
1738 | " 340,\n",
1739 | " 342,\n",
1740 | " 344,\n",
1741 | " 346,\n",
1742 | " 348,\n",
1743 | " 350,\n",
1744 | " 352,\n",
1745 | " 354,\n",
1746 | " 356,\n",
1747 | " 358,\n",
1748 | " 360,\n",
1749 | " 362,\n",
1750 | " 364,\n",
1751 | " 366,\n",
1752 | " 368,\n",
1753 | " 370,\n",
1754 | " 372,\n",
1755 | " 374,\n",
1756 | " 376,\n",
1757 | " 378,\n",
1758 | " 380,\n",
1759 | " 382,\n",
1760 | " 384,\n",
1761 | " 386,\n",
1762 | " 388,\n",
1763 | " 390,\n",
1764 | " 392,\n",
1765 | " 394,\n",
1766 | " 396,\n",
1767 | " 398,\n",
1768 | " 400,\n",
1769 | " 402,\n",
1770 | " 404,\n",
1771 | " 406,\n",
1772 | " 408,\n",
1773 | " 410,\n",
1774 | " 412,\n",
1775 | " 414,\n",
1776 | " 416,\n",
1777 | " 418,\n",
1778 | " 420,\n",
1779 | " 422,\n",
1780 | " 424,\n",
1781 | " 426,\n",
1782 | " 428,\n",
1783 | " 430,\n",
1784 | " 432,\n",
1785 | " 434,\n",
1786 | " 436,\n",
1787 | " 438,\n",
1788 | " 440,\n",
1789 | " 442,\n",
1790 | " 444,\n",
1791 | " 446,\n",
1792 | " 448,\n",
1793 | " 450,\n",
1794 | " 452,\n",
1795 | " 454,\n",
1796 | " 456,\n",
1797 | " 458,\n",
1798 | " 460,\n",
1799 | " 462,\n",
1800 | " 464,\n",
1801 | " 466,\n",
1802 | " 468,\n",
1803 | " 470,\n",
1804 | " 472,\n",
1805 | " 474,\n",
1806 | " 476,\n",
1807 | " 478,\n",
1808 | " 480,\n",
1809 | " 482,\n",
1810 | " 484,\n",
1811 | " 486,\n",
1812 | " 488,\n",
1813 | " 490,\n",
1814 | " 492,\n",
1815 | " 494,\n",
1816 | " 496,\n",
1817 | " 498,\n",
1818 | " 500,\n",
1819 | " 502,\n",
1820 | " 504,\n",
1821 | " 506,\n",
1822 | " 508,\n",
1823 | " 510,\n",
1824 | " 512,\n",
1825 | " 514,\n",
1826 | " 516,\n",
1827 | " 518,\n",
1828 | " 520,\n",
1829 | " 522,\n",
1830 | " 524,\n",
1831 | " 526,\n",
1832 | " 528,\n",
1833 | " 530,\n",
1834 | " 532,\n",
1835 | " 534,\n",
1836 | " 536,\n",
1837 | " 538,\n",
1838 | " 540,\n",
1839 | " 542,\n",
1840 | " 544,\n",
1841 | " 546,\n",
1842 | " 548,\n",
1843 | " 550,\n",
1844 | " 552,\n",
1845 | " 554,\n",
1846 | " 556,\n",
1847 | " 558,\n",
1848 | " 560,\n",
1849 | " 562,\n",
1850 | " 564,\n",
1851 | " 566,\n",
1852 | " 568,\n",
1853 | " 570,\n",
1854 | " 572,\n",
1855 | " 574,\n",
1856 | " 576,\n",
1857 | " 578,\n",
1858 | " 580,\n",
1859 | " 582,\n",
1860 | " 584,\n",
1861 | " 586,\n",
1862 | " 588,\n",
1863 | " 590,\n",
1864 | " 592,\n",
1865 | " 594,\n",
1866 | " 596,\n",
1867 | " 598,\n",
1868 | " 600,\n",
1869 | " 602,\n",
1870 | " 604,\n",
1871 | " 606,\n",
1872 | " 608,\n",
1873 | " 610,\n",
1874 | " 612,\n",
1875 | " 614,\n",
1876 | " 616,\n",
1877 | " 618,\n",
1878 | " 620,\n",
1879 | " 622,\n",
1880 | " 624,\n",
1881 | " 626,\n",
1882 | " 628,\n",
1883 | " 630,\n",
1884 | " 632,\n",
1885 | " 634,\n",
1886 | " 636,\n",
1887 | " 638,\n",
1888 | " 640,\n",
1889 | " 642,\n",
1890 | " 644,\n",
1891 | " 646,\n",
1892 | " 648,\n",
1893 | " 650,\n",
1894 | " 652,\n",
1895 | " 654,\n",
1896 | " 656,\n",
1897 | " 658,\n",
1898 | " 660,\n",
1899 | " 662,\n",
1900 | " 664,\n",
1901 | " 666,\n",
1902 | " 668,\n",
1903 | " 670,\n",
1904 | " 672,\n",
1905 | " 674,\n",
1906 | " 676,\n",
1907 | " 678,\n",
1908 | " 680,\n",
1909 | " 682,\n",
1910 | " 684,\n",
1911 | " 686,\n",
1912 | " 688,\n",
1913 | " 690,\n",
1914 | " 692,\n",
1915 | " 694,\n",
1916 | " 696,\n",
1917 | " 698,\n",
1918 | " 700,\n",
1919 | " 702,\n",
1920 | " 704,\n",
1921 | " 706,\n",
1922 | " 708,\n",
1923 | " 710,\n",
1924 | " 712,\n",
1925 | " 714,\n",
1926 | " 716,\n",
1927 | " 718,\n",
1928 | " 720,\n",
1929 | " 722,\n",
1930 | " 724,\n",
1931 | " 726,\n",
1932 | " 728,\n",
1933 | " 730,\n",
1934 | " 732,\n",
1935 | " 734,\n",
1936 | " 736,\n",
1937 | " 738,\n",
1938 | " 740,\n",
1939 | " 742,\n",
1940 | " 744,\n",
1941 | " 746,\n",
1942 | " 748,\n",
1943 | " 750,\n",
1944 | " 752,\n",
1945 | " 754,\n",
1946 | " 756,\n",
1947 | " 758,\n",
1948 | " 760,\n",
1949 | " 762,\n",
1950 | " 764,\n",
1951 | " 766,\n",
1952 | " 768,\n",
1953 | " 770,\n",
1954 | " 772,\n",
1955 | " 774,\n",
1956 | " 776,\n",
1957 | " 778,\n",
1958 | " 780,\n",
1959 | " 782,\n",
1960 | " 784,\n",
1961 | " 786,\n",
1962 | " 788,\n",
1963 | " 790,\n",
1964 | " 792,\n",
1965 | " 794,\n",
1966 | " 796,\n",
1967 | " 798,\n",
1968 | " 800,\n",
1969 | " 802,\n",
1970 | " 804,\n",
1971 | " 806,\n",
1972 | " 808,\n",
1973 | " 810,\n",
1974 | " 812,\n",
1975 | " 814,\n",
1976 | " 816,\n",
1977 | " 818,\n",
1978 | " 820,\n",
1979 | " 822,\n",
1980 | " 824,\n",
1981 | " 826,\n",
1982 | " 828,\n",
1983 | " 830,\n",
1984 | " 832,\n",
1985 | " 834,\n",
1986 | " 836,\n",
1987 | " 838,\n",
1988 | " 840,\n",
1989 | " 842,\n",
1990 | " 844,\n",
1991 | " 846,\n",
1992 | " 848,\n",
1993 | " 850,\n",
1994 | " 852,\n",
1995 | " 854,\n",
1996 | " 856,\n",
1997 | " 858,\n",
1998 | " 860,\n",
1999 | " 862,\n",
2000 | " 864,\n",
2001 | " 866,\n",
2002 | " 868,\n",
2003 | " 870,\n",
2004 | " 872,\n",
2005 | " 874,\n",
2006 | " 876,\n",
2007 | " 878,\n",
2008 | " 880,\n",
2009 | " 882,\n",
2010 | " 884,\n",
2011 | " 886,\n",
2012 | " 888,\n",
2013 | " 890,\n",
2014 | " 892,\n",
2015 | " 894,\n",
2016 | " 896,\n",
2017 | " 898,\n",
2018 | " 900,\n",
2019 | " 902,\n",
2020 | " 904,\n",
2021 | " 906,\n",
2022 | " 908,\n",
2023 | " 910,\n",
2024 | " 912,\n",
2025 | " 914,\n",
2026 | " 916,\n",
2027 | " 918,\n",
2028 | " 920,\n",
2029 | " 922,\n",
2030 | " 924,\n",
2031 | " 926,\n",
2032 | " 928,\n",
2033 | " 930,\n",
2034 | " 932,\n",
2035 | " 934,\n",
2036 | " 936,\n",
2037 | " 938,\n",
2038 | " 940,\n",
2039 | " 942,\n",
2040 | " 944,\n",
2041 | " 946,\n",
2042 | " 948,\n",
2043 | " 950,\n",
2044 | " 952,\n",
2045 | " 954,\n",
2046 | " 956,\n",
2047 | " 958,\n",
2048 | " 960,\n",
2049 | " 962,\n",
2050 | " 964,\n",
2051 | " 966,\n",
2052 | " 968,\n",
2053 | " 970,\n",
2054 | " 972,\n",
2055 | " 974,\n",
2056 | " 976,\n",
2057 | " 978,\n",
2058 | " 980,\n",
2059 | " 982,\n",
2060 | " 984,\n",
2061 | " 986,\n",
2062 | " 988,\n",
2063 | " 990,\n",
2064 | " 992,\n",
2065 | " 994,\n",
2066 | " 996,\n",
2067 | " 998]"
2068 | ]
2069 | },
2070 | "execution_count": 49,
2071 | "metadata": {},
2072 | "output_type": "execute_result"
2073 | }
2074 | ],
2075 | "source": [
2076 | "my_list = []\n",
2077 | "for number in range(0, 1000):\n",
2078 | " if number % 2 == 0:\n",
2079 | " my_list.append(number)\n",
2080 | "my_list"
2081 | ]
2082 | },
2083 | {
2084 | "cell_type": "markdown",
2085 | "metadata": {},
2086 | "source": [
2087 | "
\n",
2088 | "Now the same thing but with list comprehension."
2089 | ]
2090 | },
2091 | {
2092 | "cell_type": "code",
2093 | "execution_count": 50,
2094 | "metadata": {
2095 | "collapsed": false
2096 | },
2097 | "outputs": [
2098 | {
2099 | "data": {
2100 | "text/plain": [
2101 | "[0,\n",
2102 | " 2,\n",
2103 | " 4,\n",
2104 | " 6,\n",
2105 | " 8,\n",
2106 | " 10,\n",
2107 | " 12,\n",
2108 | " 14,\n",
2109 | " 16,\n",
2110 | " 18,\n",
2111 | " 20,\n",
2112 | " 22,\n",
2113 | " 24,\n",
2114 | " 26,\n",
2115 | " 28,\n",
2116 | " 30,\n",
2117 | " 32,\n",
2118 | " 34,\n",
2119 | " 36,\n",
2120 | " 38,\n",
2121 | " 40,\n",
2122 | " 42,\n",
2123 | " 44,\n",
2124 | " 46,\n",
2125 | " 48,\n",
2126 | " 50,\n",
2127 | " 52,\n",
2128 | " 54,\n",
2129 | " 56,\n",
2130 | " 58,\n",
2131 | " 60,\n",
2132 | " 62,\n",
2133 | " 64,\n",
2134 | " 66,\n",
2135 | " 68,\n",
2136 | " 70,\n",
2137 | " 72,\n",
2138 | " 74,\n",
2139 | " 76,\n",
2140 | " 78,\n",
2141 | " 80,\n",
2142 | " 82,\n",
2143 | " 84,\n",
2144 | " 86,\n",
2145 | " 88,\n",
2146 | " 90,\n",
2147 | " 92,\n",
2148 | " 94,\n",
2149 | " 96,\n",
2150 | " 98,\n",
2151 | " 100,\n",
2152 | " 102,\n",
2153 | " 104,\n",
2154 | " 106,\n",
2155 | " 108,\n",
2156 | " 110,\n",
2157 | " 112,\n",
2158 | " 114,\n",
2159 | " 116,\n",
2160 | " 118,\n",
2161 | " 120,\n",
2162 | " 122,\n",
2163 | " 124,\n",
2164 | " 126,\n",
2165 | " 128,\n",
2166 | " 130,\n",
2167 | " 132,\n",
2168 | " 134,\n",
2169 | " 136,\n",
2170 | " 138,\n",
2171 | " 140,\n",
2172 | " 142,\n",
2173 | " 144,\n",
2174 | " 146,\n",
2175 | " 148,\n",
2176 | " 150,\n",
2177 | " 152,\n",
2178 | " 154,\n",
2179 | " 156,\n",
2180 | " 158,\n",
2181 | " 160,\n",
2182 | " 162,\n",
2183 | " 164,\n",
2184 | " 166,\n",
2185 | " 168,\n",
2186 | " 170,\n",
2187 | " 172,\n",
2188 | " 174,\n",
2189 | " 176,\n",
2190 | " 178,\n",
2191 | " 180,\n",
2192 | " 182,\n",
2193 | " 184,\n",
2194 | " 186,\n",
2195 | " 188,\n",
2196 | " 190,\n",
2197 | " 192,\n",
2198 | " 194,\n",
2199 | " 196,\n",
2200 | " 198,\n",
2201 | " 200,\n",
2202 | " 202,\n",
2203 | " 204,\n",
2204 | " 206,\n",
2205 | " 208,\n",
2206 | " 210,\n",
2207 | " 212,\n",
2208 | " 214,\n",
2209 | " 216,\n",
2210 | " 218,\n",
2211 | " 220,\n",
2212 | " 222,\n",
2213 | " 224,\n",
2214 | " 226,\n",
2215 | " 228,\n",
2216 | " 230,\n",
2217 | " 232,\n",
2218 | " 234,\n",
2219 | " 236,\n",
2220 | " 238,\n",
2221 | " 240,\n",
2222 | " 242,\n",
2223 | " 244,\n",
2224 | " 246,\n",
2225 | " 248,\n",
2226 | " 250,\n",
2227 | " 252,\n",
2228 | " 254,\n",
2229 | " 256,\n",
2230 | " 258,\n",
2231 | " 260,\n",
2232 | " 262,\n",
2233 | " 264,\n",
2234 | " 266,\n",
2235 | " 268,\n",
2236 | " 270,\n",
2237 | " 272,\n",
2238 | " 274,\n",
2239 | " 276,\n",
2240 | " 278,\n",
2241 | " 280,\n",
2242 | " 282,\n",
2243 | " 284,\n",
2244 | " 286,\n",
2245 | " 288,\n",
2246 | " 290,\n",
2247 | " 292,\n",
2248 | " 294,\n",
2249 | " 296,\n",
2250 | " 298,\n",
2251 | " 300,\n",
2252 | " 302,\n",
2253 | " 304,\n",
2254 | " 306,\n",
2255 | " 308,\n",
2256 | " 310,\n",
2257 | " 312,\n",
2258 | " 314,\n",
2259 | " 316,\n",
2260 | " 318,\n",
2261 | " 320,\n",
2262 | " 322,\n",
2263 | " 324,\n",
2264 | " 326,\n",
2265 | " 328,\n",
2266 | " 330,\n",
2267 | " 332,\n",
2268 | " 334,\n",
2269 | " 336,\n",
2270 | " 338,\n",
2271 | " 340,\n",
2272 | " 342,\n",
2273 | " 344,\n",
2274 | " 346,\n",
2275 | " 348,\n",
2276 | " 350,\n",
2277 | " 352,\n",
2278 | " 354,\n",
2279 | " 356,\n",
2280 | " 358,\n",
2281 | " 360,\n",
2282 | " 362,\n",
2283 | " 364,\n",
2284 | " 366,\n",
2285 | " 368,\n",
2286 | " 370,\n",
2287 | " 372,\n",
2288 | " 374,\n",
2289 | " 376,\n",
2290 | " 378,\n",
2291 | " 380,\n",
2292 | " 382,\n",
2293 | " 384,\n",
2294 | " 386,\n",
2295 | " 388,\n",
2296 | " 390,\n",
2297 | " 392,\n",
2298 | " 394,\n",
2299 | " 396,\n",
2300 | " 398,\n",
2301 | " 400,\n",
2302 | " 402,\n",
2303 | " 404,\n",
2304 | " 406,\n",
2305 | " 408,\n",
2306 | " 410,\n",
2307 | " 412,\n",
2308 | " 414,\n",
2309 | " 416,\n",
2310 | " 418,\n",
2311 | " 420,\n",
2312 | " 422,\n",
2313 | " 424,\n",
2314 | " 426,\n",
2315 | " 428,\n",
2316 | " 430,\n",
2317 | " 432,\n",
2318 | " 434,\n",
2319 | " 436,\n",
2320 | " 438,\n",
2321 | " 440,\n",
2322 | " 442,\n",
2323 | " 444,\n",
2324 | " 446,\n",
2325 | " 448,\n",
2326 | " 450,\n",
2327 | " 452,\n",
2328 | " 454,\n",
2329 | " 456,\n",
2330 | " 458,\n",
2331 | " 460,\n",
2332 | " 462,\n",
2333 | " 464,\n",
2334 | " 466,\n",
2335 | " 468,\n",
2336 | " 470,\n",
2337 | " 472,\n",
2338 | " 474,\n",
2339 | " 476,\n",
2340 | " 478,\n",
2341 | " 480,\n",
2342 | " 482,\n",
2343 | " 484,\n",
2344 | " 486,\n",
2345 | " 488,\n",
2346 | " 490,\n",
2347 | " 492,\n",
2348 | " 494,\n",
2349 | " 496,\n",
2350 | " 498,\n",
2351 | " 500,\n",
2352 | " 502,\n",
2353 | " 504,\n",
2354 | " 506,\n",
2355 | " 508,\n",
2356 | " 510,\n",
2357 | " 512,\n",
2358 | " 514,\n",
2359 | " 516,\n",
2360 | " 518,\n",
2361 | " 520,\n",
2362 | " 522,\n",
2363 | " 524,\n",
2364 | " 526,\n",
2365 | " 528,\n",
2366 | " 530,\n",
2367 | " 532,\n",
2368 | " 534,\n",
2369 | " 536,\n",
2370 | " 538,\n",
2371 | " 540,\n",
2372 | " 542,\n",
2373 | " 544,\n",
2374 | " 546,\n",
2375 | " 548,\n",
2376 | " 550,\n",
2377 | " 552,\n",
2378 | " 554,\n",
2379 | " 556,\n",
2380 | " 558,\n",
2381 | " 560,\n",
2382 | " 562,\n",
2383 | " 564,\n",
2384 | " 566,\n",
2385 | " 568,\n",
2386 | " 570,\n",
2387 | " 572,\n",
2388 | " 574,\n",
2389 | " 576,\n",
2390 | " 578,\n",
2391 | " 580,\n",
2392 | " 582,\n",
2393 | " 584,\n",
2394 | " 586,\n",
2395 | " 588,\n",
2396 | " 590,\n",
2397 | " 592,\n",
2398 | " 594,\n",
2399 | " 596,\n",
2400 | " 598,\n",
2401 | " 600,\n",
2402 | " 602,\n",
2403 | " 604,\n",
2404 | " 606,\n",
2405 | " 608,\n",
2406 | " 610,\n",
2407 | " 612,\n",
2408 | " 614,\n",
2409 | " 616,\n",
2410 | " 618,\n",
2411 | " 620,\n",
2412 | " 622,\n",
2413 | " 624,\n",
2414 | " 626,\n",
2415 | " 628,\n",
2416 | " 630,\n",
2417 | " 632,\n",
2418 | " 634,\n",
2419 | " 636,\n",
2420 | " 638,\n",
2421 | " 640,\n",
2422 | " 642,\n",
2423 | " 644,\n",
2424 | " 646,\n",
2425 | " 648,\n",
2426 | " 650,\n",
2427 | " 652,\n",
2428 | " 654,\n",
2429 | " 656,\n",
2430 | " 658,\n",
2431 | " 660,\n",
2432 | " 662,\n",
2433 | " 664,\n",
2434 | " 666,\n",
2435 | " 668,\n",
2436 | " 670,\n",
2437 | " 672,\n",
2438 | " 674,\n",
2439 | " 676,\n",
2440 | " 678,\n",
2441 | " 680,\n",
2442 | " 682,\n",
2443 | " 684,\n",
2444 | " 686,\n",
2445 | " 688,\n",
2446 | " 690,\n",
2447 | " 692,\n",
2448 | " 694,\n",
2449 | " 696,\n",
2450 | " 698,\n",
2451 | " 700,\n",
2452 | " 702,\n",
2453 | " 704,\n",
2454 | " 706,\n",
2455 | " 708,\n",
2456 | " 710,\n",
2457 | " 712,\n",
2458 | " 714,\n",
2459 | " 716,\n",
2460 | " 718,\n",
2461 | " 720,\n",
2462 | " 722,\n",
2463 | " 724,\n",
2464 | " 726,\n",
2465 | " 728,\n",
2466 | " 730,\n",
2467 | " 732,\n",
2468 | " 734,\n",
2469 | " 736,\n",
2470 | " 738,\n",
2471 | " 740,\n",
2472 | " 742,\n",
2473 | " 744,\n",
2474 | " 746,\n",
2475 | " 748,\n",
2476 | " 750,\n",
2477 | " 752,\n",
2478 | " 754,\n",
2479 | " 756,\n",
2480 | " 758,\n",
2481 | " 760,\n",
2482 | " 762,\n",
2483 | " 764,\n",
2484 | " 766,\n",
2485 | " 768,\n",
2486 | " 770,\n",
2487 | " 772,\n",
2488 | " 774,\n",
2489 | " 776,\n",
2490 | " 778,\n",
2491 | " 780,\n",
2492 | " 782,\n",
2493 | " 784,\n",
2494 | " 786,\n",
2495 | " 788,\n",
2496 | " 790,\n",
2497 | " 792,\n",
2498 | " 794,\n",
2499 | " 796,\n",
2500 | " 798,\n",
2501 | " 800,\n",
2502 | " 802,\n",
2503 | " 804,\n",
2504 | " 806,\n",
2505 | " 808,\n",
2506 | " 810,\n",
2507 | " 812,\n",
2508 | " 814,\n",
2509 | " 816,\n",
2510 | " 818,\n",
2511 | " 820,\n",
2512 | " 822,\n",
2513 | " 824,\n",
2514 | " 826,\n",
2515 | " 828,\n",
2516 | " 830,\n",
2517 | " 832,\n",
2518 | " 834,\n",
2519 | " 836,\n",
2520 | " 838,\n",
2521 | " 840,\n",
2522 | " 842,\n",
2523 | " 844,\n",
2524 | " 846,\n",
2525 | " 848,\n",
2526 | " 850,\n",
2527 | " 852,\n",
2528 | " 854,\n",
2529 | " 856,\n",
2530 | " 858,\n",
2531 | " 860,\n",
2532 | " 862,\n",
2533 | " 864,\n",
2534 | " 866,\n",
2535 | " 868,\n",
2536 | " 870,\n",
2537 | " 872,\n",
2538 | " 874,\n",
2539 | " 876,\n",
2540 | " 878,\n",
2541 | " 880,\n",
2542 | " 882,\n",
2543 | " 884,\n",
2544 | " 886,\n",
2545 | " 888,\n",
2546 | " 890,\n",
2547 | " 892,\n",
2548 | " 894,\n",
2549 | " 896,\n",
2550 | " 898,\n",
2551 | " 900,\n",
2552 | " 902,\n",
2553 | " 904,\n",
2554 | " 906,\n",
2555 | " 908,\n",
2556 | " 910,\n",
2557 | " 912,\n",
2558 | " 914,\n",
2559 | " 916,\n",
2560 | " 918,\n",
2561 | " 920,\n",
2562 | " 922,\n",
2563 | " 924,\n",
2564 | " 926,\n",
2565 | " 928,\n",
2566 | " 930,\n",
2567 | " 932,\n",
2568 | " 934,\n",
2569 | " 936,\n",
2570 | " 938,\n",
2571 | " 940,\n",
2572 | " 942,\n",
2573 | " 944,\n",
2574 | " 946,\n",
2575 | " 948,\n",
2576 | " 950,\n",
2577 | " 952,\n",
2578 | " 954,\n",
2579 | " 956,\n",
2580 | " 958,\n",
2581 | " 960,\n",
2582 | " 962,\n",
2583 | " 964,\n",
2584 | " 966,\n",
2585 | " 968,\n",
2586 | " 970,\n",
2587 | " 972,\n",
2588 | " 974,\n",
2589 | " 976,\n",
2590 | " 978,\n",
2591 | " 980,\n",
2592 | " 982,\n",
2593 | " 984,\n",
2594 | " 986,\n",
2595 | " 988,\n",
2596 | " 990,\n",
2597 | " 992,\n",
2598 | " 994,\n",
2599 | " 996,\n",
2600 | " 998]"
2601 | ]
2602 | },
2603 | "execution_count": 50,
2604 | "metadata": {},
2605 | "output_type": "execute_result"
2606 | }
2607 | ],
2608 | "source": [
2609 | "my_list = [number for number in range(0,1000) if number % 2 == 0]\n",
2610 | "my_list"
2611 | ]
2612 | },
2613 | {
2614 | "cell_type": "markdown",
2615 | "metadata": {
2616 | "collapsed": true
2617 | },
2618 | "source": [
2619 | "
\n",
2620 | "# The Python Programming Language: Numerical Python (NumPy)"
2621 | ]
2622 | },
2623 | {
2624 | "cell_type": "code",
2625 | "execution_count": 51,
2626 | "metadata": {
2627 | "collapsed": true
2628 | },
2629 | "outputs": [],
2630 | "source": [
2631 | "import numpy as np"
2632 | ]
2633 | },
2634 | {
2635 | "cell_type": "markdown",
2636 | "metadata": {},
2637 | "source": [
2638 | "
\n",
2639 | "## Creating Arrays"
2640 | ]
2641 | },
2642 | {
2643 | "cell_type": "markdown",
2644 | "metadata": {},
2645 | "source": [
2646 | "Create a list and convert it to a numpy array"
2647 | ]
2648 | },
2649 | {
2650 | "cell_type": "code",
2651 | "execution_count": 72,
2652 | "metadata": {
2653 | "collapsed": false
2654 | },
2655 | "outputs": [
2656 | {
2657 | "data": {
2658 | "text/plain": [
2659 | "array([1, 2, 3])"
2660 | ]
2661 | },
2662 | "execution_count": 72,
2663 | "metadata": {},
2664 | "output_type": "execute_result"
2665 | }
2666 | ],
2667 | "source": [
2668 | "mylist = [1, 2, 3]\n",
2669 | "x = np.array(mylist)\n",
2670 | "x"
2671 | ]
2672 | },
2673 | {
2674 | "cell_type": "markdown",
2675 | "metadata": {},
2676 | "source": [
2677 | "
\n",
2678 | "Or just pass in a list directly"
2679 | ]
2680 | },
2681 | {
2682 | "cell_type": "code",
2683 | "execution_count": 73,
2684 | "metadata": {
2685 | "collapsed": false
2686 | },
2687 | "outputs": [
2688 | {
2689 | "data": {
2690 | "text/plain": [
2691 | "array([4, 5, 6])"
2692 | ]
2693 | },
2694 | "execution_count": 73,
2695 | "metadata": {},
2696 | "output_type": "execute_result"
2697 | }
2698 | ],
2699 | "source": [
2700 | "y = np.array([4, 5, 6])\n",
2701 | "y"
2702 | ]
2703 | },
2704 | {
2705 | "cell_type": "markdown",
2706 | "metadata": {},
2707 | "source": [
2708 | "
\n",
2709 | "Pass in a list of lists to create a multidimensional array."
2710 | ]
2711 | },
2712 | {
2713 | "cell_type": "code",
2714 | "execution_count": null,
2715 | "metadata": {
2716 | "collapsed": false
2717 | },
2718 | "outputs": [],
2719 | "source": [
2720 | "m = np.array([[7, 8, 9], [10, 11, 12]])\n",
2721 | "m"
2722 | ]
2723 | },
2724 | {
2725 | "cell_type": "markdown",
2726 | "metadata": {},
2727 | "source": [
2728 | "
\n",
2729 | "Use the shape method to find the dimensions of the array. (rows, columns)"
2730 | ]
2731 | },
2732 | {
2733 | "cell_type": "code",
2734 | "execution_count": null,
2735 | "metadata": {
2736 | "collapsed": false
2737 | },
2738 | "outputs": [],
2739 | "source": [
2740 | "m.shape"
2741 | ]
2742 | },
2743 | {
2744 | "cell_type": "markdown",
2745 | "metadata": {},
2746 | "source": [
2747 | "
\n",
2748 | "`arange` returns evenly spaced values within a given interval."
2749 | ]
2750 | },
2751 | {
2752 | "cell_type": "code",
2753 | "execution_count": 58,
2754 | "metadata": {
2755 | "collapsed": false
2756 | },
2757 | "outputs": [
2758 | {
2759 | "data": {
2760 | "text/plain": [
2761 | "array([ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28])"
2762 | ]
2763 | },
2764 | "execution_count": 58,
2765 | "metadata": {},
2766 | "output_type": "execute_result"
2767 | }
2768 | ],
2769 | "source": [
2770 | "n = np.arange(0, 30, 2) # start at 0 count up by 2, stop before 30\n",
2771 | "n"
2772 | ]
2773 | },
2774 | {
2775 | "cell_type": "markdown",
2776 | "metadata": {},
2777 | "source": [
2778 | "
\n",
2779 | "`reshape` returns an array with the same data with a new shape."
2780 | ]
2781 | },
2782 | {
2783 | "cell_type": "code",
2784 | "execution_count": 59,
2785 | "metadata": {
2786 | "collapsed": false
2787 | },
2788 | "outputs": [
2789 | {
2790 | "data": {
2791 | "text/plain": [
2792 | "array([[ 0, 2, 4, 6, 8],\n",
2793 | " [10, 12, 14, 16, 18],\n",
2794 | " [20, 22, 24, 26, 28]])"
2795 | ]
2796 | },
2797 | "execution_count": 59,
2798 | "metadata": {},
2799 | "output_type": "execute_result"
2800 | }
2801 | ],
2802 | "source": [
2803 | "n = n.reshape(3, 5) # reshape array to be 3x5\n",
2804 | "n"
2805 | ]
2806 | },
2807 | {
2808 | "cell_type": "markdown",
2809 | "metadata": {},
2810 | "source": [
2811 | "
\n",
2812 | "`linspace` returns evenly spaced numbers over a specified interval."
2813 | ]
2814 | },
2815 | {
2816 | "cell_type": "code",
2817 | "execution_count": 52,
2818 | "metadata": {
2819 | "collapsed": false
2820 | },
2821 | "outputs": [
2822 | {
2823 | "data": {
2824 | "text/plain": [
2825 | "array([ 0. , 0.5, 1. , 1.5, 2. , 2.5, 3. , 3.5, 4. ])"
2826 | ]
2827 | },
2828 | "execution_count": 52,
2829 | "metadata": {},
2830 | "output_type": "execute_result"
2831 | }
2832 | ],
2833 | "source": [
2834 | "o = np.linspace(0, 4, 9) # return 9 evenly spaced values from 0 to 4\n",
2835 | "o"
2836 | ]
2837 | },
2838 | {
2839 | "cell_type": "markdown",
2840 | "metadata": {},
2841 | "source": [
2842 | "
\n",
2843 | "`resize` changes the shape and size of array in-place."
2844 | ]
2845 | },
2846 | {
2847 | "cell_type": "code",
2848 | "execution_count": 54,
2849 | "metadata": {
2850 | "collapsed": false
2851 | },
2852 | "outputs": [
2853 | {
2854 | "data": {
2855 | "text/plain": [
2856 | "array([[ 0. , 0.5, 1. ],\n",
2857 | " [ 1.5, 2. , 2.5],\n",
2858 | " [ 3. , 3.5, 4. ]])"
2859 | ]
2860 | },
2861 | "execution_count": 54,
2862 | "metadata": {},
2863 | "output_type": "execute_result"
2864 | }
2865 | ],
2866 | "source": [
2867 | "o.resize(3, 3)\n",
2868 | "o"
2869 | ]
2870 | },
2871 | {
2872 | "cell_type": "markdown",
2873 | "metadata": {},
2874 | "source": [
2875 | "
\n",
2876 | "`ones` returns a new array of given shape and type, filled with ones."
2877 | ]
2878 | },
2879 | {
2880 | "cell_type": "code",
2881 | "execution_count": null,
2882 | "metadata": {
2883 | "collapsed": false
2884 | },
2885 | "outputs": [],
2886 | "source": [
2887 | "np.ones((3, 2))"
2888 | ]
2889 | },
2890 | {
2891 | "cell_type": "markdown",
2892 | "metadata": {},
2893 | "source": [
2894 | "
\n",
2895 | "`zeros` returns a new array of given shape and type, filled with zeros."
2896 | ]
2897 | },
2898 | {
2899 | "cell_type": "code",
2900 | "execution_count": null,
2901 | "metadata": {
2902 | "collapsed": false
2903 | },
2904 | "outputs": [],
2905 | "source": [
2906 | "np.zeros((2, 3))"
2907 | ]
2908 | },
2909 | {
2910 | "cell_type": "markdown",
2911 | "metadata": {},
2912 | "source": [
2913 | "
\n",
2914 | "`eye` returns a 2-D array with ones on the diagonal and zeros elsewhere."
2915 | ]
2916 | },
2917 | {
2918 | "cell_type": "code",
2919 | "execution_count": 61,
2920 | "metadata": {
2921 | "collapsed": false
2922 | },
2923 | "outputs": [
2924 | {
2925 | "data": {
2926 | "text/plain": [
2927 | "array([[ 1., 0., 0.],\n",
2928 | " [ 0., 1., 0.],\n",
2929 | " [ 0., 0., 1.]])"
2930 | ]
2931 | },
2932 | "execution_count": 61,
2933 | "metadata": {},
2934 | "output_type": "execute_result"
2935 | }
2936 | ],
2937 | "source": [
2938 | "np.eye(3)"
2939 | ]
2940 | },
2941 | {
2942 | "cell_type": "markdown",
2943 | "metadata": {},
2944 | "source": [
2945 | "
\n",
2946 | "`diag` extracts a diagonal or constructs a diagonal array."
2947 | ]
2948 | },
2949 | {
2950 | "cell_type": "code",
2951 | "execution_count": 63,
2952 | "metadata": {
2953 | "collapsed": false
2954 | },
2955 | "outputs": [
2956 | {
2957 | "data": {
2958 | "text/plain": [
2959 | "array([[4, 0, 0],\n",
2960 | " [0, 5, 0],\n",
2961 | " [0, 0, 6]])"
2962 | ]
2963 | },
2964 | "execution_count": 63,
2965 | "metadata": {},
2966 | "output_type": "execute_result"
2967 | }
2968 | ],
2969 | "source": [
2970 | "np.diag(y)"
2971 | ]
2972 | },
2973 | {
2974 | "cell_type": "markdown",
2975 | "metadata": {},
2976 | "source": [
2977 | "
\n",
2978 | "Create an array using repeating list (or see `np.tile`)"
2979 | ]
2980 | },
2981 | {
2982 | "cell_type": "code",
2983 | "execution_count": 64,
2984 | "metadata": {
2985 | "collapsed": false
2986 | },
2987 | "outputs": [
2988 | {
2989 | "data": {
2990 | "text/plain": [
2991 | "array([1, 2, 3, 1, 2, 3, 1, 2, 3])"
2992 | ]
2993 | },
2994 | "execution_count": 64,
2995 | "metadata": {},
2996 | "output_type": "execute_result"
2997 | }
2998 | ],
2999 | "source": [
3000 | "np.array([1, 2, 3] * 3)"
3001 | ]
3002 | },
3003 | {
3004 | "cell_type": "markdown",
3005 | "metadata": {},
3006 | "source": [
3007 | "
\n",
3008 | "Repeat elements of an array using `repeat`."
3009 | ]
3010 | },
3011 | {
3012 | "cell_type": "code",
3013 | "execution_count": 65,
3014 | "metadata": {
3015 | "collapsed": false
3016 | },
3017 | "outputs": [
3018 | {
3019 | "data": {
3020 | "text/plain": [
3021 | "array([1, 1, 1, 2, 2, 2, 3, 3, 3])"
3022 | ]
3023 | },
3024 | "execution_count": 65,
3025 | "metadata": {},
3026 | "output_type": "execute_result"
3027 | }
3028 | ],
3029 | "source": [
3030 | "np.repeat([1, 2, 3], 3)"
3031 | ]
3032 | },
3033 | {
3034 | "cell_type": "markdown",
3035 | "metadata": {},
3036 | "source": [
3037 | "
\n",
3038 | "#### Combining Arrays"
3039 | ]
3040 | },
3041 | {
3042 | "cell_type": "code",
3043 | "execution_count": 74,
3044 | "metadata": {
3045 | "collapsed": false
3046 | },
3047 | "outputs": [
3048 | {
3049 | "data": {
3050 | "text/plain": [
3051 | "array([[1, 1, 1],\n",
3052 | " [1, 1, 1]])"
3053 | ]
3054 | },
3055 | "execution_count": 74,
3056 | "metadata": {},
3057 | "output_type": "execute_result"
3058 | }
3059 | ],
3060 | "source": [
3061 | "p = np.ones([2, 3], int)\n",
3062 | "p"
3063 | ]
3064 | },
3065 | {
3066 | "cell_type": "markdown",
3067 | "metadata": {},
3068 | "source": [
3069 | "
\n",
3070 | "Use `vstack` to stack arrays in sequence vertically (row wise)."
3071 | ]
3072 | },
3073 | {
3074 | "cell_type": "code",
3075 | "execution_count": 69,
3076 | "metadata": {
3077 | "collapsed": false
3078 | },
3079 | "outputs": [
3080 | {
3081 | "data": {
3082 | "text/plain": [
3083 | "array([[1, 1, 1],\n",
3084 | " [1, 1, 1],\n",
3085 | " [2, 2, 2],\n",
3086 | " [2, 2, 2]])"
3087 | ]
3088 | },
3089 | "execution_count": 69,
3090 | "metadata": {},
3091 | "output_type": "execute_result"
3092 | }
3093 | ],
3094 | "source": [
3095 | "np.vstack([p, 2*p])"
3096 | ]
3097 | },
3098 | {
3099 | "cell_type": "markdown",
3100 | "metadata": {},
3101 | "source": [
3102 | "
\n",
3103 | "Use `hstack` to stack arrays in sequence horizontally (column wise)."
3104 | ]
3105 | },
3106 | {
3107 | "cell_type": "code",
3108 | "execution_count": 70,
3109 | "metadata": {
3110 | "collapsed": false
3111 | },
3112 | "outputs": [
3113 | {
3114 | "data": {
3115 | "text/plain": [
3116 | "array([[1, 1, 1, 2, 2, 2],\n",
3117 | " [1, 1, 1, 2, 2, 2]])"
3118 | ]
3119 | },
3120 | "execution_count": 70,
3121 | "metadata": {},
3122 | "output_type": "execute_result"
3123 | }
3124 | ],
3125 | "source": [
3126 | "np.hstack([p, 2*p])"
3127 | ]
3128 | },
3129 | {
3130 | "cell_type": "markdown",
3131 | "metadata": {},
3132 | "source": [
3133 | "
\n",
3134 | "## Operations"
3135 | ]
3136 | },
3137 | {
3138 | "cell_type": "markdown",
3139 | "metadata": {},
3140 | "source": [
3141 | "Use `+`, `-`, `*`, `/` and `**` to perform element wise addition, subtraction, multiplication, division and power."
3142 | ]
3143 | },
3144 | {
3145 | "cell_type": "code",
3146 | "execution_count": 75,
3147 | "metadata": {
3148 | "collapsed": false
3149 | },
3150 | "outputs": [
3151 | {
3152 | "name": "stdout",
3153 | "output_type": "stream",
3154 | "text": [
3155 | "[5 7 9]\n",
3156 | "[-3 -3 -3]\n"
3157 | ]
3158 | }
3159 | ],
3160 | "source": [
3161 | "print(x + y) # elementwise addition [1 2 3] + [4 5 6] = [5 7 9]\n",
3162 | "print(x - y) # elementwise subtraction [1 2 3] - [4 5 6] = [-3 -3 -3]"
3163 | ]
3164 | },
3165 | {
3166 | "cell_type": "code",
3167 | "execution_count": 77,
3168 | "metadata": {
3169 | "collapsed": false
3170 | },
3171 | "outputs": [
3172 | {
3173 | "name": "stdout",
3174 | "output_type": "stream",
3175 | "text": [
3176 | "[ 4 10 18]\n",
3177 | "[ 0.25 0.4 0.5 ]\n"
3178 | ]
3179 | }
3180 | ],
3181 | "source": [
3182 | "print(x * y) # elementwise multiplication [1 2 3] * [4 5 6] = [4 10 18]\n",
3183 | "print(x / y) # elementwise divison [1 2 3] / [4 5 6] = [0.25 0.4 0.5]"
3184 | ]
3185 | },
3186 | {
3187 | "cell_type": "code",
3188 | "execution_count": 76,
3189 | "metadata": {
3190 | "collapsed": false
3191 | },
3192 | "outputs": [
3193 | {
3194 | "name": "stdout",
3195 | "output_type": "stream",
3196 | "text": [
3197 | "[1 4 9]\n"
3198 | ]
3199 | }
3200 | ],
3201 | "source": [
3202 | "print(x**2) # elementwise power [1 2 3] ^2 = [1 4 9]"
3203 | ]
3204 | },
3205 | {
3206 | "cell_type": "markdown",
3207 | "metadata": {},
3208 | "source": [
3209 | "
\n",
3210 | "**Dot Product:** \n",
3211 | "\n",
3212 | "$ \\begin{bmatrix}x_1 \\ x_2 \\ x_3\\end{bmatrix}\n",
3213 | "\\cdot\n",
3214 | "\\begin{bmatrix}y_1 \\\\ y_2 \\\\ y_3\\end{bmatrix}\n",
3215 | "= x_1 y_1 + x_2 y_2 + x_3 y_3$"
3216 | ]
3217 | },
3218 | {
3219 | "cell_type": "code",
3220 | "execution_count": 78,
3221 | "metadata": {
3222 | "collapsed": false
3223 | },
3224 | "outputs": [
3225 | {
3226 | "data": {
3227 | "text/plain": [
3228 | "32"
3229 | ]
3230 | },
3231 | "execution_count": 78,
3232 | "metadata": {},
3233 | "output_type": "execute_result"
3234 | }
3235 | ],
3236 | "source": [
3237 | "x.dot(y) # dot product 1*4 + 2*5 + 3*6"
3238 | ]
3239 | },
3240 | {
3241 | "cell_type": "code",
3242 | "execution_count": null,
3243 | "metadata": {
3244 | "collapsed": false
3245 | },
3246 | "outputs": [],
3247 | "source": [
3248 | "z = np.array([y, y**2])\n",
3249 | "print(len(z)) # number of rows of array"
3250 | ]
3251 | },
3252 | {
3253 | "cell_type": "markdown",
3254 | "metadata": {},
3255 | "source": [
3256 | "
\n",
3257 | "Let's look at transposing arrays. Transposing permutes the dimensions of the array."
3258 | ]
3259 | },
3260 | {
3261 | "cell_type": "code",
3262 | "execution_count": 79,
3263 | "metadata": {
3264 | "collapsed": false
3265 | },
3266 | "outputs": [
3267 | {
3268 | "data": {
3269 | "text/plain": [
3270 | "array([[ 4, 5, 6],\n",
3271 | " [16, 25, 36]])"
3272 | ]
3273 | },
3274 | "execution_count": 79,
3275 | "metadata": {},
3276 | "output_type": "execute_result"
3277 | }
3278 | ],
3279 | "source": [
3280 | "z = np.array([y, y**2])\n",
3281 | "z"
3282 | ]
3283 | },
3284 | {
3285 | "cell_type": "markdown",
3286 | "metadata": {},
3287 | "source": [
3288 | "
\n",
3289 | "The shape of array `z` is `(2,3)` before transposing."
3290 | ]
3291 | },
3292 | {
3293 | "cell_type": "code",
3294 | "execution_count": 80,
3295 | "metadata": {
3296 | "collapsed": false
3297 | },
3298 | "outputs": [
3299 | {
3300 | "data": {
3301 | "text/plain": [
3302 | "(2, 3)"
3303 | ]
3304 | },
3305 | "execution_count": 80,
3306 | "metadata": {},
3307 | "output_type": "execute_result"
3308 | }
3309 | ],
3310 | "source": [
3311 | "z.shape"
3312 | ]
3313 | },
3314 | {
3315 | "cell_type": "markdown",
3316 | "metadata": {},
3317 | "source": [
3318 | "
\n",
3319 | "Use `.T` to get the transpose."
3320 | ]
3321 | },
3322 | {
3323 | "cell_type": "code",
3324 | "execution_count": 81,
3325 | "metadata": {
3326 | "collapsed": false
3327 | },
3328 | "outputs": [
3329 | {
3330 | "data": {
3331 | "text/plain": [
3332 | "array([[ 4, 16],\n",
3333 | " [ 5, 25],\n",
3334 | " [ 6, 36]])"
3335 | ]
3336 | },
3337 | "execution_count": 81,
3338 | "metadata": {},
3339 | "output_type": "execute_result"
3340 | }
3341 | ],
3342 | "source": [
3343 | "z.T"
3344 | ]
3345 | },
3346 | {
3347 | "cell_type": "markdown",
3348 | "metadata": {},
3349 | "source": [
3350 | "
\n",
3351 | "The number of rows has swapped with the number of columns."
3352 | ]
3353 | },
3354 | {
3355 | "cell_type": "code",
3356 | "execution_count": 84,
3357 | "metadata": {
3358 | "collapsed": false
3359 | },
3360 | "outputs": [
3361 | {
3362 | "data": {
3363 | "text/plain": [
3364 | "array([[ 4, 16],\n",
3365 | " [ 5, 25],\n",
3366 | " [ 6, 36]])"
3367 | ]
3368 | },
3369 | "execution_count": 84,
3370 | "metadata": {},
3371 | "output_type": "execute_result"
3372 | }
3373 | ],
3374 | "source": [
3375 | "z.T"
3376 | ]
3377 | },
3378 | {
3379 | "cell_type": "markdown",
3380 | "metadata": {},
3381 | "source": [
3382 | "
\n",
3383 | "Use `.dtype` to see the data type of the elements in the array."
3384 | ]
3385 | },
3386 | {
3387 | "cell_type": "code",
3388 | "execution_count": 85,
3389 | "metadata": {
3390 | "collapsed": false
3391 | },
3392 | "outputs": [
3393 | {
3394 | "data": {
3395 | "text/plain": [
3396 | "dtype('int64')"
3397 | ]
3398 | },
3399 | "execution_count": 85,
3400 | "metadata": {},
3401 | "output_type": "execute_result"
3402 | }
3403 | ],
3404 | "source": [
3405 | "z.dtype"
3406 | ]
3407 | },
3408 | {
3409 | "cell_type": "markdown",
3410 | "metadata": {},
3411 | "source": [
3412 | "
\n",
3413 | "Use `.astype` to cast to a specific type."
3414 | ]
3415 | },
3416 | {
3417 | "cell_type": "code",
3418 | "execution_count": null,
3419 | "metadata": {
3420 | "collapsed": false
3421 | },
3422 | "outputs": [],
3423 | "source": [
3424 | "z = z.astype('f')\n",
3425 | "z.dtype"
3426 | ]
3427 | },
3428 | {
3429 | "cell_type": "markdown",
3430 | "metadata": {},
3431 | "source": [
3432 | "
\n",
3433 | "## Math Functions"
3434 | ]
3435 | },
3436 | {
3437 | "cell_type": "markdown",
3438 | "metadata": {},
3439 | "source": [
3440 | "Numpy has many built in math functions that can be performed on arrays."
3441 | ]
3442 | },
3443 | {
3444 | "cell_type": "code",
3445 | "execution_count": null,
3446 | "metadata": {
3447 | "collapsed": true
3448 | },
3449 | "outputs": [],
3450 | "source": [
3451 | "a = np.array([-4, -2, 1, 3, 5])"
3452 | ]
3453 | },
3454 | {
3455 | "cell_type": "code",
3456 | "execution_count": null,
3457 | "metadata": {
3458 | "collapsed": false
3459 | },
3460 | "outputs": [],
3461 | "source": [
3462 | "a.sum()"
3463 | ]
3464 | },
3465 | {
3466 | "cell_type": "code",
3467 | "execution_count": null,
3468 | "metadata": {
3469 | "collapsed": false
3470 | },
3471 | "outputs": [],
3472 | "source": [
3473 | "a.max()"
3474 | ]
3475 | },
3476 | {
3477 | "cell_type": "code",
3478 | "execution_count": null,
3479 | "metadata": {
3480 | "collapsed": false
3481 | },
3482 | "outputs": [],
3483 | "source": [
3484 | "a.min()"
3485 | ]
3486 | },
3487 | {
3488 | "cell_type": "code",
3489 | "execution_count": null,
3490 | "metadata": {
3491 | "collapsed": false
3492 | },
3493 | "outputs": [],
3494 | "source": [
3495 | "a.mean()"
3496 | ]
3497 | },
3498 | {
3499 | "cell_type": "code",
3500 | "execution_count": null,
3501 | "metadata": {
3502 | "collapsed": false
3503 | },
3504 | "outputs": [],
3505 | "source": [
3506 | "a.std()"
3507 | ]
3508 | },
3509 | {
3510 | "cell_type": "markdown",
3511 | "metadata": {},
3512 | "source": [
3513 | "
\n",
3514 | "`argmax` and `argmin` return the index of the maximum and minimum values in the array."
3515 | ]
3516 | },
3517 | {
3518 | "cell_type": "code",
3519 | "execution_count": 87,
3520 | "metadata": {
3521 | "collapsed": false
3522 | },
3523 | "outputs": [
3524 | {
3525 | "ename": "AttributeError",
3526 | "evalue": "'function' object has no attribute 'argmax'",
3527 | "output_type": "error",
3528 | "traceback": [
3529 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
3530 | "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
3531 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0ma\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0margmax\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
3532 | "\u001b[0;31mAttributeError\u001b[0m: 'function' object has no attribute 'argmax'"
3533 | ]
3534 | }
3535 | ],
3536 | "source": [
3537 | "a.argmax()"
3538 | ]
3539 | },
3540 | {
3541 | "cell_type": "code",
3542 | "execution_count": null,
3543 | "metadata": {
3544 | "collapsed": false
3545 | },
3546 | "outputs": [],
3547 | "source": [
3548 | "a.argmin()"
3549 | ]
3550 | },
3551 | {
3552 | "cell_type": "markdown",
3553 | "metadata": {},
3554 | "source": [
3555 | "
\n",
3556 | "## Indexing / Slicing"
3557 | ]
3558 | },
3559 | {
3560 | "cell_type": "code",
3561 | "execution_count": null,
3562 | "metadata": {
3563 | "collapsed": false
3564 | },
3565 | "outputs": [],
3566 | "source": [
3567 | "s = np.arange(13)**2\n",
3568 | "s"
3569 | ]
3570 | },
3571 | {
3572 | "cell_type": "markdown",
3573 | "metadata": {},
3574 | "source": [
3575 | "
\n",
3576 | "Use bracket notation to get the value at a specific index. Remember that indexing starts at 0."
3577 | ]
3578 | },
3579 | {
3580 | "cell_type": "code",
3581 | "execution_count": null,
3582 | "metadata": {
3583 | "collapsed": false
3584 | },
3585 | "outputs": [],
3586 | "source": [
3587 | "s[0], s[4], s[-1]"
3588 | ]
3589 | },
3590 | {
3591 | "cell_type": "markdown",
3592 | "metadata": {},
3593 | "source": [
3594 | "
\n",
3595 | "Use `:` to indicate a range. `array[start:stop]`\n",
3596 | "\n",
3597 | "\n",
3598 | "Leaving `start` or `stop` empty will default to the beginning/end of the array."
3599 | ]
3600 | },
3601 | {
3602 | "cell_type": "code",
3603 | "execution_count": null,
3604 | "metadata": {
3605 | "collapsed": false
3606 | },
3607 | "outputs": [],
3608 | "source": [
3609 | "s[1:5]"
3610 | ]
3611 | },
3612 | {
3613 | "cell_type": "markdown",
3614 | "metadata": {},
3615 | "source": [
3616 | "
\n",
3617 | "Use negatives to count from the back."
3618 | ]
3619 | },
3620 | {
3621 | "cell_type": "code",
3622 | "execution_count": null,
3623 | "metadata": {
3624 | "collapsed": false
3625 | },
3626 | "outputs": [],
3627 | "source": [
3628 | "s[-4:]"
3629 | ]
3630 | },
3631 | {
3632 | "cell_type": "markdown",
3633 | "metadata": {},
3634 | "source": [
3635 | "
\n",
3636 | "A second `:` can be used to indicate step-size. `array[start:stop:stepsize]`\n",
3637 | "\n",
3638 | "Here we are starting 5th element from the end, and counting backwards by 2 until the beginning of the array is reached."
3639 | ]
3640 | },
3641 | {
3642 | "cell_type": "code",
3643 | "execution_count": null,
3644 | "metadata": {
3645 | "collapsed": false
3646 | },
3647 | "outputs": [],
3648 | "source": [
3649 | "s[-5::-2]"
3650 | ]
3651 | },
3652 | {
3653 | "cell_type": "markdown",
3654 | "metadata": {
3655 | "collapsed": false
3656 | },
3657 | "source": [
3658 | "
\n",
3659 | "Let's look at a multidimensional array."
3660 | ]
3661 | },
3662 | {
3663 | "cell_type": "code",
3664 | "execution_count": 88,
3665 | "metadata": {
3666 | "collapsed": false
3667 | },
3668 | "outputs": [
3669 | {
3670 | "data": {
3671 | "text/plain": [
3672 | "array([[ 0, 1, 2, 3, 4, 5],\n",
3673 | " [ 6, 7, 8, 9, 10, 11],\n",
3674 | " [12, 13, 14, 15, 16, 17],\n",
3675 | " [18, 19, 20, 21, 22, 23],\n",
3676 | " [24, 25, 26, 27, 28, 29],\n",
3677 | " [30, 31, 32, 33, 34, 35]])"
3678 | ]
3679 | },
3680 | "execution_count": 88,
3681 | "metadata": {},
3682 | "output_type": "execute_result"
3683 | }
3684 | ],
3685 | "source": [
3686 | "r = np.arange(36)\n",
3687 | "r.resize((6, 6))\n",
3688 | "r"
3689 | ]
3690 | },
3691 | {
3692 | "cell_type": "markdown",
3693 | "metadata": {},
3694 | "source": [
3695 | "
\n",
3696 | "Use bracket notation to slice: `array[row, column]`"
3697 | ]
3698 | },
3699 | {
3700 | "cell_type": "code",
3701 | "execution_count": null,
3702 | "metadata": {
3703 | "collapsed": false
3704 | },
3705 | "outputs": [],
3706 | "source": [
3707 | "r[2, 2]"
3708 | ]
3709 | },
3710 | {
3711 | "cell_type": "markdown",
3712 | "metadata": {},
3713 | "source": [
3714 | "
\n",
3715 | "And use : to select a range of rows or columns"
3716 | ]
3717 | },
3718 | {
3719 | "cell_type": "code",
3720 | "execution_count": null,
3721 | "metadata": {
3722 | "collapsed": false
3723 | },
3724 | "outputs": [],
3725 | "source": [
3726 | "r[3, 3:6]"
3727 | ]
3728 | },
3729 | {
3730 | "cell_type": "markdown",
3731 | "metadata": {},
3732 | "source": [
3733 | "
\n",
3734 | "Here we are selecting all the rows up to (and not including) row 2, and all the columns up to (and not including) the last column."
3735 | ]
3736 | },
3737 | {
3738 | "cell_type": "code",
3739 | "execution_count": null,
3740 | "metadata": {
3741 | "collapsed": false
3742 | },
3743 | "outputs": [],
3744 | "source": [
3745 | "r[:2, :-1]"
3746 | ]
3747 | },
3748 | {
3749 | "cell_type": "markdown",
3750 | "metadata": {},
3751 | "source": [
3752 | "
\n",
3753 | "This is a slice of the last row, and only every other element."
3754 | ]
3755 | },
3756 | {
3757 | "cell_type": "code",
3758 | "execution_count": null,
3759 | "metadata": {
3760 | "collapsed": false
3761 | },
3762 | "outputs": [],
3763 | "source": [
3764 | "r[-1, ::2]"
3765 | ]
3766 | },
3767 | {
3768 | "cell_type": "markdown",
3769 | "metadata": {},
3770 | "source": [
3771 | "
\n",
3772 | "We can also perform conditional indexing. Here we are selecting values from the array that are greater than 30. (Also see `np.where`)"
3773 | ]
3774 | },
3775 | {
3776 | "cell_type": "code",
3777 | "execution_count": 94,
3778 | "metadata": {
3779 | "collapsed": false
3780 | },
3781 | "outputs": [
3782 | {
3783 | "data": {
3784 | "text/plain": [
3785 | "array([28, 29, 30, 31, 32, 33, 34, 35])"
3786 | ]
3787 | },
3788 | "execution_count": 94,
3789 | "metadata": {},
3790 | "output_type": "execute_result"
3791 | }
3792 | ],
3793 | "source": [
3794 | "r[r > 27]"
3795 | ]
3796 | },
3797 | {
3798 | "cell_type": "markdown",
3799 | "metadata": {},
3800 | "source": [
3801 | "
\n",
3802 | "Here we are assigning all values in the array that are greater than 30 to the value of 30."
3803 | ]
3804 | },
3805 | {
3806 | "cell_type": "code",
3807 | "execution_count": 97,
3808 | "metadata": {
3809 | "collapsed": false
3810 | },
3811 | "outputs": [
3812 | {
3813 | "data": {
3814 | "text/plain": [
3815 | "array([[ 0, 1, 2, 3, 4, 5],\n",
3816 | " [ 6, 7, 8, 9, 10, 11],\n",
3817 | " [12, 13, 14, 15, 16, 17],\n",
3818 | " [18, 19, 20, 21, 22, 23],\n",
3819 | " [24, 25, 26, 27, 28, 29],\n",
3820 | " [30, 30, 30, 30, 30, 30]])"
3821 | ]
3822 | },
3823 | "execution_count": 97,
3824 | "metadata": {},
3825 | "output_type": "execute_result"
3826 | }
3827 | ],
3828 | "source": [
3829 | "r[r > 30] = 30\n",
3830 | "r"
3831 | ]
3832 | },
3833 | {
3834 | "cell_type": "markdown",
3835 | "metadata": {},
3836 | "source": [
3837 | "
\n",
3838 | "## Copying Data"
3839 | ]
3840 | },
3841 | {
3842 | "cell_type": "markdown",
3843 | "metadata": {},
3844 | "source": [
3845 | "Be careful with copying and modifying arrays in NumPy!\n",
3846 | "\n",
3847 | "\n",
3848 | "`r2` is a slice of `r`"
3849 | ]
3850 | },
3851 | {
3852 | "cell_type": "code",
3853 | "execution_count": 98,
3854 | "metadata": {
3855 | "collapsed": false
3856 | },
3857 | "outputs": [
3858 | {
3859 | "data": {
3860 | "text/plain": [
3861 | "array([[ 0, 1, 2],\n",
3862 | " [ 6, 7, 8],\n",
3863 | " [12, 13, 14]])"
3864 | ]
3865 | },
3866 | "execution_count": 98,
3867 | "metadata": {},
3868 | "output_type": "execute_result"
3869 | }
3870 | ],
3871 | "source": [
3872 | "r2 = r[:3,:3]\n",
3873 | "r2"
3874 | ]
3875 | },
3876 | {
3877 | "cell_type": "markdown",
3878 | "metadata": {},
3879 | "source": [
3880 | "
\n",
3881 | "Set this slice's values to zero ([:] selects the entire array)"
3882 | ]
3883 | },
3884 | {
3885 | "cell_type": "code",
3886 | "execution_count": 99,
3887 | "metadata": {
3888 | "collapsed": false
3889 | },
3890 | "outputs": [
3891 | {
3892 | "data": {
3893 | "text/plain": [
3894 | "array([[0, 0, 0],\n",
3895 | " [0, 0, 0],\n",
3896 | " [0, 0, 0]])"
3897 | ]
3898 | },
3899 | "execution_count": 99,
3900 | "metadata": {},
3901 | "output_type": "execute_result"
3902 | }
3903 | ],
3904 | "source": [
3905 | "r2[:] = 0\n",
3906 | "r2"
3907 | ]
3908 | },
3909 | {
3910 | "cell_type": "markdown",
3911 | "metadata": {},
3912 | "source": [
3913 | "
\n",
3914 | "`r` has also been changed!"
3915 | ]
3916 | },
3917 | {
3918 | "cell_type": "code",
3919 | "execution_count": 100,
3920 | "metadata": {
3921 | "collapsed": false
3922 | },
3923 | "outputs": [
3924 | {
3925 | "data": {
3926 | "text/plain": [
3927 | "array([[ 0, 0, 0, 3, 4, 5],\n",
3928 | " [ 0, 0, 0, 9, 10, 11],\n",
3929 | " [ 0, 0, 0, 15, 16, 17],\n",
3930 | " [18, 19, 20, 21, 22, 23],\n",
3931 | " [24, 25, 26, 27, 28, 29],\n",
3932 | " [30, 30, 30, 30, 30, 30]])"
3933 | ]
3934 | },
3935 | "execution_count": 100,
3936 | "metadata": {},
3937 | "output_type": "execute_result"
3938 | }
3939 | ],
3940 | "source": [
3941 | "r"
3942 | ]
3943 | },
3944 | {
3945 | "cell_type": "markdown",
3946 | "metadata": {},
3947 | "source": [
3948 | "
\n",
3949 | "To avoid this, use `r.copy` to create a copy that will not affect the original array"
3950 | ]
3951 | },
3952 | {
3953 | "cell_type": "code",
3954 | "execution_count": 101,
3955 | "metadata": {
3956 | "collapsed": false
3957 | },
3958 | "outputs": [
3959 | {
3960 | "data": {
3961 | "text/plain": [
3962 | "array([[ 0, 0, 0, 3, 4, 5],\n",
3963 | " [ 0, 0, 0, 9, 10, 11],\n",
3964 | " [ 0, 0, 0, 15, 16, 17],\n",
3965 | " [18, 19, 20, 21, 22, 23],\n",
3966 | " [24, 25, 26, 27, 28, 29],\n",
3967 | " [30, 30, 30, 30, 30, 30]])"
3968 | ]
3969 | },
3970 | "execution_count": 101,
3971 | "metadata": {},
3972 | "output_type": "execute_result"
3973 | }
3974 | ],
3975 | "source": [
3976 | "r_copy = r.copy()\n",
3977 | "r_copy"
3978 | ]
3979 | },
3980 | {
3981 | "cell_type": "markdown",
3982 | "metadata": {},
3983 | "source": [
3984 | "
\n",
3985 | "Now when r_copy is modified, r will not be changed."
3986 | ]
3987 | },
3988 | {
3989 | "cell_type": "code",
3990 | "execution_count": 102,
3991 | "metadata": {
3992 | "collapsed": false
3993 | },
3994 | "outputs": [
3995 | {
3996 | "name": "stdout",
3997 | "output_type": "stream",
3998 | "text": [
3999 | "[[10 10 10 10 10 10]\n",
4000 | " [10 10 10 10 10 10]\n",
4001 | " [10 10 10 10 10 10]\n",
4002 | " [10 10 10 10 10 10]\n",
4003 | " [10 10 10 10 10 10]\n",
4004 | " [10 10 10 10 10 10]] \n",
4005 | "\n",
4006 | "[[ 0 0 0 3 4 5]\n",
4007 | " [ 0 0 0 9 10 11]\n",
4008 | " [ 0 0 0 15 16 17]\n",
4009 | " [18 19 20 21 22 23]\n",
4010 | " [24 25 26 27 28 29]\n",
4011 | " [30 30 30 30 30 30]]\n"
4012 | ]
4013 | }
4014 | ],
4015 | "source": [
4016 | "r_copy[:] = 10\n",
4017 | "print(r_copy, '\\n')\n",
4018 | "print(r)"
4019 | ]
4020 | },
4021 | {
4022 | "cell_type": "markdown",
4023 | "metadata": {},
4024 | "source": [
4025 | "
\n",
4026 | "### Iterating Over Arrays"
4027 | ]
4028 | },
4029 | {
4030 | "cell_type": "markdown",
4031 | "metadata": {},
4032 | "source": [
4033 | "Let's create a new 4 by 3 array of random numbers 0-9."
4034 | ]
4035 | },
4036 | {
4037 | "cell_type": "code",
4038 | "execution_count": 103,
4039 | "metadata": {
4040 | "collapsed": false
4041 | },
4042 | "outputs": [
4043 | {
4044 | "data": {
4045 | "text/plain": [
4046 | "array([[0, 5, 9],\n",
4047 | " [7, 6, 5],\n",
4048 | " [1, 1, 2],\n",
4049 | " [0, 5, 6]])"
4050 | ]
4051 | },
4052 | "execution_count": 103,
4053 | "metadata": {},
4054 | "output_type": "execute_result"
4055 | }
4056 | ],
4057 | "source": [
4058 | "test = np.random.randint(0, 10, (4,3))\n",
4059 | "test"
4060 | ]
4061 | },
4062 | {
4063 | "cell_type": "markdown",
4064 | "metadata": {},
4065 | "source": [
4066 | "
\n",
4067 | "Iterate by row:"
4068 | ]
4069 | },
4070 | {
4071 | "cell_type": "code",
4072 | "execution_count": 104,
4073 | "metadata": {
4074 | "collapsed": false
4075 | },
4076 | "outputs": [
4077 | {
4078 | "name": "stdout",
4079 | "output_type": "stream",
4080 | "text": [
4081 | "[0 5 9]\n",
4082 | "[7 6 5]\n",
4083 | "[1 1 2]\n",
4084 | "[0 5 6]\n"
4085 | ]
4086 | }
4087 | ],
4088 | "source": [
4089 | "for row in test:\n",
4090 | " print(row)"
4091 | ]
4092 | },
4093 | {
4094 | "cell_type": "markdown",
4095 | "metadata": {},
4096 | "source": [
4097 | "
\n",
4098 | "Iterate by index:"
4099 | ]
4100 | },
4101 | {
4102 | "cell_type": "code",
4103 | "execution_count": 105,
4104 | "metadata": {
4105 | "collapsed": false
4106 | },
4107 | "outputs": [
4108 | {
4109 | "name": "stdout",
4110 | "output_type": "stream",
4111 | "text": [
4112 | "[0 5 9]\n",
4113 | "[7 6 5]\n",
4114 | "[1 1 2]\n",
4115 | "[0 5 6]\n"
4116 | ]
4117 | }
4118 | ],
4119 | "source": [
4120 | "for i in range(len(test)):\n",
4121 | " print(test[i])"
4122 | ]
4123 | },
4124 | {
4125 | "cell_type": "markdown",
4126 | "metadata": {},
4127 | "source": [
4128 | "
\n",
4129 | "Iterate by row and index:"
4130 | ]
4131 | },
4132 | {
4133 | "cell_type": "code",
4134 | "execution_count": 106,
4135 | "metadata": {
4136 | "collapsed": false
4137 | },
4138 | "outputs": [
4139 | {
4140 | "name": "stdout",
4141 | "output_type": "stream",
4142 | "text": [
4143 | "row 0 is [0 5 9]\n",
4144 | "row 1 is [7 6 5]\n",
4145 | "row 2 is [1 1 2]\n",
4146 | "row 3 is [0 5 6]\n"
4147 | ]
4148 | }
4149 | ],
4150 | "source": [
4151 | "for i, row in enumerate(test):\n",
4152 | " print('row', i, 'is', row)"
4153 | ]
4154 | },
4155 | {
4156 | "cell_type": "markdown",
4157 | "metadata": {},
4158 | "source": [
4159 | "
\n",
4160 | "Use `zip` to iterate over multiple iterables."
4161 | ]
4162 | },
4163 | {
4164 | "cell_type": "code",
4165 | "execution_count": 107,
4166 | "metadata": {
4167 | "collapsed": false
4168 | },
4169 | "outputs": [
4170 | {
4171 | "data": {
4172 | "text/plain": [
4173 | "array([[ 0, 25, 81],\n",
4174 | " [49, 36, 25],\n",
4175 | " [ 1, 1, 4],\n",
4176 | " [ 0, 25, 36]])"
4177 | ]
4178 | },
4179 | "execution_count": 107,
4180 | "metadata": {},
4181 | "output_type": "execute_result"
4182 | }
4183 | ],
4184 | "source": [
4185 | "test2 = test**2\n",
4186 | "test2"
4187 | ]
4188 | },
4189 | {
4190 | "cell_type": "code",
4191 | "execution_count": 108,
4192 | "metadata": {
4193 | "collapsed": false
4194 | },
4195 | "outputs": [
4196 | {
4197 | "name": "stdout",
4198 | "output_type": "stream",
4199 | "text": [
4200 | "[0 5 9] + [ 0 25 81] = [ 0 30 90]\n",
4201 | "[7 6 5] + [49 36 25] = [56 42 30]\n",
4202 | "[1 1 2] + [1 1 4] = [2 2 6]\n",
4203 | "[0 5 6] + [ 0 25 36] = [ 0 30 42]\n"
4204 | ]
4205 | }
4206 | ],
4207 | "source": [
4208 | "for i, j in zip(test, test2):\n",
4209 | " print(i,'+',j,'=',i+j)"
4210 | ]
4211 | },
4212 | {
4213 | "cell_type": "code",
4214 | "execution_count": 109,
4215 | "metadata": {
4216 | "collapsed": false
4217 | },
4218 | "outputs": [
4219 | {
4220 | "data": {
4221 | "text/plain": [
4222 | ""
4223 | ]
4224 | },
4225 | "execution_count": 109,
4226 | "metadata": {},
4227 | "output_type": "execute_result"
4228 | }
4229 | ],
4230 | "source": [
4231 | "zip(test,test2)"
4232 | ]
4233 | },
4234 | {
4235 | "cell_type": "code",
4236 | "execution_count": null,
4237 | "metadata": {
4238 | "collapsed": true
4239 | },
4240 | "outputs": [],
4241 | "source": []
4242 | }
4243 | ],
4244 | "metadata": {
4245 | "kernelspec": {
4246 | "display_name": "Python 3",
4247 | "language": "python",
4248 | "name": "python3"
4249 | },
4250 | "language_info": {
4251 | "codemirror_mode": {
4252 | "name": "ipython",
4253 | "version": 3
4254 | },
4255 | "file_extension": ".py",
4256 | "mimetype": "text/x-python",
4257 | "name": "python",
4258 | "nbconvert_exporter": "python",
4259 | "pygments_lexer": "ipython3",
4260 | "version": "3.5.2"
4261 | }
4262 | },
4263 | "nbformat": 4,
4264 | "nbformat_minor": 0
4265 | }
4266 |
--------------------------------------------------------------------------------