I am creating a program to calculate b1 for a linear regression program in Python.
When trying to assign a value to my b1 global variable, my IDE doesn’t find it. You can find the variable assignment at the last line of my function.
x_data = [4, 8, 6, 3, 4, 6, 2, 1, 3]
y_data = [2, 4, 7, 3, 4, 8, 3, 2, 5]
b1 = 0
def b0_b1_calc():
x_sum = 0
y_sum = 0
for i in x_data:
x_sum += x_data[i]
y_sum += y_data[i]
x_mean = round(1, x_sum / len(x_data))
y_mean = round(1, y_sum / len(y_data))
x_list = []
y_list = []
for i in x_data:
x_list.append(i - x_mean)
y_list.append(i - y_mean)
x_minus_x_squared = []
for i in x_list:
x_minus_x_squared.append((x_list[i] - x_mean) ** 2)
x_sum = sum(x_minus_x_squared)
x_y = []
for i in x_data:
x_y.append(y_list * x_list)
x_y = sum(x_y)
b1 = x_y / x_sum
print(str(b1))
>Solution :
You have to use global to access global variables. There were a few more errors in the code that I have fixed and annotated too:
def b0_b1_calc():
# Use global to access global variables
global b1
# sum() summates a list
x_sum = sum(x_data)
y_sum = sum(y_data)
# This was the wrong way around
x_mean = round(x_sum / len(x_data))
y_mean = round(y_sum / len(y_data))
x_list = [] # could also be [i - x_mean for i in x_data]
y_list = [] # could also be [i - y_mean for i in x_data]
for i in x_data:
x_list.append(i - x_mean)
y_list.append(i - y_mean)
# could also be [(x_list[i] - x_mean) ** 2 for i in x_list]
x_minus_x_squared = []
for i in x_list:
x_minus_x_squared.append((x_list[i] - x_mean) ** 2)
x_sum = sum(x_minus_x_squared)
x_y = []
for i in x_data:
# could be simplified to [np.array(y_list) * np.array(x_list) for i in x_data]
# use np.array for vectorial array multiplication
x_y.append(np.array(y_list) * np.array(x_list))
x_y = sum(x_y)
b1 = x_y / x_sum
b0_b1_calc()
The preferred way to get around globals is to call and return:
def b0_b1_calc():
# ... code here
return b1
b1 = b0_b1_calc()