Question: Here's my code and I'm getting an error: length=[22.7, 22.4, 25.8, 21.3, 20.1, 22.1, 21.1, 25.3, 26.9, 26.9, 23.0, 23.8, 26.2, 20.4, 23.0, 21.9, 23.5,

Problem 6: Calculating Sum of Squared Errors Suppose that a biologist is

Here's my code and I'm getting an error:
length=[22.7, 22.4, 25.8, 21.3, 20.1, 22.1, 21.1, 25.3, 26.9, 26.9, 23.0, 23.8, 26.2, 20.4, 23.0, 21.9, 23.5, 27.8, 25.3, 25.9]
weight=[9.2, 8.8, 10.7, 8.3, 6.2, 8.6, 7.2, 11.2, 10.5, 11.3, 9.6, 9.9, 10.9, 5.9, 9.5,9.1, 9.7, 11.6, 10.2, 10.5]
pred_weight=[]
length=[22.7, 22.4, 25.8, 21.3, 20.1, 22.1, 21.1, 25.3, 26.9, 26.9, 23.0, 23.8, 26.2,20.4, 23.0, 21.9, 23.5, 27.8, 25.3, 25.9]
weight=[9.2, 8.8, 10.7, 8.3, 6.2, 8.6, 7.2, 11.2, 10.5, 11.3, 9.6, 9.9, 10.9, 5.9, 9.5,9.1, 9.7, 11.6, 10.2, 10.5]
pred_weight=[]
SSE=0
for i in range(20):
   pred_weight.append(round(0.6*length[i]-5.2))
   diff = weight[i]-pred_weight[i]
   SSE+= diff **2
print(pred_weight)
print("Sum of Squared Error(SSE)= %.4f" %SSE)[8, 8, 10, 8, 7, 8, 7, 10, 11, 11, 9, 9, 11, 7, 9, 8, 9, 11, 10, 10]

 

ERROR:
[8, 8, 10, 8, 7, 8, 7, 10, 11, 11, 9, 9, 11, 7, 9, 8, 9, 11, 10, 10] Sum of Squared Error(SSE)= 10.4700

--------------------------------------------------------------------------- TypeError                                 

Traceback (most recent call last) Cell In[46], line 13      

11     SSE+= diff **2      

12 print(pred_weight) ---> 

13 print("Sum of Squared Error(SSE)= %.4f" %SSE)[8, 8, 10, 8, 7, 8, 7, 10, 11, 11, 9, 9, 11, 7, 9, 8, 9, 11, 10, 10]

TypeError: 'NoneType' object is not subscriptable


CAN YOU HELP FIX MY ERROR? 

Problem 6: Calculating Sum of Squared Errors Suppose that a biologist is studying the relationship between the length and weight of yellow-bellied marmots. Based on prior work, she believes that the length and weight of adult marmots can be approximately modeled by a relationship of the following form: weight = 0.6* length - 5.2 To test this model, the biologist collects a sample of 20 adult marmots. The lengths and weights of the marmots are as follows: length = [22.7, 22.4, 25.8, 21.3, 20.1, 22.1, 21.1, 25.3, 26.9, 26.9, 23.0, 23.8, 26.2, 20.4, 23.0, 21.9, 23.5, 27.8, 25.3, 25.9] weight = [9.2, 8.8, 10.7, 8.3, 6.2, 8.6, 7.2, 11.2, 10.5, 11.3, 9.6, 9.9, 10.9, 5.9, 9.5, 9.1, 9.7, 11.6, 10.2, 10.5] Create these lists, as the appear here. You do not need to print anything in this cell. Create a list named pred_weight containing the predicted weight for each marmot, according to the biologist's model. You should begin by creating pred_weight as an empty list. Then loop over the elements of length. Each iteration of the loop should calculate a new predicted weight using the formula provided above, and rounded to 2 decimal places. The round value for each predicted weight should be appended to the list pred_weight. Print pred_weight. The biologist wishes to score her model using the sum of squared errors (SSE) metric. This metric is calculated as follows: 1. For each observation, calculate the difference between the true weight and the predicted weight. 2. Square this difference. 3. Sum the squared differences over all observations Use a for loop to calculate the model's SSE score for this sample of marmots. There are multiple ways to perform this task, but for the sake of efficiency, I would like for you to do it without creating any new lists and using only one loop. Print the value of SSE, rounded to four decimal places. Note that the value stored in SSE should not be rounded, only the value displayed.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Algorithms Questions!