我正在研究一个月内每分钟的太阳频率。所以我有一个包含 43200 个元素的矩阵 M,每分钟一个。
对所有元素进行功率谱的方法是:
import numpy as np
import pylab as pl
from scipy import fftpack
M=np.loadtxt('GOLF-SOHO-Sol.dat')
N=len(M)
dt=1
t= np.arange(0., N, dt)
yt = M
frecs= fftpack.fftfreq(yt.size, dt)
fft_yt = fftpack.fft(yt)
vector_amp = np.abs(fft_yt)
pl.subplot(212)
pl.xlim(-0.5, 0.5)
pl.plot(frecs, vector_amp, color="blue", linestyle="-", linewidth=1.5)
pl.xlabel('Frecuencia (Hz)')
pl.ylabel('Espec. amplitud')
pl.title('Espectro de amplitud')
marcasx = np.arange(-0.5, 0.5, 0.1) # vector de marcas en x
pl.xticks(marcasx)
pl.show()
问题是现在我想对这个矩阵进行一些削减。我只需要每 12 小时一次的数据(晴天时)。因此,在我的矩阵 M 中,我需要例如前 720 相同,但接下来的 720 必须为 0,接下来的 720 为原始数据,接下来的 720 为 0,等等。
我该如何计算这个?我应该用 while 做一个 bucle,其中每 720 个日期都会改变一次。
提前谢谢您。我希望我说得清楚。
最佳答案
# your data values (all ones for illustration)
>>> values = numpy.ones( (43200,) )
# reshape to matrix with rows of 720 samples
>>> mat = values.reshape( (60, 720) )
# now it's easy to set alternating rows to 0.0
>>> mat[1::2, :] = 0
# and because y is a view of your data, "values" now
# has zeroes in the right places
>>> values[710:730]
array([ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0.])
关于python - 用python切割数据集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29236097/