python big picture numpy
play

Python Big Picture numPy Some of these slides you will need to - PDF document

Python Big Picture numPy Some of these slides you will need to review on your Offers Matlab-ish capabilities within Python own primarily the python introductory slides. Fast array operations 2D Arrays, multi-D arrays, linear


  1. Python Big Picture numPy • Some of these slides you will need to review on your • Offers Matlab-ish capabilities within Python own – primarily the python introductory slides. • Fast array operations • 2D Arrays, multi-D arrays, linear algebra and more. • Tutorial: – http://www.scipy.org/Tentative_NumPy_Tutorial Maria Hybinette, UGA Maria Hybinette, UGA sciPy: Scientific Python pandas • Python “Spreadsheets” • Extends numPY • Pandas uses a DataFrame object which can be • Gathers a variety of high level science and thought of as a table of data engineering modules together: • Handles Time Series • stats: statistical functions • It was built by the finance sector to aid with data • spatial: KD-trees, nearest neighbors, distance manipulation and data analysis functions • It has loads of brilliant functions to dig into your data • interpolate: interpolation tools e.g. IDW, RBF • optimize: optimization algorithms including linear • It has useful functions for reading and writing to file programming types such as csv (excel, google sheet) Maria Hybinette, UGA Maria Hybinette, UGA Versions matplotlib #!/usr/bin/env python • Version History import numpy as np • plotting library to make import matplotlib.mlab as mlab – Python 0.9.0 (1991 first published version of code) graphs. import matplotlib.pyplot as plt – Python 1.x (1994 legacy) • easily customized and mu, sigma = 100, 15 x = mu + sigma*np.random.randn(10000) – Python 2.7.x (2000, list comprehensions, Haskell) produce publication # the histogram of the data – Python 3.2.x (3 branch started in 2008, remove redundancies in code, quality plots n, bins, patches = plt.hist(x, 50, normed=1, facecolor='green', only one “obvious” way to do it) alpha=0.75) • Using the Matplotlib, • Developing environments: # add a 'best fit' line NumPy and Pandas y = mlab.normpdf( bins, mu, sigma) – IDLE (basic) l = plt.plot(bins, y, 'r--', linewidth=1) libraries together make • coded in 100% pure Python, using the tkinter GUI toolkit data analysis much plt.xlabel('Smarts') • cross-platform: works on Windows and Unix plt.ylabel('Probability') easier and reproducible plt.title(r'$\mathrm{Histogram\ of\ IQ:}\ \mu=100,\ \sigma=15$') • Python shell window (a.k.a. interactive interpreter) plt.axis([40, 160, 0, 0.03]) than in Excel plt.grid(True) • debugger (not complete, but you can do the basics, set breakpoints, view and step) plt.show() – ipython, Spyder ( Anaconda ) – Eclipse module Maria Hybinette, UGA Maria Hybinette, UGA

  2. Installing Python IDLE Development Environment • Shell for interactive evaluation • Easy to get and install for Win/Mac from (2.7) • Text editor with color-coding and smart http://www.python.org indenting for creating python files. • Intro: Wikipedia's Python • Menu commands for changing system • We recommend Anaconda installation. settings and running files. • When you bumit http://hkn.eecs.berkeley.edu/~dyoo/python/idle_intro/index.html Maria Hybinette, UGA Maria Hybinette, UGA Interpreter: On my Mac IDLE: Working with a file.py • IDLE - 1. File -> new window 2. type commands in new window area 3. save as � file name � .py (typical extension) – if you don’t you don’t see ‘colors’ in IDLE – but programs still run. 4. Select “Run module” (from menu) • Type “python” to start interpreter • Type CTRL-D to exit the interpreter • Python evaluates all inputs dynamically Maria Hybinette, UGA Maria Hybinette, UGA Anaconda’s Spyder Editor Running Programs on UNIX • Debugger • #! /opt/sfw/bin/python • Help/Documentation easily accessible – (makes it runnable as an executable) {saffron:ingrid:1563} more filename.py #! /usr/local/bin/python print "hello world" print "here are the ten numbers from 0 to 9" for i in range(10): {saffron:ingrid:1562} python filename.py print i, hello world print "I'm done!" here are the ten numbers from 0 to 9 0 1 2 3 4 5 6 7 8 9 I'm done! {saffron:ingrid:1563} filename.py // what will happen? Maria Hybinette, UGA Maria Hybinette, UGA

  3. import firstprog Other IDE(s): Anaconda’s Spyder • Why we recommend Anaconda Python? – We could use Python IDLE … some code ahead is depicted using this interface ... BUT! Then • Anaconda…. – Already has many packages installed – Has a Script Editor and Console Window – Allows for efficient debugging – Breakpoints, using Console • Also has a Notebook feature • Other IDEs – PyCharm , iPython Notebook. (PyCharm is also a good alternative). Maria Hybinette, UGA Maria Hybinette, UGA Look at a sample of code… Anaconda Spyder (use your favorite development environment) script.txt x = 34 - 23 # A comment. y = “Hello” # Another one. z = 3.45 if z == 3.45 or y == “Hello”: x = x + 1 Script Editor Object Explorer y = y + � World � # String concat. print x Sequential Code - Execution print y Console Result of Running Code Colors Please Maria Hybinette, UGA Maria Hybinette, UGA Look at a sample of code… Enough to Understand the Code … and the perils copying and pasting § Assignment uses = and script.py x = 34 - 23 # A comment. § Comparison uses ==. # same gotcha as C++ (logic.py) y = � Hello � # Another one. § For numbers +-*/% are as expected. z = 3.45 if z == 3.45 or y == � Hello � : § Special use of + for string concatenation. x = x + 1 § Special use of % for string formatting. y = y + � World � # String concat. § Logical operators are words ( and, or, not ) print x not symbols (&&, ||, !). print y § The basic printing command is “ .” (more later) § % and .format() # old schools >>> § Fancier. 12 HelloWorld § First assignment to a variable will create it. Indentations § Variable types don’t need to be declared. Weird Characters (how to see) § Python figures out the variable types on its own (inference). in vi in command mode (bottom : mode) :set list Maria Hybinette, UGA Maria Hybinette, UGA https://docs.python.org/2/tutorial/inputoutput.html#

  4. Basic Datatypes • Review programming features of python • Integers (default for numbers) z = 5 / 2 # Answer is 2, integer division. – Slide 19-44. (on your own) • Floats – Understand python data structures, lists, dictionary etc. x = 3.456 • We will continue our review at: • Strings – for loop structure Slide 45. “More for Loops” Can use �� or �� to specify. � abc ���� abc � (Same thing.) Unmatched ones can occur within the string. � maria’s � Use triple double-quotes for multi-line strings or strings that contain both � and �� inside of them: ��� a � b � c ��� Maria Hybinette, UGA Maria Hybinette, UGA Whitespace Comments #, ”””, • Whitespace is meaningful in Python: • Start comments with # – the rest of line is ignored. especially indentation and placement of • A � documentation string � is often the first line of any newlines. new function or class that you define. – Use a newline to end a line of code. • The development environment, debugger, and other (Not a semicolon ; like in C++ or Java.) tools use it: it’s good style to include one. (Use \ when must go to next line prematurely.) def my_function(x, y): – No braces { } are needed to mark blocks of code in Python… Use consistent indentation instead. The first line with ��� This is the docstring. This function does blah blah blah. ��� a new indentation is considered outside of the block. # The code would go here... – Often a colon “:” appears at the start of a new block. (We’ll see this later for function and class definitions.) x = y + 1 return x Maria Hybinette, UGA Maria Hybinette, UGA Look at more sample of code… Python and Types x = 34 - 23 # A comment. Python determines the data types y = � Hello ������������� # Another one. in a program automatically at runtime. � Dynamic Typing � z = 3.45 if z == 3.45 or y == � Hello � : But Python is not casual about types, it enforces them after it figures them out. � Strong Typing � x = x + 1 y = y + � World ���� # String concat. So, for example, you can’t just append an integer to a string. You must first print x convert the integer to a string itself. print y x = � the answer is ��� # Decides x is string. y = 23 # Decides y is integer. print x + y # Python will complain about this. Maria Hybinette, UGA Maria Hybinette, UGA

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend