Math 15 Introduction to Scientific Data Analysis Lecture 10 Python Programming – Part 4 University...
-
Upload
matilda-banks -
Category
Documents
-
view
214 -
download
1
Transcript of Math 15 Introduction to Scientific Data Analysis Lecture 10 Python Programming – Part 4 University...
![Page 1: Math 15 Introduction to Scientific Data Analysis Lecture 10 Python Programming – Part 4 University of California, Merced Today – We have A Quiz!](https://reader030.fdocuments.in/reader030/viewer/2022032605/56649e7e5503460f94b82695/html5/thumbnails/1.jpg)
Math 15Introduction to Scientific Data Analysis
Lecture 10Python Programming – Part 4
University of California, Merced
Today – We have A Quiz!
![Page 2: Math 15 Introduction to Scientific Data Analysis Lecture 10 Python Programming – Part 4 University of California, Merced Today – We have A Quiz!](https://reader030.fdocuments.in/reader030/viewer/2022032605/56649e7e5503460f94b82695/html5/thumbnails/2.jpg)
UC Merced 2
Schedule
Final ExaminationDecember ?Final
Project #2Python Programming - #6December 515
Python Programming – #5November 2814
Quiz #6Python Programming – #4November 2113
No LectureNovember 1412
Quiz #5Python Programming – #2November 711
Python Programming – #1October 3110
Quiz #4Programming – Introduction to Python Part - IIOctober 249
Project #1Programming – Introduction to Python Part - IOctober 178
Quiz #3Excel #6 – Interactive ProgrammingOctober 107
Excel #5 – Data AnalysisOctober 36
Quiz #2Excel #4 – Regression AnalysisSeptember 265
Excel #3 – Statistical AnalysisSeptember 194
Quiz #1Excel #2 – Plotting Graphs/ChartsSeptember 123
Excel #1 – General TechniquesSeptember 52
Introduction to the data analysisAugust 291
Project DueConceptsDateWeek
Final ExaminationDecember ?Final
Project #2Python Programming - #6December 515
Python Programming – #5November 2814
Quiz #6Python Programming – #4November 2113
No LectureNovember 1412
Quiz #5Python Programming – #2November 711
Python Programming – #1October 3110
Quiz #4Programming – Introduction to Python Part - IIOctober 249
Project #1Programming – Introduction to Python Part - IOctober 178
Quiz #3Excel #6 – Interactive ProgrammingOctober 107
Excel #5 – Data AnalysisOctober 36
Quiz #2Excel #4 – Regression AnalysisSeptember 265
Excel #3 – Statistical AnalysisSeptember 194
Quiz #1Excel #2 – Plotting Graphs/ChartsSeptember 123
Excel #1 – General TechniquesSeptember 52
Introduction to the data analysisAugust 291
Project DueConceptsDateWeek
December 176-9pm
Quiz #6
But, there will be a lab!
Masa will be in Reno next week!
![Page 3: Math 15 Introduction to Scientific Data Analysis Lecture 10 Python Programming – Part 4 University of California, Merced Today – We have A Quiz!](https://reader030.fdocuments.in/reader030/viewer/2022032605/56649e7e5503460f94b82695/html5/thumbnails/3.jpg)
UC Merced 3
Math 15 Now till the end of Fall semester
Four more Labs
One more Assignment ( HW #6 due November 18)
Total of 6 assignments
Two more Quizzes (Today-20pts and Nov. 28-30pts) Total of 6 quizzes
Project #2 (due December 7) – Available @ November 19th
Final Exam – December 17th
![Page 4: Math 15 Introduction to Scientific Data Analysis Lecture 10 Python Programming – Part 4 University of California, Merced Today – We have A Quiz!](https://reader030.fdocuments.in/reader030/viewer/2022032605/56649e7e5503460f94b82695/html5/thumbnails/4.jpg)
UC Merced 4
Grading for Math 15
Activity Points % Final Grade
Assignments 100 20%
In-Class Quizzes 100 20%
Computer Labs 60 12%
Project #1 70 14%
Project #2 70 14%
Final Exam 100 20%
Total 500 100%
Activity Points
Assignments 110
In-Class Quizzes 130
Computer Labs 60
Project #1 70
Project #2 70
Final project 100
Total 540
Projected points
Grade Total points achieved
A Over 425
B Over 375
C Over 325
D Over 275
![Page 5: Math 15 Introduction to Scientific Data Analysis Lecture 10 Python Programming – Part 4 University of California, Merced Today – We have A Quiz!](https://reader030.fdocuments.in/reader030/viewer/2022032605/56649e7e5503460f94b82695/html5/thumbnails/5.jpg)
UC Merced 5
Bonus or Extra Points
So far Lab/homework/project – 15 points Seminar Report – 50 points (Done) Halloween Bonus – 10 points (Done)
Near future Maybe …
![Page 6: Math 15 Introduction to Scientific Data Analysis Lecture 10 Python Programming – Part 4 University of California, Merced Today – We have A Quiz!](https://reader030.fdocuments.in/reader030/viewer/2022032605/56649e7e5503460f94b82695/html5/thumbnails/6.jpg)
UC Merced 6
Any Questions?
![Page 7: Math 15 Introduction to Scientific Data Analysis Lecture 10 Python Programming – Part 4 University of California, Merced Today – We have A Quiz!](https://reader030.fdocuments.in/reader030/viewer/2022032605/56649e7e5503460f94b82695/html5/thumbnails/7.jpg)
UC Merced 7
After Three weeks of Python Programming experience, what did you think?
python
![Page 8: Math 15 Introduction to Scientific Data Analysis Lecture 10 Python Programming – Part 4 University of California, Merced Today – We have A Quiz!](https://reader030.fdocuments.in/reader030/viewer/2022032605/56649e7e5503460f94b82695/html5/thumbnails/8.jpg)
UC Merced 8
Don’t make Python bother you.Don’t be scared!Python is just a Tool!
![Page 9: Math 15 Introduction to Scientific Data Analysis Lecture 10 Python Programming – Part 4 University of California, Merced Today – We have A Quiz!](https://reader030.fdocuments.in/reader030/viewer/2022032605/56649e7e5503460f94b82695/html5/thumbnails/9.jpg)
UC Merced 9
This Week
More Programming
Review New materials for Next Week’s Lab
Dictionaries Input from a file / Output to a file Defining function
Homework #6 is due November 18th!
Next Week’s Lab (#9) is available at the UCMCROPS.
![Page 10: Math 15 Introduction to Scientific Data Analysis Lecture 10 Python Programming – Part 4 University of California, Merced Today – We have A Quiz!](https://reader030.fdocuments.in/reader030/viewer/2022032605/56649e7e5503460f94b82695/html5/thumbnails/10.jpg)
UC Merced 10
Quick Reminder – Part 1
List
UC = [“UCLA”, “UC Davis”, “UC Merced”, “UC Berkeley”, “UCSF”, “UCI”]
# of elements in UC = 6
>>> print UC[1]UC Davis>>> print UC[6]Error!
Python – Index of List starts at 0
![Page 11: Math 15 Introduction to Scientific Data Analysis Lecture 10 Python Programming – Part 4 University of California, Merced Today – We have A Quiz!](https://reader030.fdocuments.in/reader030/viewer/2022032605/56649e7e5503460f94b82695/html5/thumbnails/11.jpg)
UC Merced 11
Quick Reminder – Part 2
range() function range(n) : a list of integers from 0 to up to n, but not
including n.
Concept of “Counter”
>>> range(10)[0,1,2,3,4,5,6,7,8,9]
N = 15icount = 0 # Initializing a variablefor var in range(1,N,2): icount = icount + 1 print var, icount
>>> 1 13 25 37 49 511 613 7
![Page 12: Math 15 Introduction to Scientific Data Analysis Lecture 10 Python Programming – Part 4 University of California, Merced Today – We have A Quiz!](https://reader030.fdocuments.in/reader030/viewer/2022032605/56649e7e5503460f94b82695/html5/thumbnails/12.jpg)
UC Merced 12
Dictionary Mutable (or changeable) table of object
references, accessed by keys.
Is like an address-book where you can find the address or contact details of a person by knowing only his/her name i.e. we associate keys (name) with corresponding
values (details).
Note that the key must be unique to have associated values. just like you cannot find the correct information if
you have two persons with the exact same name.
![Page 13: Math 15 Introduction to Scientific Data Analysis Lecture 10 Python Programming – Part 4 University of California, Merced Today – We have A Quiz!](https://reader030.fdocuments.in/reader030/viewer/2022032605/56649e7e5503460f94b82695/html5/thumbnails/13.jpg)
UC Merced 13
Dictionary – cont.
In python, dictionary entries are enclosed in braces {}. Here is the general syntax:
Name_of_dictionary = { key1 : value1, key2 : value2 }
UC = { 'Bruins' : ‘UCLA', ‘Bobcats' : ‘UC Merced',
'Highlanders' : ‘UC Riverside', ‘Anteaters' : ‘UCI' ‘Banana Slugs’: ‘UC Santa Cruz’}
>>> UC[‘Highlanders’]UC Riverside
![Page 14: Math 15 Introduction to Scientific Data Analysis Lecture 10 Python Programming – Part 4 University of California, Merced Today – We have A Quiz!](https://reader030.fdocuments.in/reader030/viewer/2022032605/56649e7e5503460f94b82695/html5/thumbnails/14.jpg)
UC Merced 14
Example: DNA Sequence Analysis
Objectives: Convert a DNA sequence to a protein sequence by using the
translation table.
Then, use the resulting sequence to identify your protein and its function.
ATGAGGAAAATGCTGACCGCTGTGCTGTCTCACGTATTTTCGGGAATGGTCCAAAAGCCAGCTCTCAGAGGACTGCTGTCATCTCTGAAGTTCTCCAACGACGCCACCTGTGACATTAAGAAATGTGACCTGTACCGGCTGGAGGAGGGCCCACCGACCTCCACCGTGCTCACCCGAGCCGAGGCCCTCAAGTACTACCGGACCATGCAGGTAATTCGGCGCATGGAGTTGAAGGCCGACCAGCTGTATAAGCAGAAATTCATCCGTGGTTTCTGTCACCTGTGTGATGGGCAGGAAGCCTGCTGCGTGGGGCTGGAGGCAGGGATAAATCCCACGGATCACGTCATCACGTCCTACCGGGCTCATGGCTTCTGCTACACGCGAGGACTGTCCGTGAAGTCCATTCTCGCCGAGCTGACTGGACGCAAAGGAGGCTGTGCTAAAGGCAAGGGAGGCTCCATGCACATGTACGGCAAGAACTTCTACGGTGGCAATGGCATTGTTGGGGCCCAGGTACCCCTGGGAGCTGGTGTGGCTTTTGCCTGTAAATACCTGAAGAATGGTCAGGTCTGCTTGGCTTTGTACGGCGATGGTGCGGCTAACCAAGGGCAGGTATTCGAAGCATACAATATGTCAGCCTTGTGGAAATTACCCTGTGTTTTCATCTGTGAGAATAACCTCTATGGAATGGGAACCTCCAACGAGAGATCAGCAGCCAGTACTGATTACCACAAGAAAGGTTTTATTATCCCCGGACTGAGGGTGAATGGGATGGATATTCTCTGTGTTCGGGAGGCAACCAAGTTTGCAGCTGATCACTGCAGATCTGGAAAGGGGCCCATTGTGATGGAGCTGCAGACCTACCGTTATCATGGACACAGTATGAGCGACCCAGGGATCAGTTATCGTTCACGAGAAGAAGTTCATAACGTGAGAAGTAAGAGTGATCCTATAATGCTGCTCCGAGAGAGAATTATCAGCAACAACCTCAGCAATATTGAAGAATTGAAAGAAATTGATGCAGATGTGAAGAAAGAGGTGGAGGACGCAGCTCAGTTTGCTACGACTGATCCAGAACCAGCTGTGGAAGATATAGCCAATTACCTCTACCACCAAGATCCACCTTTTGAAGTCCGTGGTGCACATAAGTGGCTCAAGTATAAGTCCCACAGTTAG
![Page 15: Math 15 Introduction to Scientific Data Analysis Lecture 10 Python Programming – Part 4 University of California, Merced Today – We have A Quiz!](https://reader030.fdocuments.in/reader030/viewer/2022032605/56649e7e5503460f94b82695/html5/thumbnails/15.jpg)
UC Merced 15
How to convert? The modern genetic code uses triplets
of letters to form the words or codons of the genetic language.
Table 1. Amino Acid Codon Table Second Base
First Base T C A G Third Base
T Phe (F) Phe (F) Leu (L) Leu (L)
Ser (S) Ser (S) Ser (S) Ser (S)
Tyr (Y) Tyr (Y)
Stop Stop
Cys (C) Cys (C)
Stop Trp (T)
T C A G
C Leu (L) Leu (L) Leu (L) Leu (L)
Pro (P) Pro (P) Pro (P) Pro (P)
His (H) His (H) Gln (Q) Gln (Q)
Arg (R) Arg (R) Arg (R) Arg (R)
T C A G
A Ile (I) Ile (I) Ile (I)
Met (M)
Thr (T) Thr (T) Thr (T) Thr (T)
Asn (N) Asn (N) Lys (K) Lys (K)
Ser (S) Ser (S) Arg (R) Arg (R)
T C A G
G Val (V) Val (V) Val (V) Val (V)
Ala (A) Ala (A) Ala (A) Ala (A)
Asp (D) Asp (D) Glu (E) Glu (E)
Gly (G) Gly (G) Gly (G) Gly (G)
T C A G
![Page 16: Math 15 Introduction to Scientific Data Analysis Lecture 10 Python Programming – Part 4 University of California, Merced Today – We have A Quiz!](https://reader030.fdocuments.in/reader030/viewer/2022032605/56649e7e5503460f94b82695/html5/thumbnails/16.jpg)
UC Merced 16
Create the simple Python program to do the job!
First: Convert a codon table into Python’s dictionary
Code = {'TTT': 'F', 'TCT': 'S', 'TAT': 'Y', 'TGT': 'C', 'TTC': 'F', 'TCC': 'S', 'TAC': 'Y', 'TGC': 'C', 'TTA': 'L', 'TCA': 'S', 'TAA': '*', 'TGA': 'W', 'TTG': 'L', 'TCG': 'S', 'TAG': '*', 'TGG': 'W', 'CTT': 'L', 'CCT': 'P', 'CAT': 'H', 'CGT': 'R', 'CTC': 'L', 'CCC': 'P', 'CAC': 'H', 'CGC': 'R', 'CTA': 'L', 'CCA': 'P', 'CAA': 'Q', 'CGA': 'R', 'CTG': 'L', 'CCG': 'P', 'CAG': 'Q', 'CGG': 'R', 'ATT': 'I', 'ACT': 'T', 'AAT': 'N', 'AGT': 'S', 'ATC': 'I', 'ACC': 'T', 'AAC': 'N', 'AGC': 'S', 'ATA': 'M', 'ACA': 'T', 'AAA': 'K', 'AGA': 'R', 'ATG': 'M', 'ACG': 'T', 'AAG': 'K', 'AGG': 'R', 'GTT': 'V', 'GCT': 'A', 'GAT': 'D', 'GGT': 'G', 'GTC': 'V', 'GCC': 'A', 'GAC': 'D', 'GGC': 'G', 'GTA': 'V', 'GCA': 'A', 'GAA': 'E', 'GGA': 'G', 'GTG': 'V', 'GCG': 'A', 'GAG': 'E', 'GGG': 'G'}
![Page 17: Math 15 Introduction to Scientific Data Analysis Lecture 10 Python Programming – Part 4 University of California, Merced Today – We have A Quiz!](https://reader030.fdocuments.in/reader030/viewer/2022032605/56649e7e5503460f94b82695/html5/thumbnails/17.jpg)
UC Merced 17
Functions
Functions are reusable pieces of programs. i.e. range(5), math.sqrt(9), pow(5,2) etc.
Using that function any number of times and anywhere in your program.
This is known as calling the function.
![Page 18: Math 15 Introduction to Scientific Data Analysis Lecture 10 Python Programming – Part 4 University of California, Merced Today – We have A Quiz!](https://reader030.fdocuments.in/reader030/viewer/2022032605/56649e7e5503460f94b82695/html5/thumbnails/18.jpg)
UC Merced 18
Defining Functions
Functions are defined using the def keyword. This is followed by an identifier name for the
function followed by a pair of parentheses which may enclose some names of variables and the line ends with a colon.
This is followed by a block of statements that form the body of the function.
Syntax
def function_name(arguments):“function_documentaion_string”function_body_suite
![Page 19: Math 15 Introduction to Scientific Data Analysis Lecture 10 Python Programming – Part 4 University of California, Merced Today – We have A Quiz!](https://reader030.fdocuments.in/reader030/viewer/2022032605/56649e7e5503460f94b82695/html5/thumbnails/19.jpg)
UC Merced 19
Defining Functions - cont
Example# python Program#def say_hello():
print 'Hello World!' # body of the function
say_hello() # calling the functionsay_hello()say_hello()
Hello World! Hello World!Hello World!
![Page 20: Math 15 Introduction to Scientific Data Analysis Lecture 10 Python Programming – Part 4 University of California, Merced Today – We have A Quiz!](https://reader030.fdocuments.in/reader030/viewer/2022032605/56649e7e5503460f94b82695/html5/thumbnails/20.jpg)
UC Merced 20
Defining Functions – cont. Another Example
# Python Program# Min & Max #def print_max(x, y):
if x > y: print x, 'is maximum'
else: print y, 'is maximum'
print_max(3, 4) # directly give literal constants as arguments
a = 5 b = 7 print_max(a, b) # pass in variables as arguments
4 is maximum 7 is maximum
![Page 21: Math 15 Introduction to Scientific Data Analysis Lecture 10 Python Programming – Part 4 University of California, Merced Today – We have A Quiz!](https://reader030.fdocuments.in/reader030/viewer/2022032605/56649e7e5503460f94b82695/html5/thumbnails/21.jpg)
UC Merced 21
Defining Functions Yet Another Example
The return statement is used to return from a function (i.e. break out from a function).
We can optionally return a value from the function as well.
# python No. 3
def maximum(x, y): if x > y:
return x else:
return y
print maximum(2, 3) 3
![Page 22: Math 15 Introduction to Scientific Data Analysis Lecture 10 Python Programming – Part 4 University of California, Merced Today – We have A Quiz!](https://reader030.fdocuments.in/reader030/viewer/2022032605/56649e7e5503460f94b82695/html5/thumbnails/22.jpg)
UC Merced 22
Calculation of n! and (n!/r!)
One way:n = input ("What is n? ")icount = 1for i in range(1,n+1): icount = icount * infact = icountpnint n,"! =", nfact
r = input("What is r? ")icount = 1for i in range(1,r+1): icount = icount * irfact = icountprint r,"! = ", rfact
print float(nfact)/(rfact)
n!
r!
Another way:#==================================def fact(x): icount = 1 for i in range(1,x+1): icount = icount * i fact = icount print n,"! = ", fact return fact#==================================
n = input("What is n? ")r = input("What is r? ")
a = fact(n)b = fact(r)
print float(a)/(b)
Reusing the function
![Page 23: Math 15 Introduction to Scientific Data Analysis Lecture 10 Python Programming – Part 4 University of California, Merced Today – We have A Quiz!](https://reader030.fdocuments.in/reader030/viewer/2022032605/56649e7e5503460f94b82695/html5/thumbnails/23.jpg)
UC Merced 23
Files and Input/Output
Working with files is a lot like working with notebooks. To use a notebook, you have to open it. When you're done, you have to close it.
With Python, it’s easy to read strings from plain text files and write to plain text files.
![Page 24: Math 15 Introduction to Scientific Data Analysis Lecture 10 Python Programming – Part 4 University of California, Merced Today – We have A Quiz!](https://reader030.fdocuments.in/reader030/viewer/2022032605/56649e7e5503460f94b82695/html5/thumbnails/24.jpg)
UC Merced 24
Open a file for reading data!
This time for reading, and read the contents into a string. This time, the mode argument is "r" for reading:
>>> shrimp = open("test.dat","r") If we try to open a file that doesn't exist, we get an error.
The readline method reads all the characters up to and including the next newline character:
>>> shrimp = open("test.dat","r") >>> print shrimp.readline() Now is the time to close the file
The read method reads data from the file. With no arguments, it reads the entire contents of the file:
>>> text = shrimp.read() >>> print text Now is the time to close the file
![Page 25: Math 15 Introduction to Scientific Data Analysis Lecture 10 Python Programming – Part 4 University of California, Merced Today – We have A Quiz!](https://reader030.fdocuments.in/reader030/viewer/2022032605/56649e7e5503460f94b82695/html5/thumbnails/25.jpg)
UC Merced 25
Open a file for writing!
Opening a file creates a file object. In this example, the variable bobcat refers to the new file object.
>>> bobcat = open("test.dat","w") The open function takes two arguments.
The first is the name of the file, and the second is the mode. Mode "w" means that we are opening
the file for writing. If there is no file named test.dat, it will be created. If
there already is one, it will be replaced by the file we are writing.
To put data in the file we invoke the write method on the file object:
>>> bobcat.write("Now is the time") >>> bobcat.write("to close the file")
Closing the file tells the system that we are done writing and makes the file available for reading:
>>> bobcat.close()
![Page 26: Math 15 Introduction to Scientific Data Analysis Lecture 10 Python Programming – Part 4 University of California, Merced Today – We have A Quiz!](https://reader030.fdocuments.in/reader030/viewer/2022032605/56649e7e5503460f94b82695/html5/thumbnails/26.jpg)
UC Merced 26
Example – No.1:
#
# Program to read and print a file
#
file = open(“I_hate_Python.txt","r")
text = file.readlines()
file.close()
for line in text:
print line,
file_out = open(“I_Love_Python.txt","w")
file_out.writelines(text)
file_out.close()
![Page 27: Math 15 Introduction to Scientific Data Analysis Lecture 10 Python Programming – Part 4 University of California, Merced Today – We have A Quiz!](https://reader030.fdocuments.in/reader030/viewer/2022032605/56649e7e5503460f94b82695/html5/thumbnails/27.jpg)
UC Merced 27
Example- No. 2:
Raw_data.txt - 3.0,4.0,5.0,6.0
Incorrect way to do this:infile = open("raw_data.txt","r")icount = 0for line in infile: # Loop through each line of the file icount = icount + line print line, icount
Correct way to do thisinfile = open("raw_data.txt","r")icount = 0for line in infile: icount = icount + float(line) print line, icount
![Page 28: Math 15 Introduction to Scientific Data Analysis Lecture 10 Python Programming – Part 4 University of California, Merced Today – We have A Quiz!](https://reader030.fdocuments.in/reader030/viewer/2022032605/56649e7e5503460f94b82695/html5/thumbnails/28.jpg)
UC Merced 28
Type Conversion
Type conversion functions are useful. Especially, a time when you need to read data sets from the file.
Python reads data sets as strings. You need to convert to numerical types in order to conduct some mathematical operations.
FunctionDescription
int(x ) converts x to an integer
float(x)converts x to a floating-point number (or real number)
str(x) converts x to a string representation
list(s) converts a sequence to a list
chr(x) converts an integer to a character
![Page 29: Math 15 Introduction to Scientific Data Analysis Lecture 10 Python Programming – Part 4 University of California, Merced Today – We have A Quiz!](https://reader030.fdocuments.in/reader030/viewer/2022032605/56649e7e5503460f94b82695/html5/thumbnails/29.jpg)
UC Merced 29
Let’s start programming
Again!The best way to learn a programming is
To Practice!python