ValueError:新阵列的总大小必须保持不变(numpy表示重新成形)
我想重塑我的数据向量,但当我运行代码时ValueError:新阵列的总大小必须保持不变(numpy表示重新成形),numpy,reshape,Numpy,Reshape,我想重塑我的数据向量,但当我运行代码时 from pandas import read_csv import numpy as np #from pandas import Series #from matplotlib import pyplot series =read_csv('book1.csv', header=0, parse_dates=[0], index_col=0, squeeze=True) A= np.array(series) B = np.reshape(10,1
from pandas import read_csv
import numpy as np
#from pandas import Series
#from matplotlib import pyplot
series =read_csv('book1.csv', header=0, parse_dates=[0], index_col=0, squeeze=True)
A= np.array(series)
B = np.reshape(10,10)
print (B)
我发现了错误
结果=getattr(asarray(obj),方法)(*args,**kwds)
ValueError:新数组的总大小必须保持不变
我的数据
Month xxx
1749-01 58
1749-02 62.6
1749-03 70
1749-04 55.7
1749-05 85
1749-06 83.5
1749-07 94.8
1749-08 66.3
1749-09 75.9
1749-10 75.5
1749-11 158.6
1749-12 85.2
1750-01 73.3
.... ....
.... ....
你尝试做的事情似乎有两个问题。第一个涉及如何读取熊猫中的数据:
series = read_csv('book1.csv', header=0, parse_dates=[0], index_col=0, squeeze=True)
print(series)
>>>>Empty DataFrame
Columns: []
Index: [1749-01 58, 1749-02 62.6, 1749-03 70, 1749-04 55.7, 1749-05 85, 1749-06 83.5, 1749-07 94.8, 1749-08 66.3, 1749-09 75.9, 1749-10 75.5, 1749-11 158.6, 1749-12 85.2, 1750-01 73.3]
这并不是在数据框中为您提供一列带有索引日期的浮动,而是将每一行放入索引、日期和值中。我认为您需要添加delimtier='
,以便正确地分割行:
series =read_csv('book1.csv', header=0, parse_dates=[0], index_col=0, delimiter=' ', squeeze=True)
>>>> Month
1749-01-01 58.0
1749-02-01 62.6
1749-03-01 70.0
1749-04-01 55.7
1749-05-01 85.0
1749-06-01 83.5
1749-07-01 94.8
1749-08-01 66.3
1749-09-01 75.9
1749-10-01 75.5
1749-11-01 158.6
1749-12-01 85.2
1750-01-01 73.3
Name: xxx, dtype: float64
这将为您提供日期作为索引,列中有“xxx”值
其次是重塑。在这种情况下,错误是相当描述性的。如果要使用numpy.reformate
则不能将布局的元素数与原始数据的元素数不同。例如:
import numpy as np
a = np.array([1, 2, 3, 4, 5, 6]) # size 6 array
a.reshape(2, 3)
>>>> [[1, 2, 3],
[4, 5, 6]]
这很好,因为数组开始时的长度为6,我正在将其重塑为2x3
,并且2x3=6
但是,如果我尝试:
a.reshape(10, 10)
>>>> ValueError: cannot reshape array of size 6 into shape (10,10)
我得到了这个错误,因为我需要10 x 10=100个元素来完成这个重塑,而我只有6个元素
没有完整的数据集是不可能确定的,但我认为这与您遇到的问题相同,尽管您正在将整个数据帧转换为numpy数组