使用 numpy reshape 和 "array must be unchanged" 错误

Using numpy reshape and the "array must be unchanged" error

我收到错误消息:

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)

我很难提供数据集;因此,非常感谢任何没有包含我的数据的建议!

您必须删除一些数据点才能适应大小为 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)整数。