如果我有一个形状为 (4,1,10) 的 numpy 数组,如何创建另一个具有相同元素但形状为 (10,1,4) 的数组。因为我尝试了转置功能,但没有帮助。我尝试单独转置仍然没有运气。谁能告诉我在下面的代码中我做错了什么。
import pandas
import numpy
dataframe = pandas.read_csv('LBMP_PJM2017.csv', delimiter=',', usecols=
[4,5,6,7,8], engine='python')
features = dataframe[:10]
labels = numpy.array(features['TWI Zonal LBMP'])
print(labels)
attribute1 = numpy.array(features['TWI Zonal Losses'])
attribute1 = numpy.transpose(attribute1)
attribute2 = numpy.array(features['DAM Zonal LBMP'])
attribute2 = numpy.transpose(attribute2)
attribute3 = numpy.array(features['DAM Zonal Losses'])
attribute3 = numpy.transpose(attribute3)
attribute4 = numpy.array(features['DAM Zonal Congestion'])
attribute4 = numpy.transpose(attribute4)
features.drop('TWI Zonal LBMP', axis=1)
features = numpy.array(features)
data_x = numpy.array([[attribute1[:]],[attribute2[:]],[attribute3[:]],
[attribute4[:]]])
print('--dataX--',data_x.shape,'--dataX--')
...
输出:
.
:
--dataX-- (4, 1, 10) --dataX--
最佳答案
使用 numpy reshape() 实用程序。
假设您有形状为 (4, 1, 10)
的 my_old_array
,您可以执行以下操作:
my_new_array = my_old_array.reshape(10, 1, 4)
完整文档:https://docs.scipy.org/doc/numpy-1.15.1/reference/generated/numpy.reshape.html
关于python - 如何将 numpy 数组从 3D (4,1,10) 转换为 (10,1,4),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52914747/