Class 28: Entropy

373
-1

Published on

Liberal Arts Checkup
Python Tuples and Lists
Reversing in Python
Entropy

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
373
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
9
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Class 28: Entropy

  1. 1. A giant python swallows an alligator in Everglades National Park, Fla.. Paul Zenk/PBS Nature “Invasion of the Giant Pythons” (yesterday’s New York Times)cs1120 Fall 2011David Evans Class 28:28 October 2011 Entropy
  2. 2. PlanStory so Far: Liberal Arts RecapReversing in PythonShannon’s Information Theory: EntropyReturning Quiz 3, PS5, etc. 2
  3. 3. Ch 1: Computing Ch 2: LanguageAnalysis Synthesis Ch 3: Programming Ch 4: Procedures Ch 5: Data Ch 6: Machines Ch 7: Cost Ch 8: Sorting and Searching PS5, Ch 9: State You are PS6, Ch 10: Objects Course Roadmap here PS7, Ch 11: Interpreters Ch 12: Computability PS8, 9 Intractability
  4. 4. cs1120 Main TopicsLanguage: How to describe information processes by defining procedures (Chapters 3, 4, 5, 9, 10) Programming with procedures, lists, recursion (PS1-4) Programming with state (PS5), objects (PS6), languages (PS7)Logic: How to predict properties about information processes (Chapter 6, 7, 8) Predicting how running time grows with input size Are there problems which can’t be solved by algorithms? (Ch 12)Machines: How to efficiently implement information processes (Chapter 6) (not much on this) Machines, Digital Logic How to implement a Scheme interpreter (Ch 11, PS7)
  5. 5. From Lecture 1:
  6. 6. Grammar: study of meaning in BNF, RTN, rules of written expression evaluation for meaningTrivium Rhetoric: comprehension of verbal Interfaces between components and written discourse (PS6), evaluation rules Logic: argumentative discourse for Rules of evaluation, if, recursive definitions discovering truth Arithmetic: understanding adding with logic, representing numbers numbersQuadrivium Curves as procedures, Geometry: quantification of space fractals (PS3) Music: number in time Ada, GEB Chapter Astronomy Nothing! But…read the Neil DeGrasse Tyson essay!
  7. 7. Lists (Tuples) in Python Scheme (list) Python (tuple)> (define p (list 1 2 3)) >>> p = (1, 2, 3)> (car p) >>> p[0]1 1> (cdr p) >>> p[1:](2 3) (2, 3)> (cdr (cdr (cdr p))) >>> p[1:][1:][1:]null () 7
  8. 8. Python Shortcuts>>> p = (1, 2, 3)>>> p[2]3 list[i]>>> p[3:] Evaluates to ith (counting from 0) element of tuple/list() Runs in (approximately)>>> p[:2] constant time!(1, 2)
  9. 9. Mutable Lists (Lists) in PythonScheme (mutable list) Python [List] > (define p (mlist 1 2 3)) >>> p = [1, 2, 3] > (mcar p) >>> p[0] 1 1 > (mcdr p) >>> p[1:] (2 3) (2, 3) > (mcdr (mcdr (mcdr p))) >>> p[1:][1:][1:] null () 9
  10. 10. Using Mutable Lists>>> p = [1, 2, 3] Scheme Application:>>> p[0] (<verb> <operands>)1 Python procedure call:>>> p[0] = 2 <verb>(<operand>)>>> p Method call:[2, 2, 3] <object>.<verb>(<parameters>)>>> p.append(4) Note: this is different from mappend!>>> p It take a single element, not a list.[2, 2, 3, 4]
  11. 11. Trick Question>>> p = (1, 2, 3)>>> p[0] = 2Traceback (most recent call last): File "<pyshell#23>", line 1, in <module> p[0] = 2TypeError: tuple object does notsupport item assignment
  12. 12. Reversing a Mutable ListScheme: p: 1 2 3Python: p: 1 2 3
  13. 13. mlist-reverse!(define (mlist-reverse! p) (if (null? (mcdr p)) (void) (let ((rest (mcdr p)) (carp (mcar p))) (mlist-reverse! rest) (set-mcar! p (mcar rest)) (set-mcdr! p (mcdr rest)) (mlist-append! p (mcons carp null)))))
  14. 14. Literal Translation(define (mlist-reverse! p) (if (null? (mcdr p)) (void) (let ((rest (mcdr p)) (carp (mcar p))) (mlist-reverse! rest) (set-mcar! p (mcar rest)) (set-mcdr! p (mcdr rest)) (mlist-append! p (mcons carp null)))))
  15. 15. Literal Translation(define (mlist-reverse! p) def sreverse(p): (if (null? (mcdr p)) if len(p) == 1: (void) return (let ((rest (mcdr p)) (carp (mcar p))) else: (mlist-reverse! rest) rest = p[1:] (set-mcar! p (mcar rest)) first = p[0] (set-mcdr! p (mcdr rest)) sreverse(rest) (mlist-append! p[0] = rest[0] p (mcons carp null))))) p[1:] = rest[1:] p.append(first) >>> p=[1,2,3] This is correct, but no sane Python >>> sreverse(p) programmer would do it this way! >>> p [3, 2, 1]
  16. 16. Python forStatement ::= (define (loop index result test update proc) for Varible in Expression: (if (test index) (loop (update index) Block (proc index result) test update proc) result))def gaussSum (n): sum = 0 (define (gauss-sum n) (loop 1 0 (lambda (i) (<= i n)) for i in range(1, n+1): (lambda (i) (+ i 1)) (lambda (i sum) (+ i sum)))) sum = sum + i return sum
  17. 17. Reverse in Python p: 1 2 3 Note: there is a built-in list method, p.reverse().
  18. 18. int(len(p) / 2) len(p) >> 1def reverse(p): for i in range(0, len(p) >> 1): p[i], p[len(p) – 1 - i] = p[len(p) – 1 - i], p[i]
  19. 19. Information Theory 19
  20. 20. EntropyH = - pi log2 pi 20
  21. 21. flickr cc:Daniel Dale (Ideal) Coin TossH = - pi log2 pi 21
  22. 22. Nucleotide N {A, C, G, T}Intuition: need two bits to distinguish four possible values 00 = A, 01 = C, 10 = G, 11 = T H = - pi log2 pi 22
  23. 23. 23
  24. 24. Real Coin Toss?Claude Shannon’sJuggling W. C. Fields 24
  25. 25. Real Coin TossAccording to Susan Holmes, 51% of the time, acoin will land same way it started. [link] H = - pi log2 pi 25
  26. 26. 26
  27. 27. Are Randall Munroe’s entropy estimates correct?Left for course blog discussion. 27
  28. 28. 0.9880135796356116381290864023063602427958811193759308857790... Class Sorting using “Odette” Assuming alphabet randomly distributed: O is 15th out of 26 letters in alphabet = 0.988… 28
  29. 29. 0.9880135796356116381290864023063602427958811193759308857790... Class Sorting using “Odette” The real class: 41 out of 49 students have names before “Odette”! -((41/49) * log_2 (41/49) + (7/49) * log_2 (7/49)) = 0.6162235892609252 29
  30. 30. Better Class Sorting Front of Classcrm Caroline Hannah – Li- - Gregory Chang Anthony - mdc Michael Casey - Yuan 30
  31. 31. ChargeOffice hours: Peter – right now Sunday, 1-6pm Everyone should be making progress on PS6Monday: Trick-or-Treat Protocols! Inheritance

×