PROGRAMMING CREEPY CRAWLIES - Chris Bilder · PROGRAMMING WITH CREEPY CRAWLIES: PYTHON, ANACONDA,...

36
PROGRAMMING WITH CREEPY CRAWLIES: PYTHON, ANACONDA, AND SPYDER Brianna Hitt Department of Statistics University of Nebraska-Lincoln April 21, 2015

Transcript of PROGRAMMING CREEPY CRAWLIES - Chris Bilder · PROGRAMMING WITH CREEPY CRAWLIES: PYTHON, ANACONDA,...

Page 1: PROGRAMMING CREEPY CRAWLIES - Chris Bilder · PROGRAMMING WITH CREEPY CRAWLIES: PYTHON, ANACONDA, AND SPYDER Brianna Hitt Department of Statistics University of Nebraska-Lincoln April

PROGRAMMING WITH

CREEPY CRAWLIES: PYTHON, ANACONDA, AND SPYDER

Brianna Hitt

Department of Statistics

University of Nebraska-Lincoln

April 21, 2015

Page 2: PROGRAMMING CREEPY CRAWLIES - Chris Bilder · PROGRAMMING WITH CREEPY CRAWLIES: PYTHON, ANACONDA, AND SPYDER Brianna Hitt Department of Statistics University of Nebraska-Lincoln April

Introduction

About

Downloads

Python Basics

Advanced Python

Functions

Examples

Interacting with other

languages

Page 3: PROGRAMMING CREEPY CRAWLIES - Chris Bilder · PROGRAMMING WITH CREEPY CRAWLIES: PYTHON, ANACONDA, AND SPYDER Brianna Hitt Department of Statistics University of Nebraska-Lincoln April

INTRODUCTION

Page 4: PROGRAMMING CREEPY CRAWLIES - Chris Bilder · PROGRAMMING WITH CREEPY CRAWLIES: PYTHON, ANACONDA, AND SPYDER Brianna Hitt Department of Statistics University of Nebraska-Lincoln April

ABOUT PYTHON

Named after “Monty Python’s Flying Circus”

Free and Open Source

Object-oriented

Includes:

IDLE (Python shell, GUI)

Python Command Line

Other implementations:

IronPython (running on .NET)

Jython (running on Java)

PyPy (fast, includes JIT compiler)

Page 5: PROGRAMMING CREEPY CRAWLIES - Chris Bilder · PROGRAMMING WITH CREEPY CRAWLIES: PYTHON, ANACONDA, AND SPYDER Brianna Hitt Department of Statistics University of Nebraska-Lincoln April

ABOUT ANACONDA

Re-packaged Python

Tons of packages for math and data analysis

Free from Continuum Analytics

Allows switching between Python versions

Other re-packagings:

Winpython (portable, for Windows)

Portable Python (for portable devices, add-ons)

Many more

Page 6: PROGRAMMING CREEPY CRAWLIES - Chris Bilder · PROGRAMMING WITH CREEPY CRAWLIES: PYTHON, ANACONDA, AND SPYDER Brianna Hitt Department of Statistics University of Nebraska-Lincoln April

MORE ABOUT ANACONDA

Includes:

Anaconda Command Prompt

IPython

Interactive Python shell

IPython Notebook

Web-based, single document

IPython QtConsole

Application framework

Spyder

IDE for Python, 3.8/5

Wakari

Data analytics platform

Page 7: PROGRAMMING CREEPY CRAWLIES - Chris Bilder · PROGRAMMING WITH CREEPY CRAWLIES: PYTHON, ANACONDA, AND SPYDER Brianna Hitt Department of Statistics University of Nebraska-Lincoln April

ABOUT SPYDER

Scientific Python Development EnviRonment

Rstudio for Python

Color-coded editing

Interactive testing of code

Built-in debugging

Plotting features

Uses IPython as default command line

Other IDEs:

Interactive Editor for Python (IEP)

PyDev

Enthought Canopy

Page 8: PROGRAMMING CREEPY CRAWLIES - Chris Bilder · PROGRAMMING WITH CREEPY CRAWLIES: PYTHON, ANACONDA, AND SPYDER Brianna Hitt Department of Statistics University of Nebraska-Lincoln April

WHY PYTHON?

Used by Google,

YouTube, and

Instagram

Easy to read & write

Object-oriented like R

Better than Java

Free and Open Source

Interpreted vs.

compiled slower

Rare in mobile

computing so far

Design issues

Errors appear at run

time

Use of one thread only

Indentation rather

than brackets, etc.

Pros Cons

Page 9: PROGRAMMING CREEPY CRAWLIES - Chris Bilder · PROGRAMMING WITH CREEPY CRAWLIES: PYTHON, ANACONDA, AND SPYDER Brianna Hitt Department of Statistics University of Nebraska-Lincoln April

DOWNLOADS

Download Python from

https://www.python.org/downloads/

Choose Python 3.4.3

Run the .msi file

Download Anaconda from

http://continuum.io/downloads#py34

Choose Python 3.4

Download Windows 64-Bit Python 3.4 Graphical

Installer

Run the .exe file

Page 10: PROGRAMMING CREEPY CRAWLIES - Chris Bilder · PROGRAMMING WITH CREEPY CRAWLIES: PYTHON, ANACONDA, AND SPYDER Brianna Hitt Department of Statistics University of Nebraska-Lincoln April

PYTHON BASICS

Page 11: PROGRAMMING CREEPY CRAWLIES - Chris Bilder · PROGRAMMING WITH CREEPY CRAWLIES: PYTHON, ANACONDA, AND SPYDER Brianna Hitt Department of Statistics University of Nebraska-Lincoln April

JUST PYTHON

Type commands in IDLE or the command line

Type commands after a prompt

>>>

Comments

# - start with a hashtag

Page 12: PROGRAMMING CREEPY CRAWLIES - Chris Bilder · PROGRAMMING WITH CREEPY CRAWLIES: PYTHON, ANACONDA, AND SPYDER Brianna Hitt Department of Statistics University of Nebraska-Lincoln April

PYTHON SHELL (IDLE)

Open the file hello.py

Run Run Module (F5)

>>> ================================ RESTART

================================

>>>

Hello world!

You’ve just run your first Python script!

Page 13: PROGRAMMING CREEPY CRAWLIES - Chris Bilder · PROGRAMMING WITH CREEPY CRAWLIES: PYTHON, ANACONDA, AND SPYDER Brianna Hitt Department of Statistics University of Nebraska-Lincoln April

ANACONDA

Page 14: PROGRAMMING CREEPY CRAWLIES - Chris Bilder · PROGRAMMING WITH CREEPY CRAWLIES: PYTHON, ANACONDA, AND SPYDER Brianna Hitt Department of Statistics University of Nebraska-Lincoln April

SPYDER

Text Editor

IPython Console

Object

Inspector

Page 15: PROGRAMMING CREEPY CRAWLIES - Chris Bilder · PROGRAMMING WITH CREEPY CRAWLIES: PYTHON, ANACONDA, AND SPYDER Brianna Hitt Department of Statistics University of Nebraska-Lincoln April

QUIRKS IN PYTHON 2

Cannot understand non-English characters

3/4 = 0 and 1.5 + 2 = 3

Cannot interpret numbers with leading zeros >>> 0345

SyntaxError: invalid token

>>> 0o345

229

Two different kinds of integer

10 and 10L

Print statement behaved weirdly

Most problems solved with Python 3

Page 16: PROGRAMMING CREEPY CRAWLIES - Chris Bilder · PROGRAMMING WITH CREEPY CRAWLIES: PYTHON, ANACONDA, AND SPYDER Brianna Hitt Department of Statistics University of Nebraska-Lincoln April

DATA TYPES

Numbers

Basic operations (+ - * / **)

Integer, floating point, decimal, fraction, complex

Use equal sign to create variables (case-sensitive)

Strings

Enclosed in single or double quotes

Triple quotes for strings spanning multiple lines

Combine, repeat, index, and slice

Lists

Index, slice, concatenate

Can change, remove, or add components

Can nest

Page 17: PROGRAMMING CREEPY CRAWLIES - Chris Bilder · PROGRAMMING WITH CREEPY CRAWLIES: PYTHON, ANACONDA, AND SPYDER Brianna Hitt Department of Statistics University of Nebraska-Lincoln April

PYTHON AS A CALCULATOR

Open basics.py in Spyder >>> 2.3 + 3.6

5.9

>>> 49 - 2*8

33

>>> 12 / 7

1.7142857142857142

>>> 5 ** 6

15625

>>> a = 5

>>> b = 7

>>> a * b

35

>>> a + _

40

Page 18: PROGRAMMING CREEPY CRAWLIES - Chris Bilder · PROGRAMMING WITH CREEPY CRAWLIES: PYTHON, ANACONDA, AND SPYDER Brianna Hitt Department of Statistics University of Nebraska-Lincoln April

CONTROL FLOW STATEMENTS

if, while, for loops

Body of the loop is indented

No semicolons or brackets

if

elif and else

for

Iterates over lists and strings

break, continue, and try statements for flow

Page 19: PROGRAMMING CREEPY CRAWLIES - Chris Bilder · PROGRAMMING WITH CREEPY CRAWLIES: PYTHON, ANACONDA, AND SPYDER Brianna Hitt Department of Statistics University of Nebraska-Lincoln April

CONTROL FLOW EXAMPLE

Open basic.py in Spyder >>> sum10, a = 0, 1

>>> while a <= 10:

print(a)

sum10 = sum10 + a

a = a + 1

1

<Output edited>

10

>>> sum10

55

>>> print('The value of the sum is', sum10)

The value of the sum is 55

Page 20: PROGRAMMING CREEPY CRAWLIES - Chris Bilder · PROGRAMMING WITH CREEPY CRAWLIES: PYTHON, ANACONDA, AND SPYDER Brianna Hitt Department of Statistics University of Nebraska-Lincoln April

MORE

ADVANCED

PYTHON

Page 21: PROGRAMMING CREEPY CRAWLIES - Chris Bilder · PROGRAMMING WITH CREEPY CRAWLIES: PYTHON, ANACONDA, AND SPYDER Brianna Hitt Department of Statistics University of Nebraska-Lincoln April

FUNCTIONS

Creating a function

def <function_name>(parameters):

“””String literal is enclosed by triple quotes””””

Body of the function is indented

First statement of a function is a docstring

Good practice

First line is complete sentence

Following paragraphs describe function

Page 22: PROGRAMMING CREEPY CRAWLIES - Chris Bilder · PROGRAMMING WITH CREEPY CRAWLIES: PYTHON, ANACONDA, AND SPYDER Brianna Hitt Department of Statistics University of Nebraska-Lincoln April

FUNCTIONS

Value of a function name has a type

Can be assigned to other names

methods

Function belonging to an object

obj.methodname

Can set default arguments (just like in R) def function(x, n = 20, a = ‘Hello’):

Can use keyword arguments (just like in R) function(2)

function(x = 2)

Page 23: PROGRAMMING CREEPY CRAWLIES - Chris Bilder · PROGRAMMING WITH CREEPY CRAWLIES: PYTHON, ANACONDA, AND SPYDER Brianna Hitt Department of Statistics University of Nebraska-Lincoln April

FUNCTION EXAMPLES

>>> def hello():

"""Prints 'Hello world!'\n """

print('Hello world!')

>>> hello()

Hello world!

The docstring tells us what the function does Appears in the console when using the function

Page 24: PROGRAMMING CREEPY CRAWLIES - Chris Bilder · PROGRAMMING WITH CREEPY CRAWLIES: PYTHON, ANACONDA, AND SPYDER Brianna Hitt Department of Statistics University of Nebraska-Lincoln April

FUNCTION EXAMPLES

>>> def sum2(n): #sum is a built-in function

"""Return the sum of the first n integers."""

sum_new, a = 0, 1

num_list = []

while a <= n:

num_list.append(a)

sum_new = sum_new + a

a = a + 1

return (num_list, sum_new)

>>> sum10 = sum2(10)

>>> sum10

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

Page 25: PROGRAMMING CREEPY CRAWLIES - Chris Bilder · PROGRAMMING WITH CREEPY CRAWLIES: PYTHON, ANACONDA, AND SPYDER Brianna Hitt Department of Statistics University of Nebraska-Lincoln April

MODULES

File containing functions

Module summation.py containing sum2 function

May need to change file path

See code in module.py

import statement loads the module

module_name.function_name(arguments)

>>> import summation #load the module you created

>>> from summation import * #import all names from module

Page 26: PROGRAMMING CREEPY CRAWLIES - Chris Bilder · PROGRAMMING WITH CREEPY CRAWLIES: PYTHON, ANACONDA, AND SPYDER Brianna Hitt Department of Statistics University of Nebraska-Lincoln April

MODULES

Can assign local names >>> summ = summation.sum2 #assign a local name

Can also directly import functions >>> from summation import sum2 #import names from module

dir( ) function returns names defined by a module

No argument all names currently defined

>>> dir() #lists names you have defined

>>> dir(summation) #lists names the module defines

Page 27: PROGRAMMING CREEPY CRAWLIES - Chris Bilder · PROGRAMMING WITH CREEPY CRAWLIES: PYTHON, ANACONDA, AND SPYDER Brianna Hitt Department of Statistics University of Nebraska-Lincoln April

READING & WRITING FILES

open( ) function returns a file object

Two arguments: filename and mode

Modes: ‘r’ = read, ‘w’ = write, ‘r+’ = read and

write

Default is ‘r’

Read the function using .read( ) or .readlines( )

Write to the function using .write( )

Must be strings

Make sure to close the file with .close( )

Page 28: PROGRAMMING CREEPY CRAWLIES - Chris Bilder · PROGRAMMING WITH CREEPY CRAWLIES: PYTHON, ANACONDA, AND SPYDER Brianna Hitt Department of Statistics University of Nebraska-Lincoln April

READING AND WRITING FILES EXAMPLE

Open files.py in Spyder f = open('C:\\Users\\bkallman2\\Desktop\\File.txt', 'r+')

f = open('C:\\Users\\bkallman2\\Desktop\\File.txt', 'r+')

for line in f:

print(line, end='')

This file will be used in Python.

This is the third line of this file.

This is the end of the file.

f.write('This is an additional line.\n')

Page 29: PROGRAMMING CREEPY CRAWLIES - Chris Bilder · PROGRAMMING WITH CREEPY CRAWLIES: PYTHON, ANACONDA, AND SPYDER Brianna Hitt Department of Statistics University of Nebraska-Lincoln April

READING AND WRITING FILES EXAMPLE

value = ('My favorite number is', 12)

s = str(value)

f.write(s)

Out[3]: 29

Read in the file again

This file will be used in Python.

This is the third line of this file.

This is the end of the file.This is an additional line.

('My favorite number is', 12)

f.close()

Page 30: PROGRAMMING CREEPY CRAWLIES - Chris Bilder · PROGRAMMING WITH CREEPY CRAWLIES: PYTHON, ANACONDA, AND SPYDER Brianna Hitt Department of Statistics University of Nebraska-Lincoln April

DESCRIPTIVE STATISTICS EXAMPLE

Open statistics.py in Spyder

Read in the wind_speed.csv file from HW 5

from pandas import read_csv

from pandas import read_excel

csv_data = read_csv('C:\\Users\\bkallman2\\Desktop\\STAT 992\\Assignments\\Assignment 5\\wind_speed.csv')

csv_data[:2]

Out[255]:

Year Day x

0 2004 1 9.4

1 2004 2 12.7

Page 31: PROGRAMMING CREEPY CRAWLIES - Chris Bilder · PROGRAMMING WITH CREEPY CRAWLIES: PYTHON, ANACONDA, AND SPYDER Brianna Hitt Department of Statistics University of Nebraska-Lincoln April

DESCRIPTIVE STATISTICS EXAMPLE

Create a subset and write a new .csv file

subset = csv_data[:10]

subset

subset.to_csv('C:\\Users\\bkallman2\\Desktop\\wind_speed_subset.csv')

Page 32: PROGRAMMING CREEPY CRAWLIES - Chris Bilder · PROGRAMMING WITH CREEPY CRAWLIES: PYTHON, ANACONDA, AND SPYDER Brianna Hitt Department of Statistics University of Nebraska-Lincoln April

DESCRIPTIVE STATISTICS EXAMPLE

Descriptive statistics csv_data.describe()

Out[259]:

Page 33: PROGRAMMING CREEPY CRAWLIES - Chris Bilder · PROGRAMMING WITH CREEPY CRAWLIES: PYTHON, ANACONDA, AND SPYDER Brianna Hitt Department of Statistics University of Nebraska-Lincoln April

SIMPLE LINEAR REGRESSION EXAMPLE

import scipy.stats as stats

import numpy.random as rnd

rnd.seed(1002)

x = rnd.randn(100)

y = x + rnd.randn(100)

slope, intercept, rvalue, pvalue, stderr = stats.linregress(x,y)

rsquare = rvalue**2

print(slope, rsquare, intercept, pvalue)

1.00248266595 0.487257910196 0.53739092909 6.9491397096e-16

Page 34: PROGRAMMING CREEPY CRAWLIES - Chris Bilder · PROGRAMMING WITH CREEPY CRAWLIES: PYTHON, ANACONDA, AND SPYDER Brianna Hitt Department of Statistics University of Nebraska-Lincoln April

INTERACTION WITH R

Packages:

Rpy2

PypeR

pyRserve

Rpy2 can be downloaded at

http://www.lfd.uci.edu/~gohlke/pythonlibs/#rpy2

rPython package in R

Not built for R 3.1.1

Import or export data

Call Python code, functions, methods

Page 36: PROGRAMMING CREEPY CRAWLIES - Chris Bilder · PROGRAMMING WITH CREEPY CRAWLIES: PYTHON, ANACONDA, AND SPYDER Brianna Hitt Department of Statistics University of Nebraska-Lincoln April

REFERENCES

Python: https://www.python.org/

Monty Python Graphic: http://theredlist.com/wiki-2-17-1483-1492-1494-view-comedy-

romance-10-profile-monty-python-s-flying-circus.html

History: https://docs.python.org/3/license.html

Pros and Cons: http://www.infoworld.com/article/2887974/application-development/a-

developer-s-guide-to-the-pro-s-and-con-s-of-python.html

Anaconda: https://store.continuum.io/cshop/anaconda/

Spyder: https://pythonhosted.org/spyder/

IDE Reviews: http://xcorr.net/2013/04/17/evaluating-ides-for-scientific-python/

Python Tutorial: https://docs.python.org/3/tutorial/

Fast Lane to Python:

http://heather.cs.ucdavis.edu/~matloff/Python/PLN/FastLanePython.pdf

Using R with Python: https://sites.google.com/site/aslugsguidetopython/data-

analysis/pandas/calling-r-from-python

Statistics in Python:

https://www.kevinsheppard.com/images/0/09/Python_introduction.pdf