首页 > 解决方案 > 将函数输出保存为 .pdf

问题描述

我在数据框中的列上迭代一个函数,如下所示:

for name, column in train_set.iteritems():
    adfuller_test(column, name=column.name)

我想将这些迭代的输出保存到 .txt 或 .pdf 格式的外部文件中。我发现了这种从脚本创建 PDF 文件的方法:

import fpdf    
pdf = FPDF()
pdf.add_page()
pdf.set_font("Arial", size=12)
pdf.cell(200, 10, txt=print(), ln=1, align="C")
pdf.output("Augmented Dickey Fuller Test.pdf")

但是,如何将 for 循环的输出带入内部txt?我尝试使用 将循环的结果存储在列表中list.append,但不幸的是该adfuller_test函数返回 NoneType 对象。

更新:adfuller_test对时间序列数据执行增强的 Dickey Fuller 测试。函数定义为:

def adfuller_test(series, signif=0.05, name='', verbose=False):
    """Perform ADFuller to test for Stationarity of given series and print report"""
    r = adfuller(series, autolag='AIC')
    output = {'test_statistic':round(r[0], 4), 'pvalue':round(r[1], 4), 'n_lags':round(r[2], 4), 'n_obs':r[3]}
    p_value = output['pvalue'] 
    def adjust(val, length= 6): return str(val).ljust(length)
    print(f'    Augmented Dickey-Fuller Test on "{name}"', "\n   ", '-'*47)
    print(f' Null Hypothesis: Data has unit root. Non-Stationary.')
    print(f' Significance Level    = {signif}')
    print(f' Test Statistic        = {output["test_statistic"]}')
    print(f' No. Lags Chosen       = {output["n_lags"]}')
    for key,val in r[4].items():
        print(f' Critical value {adjust(key)} = {round(val, 3)}')
    if p_value <= signif:
        print(f" => P-Value = {p_value}. Rejecting Null Hypothesis.")
        print(f" => Series is Stationary.")
    else:
        print(f" => P-Value = {p_value}. Weak evidence to reject the Null Hypothesis.")
        print(f" => Series is Non-Stationary.") 

当在系列上运行时,它会打印以下输出(示例):

 Null Hypothesis: Data has unit root. Non-Stationary.
 Significance Level    = 0.05
 Test Statistic        = -0.5388
 No. Lags Chosen       = 2
 Critical value 1%     = -3.437
 Critical value 5%     = -2.864
 Critical value 10%    = -2.568
 => P-Value = 0.8842. Weak evidence to reject the Null Hypothesis.
 => Series is Non-Stationary.

该脚本循环遍历具有 30 列的 DataFrame,因此返回 30 个这样的输出。我想做的是将这些输出存储在一个文件中,因为它们打印在终端窗口中。文件格式无关紧要,.txt 文件也可以。

标签: pythonloopspdffpdf

解决方案


您可以通过这种方式更改函数adfuller_test以将描述作为字符串返回,而不是将其打印到控制台:

def adfuller_test(series, signif=0.05, name='', verbose=False):
    """Perform ADFuller to test for Stationarity of given series and print report"""
    description = []
    r = adfuller(series, autolag='AIC')
    output = {'test_statistic':round(r[0], 4), 'pvalue':round(r[1], 4), 'n_lags':round(r[2], 4), 'n_obs':r[3]}
    p_value = output['pvalue'] 
    def adjust(val, length= 6): return str(val).ljust(length)
    description.append(f'    Augmented Dickey-Fuller Test on "{name}"\n   {"-"*47}')
    description.append(f' Null Hypothesis: Data has unit root. Non-Stationary.')
    description.append(f' Significance Level    = {signif}')
    description.append(f' Test Statistic        = {output["test_statistic"]}')
    description.append(f' No. Lags Chosen       = {output["n_lags"]}')
    for key,val in r[4].items():
        description.append(f' Critical value {adjust(key)} = {round(val, 3)}')
    if p_value <= signif:
        description.append(f" => P-Value = {p_value}. Rejecting Null Hypothesis.")
        description.append(f" => Series is Stationary.")
    else:
        description.append(f" => P-Value = {p_value}. Weak evidence to reject the Null Hypothesis.")
        description.append(f" => Series is Non-Stationary.") 

    description = "\n".join(description)
    print(description)
    return description

这基本上实例化了一个列表,存储所有描述字符串并逐行连接它们。

将其保存到 txt 文件很简单:

from pathlib import Path

description = []
for name, column in train_set.iteritems():
    description.append(adfuller_test(column, name=column.name))

description = "\n\n".join(description)

Path("output_file_name.txt").write_text(description)

推荐阅读