我可以 trim 来自矩阵的数据,在 Python 中最后一行部分填满吗?
Can I trim data from a matrix with a partially full last row in Python?
我目前正在处理一个可变长度的向量(大约 500,000 个元素),我正在尝试使用 numpy 重塑它。我的代码如下。
tm = 1800 #Number of samples per row (total number of columns)
xyzd = np.genfromtxt(filename, delimiter=',', usecols=(4,5,6,14), encoding='ISO-8859–1', skip_header=1) #reading in the data
xtm = np.reshape(xyzd[:,0],(tm,len(xyzd)//tm+1)) #my attempt to resize
我认为这是一个错误:
ValueError: cannot reshape array of size 488200 into shape (1800,272)
I believe this is because the reshaping leaves the last row only partially full.
有没有一种方法可以告诉 reshape 只填充直到填满它的最后一整行,然后将其余数据扔掉,或者以某种方式进行 reshape 以允许它接受重新整形的矩阵,然后我可以 trim 最后一行。
正如评论中 hpaulj 所建议的那样,reshape
似乎太笨了,没有简单的方法可以做到这一点。所以我的解决方案只涉及做一些数学运算。
columns = len(xyzd)//tm
elements = tm * columns
initial_length = len(xyzd)
n = abs(initial_length-elements)
xyzd = xyzd[:-n, :]
这解决了我的问题。我假设我的数据集的最后几百个数据点并不是那么重要,但我们的采样率如此之高,以至于我们将 5 天的样本减少了大约 2 分钟,但我我愿意冒着什么都不会发生的风险。
我目前正在处理一个可变长度的向量(大约 500,000 个元素),我正在尝试使用 numpy 重塑它。我的代码如下。
tm = 1800 #Number of samples per row (total number of columns)
xyzd = np.genfromtxt(filename, delimiter=',', usecols=(4,5,6,14), encoding='ISO-8859–1', skip_header=1) #reading in the data
xtm = np.reshape(xyzd[:,0],(tm,len(xyzd)//tm+1)) #my attempt to resize
我认为这是一个错误:
ValueError: cannot reshape array of size 488200 into shape (1800,272) I believe this is because the reshaping leaves the last row only partially full.
有没有一种方法可以告诉 reshape 只填充直到填满它的最后一整行,然后将其余数据扔掉,或者以某种方式进行 reshape 以允许它接受重新整形的矩阵,然后我可以 trim 最后一行。
正如评论中 hpaulj 所建议的那样,reshape
似乎太笨了,没有简单的方法可以做到这一点。所以我的解决方案只涉及做一些数学运算。
columns = len(xyzd)//tm
elements = tm * columns
initial_length = len(xyzd)
n = abs(initial_length-elements)
xyzd = xyzd[:-n, :]
这解决了我的问题。我假设我的数据集的最后几百个数据点并不是那么重要,但我们的采样率如此之高,以至于我们将 5 天的样本减少了大约 2 分钟,但我我愿意冒着什么都不会发生的风险。