Pdf-10 - HW 10 SOL PDF

Title Pdf-10 - HW 10 SOL
Course Introduction to Data Science
Institution University of California, Berkeley
Pages 4
File Size 175.3 KB
File Type PDF
Total Downloads 82
Total Views 135

Summary

HW 10 SOL...


Description

Week 6: Iteration, Conditionals, Chance Data 8 Tutoring

1 Conditional Statements and Iteration Key Concepts Conditional Statements We can use conditional statements and iteration to create powerful functions that perform different operations based on certain conditions. As a reference, here is how conditional statements work: if x > 10: Do something elif x > 5: Do something else: Do something

Iteration As a reference, here is how for loops work in Python: for item in some_array: print(item) or for i in np.arange(len(array)): print(array[i]) Note that in the first example, we iterate through the actual items in an array (they could be integers, strings, etc.), and in the second example, we iterate through the array’s indices.

Practice Problems 1.1 Warmup: Recall that conditional statements are not limited to numbers. You can also compare strings or even arrays. # assume this block of code has already run numbers1 = make_array(2, 5, 7, 9) numbers2 = make_array(1, 6, 7, 8) Give the output of the following expressions as if we were to run them in a Jupyter notebook cell: a. numbers1 > numbers2 b. numbers2 > 6 c. numbers1 == 7

2. Let’s use our knowledge of conditional statements and iteration to write a function that categorizes movies based on their box office gross. 2.1 Write a function called categorize_movies   which takes in an array of numbers that represent the box office gross for a movie, and returns another array containing the values “low”, “med”, and “high” depending on the conditions in the function description.   to add to array. Hint: Use np.append() def categorize_movies(box_office_vals): “““Categorizes an array of box office totals. Takes in an array of numbers and returns an array of strings representing categories. If an individual gross is: less than 3500, the category is “low” above 3500 and below 4000, the category is “med” above 4000, the category is “high” ”””  categories = make_array()  for ______ in __________________:        return ______________

2.2 Assume that you have a table called actors  , that has 3 columns: “Movie”, “Actor”, and “Box Office Gross”. Now, use the function from 2.1 with actors   to categorize the different movies. Create a new array of “low”, “med”, and “high” values, and add it as a column to the existing actors table. Your final table should look like this:

2.3 You are in the lottery to win dinner with the professors! You are given a die, and you are allowed to roll the dice 500 times. If the dice returns the number 6 over 12% of the time, then you win the dinner! Unfortunately, you are given a 9-sided unfair die with values: [1,1,2,3,4,5,5,5,6] Will you still be able to win the lottery? Write a function called roll_die   that rolls this die 500 times and computes the proportion of 6’s in your rolls. If the proportion higher than 12%, return True. Hint: What function can you use to choose a value at random from an array? How can you do this 500 times? def roll_die(): die = [1,1,2,3,4,5,5,5,6]        

2 Chance Key Concepts When an Event Doesn’t Happen P(an event doesn’t happen) = 1 - P(the event happens)

Equally Likely Outcomes P(an event happens) =

#{outcomes that make the event happen} #{all outcomes}

When Two Events Must Both Happen P(two events both happen) = P(one event happens) x P(the other event happens, given that the first one happened)

When an Event Can Happen in Two Different Ways P(an event happens) = P(first way it can happen) + P(second way it can happen)

At Least One Success P(at least one success) = 1 - P(no successes) To simulate choosing elements from an array with replacement, we can use the following: np.random.choice(array, num_draws) This would return an array of num_draws   elements randomly drawn from the array with replacement. If num_draws   is 1, then a single item is returned.

Practice Problems 1 There are 17 socks in a drawer. 6 are blue, 7 are red, 4 are yellow. You draw socks without replacement. Assume each question takes place independently of the others. 1.1 Chance of getting 1 red sock or 1 yellow sock when drawing 1 sock:

1.2 Chance of drawing 2 blue socks:

1.3 Chance of drawing 2 blue socks or 2 red socks:

1.4 Chance of not  drawing a blue sock in 3 tries.

1.5 Chance of drawing at least one blue sock in 3 tries. Hint: Can you use your answer in (b) in some way?

2 John is playing cards with a standard 52 card deck. Recall that a deck of cards has 4 suits: clubs, diamonds, hearts and spades. There are 13 cards in each suit: 2 through 10, jack, queen, king, ace. Assume the draws in each question are performed independently of the others, and leave each answer unsimplified. What is the probability that: 2.1 John deals a king.

2.2 John deals two aces in a row.

2.3 John deals two cards: one is a heart, and one is a club.

...


Similar Free PDFs