Home > Mobile >  How to find the average of results in a linear regression equation
How to find the average of results in a linear regression equation

Time:09-26

I have the equation and I've been asked to find the average of x from 2010 to 2015. I started a loop to first get the values for 2010-2015 but I'm stuck on how to get the average of those values. Below is what I have so far:

a = -22562.8
b = 11.24
i = 2010
while i <=2015:
    sum_estimated_riders = (a   (i * b)) * 100000
    print(sum_estimated_riders)
    i = i   1 

CodePudding user response:

You can use numpy.mean() for this Make a list, append it with each value, then average that.

import numpy as np


estimated_riders = []
a = -22562.8
b = 11.24
i = 2010
while i <=2015:
    sum_estimated_riders = (a   (i * b)) * 100000
    estimated_rides.append(sum_estimated_riders)
    i = i   1 

avg = np.mean(estimated_riders)
print(avg)

CodePudding user response:

You overwrite sum_estimated_riders every time. Instead, initialize it to 0 before the loop and add to it inside the loop. Then divide by the number of iterations.

a = -22562.8
b = 11.24
i = 2010
sum_estimated_riders = 0
num_years = 0
while i <=2015:
    sum_estimated_riders  = (a   (i * b)) * 100000
    num_years  = 1
    i = i   1 

mean_estimated_riders = sum_estimated_riders / num_years
print(mean_estimated_riders)

Alternatively, you could create a list of estimated_riders for each year. Then, use sum() to calculate the sum and divide by the length of the list.

estimated_riders = []
while i <= 2015:
    estimated_riders.append((a   (i * b)) * 100000)

mean_estimated_riders = sum(estimated_riders) / len(estimated_riders)

Or, as a list comprehension:

estimated_riders = [(a   (i * b)) * 100000 for i in range(2010, 2016)] # 2016 because range() excludes the end
mean_estimated_riders = sum(estimated_riders) / len(estimated_riders)
  • Related