hw01

pdf

School

University of California, Berkeley *

*We aren’t endorsed by this school

Course

C8

Subject

Computer Science

Date

Dec 6, 2023

Type

pdf

Pages

13

Uploaded by AdmiralAtom103517

Report
hw01 November 30, 2023 [1]: # Initialize Otter import otter grader = otter . Notebook( "hw01.ipynb" ) 1 Homework 1: Causality and Expressions Please complete this notebook by filling in the cells provided. Before you begin, run the previous cell to load the provided tests. Recommended Readings: What is Data Science? Causality and Experiments Programming in Python For all problems that you must write explanations and sentences for, you must provide your answer in the designated space. Moreover, throughout this homework and all future ones, please be sure to not re-assign variables throughout the notebook! For example, if you use max_temperature in your answer to one question, do not reassign it later on. Otherwise, you will fail tests that you thought you were passing previously! Deadline: This assignment is due Wednesday, 8/30 at 11:00pm PT . Turn it in by Tuesday, 8/29 at 11:00pm PT for 5 extra credit points. Late work will not be accepted as per the policies page. Note: This homework has hidden tests on it. That means even though tests may say 100% passed, it doesn’t mean your final grade will be 100%. We will be running more hidden tests for correctness once everyone turns in the homework. Directly sharing answers is not okay, but discussing problems with the course staff or with other students is encouraged. Refer to the policies page to learn more about how to learn cooperatively. You should start early so that you have time to get help if you’re stuck. Offce hours are held Monday-Friday. The schedule appears on our website . 1.1 1. Scary Arithmetic An ad for ADT Security Systems says, “When you go on vacation, burglars go to work […] According to FBI statistics, over 25% of home burglaries occur between Memorial Day to Labor Day.” 1
Do the data in the ad support the claim that burglars are more likely to go to work during the time between Memorial Day to Labor Day? Please explain your answer. (6 Points) Note: You can assume that “over 25%” means only slightly over. Had it been much over, say closer to 30%, then the marketers would have said so. Note: Memorial Day is observed on the last Monday of May and Labor Day is observed on the first Monday of September. No, because there are only 12 months in a year and this period spans 3 about months. Thus, the fact that around 25% of all burglaries occur during this time does not mean that these are partciularly active months for burglers. Rather, it suggests that the number of burglaries is an expected amount, since 25% of them occur in 25% of the calendar year. 1.2 2. Characters in Little Women In lecture, we counted the number of times that the literary characters were named in each chapter of the classic book, Little Women . In computer science, the word “character” also refers to a letter, digit, space, or punctuation mark; any single element of a text. The following code generates a scatter plot in which each dot corresponds to a chapter of Little Women . The horizontal position of a dot measures the number of periods in the chapter. The vertical position measures the total number of characters. [2]: # Just run this cell. # This cell contains code that hasn't yet been covered in the course, # but you should be able to interpret the scatter plot it generates. from datascience import * from urllib.request import urlopen import numpy as np % matplotlib inline little_women_url = 'https://www.inferentialthinking.com/data/little_women.txt' chapters = urlopen(little_women_url) . read() . decode() . split( 'CHAPTER ' )[ 1 :] text = Table() . with_column( 'Chapters' , chapters) Table() . with_columns( 'Periods' , np . char . count(chapters, '.' ), 'Characters' , text . apply( len , 0 ) ) . scatter( 0 ) 2
Question 1. Around how many periods are there in the chapter with the most characters? Assign either 1, 2, 3, 4, or 5 to the name characters_q1 below. (4 Points) 1. 250 2. 390 3. 440 4. 32,000 5. 40,000 Note: If you run into a NameError: name 'grader' is not defined error in the autograder cell below (and in any assignment), please re-run the first cell at the very top of this notebook! [3]: characters_q1 = 2 [4]: grader . check( "q2_1" ) [4]: q2_1 results: All test cases passed! The test above checks that your answers are in the correct format. This test does not check that you answered correctly , only that you assigned a number successfully in each multiple-choice answer cell. 3
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Question 2. Which of the following chapters has the most characters per period? Assign either 1, 2, or 3 to the name characters_q2 below. (4 Points) 1. The chapter with about 60 periods 2. The chapter with about 350 periods 3. The chapter with about 440 periods [5]: characters_q2 = 3 [6]: grader . check( "q2_2" ) [6]: q2_2 results: All test cases passed! Again, the test above checks that your answers are in the correct format, but not that you have answered correctly. To discover more interesting facts from this plot, check out Section 1.3.2 in the textbook. 1.3 3. Names and Assignment Statements Question 1. When you run the following cell, Python produces a cryptic error message. [8]: 4 = 2 + 2 Cell In[8], line 1 4 = 2 + 2 ^ SyntaxError: cannot assign to literal Choose the best explanation of what’s wrong with the code, and then assign 1, 2, 3, or 4 to names_q1 below to indicate your answer. (4 Points) 1. Python is smart and already knows 4 = 2 + 2 . 2. In Python, it’s a rule that the = sign must have a variable name to its left, and 4 isn’t a variable name. 3. It should be 2 + 2 = 4 . 4. I don’t get an error message. This is a trick question. [9]: names_q1 = 2 [10]: grader . check( "q3_1" ) [10]: q3_1 results: All test cases passed! Question 2. When you run the following cell, Python will produce another cryptic error message. 4
[11]: two = 3 six = two plus two Cell In[11], line 2 six = two plus two ^ SyntaxError: invalid syntax Choose the best explanation of what’s wrong with the code and assign 1, 2, 3, or 4 to names_q2 below to indicate your answer. (4 Points) 1. The plus operation only applies to numbers, not the word “two”. 2. The name “two” cannot be assigned to the number 3. 3. Two plus two is four, not six. 4. The name plus isn’t a built-in operator; instead, addition uses + . [12]: names_q2 = 4 [13]: grader . check( "q3_2" ) [13]: q3_2 results: All test cases passed! Question 3. Run the following cell. [14]: x = 2 y = 3 * x x = 4 What is y after running this cell, and why? Choose the best explanation and assign 1, 2, 3, or 4 to names_q3 below to indicate your answer. (4 Points) 1. y is equal to 6, because the second x = 4 has no effect since x was already defined. 2. y is equal to 6, because x was 2 when y was assigned, and 3 * 2 is 6. 3. y is equal to 12, because x is 4 and 3 * 4 is 12. 4. y is equal to 12, because assigning x to 4 will update y to 12 since y was defined in terms of x . [15]: names_q3 = 2 [16]: grader . check( "q3_3" ) [16]: q3_3 results: All test cases passed! 5
1.4 4. Differences Between Majors Berkeley’s Offce of Planning and Analysis (OPA) provides data on numerous aspects of the campus. Adapted from the OPA website, the table below displays the number of degree recipients in three majors in the 2008-2009 and 2017-2018 academic years. Major 2008-2009 2017-2018 Gender and Women’s Studies 17 28 Linguistics 49 67 Rhetoric 113 56 Question 1. Suppose you want to find the biggest absolute difference between the number of degree recipients in the two years, among the three majors. In the cell below, compute this value and call it biggest_change . Use a single expression (a single line of code) to compute the answer. Let Python perform all the arithmetic (like subtracting 49 from 67) rather than simplifying the expression yourself. The built-in abs function takes a numerical input and returns the absolute value. The built-in max function can take in 3 arguments and returns the maximum of the three numbers. (5 Points) [17]: biggest_change = max ( abs ( 28-17 ), abs ( 67-49 ), abs ( 56-113 )) biggest_change [17]: 57 [18]: grader . check( "q4_1" ) [18]: q4_1 results: All test cases passed! Question 2. Which of the three majors had the smallest absolute difference? Assign smallest_change_major to 1, 2, or 3 where each number corresponds to the following major: 1. Gender and Women’s Studies 2. Linguistics 3. Rhetoric Choose the number that corresponds to the major with the smallest absolute difference. (4 Points) Hint: You should be able to answer by rough mental arithmetic, without having to calculate the exact value for each major. [21]: smallest_change_major = 1 smallest_change_major [21]: 1 [22]: grader . check( "q4_2" ) 6
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
[22]: q4_2 results: All test cases passed! Question 3. For each major, define the “relative change” to be the following: absolute difference value in 2008-2009 ∗ 100 Fill in the code below such that gws_relative_change , linguistics_relative_change and rhetoric_relative_change are assigned to the relative changes for their respective majors. (5 Points) [23]: gws_relative_change = ( abs ( 28-17 ) / 17 ) * 100 linguistics_relative_change = ( abs ( 67-49 ) / 49 ) * 100 rhetoric_relative_change = ( abs ( 56-113 ) / 113 ) * 100 gws_relative_change, linguistics_relative_change, rhetoric_relative_change [23]: (64.70588235294117, 36.734693877551024, 50.442477876106196) [24]: grader . check( "q4_3" ) [24]: q4_3 results: All test cases passed! Question 4. Assign biggest_rel_change_major to 1, 2, or 3 where each number corresponds to to the following: 1. Gender and Women’s Studies 2. Linguistics 3. Rhetoric Choose the number that corresponds to the major with the biggest relative change. (4 Points) [25]: biggest_rel_change_major = 1 biggest_rel_change_major [25]: 1 [26]: grader . check( "q4_4" ) [26]: q4_4 results: All test cases passed! 1.5 5. Nearsightedness Study Myopia , or nearsightedness, results from a number of genetic and environmental factors. In 1999, Quinn et al studied the relation between myopia and ambient lighting at night (for example, from nightlights or room lights) during childhood. Question 1. The data were gathered by the following procedure, reported in the study. “Between January and June 1998, parents of children aged 2-16 years […] that were seen as outpatients in a university pediatric ophthalmology clinic completed a questionnaire on the child’s light exposure both at present and before the age of 2 years.” Was this study observational, or was it a controlled experiment? Explain. (5 Points) 7
Observational because no treatment was imposed Question 2. The study found that of the children who slept with a room light on before the age of 2, 55% were myopic. Of the children who slept with a night light on before the age of 2, 34% were myopic. Of the children who slept in the dark before the age of 2, 10% were myopic. The study concluded the following: “The prevalence of myopia […] during childhood was strongly associated with ambient light exposure during sleep at night in the first two years after birth.” Do the data support this statement? Why or why not? You may interpret “strongly” in any reasonable qualitative way. (5 Points) Yes, because those who slept with light exposure were significantly more likely to be myopic Question 3. On May 13, 1999, CNN reported the results of this study under the headline, “Night light may lead to nearsightedness.” Does the original study claim that night light causes nearsightedness? (5 Points) No, it doesn’t claim that night lights actually cause the nearsightedness, just that exposure light while sleeping is correlated with myopia Question 4. The final paragraph of the CNN report said that “several eye specialists” had pointed out that the study should have accounted for heredity. Myopia is passed down from parents to children. Myopic parents are more likely to have myopic children, and may also be more likely to leave lights on habitually (since the parents have poor vision). In what way does the knowledge of this possible genetic link affect how we interpret the data from the study? Explain. (5 Points) The heredity is a confounding variable because the children with myopic parents would be more likely to sleep with some lights that were left on and would also be more likely to be myopic because of heredity. Thus, the myopia of the children would likely be caused not by the amount of light they slept with, but by their genetics. 1.6 6. Studying the Survivors The Reverend Henry Whitehead was skeptical of John Snow’s conclusion about the Broad Street pump. After the Broad Street cholera epidemic ended, Whitehead set about trying to prove Snow wrong. (The history of the event is detailed here .) He realized that Snow had focused his analysis almost entirely on those who had died. Whitehead, therefore, investigated the drinking habits of people in the Broad Street area who had not died in the outbreak. What is the main reason it was important to study this group? Assign either 1, 2, or 3 to the name survivor_answer below. (4 Points) 1. If Whitehead had found that many people had drunk water from the Broad Street pump and not caught cholera, that would have been evidence against Snow’s hypothesis. 2. Survivors could provide additional information about what else could have caused the cholera, potentially unearthing another cause. 3. Through considering the survivors, Whitehead could have identified a cure for cholera. 8
[27]: survivor_answer = 1 [28]: grader . check( "q6_1" ) [28]: q6_1 results: All test cases passed! Note: Whitehead ended up finding further proof that the Broad Street pump played a central role in spreading the disease to the people who lived near it. Eventually, he became one of Snow’s greatest defenders. 1.7 7. Policies and Administrivia This section of the homework is to ensure that you have read over the policies and frequently asked questions for the course. It’s important that you read through this section of the homework very carefully . If you can get through all of this section and are sure you have all of the correct resources set up, you will be able to focus on the actual material this semester! Reading through the policies and the FAQ will help you get through this section very easily. It is recommended you do this before proceeding. Question 1. You have a question regarding the grading of your assignments that has not been previously answered on Ed or the FAQ. Who do you contact? Assign contact to the number corresponding to the best choice below. (4 Points) 1. The Instructors 2. Post on Ed 3. Contact your Lab TA [29]: contact = 3 [30]: grader . check( "q7_1" ) [30]: q7_1 results: All test cases passed! Question 2. Why are there typically 2 items listed on Gradescope for each homework assignment? Assign grades to the number corresponding to the best choice below. (4 Points) 1. There was a mistake in the grading. I should contact someone about this. 2. One assignment is for coding questions (which I will submit to), and the other is automatically submitted for me and contains my written work. 3. Trick question. [31]: grades = 2 [32]: grader . check( "q7_2" ) [32]: q7_2 results: All test cases passed! 9
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Question 3. Regrade deadline dates will always be posted on the same Ed post that releases the assignment grades, common mistakes, and solutions. Can you ask for parts of your assignment re- graded after the regrade request window has passed? Assign regrade to the number corresponding to the best choice below. (4 Points) 1. Yes 2. No [33]: regrade = 2 [34]: grader . check( "q7_3" ) [34]: q7_3 results: All test cases passed! Question 4. Do you have an Gradescope account? Head to gradescope.com and check if you see Data 8. If you do not, please send your Lab TA an email with your email and student ID number. Once you have been enrolled, go to the Data 8 Gradescope course website. At the end of the url link, you should see a six-digit number. Assign gradescope to that number. (4 Points) [35]: gradescope = 576157 [36]: grader . check( "q7_4" ) [36]: q7_4 results: All test cases passed! Question 5. Given the following scenarios, assign acceptable to the corresponding number of the scenario that is permissible given the guidelines on the policies page. (4 Points) 1. Nicole gets stuck on a homework assignment, so she googles a fix. She stumbles across a pdf of the solutions for the homework assignment from a previous semester’s offering of Data 8. After inspecting the solution, Nicole writes her own solution and submits the assignment. 2. After getting confused by a project, James asks his friend for help. His friend Padma helps by walking James through her own logic, without showing her code, pointing out areas that are important given the context of the question. Upon hearing his friend’s logic, James writes his own code and completes the project. 3. Ciara (who is in a regular lab) has an extremely busy schedule, so she really wants to leave lab early by finishing it and getting checked off. Her neighbor, Prasann, simply turns his computer so Ciara can see how he completed some questions. After looking at his code, Ciara finishes the lab and gets checked off. [41]: acceptable = 2 [42]: grader . check( "q7_5" ) [42]: q7_5 results: All test cases passed! Question 6. To make sure you have read through the policies and the FAQ carefully, how many HW and lab drops are there? Assign drops to the number corresponding to the best choice below. 10
(4 Points) 1. Two homework drops and three lab drops 2. Two homework drops and two lab drops 3. Only two homework drops 4. One homework drop and two lab drops [43]: drops = 2 [44]: grader . check( "q7_6" ) [44]: q7_6 results: All test cases passed! Question 7. Does Data 8 offer alternate final exam to those with class conflicts? Assign exams to the number corresponding to the best choice below. (3 Points) 1. Yes 2. No [47]: exams = 2 [48]: grader . check( "q7_7" ) [48]: q7_7 results: All test cases passed! Question 8: Are you actually checking Ed? Go to this semester’s Data 8 Ed and find an instructor posted thread with a certain secret phrase. Assign secret to this secret phrase in quotes (i.e. as a string). (4 Points) [51]: secret = "beepboop" [52]: grader . check( "q7_8" ) [52]: q7_8 results: All test cases passed! 1.8 8. Welcome Survey Question 1. Please complete the welcome survey below in order to receive credit for homework 1. Keep an eye out for the secret once} you submit! (1 Point) Fall 2023 Welcome Survey Assign survey to the secret phrase given at the end of the welcome survey. Make sure the phrase is in quotes (i.e. is a string)! [55]: survey = "data8 is data gr8" [56]: grader . check( "q8_1" ) [56]: q8_1 results: All test cases passed! 11
You’re done with Homework 1! Important submission information: Be sure to run the tests and verify that they all pass, then choose Save Notebook from the File menu, then run the final cell and click the link to download the zip file. Then, go to Gradescope and submit the zip file to the corresponding assignment. The name of this assignment is “HW 01 Autograder”. It is your responsibility to make sure your work is saved before running the last cell. Once you have submitted, your Gradescope assignment should look something like the following image if you have passed all tests. Note: This is a photo of a generic Gradescope submission result, and it does not included the same test numbers as this assignment. Please check that all test cases have passed for each question. 1.9 Pets of Data 8 Congrats on finishing homework 1! Dog of the week: Snowball 1.10 Submission Make sure you have run all cells in your notebook in order before running the cell below, so that all images/graphs appear in the output. The cell below will generate a zip file for you to submit. Please save before exporting! [57]: # Save your notebook first, then run this cell to export your submission. grader . export(pdf = False , run_tests = True ) Running your submission against local test cases… Your submission received the following results when run against available test cases: q2_1 results: All test cases passed! q2_2 results: All test cases passed! q3_1 results: All test cases passed! q3_2 results: All test cases passed! q3_3 results: All test cases passed! q4_1 results: All test cases passed! q4_2 results: All test cases passed! q4_3 results: All test cases passed! 12
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
q4_4 results: All test cases passed! q6_1 results: All test cases passed! q7_1 results: All test cases passed! q7_2 results: All test cases passed! q7_3 results: All test cases passed! q7_4 results: All test cases passed! q7_5 results: All test cases passed! q7_6 results: All test cases passed! q7_7 results: All test cases passed! q7_8 results: All test cases passed! q8_1 results: All test cases passed! <IPython.core.display.HTML object> 13