使用numpy reshape和"数组必须保持不变"错误

时间:2017-04-01 00:20:34

标签: python numpy reshape

我收到错误:

ValueError: total size of new array must be unchanged

我似乎无法弄清楚我的数组是如何被更改以抛出此错误,并指向最后一行:

      1 data = hourlyElectricityForVisualization.values
----> 2 data = data.reshape((len(data)/24/7),24*7))

请注意:

data.shape = (2137,)
len(data) = 2137

导致此错误的代码如下:

hourlyElectricity = pd.read_excel('D:/hourlyElectricity.xlsx')

index = (hourlyElectricity['startTime'] >= np.datetime64('2016-01-01')) & (hourlyElectricity['startTime'] < np.datetime64('2016-03-31'))
hourlyElectricityForVisualization = hourlyElectricity.loc[index,'electricity-kWh']

print ("Data length: ", len(hourlyElectricityForVisualization)/24/7, " weeks")
data = hourlyElectricityForVisualization.values
data = data.reshape((len(data)/24/7,24*7)

我很难提供数据集;所以没有我包含数据的任何建议都非常感谢!

1 个答案:

答案 0 :(得分:1)

您必须删除一些数据点才能适应大小为N到24 * 7的新矩阵。您可以执行以下操作:

data = data[:(np.shape(data)[0] - np.shape(data)[0]%(24*7))] data = np.reshape(data, (len(data)/24/7, 24*7))

此代码将删除最后121个点,使数据长度除以(24 * 7)整数的结果。