【问题标题】:How to organize python array with numpy zeros如何用 numpy zeros 组织 python 数组
【发布时间】:2016-11-14 00:46:34
【问题描述】:

研究一个简单的 SIR 模型。 我需要组织打印到终端的值,以便以我的老师可以轻松遵循的方式进行组织。 我被告知要查看 numpy.zeros,但我是 python 新手,需要有人帮助。

import matplotlib.pyplot as plt
import numpy
beta = 0.24
gamma = 0.142857
Tstart = 0
Tend = 151
r = 0
s = (306.8 * 10**6)
i = (22 * 10**6)

def compute_next_day(t,R,I,S):
    R[t] = gamma * I[t - 1] + R[t - 1]
    I[t] = (beta * I[t-1] * S[t-1] / (r+i+s)) - gamma * I[t-1] + I[t-1]
    S[t] = - (beta * I[t-1] * S[t-1] / (r+i+s)) + S[t-1]
    print S[t-1], I[t-1], R[t-1]


def compute_entire_period(Tstart, Tend, R, I, S):
    R[Tstart] = r
    I[Tstart] = i
    S[Tstart] = s
    for t in range(Tstart + 1, Tend):
        compute_next_day(t, R, I, S)


R = range(Tstart, Tend)
I = range(Tstart, Tend)
S = range(Tstart, Tend)

def graph(R, I, S):
    plt.plot(R)
    plt.plot(I)
    plt.plot(S)

plt.ylabel("Population")
plt.xlabel("Time")
plt.show()

(compute_entire_period(Tstart, Tend, R, I, S))
(graph(R,I,S))

打印到终端的值是 S、I、R 值...

what currently appears on terminal want to organize like this

【问题讨论】:

  • 我觉得你的问题很不清楚。你到底想做什么?
  • 展示示例 - 你得到什么和你期望什么。
  • 添加了一些图片,以便您了解我的目标
  • 你需要从你的列表(范围)中创建一个单一的 numpy 数组,如果你print它,它的漂亮打印版本将看起来像你想要的那样。
  • 整理好了!但是,是否可以将值导出到另一个文档(例如 csv)?

标签: python arrays python-3.x numpy matplotlib


【解决方案1】:

我没有足够的积分来评论,所以我会在评论部分“回答”你的问题,你可以像这样将值导出到 csv:

import csv

a = range(2, 10)
with open("output.csv", "wb") as file:
    writer = csv.writer(file)
    writer.writerows(a)

【讨论】:

    猜你喜欢
    • 2020-09-01
    • 2011-07-17
    • 1970-01-01
    • 1970-01-01
    • 2021-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多