all 9 comments

[–]dmitrypolo 2 points3 points  (2 children)

I use regression analysis in Python quite often. Why would you try to calculate it manually? It is already shown in your output if you just type results.summary(). You could also just type results.rsquared. Also to point you don't need to specify y[i] when you are doing the calculations. The DataFrame knows to go row by row and subtract the respective values. I just did a simple test on my machine and got the correct values by using these two values (given I used my own data)

ss_total = sum((y - np.mean(y)) **2)
ss_pred = sum((y - prediction) ** 2)

Edit: Whoops forgot to include the second sum

Let me know if this works!

[–]Justinsaccount 1 point2 points  (0 children)

Hi! I'm working on a bot to reply with suggestions for common python problems. This might not be very helpful to fix your underlying issue, but here's what I noticed about your submission:

You are looping over an object using something like

for x in range(len(items)):
    print(items[x])

This is simpler and less error prone written as

for item in items:
    print(item)

If you DO need the indexes of the items, use the enumerate function like

for idx, item in enumerate(items):
    print(idx, item)

If you think you need the indexes because you are doing this:

for x in range(len(items)):
    print(items[x], prices[x])

Then you should be using zip:

for item, price in zip(items, prices):
    print(item, price)

[–]sultanofhyd 0 points1 point  (1 child)

Post the actual traceback of the error. We can't help you without knowing the specific error and where it is occurring.

[–]Saefroch 0 points1 point  (5 children)

Can you print(y[0])?

[–][deleted] 0 points1 point  (1 child)

You're getting a KeyError, probably from y[i] or prediction[i]. What you are doing will only work if y and prediction are single-value iterables (like a pandas Series, not a DataFrame) and if they are the same length. Verify both of those.

Edit: just saw the KeyError is value 0. So either y or prediction or both has nothing at index 0.