I am trying a multi linear regression with 4 independent parameters which are compared against these parameters for a class with label 0 to linearly fit the model.
from sklearn import linear_model
from pandas import DataFrame
covariates = DataFrame({'Param1':P1, 'Param2':P2, 'Param3':P3, 'Param4':P4}).iloc[cov_idx, :]
covariates = covariates.to_numpy(dtype=np.float32)
covCN = covariates[labels['Group'] == 0] # only controls as reference group to estimate effect of covariates
lm = linear_model.LinearRegression()
for k in range(images.shape[3]):
for j in range(images.shape[2]):
for i in range(images.shape[1]):
if any(images[:, i, j, k, 0] != 0):
tmpdat = images[labels['Group'] == 0, i, j, k, 0]
lm.fit(covCN, tmpdat)
pred = lm.predict(covariates)
images[:, i, j, k, 0] = images[:, i, j, k, 0] - pred
I want to save the models of linear regression to predict on a different data set having similar parameters. When I tried the below code to save the model, I figured that only one model is getting saved.
import pickle
with open("model","wb") as f:
pickle.dump(lm,f)
with open("model","rb") as f:
model = pickle.load(f)
covariates = DataFrame({'Param1':A1, 'Param2':A2, 'Param3':A3, 'Param4':A4}).iloc[cov_idx, :]
covariates = covariates.to_numpy(dtype=np.float32)
for k in range(images.shape[3]):
for j in range(images.shape[2]):
for i in range(images.shape[1]):
if any(images[:, i, j, k, 0] != 0):
pred = model.predict(covariates)
images[:, i, j, k, 0] = images[:, i, j, k, 0] - pred
How do I save all the linear regression models and use it to predict on the different dataset?