numpy and matplotlib provide pre-packaged useful functionalityFundamentals of the Python scientific computing ecosystem
numpymatplotlibFundamentals of data storage
numpyaka np, because it’s canonically imported as:
numpyarray data type. Like a list but:
for loops!arange: range that makes arrayszeros / ones / full: make new arraysAll ints:
All floats:
All complex:
np.arangeLike range, but:
arraysfloatsfor loops!)Applying a function to every element:
matplotlibImport:
What type of thing is y?

import matplotlib.pyplot as pltimport numpy as npplt.plot() to construct plot
plt.hist(), plt.scatter(), plt.bar(), …plt.savefig('filename.png')plt.show()import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0.0, 2.0, .01)
plt.plot(x, x)
plt.show()numpy arrays, but it’s often handy.plt.histogram/Users/me/Documents/Research/experiment1.csv). includes:
untitled5023.py).csv, .py, .docx)string)Aside: organize your files!
"data.csv" actually means os.getcwd() + "/" + "data.csv".py filedata folder and use data/data.csvA file behaves like a list of strings, one per line:
Name,Location,URL,Students
Westminster College,"Salt Lake City, UT",westminstercollege.edu,2135
Muhlenberg College,"Allentown, PA",muhlenberg.edu,2330
University of Maine,"Orono, ME",umaine.edu,8677
James Madison University,"Harrisonburg, VA",jmu.edu,19019
Michigan State University,"East Lansing, MI",msu.edu,38853
Why the blank lines between?
Name,Location,URL,Students
!Westminster College,"Salt Lake City, UT",westminstercollege.edu,2135
!Muhlenberg College,"Allentown, PA",muhlenberg.edu,2330
!University of Maine,"Orono, ME",umaine.edu,8677
!James Madison University,"Harrisonburg, VA",jmu.edu,19019
!Michigan State University,"East Lansing, MI",msu.edu,38853
!
Note: We’re simplifying some details here around file handles; see the textbook for details.
['Name', 'Location', 'URL', 'Students\n']
['Westminster College', '"Salt Lake City', ' UT"', 'westminstercollege.edu', '2135\n']
['Muhlenberg College', '"Allentown', ' PA"', 'muhlenberg.edu', '2330\n']
['University of Maine', '"Orono', ' ME"', 'umaine.edu', '8677\n']
['James Madison University', '"Harrisonburg', ' VA"', 'jmu.edu', '19019\n']
['Michigan State University', '"East Lansing', ' MI"', 'msu.edu', '38853\n']
What’s wrong?
['Name', 'Location', 'URL', 'Students']
['Westminster College', 'Salt Lake City, UT', 'westminstercollege.edu', '2135']
['Muhlenberg College', 'Allentown, PA', 'muhlenberg.edu', '2330']
['University of Maine', 'Orono, ME', 'umaine.edu', '8677']
['James Madison University', 'Harrisonburg, VA', 'jmu.edu', '19019']
['Michigan State University', 'East Lansing, MI', 'msu.edu', '38853']
import csv
csv_data = list(csv.reader(open("data.csv")))
names = csv_data[0]
print("Column names:", names)
for row in csv_data[1:]:
print(row)Column names: ['Name', 'Location', 'URL', 'Students']
['Westminster College', 'Salt Lake City, UT', 'westminstercollege.edu', '2135']
['Muhlenberg College', 'Allentown, PA', 'muhlenberg.edu', '2330']
['University of Maine', 'Orono, ME', 'umaine.edu', '8677']
['James Madison University', 'Harrisonburg, VA', 'jmu.edu', '19019']
['Michigan State University', 'East Lansing, MI', 'msu.edu', '38853']
DictReaderopen("data.csv")open(data.csv)File names are strings.
Files are always just strings. If we want ints, we need to convert:
Debug and add comments to the following code.
pandas| Name | Location | URL | Students | |
|---|---|---|---|---|
| 0 | Westminster College | Salt Lake City, UT | westminstercollege.edu | 2135 |
| 1 | Muhlenberg College | Allentown, PA | muhlenberg.edu | 2330 |
| 2 | University of Maine | Orono, ME | umaine.edu | 8677 |
| 3 | James Madison University | Harrisonburg, VA | jmu.edu | 19019 |
| 4 | Michigan State University | East Lansing, MI | msu.edu | 38853 |
We’ll study this next week.