我已经设置了一个测试,看看在哪里可以使用print_fn。我还检查了最后一个问题发布的解决方案,但在文档中找不到print_fn。
我试图转换为制表,以便将摘要保存为png格式:
import matplotlib.pyplot as plt
import pandas as pd
# Convert the summary table to a pandas DataFrame
# change tables [0] to [1] to get the second table
summary_df = pd.read_html(model.summary().tables[0].as_html(), header=0, index_col=0)[0]
# Get the headers
headers = summary_df.columns.tolist()
# Convert the DataFrame to a list of lists and add the headers
summary_list = [headers] + summary_df.values.tolist()
# Create a new figure
fig, ax = plt.subplots()
# Remove the axes
ax.axis('off')
# Add a table to the figure
table = plt.table(cellText=summary_list, loc='center')
# Auto scale the table
table.auto_set_font_size(False)
table.set_fontsize(10)
table.scale(1, 1.5)
# Save the figure as a PNG file
plt.savefig('summary2.png', dpi=300, bbox_inches='tight')
在我看来,将数据保存为png是一种非常不寻常的情况。它阻止用户共享信息。有一些选项可以将摘要导出到csv和latex。
如果您手动执行此操作,我建议导出为csv并复制粘贴为图像。或者另存为txt,甚至截图。
供参考:
model.summary().as_csv()
# save as csv
with open('summary.csv', 'w') as file:
file.write(model.summary().as_csv())
或
text = model.summary().as_text()
# save to txt
with open('summary.txt', 'w') as file:
file.write(text)