Skip to content
Advertisement

Creating scatterplot / regression line using python

I am stuck on this problem but cannot figure out why it isn’t working as intended. I have a text file with a bunch of x and y coordinates which I need to use to find the average of all x and y values in order to calculate the slope for my regression line. It seems like stamping the individual coordinates works but apparently appending each x or y value to my lists isn’t working right as the error I am getting is “ZeroDivisionError: division by zero”.

Here’s my code:

JavaScript

I really appreciate any input.

Advertisement

Answer

I tried out your code. The reason for the “divide by zero” occurs because your calculation of mean values occurs immediately after you have defined your “sum_of_x” and “sum_of_y” lists. So on the initial go, there are no data points in those lists and thus the numerator and denominator are going to be zero. As a test, I moved the calculation of those mean values after the retrieval of data from the file as noted in the following code snippet.

JavaScript

I just used some made up data points in placed them into a file named “data.txt” just to see if the program would run and it did. Not a very impressive image but it did produce output.

Sample Window

Hope that helps you out.

Regards.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement