我正在尝试使用 4 个独立参数进行多元线性回归,这些参数与标签为 0 的类的这些参数进行比较,以线性拟合模型。
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
我想保存线性回归模型以预测具有相似参数的不同数据集。当我尝试使用以下代码保存模型时,我发现只有一个模型被保存。
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
如何保存所有线性回归模型并使用它来预测不同的数据集?