且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Numpy TypeError:仅长度为1的数组可以转换为Python标量(重塑)

更新时间:2022-03-21 18:41:48

您想要X_train_s的第一维和第二维的长度,但是当您这样做时

You wanted the lengths of the first and second dimensions of X_train_s, but when you did

(X_train_s[0], X_train_s[1], 1)

您选择了第一行和第二行整行,而不是第一维和第二维的长度.如果要访问尺寸长度,则应已索引数组的shape:

you took the first and second entire rows, not the lengths of the first and second dimensions. If you wanted to access dimension lengths, you should have indexed the array's shape:

(X_train_s.shape[0], X_train_s.shape[1], 1)

您可能还想考虑将这个额外的长度为1的轴添加到数组的其他方法,例如使用np.newaxis(又称为None)建立索引:

You might also want to consider other ways of adding this extra length-1 axis to the array, such as indexing with np.newaxis (a.k.a. None):

a1 = X_train_s[:, :, np.newaxis]