Learn to Code:

Zero to Sixty Data Analysis

Website: goo.gl/i5O6qR

Or wackywendell.github.io/learntocode

QR Code

Goals

  • Learn the basics of programming
  • Learn to import, manipulate, and plot data

Why Python?

  • Easy to learn and use
  • Relatively simple
  • Good for data analysis and plotting
  • Good for machine learning, web development, scripting, text parsing, and many things.

Why Python?

Example Plot

Python is a full-featured language for generic programming, and data analysis, and it can also make beautiful plots. I made this plot last week. It took about 20-40 lines of code!

Installing Python

Install Anaconda, and get the Python 3.4 version!

http://continuum.io/downloads

Let's install it now.

  • Windows or OS X: use the link above.
  • Linux: Use the package manager, and/or ask for help.

Let's try it!

  1. Open the Terminal
  2. Type ipython notebook
  3. Press Enter
  4. A window should open in your browser!
    • If it does not, look for text like
      The IPython Notebook is running at: http://localhost:8888/
    • Open that link
    • Try Ctrl-Click if necessary.
  5. At the top right, click New, and under Notebooks click Python 3.

Let's do some plotting!

In [1]:
%pylab inline
Populating the interactive namespace from numpy and matplotlib
In [2]:
# This is hidden in the slides, but makes the plots small enough to fit in a slide
import matplotlib
phi = (1 + np.sqrt(5)) / 2.
matplotlib.rc('figure', figsize=(3,3/phi))
In [3]:
xs = np.linspace(0, np.pi*2)
ys = np.sin(xs)
plot(xs, ys)
Out[3]:
[<matplotlib.lines.Line2D at 0x7f3f2a684cc0>]

Debugging


plot([1,2,3,4,5,6,7,8,9,10], [1,4,8,2,3,5,8,9,3,6,7])

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-18-ec77aff7bf8c> in <module>()
----> 1 plot([1,2,3,4,5,6,7,8,9,10], [1,4,8,2,3,5,8,9,3,6,7])

/usr/lib/python3.5/site-packages/matplotlib/pyplot.py in plot(*args, **kwargs)
   3097         ax.hold(hold)
   3098     try:
-> 3099         ret = ax.plot(*args, **kwargs)
   3100         draw_if_interactive()
   3101     finally:

[…]

/usr/lib/python3.5/site-packages/matplotlib/axes/_base.py in _xy_from_xy(self, x, y)
    221         y = np.atleast_1d(y)
    222         if x.shape[0] != y.shape[0]:
--> 223             raise ValueError("x and y must have same first dimension")
    224         if x.ndim > 2 or y.ndim > 2:
    225             raise ValueError("x and y can be no greater than 2-D")

ValueError: x and y must have same first dimension
In [4]:
plot([1,2,3,4,5,6,7,8,9,10], [1,4,8,2,3,5,8,9,3,6,7])
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-4-ec77aff7bf8c> in <module>()
----> 1 plot([1,2,3,4,5,6,7,8,9,10], [1,4,8,2,3,5,8,9,3,6,7])

/usr/lib/python3.5/site-packages/matplotlib/pyplot.py in plot(*args, **kwargs)
   3097         ax.hold(hold)
   3098     try:
-> 3099         ret = ax.plot(*args, **kwargs)
   3100         draw_if_interactive()
   3101     finally:

/usr/lib/python3.5/site-packages/matplotlib/axes/_axes.py in plot(self, *args, **kwargs)
   1371         lines = []
   1372 
-> 1373         for line in self._get_lines(*args, **kwargs):
   1374             self.add_line(line)
   1375             lines.append(line)

/usr/lib/python3.5/site-packages/matplotlib/axes/_base.py in _grab_next_args(self, *args, **kwargs)
    302                 return
    303             if len(remaining) <= 3:
--> 304                 for seg in self._plot_args(remaining, kwargs):
    305                     yield seg
    306                 return

/usr/lib/python3.5/site-packages/matplotlib/axes/_base.py in _plot_args(self, tup, kwargs)
    280             x = np.arange(y.shape[0], dtype=float)
    281 
--> 282         x, y = self._xy_from_xy(x, y)
    283 
    284         if self.command == 'plot':

/usr/lib/python3.5/site-packages/matplotlib/axes/_base.py in _xy_from_xy(self, x, y)
    221         y = np.atleast_1d(y)
    222         if x.shape[0] != y.shape[0]:
--> 223             raise ValueError("x and y must have same first dimension")
    224         if x.ndim > 2 or y.ndim > 2:
    225             raise ValueError("x and y can be no greater than 2-D")

ValueError: x and y must have same first dimension

Debugging

Writing code means fixing errors. Lots of errors.

  • Don't be afraid to try new things and raise new errors.

  • We'll get lots of practice with debugging in this series!

Exiting IPython Notebook

  1. Go back to the terminal
    • You'll see a line like [I 13:38:34.691 NotebookApp] Kernel started: 26190742-9d29-420f-9f0f-5c8aae026a62
  2. Press Ctrl-C
    • It will ask you Shutdown this notebook server (y/[n])?
  3. Press y then Enter
  4. Close any windows in your browser

Getting back to the IPython Notebook

  1. Open the Terminal
  2. Type ipython notebook
  3. Press Enter
  4. A window should open in your browser!
    • If it does not, look for text like
      The IPython Notebook is running at: http://localhost:8888/
    • Open that link
    • Try Ctrl-Click if necessary.
  5. Look for the notebook from before, probably named Untitled.ipynb, and click on it

Thanks for coming!

  • Please fill out your feedback cards, and leave them by the door!
  • Come on Thursday for our first Open Session!
  • Try the practice problems, available on the website!