PROGRAMMING WITH CREEPY CRAWLIES: PYTHON, ANACONDA, AND SPYDER Brianna Hitt Department of Statistics

 
CONTINUE READING
PROGRAMMING WITH CREEPY CRAWLIES: PYTHON, ANACONDA, AND SPYDER Brianna Hitt Department of Statistics
PROGRAMMING WITH
CREEPY CRAWLIES:
PYTHON, ANACONDA, AND SPYDER

  Brianna Hitt

  Department of Statistics
  University of Nebraska-Lincoln

  April 21, 2015
PROGRAMMING WITH CREEPY CRAWLIES: PYTHON, ANACONDA, AND SPYDER Brianna Hitt Department of Statistics
 Introduction

     About
     Downloads
 Python   Basics
 Advanced    Python
     Functions
     Examples
     Interacting with other
      languages
PROGRAMMING WITH CREEPY CRAWLIES: PYTHON, ANACONDA, AND SPYDER Brianna Hitt Department of Statistics
INTRODUCTION
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)
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
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
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
WHY PYTHON?
Pros                       Cons

 Used by Google,           Interpreted vs.
  YouTube, and               compiled  slower
  Instagram                 Rare in mobile
 Easy to read & write       computing so far
 Object-oriented like R    Design issues

 Better than Java              Errors appear at run
                                 time
 Free and Open Source
                                Use of one thread only
                                Indentation rather
                                 than brackets, etc.
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
PYTHON BASICS
JUST PYTHON
   Type commands in IDLE or the command line

   Type commands after a prompt
     >>>
     …

   Comments
       # - start with a hashtag
PYTHON SHELL (IDLE)
 Open the file hello.py
 Run  Run Module (F5)

>>> ================================ RESTART
  ================================
>>>
Hello world!

   You’ve just run your first Python script!
ANACONDA
SPYDER

                 Object
                 Inspector

   Text Editor

                             IPython Console
QUIRKS IN PYTHON 2
 Cannot      understand non-English characters
        and 1.5 + 2 = 3
    3/4 = 0
 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
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
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
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
CONTROL FLOW EXAMPLE
    Open basic.py in Spyder
>>> sum10, a = 0, 1
>>> while a >> sum10
55
>>> print('The value of the sum is', sum10)
The value of the sum is 55
MORE
ADVANCED
PYTHON
FUNCTIONS
   Creating a function
       def (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
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)
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
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 >> sum10 = sum2(10)
>>> sum10
([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 55)
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
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
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( )
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')
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()
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
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_su
  bset.csv')
DESCRIPTIVE STATISTICS EXAMPLE
   Descriptive statistics
csv_data.describe()

Out[259]:
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
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
INTERACTION WITH OTHER LANGUAGES
   Read sas7bdat files with Python
     https://pypi.python.org/pypi/sas7bdat
     Also converts to .csv files

   MATLAB Engine for Python
     http://www.mathworks.com/help/matlab/matlab-
      engine-for-python.html
     Need MATLAB installed on your computer
     Package calls MATLAB from Python
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
You can also read