0

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))
 
asked Apr 4, 2022 at 11:24
3
  • 1
    1. Try to use global variables as little as possible. 2. If you really do NEED to use global, mark the variable as such with global keyword Commented Apr 4, 2022 at 11:26
  • In your last line of your function, you create a new variable b1 which is only available inside the function. If you want to access that value later, you have to: 1) call your function, 2) either print the result inside the function or return the result. I would advise you against global variables here, simply change your last line in the function to return return x_y / x_sum, and secondly replace your statement print(str(b1)) with print(b0_b1_calc())) Commented Apr 4, 2022 at 11:31
  • The normal way to do this would be to pass in data as function arguments, and return results from function by return. Why don't you do that? Commented Apr 4, 2022 at 11:33

2 Answers 2

1

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()
answered Apr 4, 2022 at 11:35

2 Comments

Hold on, the code has some errors in it. Fixing now
Let me know if that answer doesn't work
0

Please declare b1 as global in the function as following:

 def b0_b1_calc():
 global b1
 # remaining code in the function
answered Apr 4, 2022 at 11:29

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.