粗略周期性查找

发现动物种群演化中的周期 (../../../../data/populations.txt)

加载数据

import numpy as np
data = np.loadtxt("../../../../data/populations.txt")
years = data[:, 0]
populations = data[:, 1:]

绘制数据

import matplotlib.pyplot as plt
plt.figure()
plt.plot(years, populations * 1e-3)
plt.xlabel("Year")
plt.ylabel(r"Population number ($\cdot10^3$)")
plt.legend(["hare", "lynx", "carrot"], loc=1)
plot periodicity finder
<matplotlib.legend.Legend object at 0x7f791eb29b80>

绘制周期

import scipy as sp
ft_populations = sp.fft.fft(populations, axis=0)
frequencies = sp.fft.fftfreq(populations.shape[0], years[1] - years[0])
periods = 1 / frequencies
plt.figure()
plt.plot(periods, abs(ft_populations) * 1e-3, "o")
plt.xlim(0, 22)
plt.xlabel("Period")
plt.ylabel(r"Power ($\cdot10^3$)")
plt.show()
plot periodicity finder
/home/runner/work/scientific-python-lectures/scientific-python-lectures/intro/scipy/examples/solutions/plot_periodicity_finder.py:39: RuntimeWarning: divide by zero encountered in divide
periods = 1 / frequencies

可能存在一个大约 10 年的周期(从图中可以明显看出),但是对于这种粗略的方法,数据不足以得出更多结论。

脚本总运行时间:(0 分钟 0.130 秒)

由 Sphinx-Gallery 生成的图库