1

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?

Arjun
  • 107
  • 1
  • 1
  • 6
  • Where are these independent parameters? You have created just one LogisticRegression model with the default parameters – Anwarvic May 27 '20 at 11:54
  • I am fetching the parameters from excel sheet, where each columns have different values. eg. ``` Param1 Param2 Param3 Param4 10 1 1000 3 20 0 2000 2 30 1 3000 3 ``` – Arjun May 27 '20 at 12:58
  • So, the data is different, but the model is the same. Why do you want to save different models then? – Anwarvic May 27 '20 at 13:02
  • Basically I want to do the process done in the 1st block of code where I fit and model and predict ... to get the same results for the 3rd block of code where I use the coefficients saved from the regression model to predict. I will need to apply this linear prediction to test on different images with different covariates – Arjun May 27 '20 at 13:06

0 Answers0