将函数输出保存为 .pdf
Save function output to .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
函数 returns NoneType 对象。
更新:adfuller_test
对时间序列数据执行 Augmented 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 上循环,因此 returns 有 30 个这样的输出。我想做的是将这些输出存储在一个文件中,因为它们在终端 window 中打印出来。文件格式无关紧要,.txt 文件也可以。
您可以通过这种方式将函数 adfuller_test
更改为 return 将描述作为字符串而不是将其打印到控制台:
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)
我正在对数据框中的列迭代一个函数,如下所示:
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
函数 returns NoneType 对象。
更新:adfuller_test
对时间序列数据执行 Augmented 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 上循环,因此 returns 有 30 个这样的输出。我想做的是将这些输出存储在一个文件中,因为它们在终端 window 中打印出来。文件格式无关紧要,.txt 文件也可以。
您可以通过这种方式将函数 adfuller_test
更改为 return 将描述作为字符串而不是将其打印到控制台:
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)