python - 用python切割数据集

标签 python python-2.7 python-3.x numpy

我正在研究一个月内每分钟的太阳频率。所以我有一个包含 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/

相关文章:

python - 15 个 Python 脚本变成一个可执行文件?

python-2.7 - 使用 Scipy 的 coo_matrix.todense() 的 Python TypeError

python - 如何在 python 中使用继承将多个对象连接成一个对象? (在运行时)

python - CSV 文件到 SQL 插入语句

Python最长/平均 'losing'以字符串中的二进制数字序列运行

python - 如何在一个函数中休眠而不休眠整个程序

python - opencv安装ffmpeg报错

python - 如何在Python的IDE中禁止显示警告? (IDE为Jupyter)

python - 在 Blender 中以编程方式创建模型

Python:尽可能高效地使用三角函数估计 Pi