analyzing-data-7

analyzing-data-7 - Collecting and Analyzing Data Keith...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Collecting and Analyzing Data Keith O’Hara kohara@bard.edu CMSC 143 Sep 22 2010 CMSC143 - O'Hara 1 1 Robot Interpreter Problem Give time parameter to command? f2b1a3 cmdstr = raw_input(“Enter Commands: “) idx = 0 while idx < len(cmdstr): if cmdstr[idx] == “f”: forward(1,1) elif cmdstr[idx] == “b”: backward(1,1 elif cmdstr[idx] == “l”: turnLeft(1,1) elif cmdstr[idx] == “r”: turnRight(1,1) elif cmdstr[idx] == “a”: beep(1, 440) else: print “Unknown Command”, cmdstr[idx] idx = idx + 1 CMSC143 - O'Hara 2 Sep 20 2010 Sensing the World(s) Sensors give you information about the world(s) Geology of Mars Temperature and humidity of a forest Structural Soundness of a building of bridge Enemies on Battlefield Sometimes processed on the robot before transmission Sep 22 2010 Too much data! CMSC143 - O'Hara 3 Gather Data with the Scribbler Program to gather and analyze data about light levels of the room Average Minimum Maximum Variance CMSC143 - O'Hara Light Sensors Sep 22 2010 4 Computing Statistics Compute statistics about light levels of the room Average Minimum Maximum Variance data = getLightData(10) avg = computeAverage(data) min = computeMinimum(data) max = computeMaximum(data) variance = computeVariance(data) print “avg = “, avg, “var = “, var avg = 9.1, var = 66.09 Use lists to store data Sep 22 2010 CMSC143 - O'Hara 5 Lists So Far range(5) # evals to [0, 1, 2, 3, 4] notes = [440, 466, 494, 523] for i in range(5): print i for note in notes: beep(1, note) Sep 22 2010 CMSC143 - O'Hara 6 List Operations val = [2, 3, 5, 7, 11] Length of Lists len(val) len(range(9)) #evals to 5 #evals to 9 Indexing val[0] #evals to 2 val[4] #evals to 11 val[5] #error! Lists are Mutable val[1] = “v” Member of a list? 2 in val 10 in val #evals True #evals to False Concatenation [1, 3, 5] + [2, 4, 6] # evals to [1, 3, 5, 2, 4, 6] + [1] # evals to [1] Sep 22 2010 CMSC143 - O'Hara 7 List Arithmetic v1 = [2, “3”, 5.0, 7, “11”] v2 = [2, 3, 5] Appending (concatenating) to a list v3 = v1 + [12] #v3 = [2, “3”, 5.0, 7, “11”, 12] Repeating a list v4 = v2 * 3 #v4 = [2, 3, 5, 2, 3, 5, 2, 3, 5] Deleting from a list del v1[1] #v1 = [2, 5.0, 7, 11] Sep 22 2010 CMSC143 - O'Hara 8 Iterating through Lists notes = [440, 466, 494, 523] idx = 0 while idx < len(notes): beep(1, notes[idx]) idx = idx + 1 Sep 22 2010 CMSC143 - O'Hara 9 Modifying Lists notes = [440, 466, 494, 523] idx = 0 while idx < len(notes): notes[idx] = notes[idx] * 2 beep(1, notes[idx]) idx = idx + 1 Sep 22 2010 CMSC143 - O'Hara 10 Collecting data Rotate the robot and collect data from center light sensor Store the data in a list Return the data to the function caller def getLightData(num): readings = for i in range(num): value = getLight(“center”) readings = readings +[value] turnRight(1,.1) return readings data = getLightData(100) Sep 22 2010 CMSC143 - O'Hara 11 Computing Statistics Function to compute average of a list of numbers data = getLightData(10) avg = computeAverage(data) min = computeMinimum(data) max = computeMaximum(data) # Function takes a list # of numbers and returns # the average as a float def computeAverage(data): Sep 22 2010 CMSC143 - O'Hara 12 Computing the Average of a List # Function takes a list of numbers and # returns the average as a float def computeAverage(data): running_sum = 0.0 #sum is a float for datum in data: running_sum = running_sum + datum average = running_sum / len(data) return average Sep 22 2010 CMSC143 - O'Hara 13 Using Modules Modules allow you to split up your code into multiple files Easier to read, maintain, and test # we assume computeAverageFile.py # contains a function # named computeAverage from computeAverageFile import * avg = computeAverage([1, 2, 3]) print avg You “import” modules Namespace File must be in same directory or in your PYTHONPATH Sep 22 2010 CMSC143 - O'Hara 14 Using a Module 3 Different Ways import computeAverageFile avg = computeAverageFile.computeAverage([1, 2, 3]) print avg from computeAverageFile import computeAverage avg = computeAverage([1, 2, 3]) print avg from computeAverageFile import * avg = computeAverage([1, 2, 3]) print avg Sep 22 2010 CMSC143 - O'Hara 15 Testing Your Modules # Function takes a list of numbers and # returns the average as a float def computeAverage(data): running_sum = 0.0 #sum is a float for datum in data: running_sum += datum average = running_sum / len(data) return average if __name__ == ‘__main__’: # this only gets executed if we run this file directly # let’s run a simple test lst = [1, 2, 3, 4, 5] print “The average of “, lst, “=“, computeAverage(lst) Sep 22 2010 CMSC143 - O'Hara 16 Computing the Minimum # Function takes a non-empty list # of numbers and returns the minimum def computeMinimum(data): minimum = data[0] for datum in data: if datum < minimum: minimum = datum return minimum Sep 22 2010 CMSC143 - O'Hara 17 Computing the Minimum 2 # Function takes a non-empty list # of numbers and returns the minimum def computeMinimum2(data): minimum = data[0] idx = 0 while idx < len(data): if data[idx] < minimum: minimum = data[idx] idx = idx + 1 return minimum Sep 22 2010 CMSC143 - O'Hara 18 What about doing it recursively? Sep 22 2010 CMSC143 - O'Hara 19 Slices of Lists val = [2, “3”, 5.0, 7, “11”] Length of Lists len(val) len(val*2) val[0:1] val[2:5] val[:2] val[2:] val[1:3] #evals to 5 #evals to 10 [2] [5.0, 7, “11”] [2, “3”] [5.0, 7, “11”] #val = [2, 0, 1, 7, “11”] Slices - list[n:m] #evals to #evals to #evals to #evals to = [0, 1] Sep 22 2010 CMSC143 - O'Hara 20 The “Head” and “Tail” of a list # Takes a non-empty list and returns the first # element or the “head” of the list def head(data): return data[0] # Function takes a non-empty list # and returns everything but the “head” of the list def tail(data): return data[1:] Sep 22 2010 CMSC143 - O'Hara 21 The Minimum Recursively # Function takes a non-empty list # of numbers and an initial minimum value # and returns the minimum value def computeMinimum3(data): if len(data) == 1: return data[0] else: minOfTail = computeMinimum3(data[1:]) if data[0] < minOfTail: return data[0] else: return minOfTail Sep 22 2010 CMSC143 - O'Hara 22 Variance Dispersion or spread of the data How much the data differs from the mean (average) Square of the standard Sep 22 2010 CMSC143 - O'Hara deviation 23 Computing the Variance # Function takes a list of numbers and # returns the variance as a float def computeVariance(data): avg = computeAverage(data) rsum = 0.0 for datum in data: rsum = rsum + (datum - avg)**2 variance = rsum / len(data) return variance Sep 22 2010 CMSC143 - O'Hara 24 Computing Statistics Program to keep statistics about light levels of the robot Average Minimum Maximum data = getLightData(10) print data avg = computeAverage(data) minimum = computeMinimum(data) maximum = computeMinimum(data) Use Lists to store data Sep 22 2010 CMSC143 - O'Hara 25 Returning Multiple Statistics? One function to return Minimum Maximum Return multiple values from function? Sep 22 2010 CMSC143 - O'Hara 26 Returning Multiple Statistics One function to data return Minimum Maximum = getLightData(10) minimum, maximum = computeMinAndMax(data) Return multiple values from function Use sparingly Could use lists, but Python has Tuples just for this! Sep 22 2010 CMSC143 - O'Hara 27 Tuples - Immutable Lists val = (2, 3, 5, 7, 11) Length of Lists len(val) len(val*2) #evals to 5 #evals to 10 Tuples are Immutable val[1] = “v” #error! Indexing val[0] #evals to 2 val[4] #evals to 11 val[5] #error! Member of a tuple? 2 in val 10 in val a, b = c, d #evals True #evals to False Tuple Assignment Sep 22 2010 CMSC143 - O'Hara 28 Computing the Min and Max # Function takes a non-empty list # of numbers and returns the minimum def computeMinAndMax(data): minimum = data[0] maximum = data[0] for datum in data: if datum < minimum: minimum = datum if datum > maximum: maximum = datum return minimum, maximum Sep 22 2010 CMSC143 - O'Hara 29 Multiple Robots You can control multiple robots from one python shell Use the “object” interface to the robot Methods functions on objects alice = Scribbler(‘com30’) bob = Scribbler(‘com31’) alice.forward(0.75, 1) bob.beep(1, 440) apic = r1.takePicture() bpic = r2.takePicture() Sep 22 2010 CMSC143 - O'Hara 30 ...
View Full Document

Page1 / 30

analyzing-data-7 - Collecting and Analyzing Data Keith...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online