python - 如何将 numpy 数组从 3D (4,1,10) 转换为 (10,1,4)

标签 python python-3.x

如果我有一个形状为 (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/

相关文章:

python - 使用 Django 在 AWS 上使用 EC2 对 Websockets 进行故障排除

python - 从字符串中提取函数参数

Python - 需要类似字节的对象,而不是 'str'

python - 尝试将视频分成图像阵列以用于ImageTk

python - Tkinter create_image 不显示

python - "ValueError: Trying to share variable $var, but specified dtype float32 and found dtype float64_ref"尝试使用 get_variable 时

python - 如何在python中以唯一文件名将多个图形保存在目录中?

python - 开放式 CV 轮廓面积计算错误

python - 几个测试的pytest参数化执行顺序

python - 文件内容没有预期的那么长