我如何存储我的结果的形式和数值输出的形式/是否有一种通用方法来存储任何类型的结果?

发布于 2025-02-01 15:14:49 字数 302 浏览 2 评论 0原文

我正在编写一个很长一段时间的程序。我想多次运行此程序,以便可以看到结果对参数调整的依赖性。 因此,假设与以下情况类似的情况:

parameter=1

"Big code that takes a long time"


print(output, "output that depends on t")
plt.plot(x,y)

现在将参数更改为2并重新运行。我希望能够提取前一个结果,以便可以比较它们。

因此,我想以某种方式存储它们,以便下次我需要查看结果时,我只需要执行几行,而存储的结果确实很快出现。

I am writing a program that runs for a long time. I want to run this program many times so that I can see the dependence of my results on the tweaking of parameters.
So, suppose a situation similar to the following:

parameter=1

"Big code that takes a long time"


print(output, "output that depends on t")
plt.plot(x,y)

Now change the parameter to 2 and re-run again. I want to be able to pull the results of the previous one so that I can compare them.

So I want to sort of store them somehow so that the next time I need to look at the results I just have to execute a few lines and the stored results come up really quickly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执笔绘流年 2025-02-08 15:14:49

您可以将所有信息(例如输入,参数和输出)存储在字典中。然后,您可以使用dict进行进一步的绘图和分析。

在这里,我添加了一个最小的可重复示例。您可以将其用作您需求的参考。以下代码将此图作为输出产生。 ”

import matplotlib.pyplot as plt
import numpy as np
import random

def big_code(param, input):
    output = [i + param**(random.randrange(2, 5)) for i in input]
    return output

def plot_experiments(info):
    rows, cols = 1, 6
    _, axs = plt.subplots(rows,cols)
    i = 0

    for val in info.values():
        param_idx = val['param']
        axs[i].plot(val['input'], val['output'])
        axs[i].set_title(f'param {param_idx}')       
        i+=1

    for ax in axs.flat:
        ax.set(xlabel='x-label', ylabel='y-label')

    # Hide x labels and tick labels for top plots and y ticks for right plots.
    for ax in axs.flat:
        ax.label_outer()
    plt.show()

if __name__ == '__main__':
    input_params = [1,2,3,4,5,6]
    input_list = np.array(list(range(2000)))
    info = {}
    
    for exp_id ,param in enumerate(input_params):
        # Run your big code to get output
        output = big_code(param, input_list)

        # Save your output to a dataframe
        info[exp_id] = {'input': input_list, 'output': output, 'param': param }
        
    # Access your dict and plot
    plot_experiments(info)

You can store all the information such as the inputs, params, and outputs in a dictionary. You can then use the dict to do further plotting and analysis.

Here I add a minimal reproducible example. You can use this as a reference for your needs. The below code produces this plot as an output.input_vs_output_various_params

import matplotlib.pyplot as plt
import numpy as np
import random

def big_code(param, input):
    output = [i + param**(random.randrange(2, 5)) for i in input]
    return output

def plot_experiments(info):
    rows, cols = 1, 6
    _, axs = plt.subplots(rows,cols)
    i = 0

    for val in info.values():
        param_idx = val['param']
        axs[i].plot(val['input'], val['output'])
        axs[i].set_title(f'param {param_idx}')       
        i+=1

    for ax in axs.flat:
        ax.set(xlabel='x-label', ylabel='y-label')

    # Hide x labels and tick labels for top plots and y ticks for right plots.
    for ax in axs.flat:
        ax.label_outer()
    plt.show()

if __name__ == '__main__':
    input_params = [1,2,3,4,5,6]
    input_list = np.array(list(range(2000)))
    info = {}
    
    for exp_id ,param in enumerate(input_params):
        # Run your big code to get output
        output = big_code(param, input_list)

        # Save your output to a dataframe
        info[exp_id] = {'input': input_list, 'output': output, 'param': param }
        
    # Access your dict and plot
    plot_experiments(info)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文