I have been iterating over a dictionary of arrays and applying linear regression for each array element in the dictionary.
from sklearn.linear_model import LinearRegression
model = LinearRegression()
for i in my_dict.keys():
test = model.fit(x_val.reshape(-1,1), my_dict[i].reshape(-1,1))
coeff = float(test.coef_)
intercept = float(test.intercept_)
my_dict[i] = lambda x: coeff * x + intercept
At each iteration, I'm pretty confident that the proper coeff and intercepts are being assigned to the lambda function. However, it seems that every stored lambda function in the dictionary is using the coefficient and intercepts for the "last" key in the dictionary. I can't seem to put my finger on why that is. Thanks!
Edit: I'm aware I can just assign the linear regressor object to each key instead of using a lambda function (I just preferred lambda functions). However, that hasn't solved this problem.