生成了以下表格:
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
# Generate 'random' data
np.random.seed(0)
X = 2.5 * np.random.randn(10) + 1.5
res = 0.5 * np.random.randn(10)
y = 2 + 0.3 * X + res
Name = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']
# Create pandas dataframe to store our X and y values
df = pd.DataFrame(
{'Name': Name,
'X': X,
'y': y})
# Show the dataframe
df
结果如下表:
<表类>名称 X y tbody><<tr>5.910131 3.845061 B2.500393 3.477255 C3.946845 3.564572 D7.102233 4.191507 E6.168895 4.072600 F-0.943195 1.883879 G td> 3.909606 H 1.121607 2.233903 我1.241953 2.529120 J2.526496 2.330901 表类>
你只需要添加这个:model.save(f"model_{row_index}.pkl")
在你的循环
存储训练好的模型:假设您对每个模型文件mf都有一些可用的命名过程,您可以使用pickle存储模型。
import statsmodels.api as sm
import pickle
# Train your model
model = sm.OLS(y, X).fit()
# Save the model to a file
with open('model.pickle', 'wb') as f:
pickle.dump(model, f)
# Load the model from the file
with open('model.pickle', 'rb') as f:
loaded_model = pickle.load(f)
print(loaded_model.summary())
给出如下输出:
OLS Regression Results
=======================================================================================
Dep. Variable: y R-squared (uncentered): 0.525
Model: OLS Adj. R-squared (uncentered): 0.472
Method: Least Squares F-statistic: 9.931
Date: Mon, 03 Apr 2023 Prob (F-statistic): 0.0117
Time: 12:42:57 Log-Likelihood: -20.560
No. Observations: 10 AIC: 43.12
Df Residuals: 9 BIC: 43.42
Df Model: 1
Covariance Type: nonrobust
==============================================================================
coef std err t P>|t| [0.025 0.975]
------------------------------------------------------------------------------
x1 0.8743 0.277 3.151 0.012 0.247 1.502
==============================================================================
Omnibus: 1.291 Durbin-Watson: 0.989
Prob(Omnibus): 0.524 Jarque-Bera (JB): 0.937
Skew: 0.637 Prob(JB): 0.626
Kurtosis: 2.209 Cond. No. 1.00
==============================================================================
Notes:
[1] R² is computed without centering (uncentered) since the model does not contain a constant.
[2] Standard Errors assume that the covariance matrix of the errors is correctly specified.
注意,为了简化,模型导入与您的稍有不同。但是,您应该能够以相同的方式存储和加载您的模型。
我不太确定我是否正确理解了你关于输出和绘图间距的问题。
用空格隔开摘要:也许只是添加空的print()语句?
图间距:你每次都在生成全新的情节,所以我不明白这个问题。请随时提供其他信息,我会回复你的。