在NumPy中,如何有效地将一维对象转换成二维对象,其中从当前对象推断出单重维度(即列表应该指向1xlength或lengthx1向量)?
# This comes from some other, unchangeable code that reads data files.
my_list = [1,2,3,4]
# What I want to do:
my_numpy_array[some_index,:] = numpy.asarray(my_list)
# The above doesn't work because of a broadcast error, so:
my_numpy_array[some_index,:] = numpy.reshape(numpy.asarray(my_list),(1,len(my_list)))
# How to do the above without the call to reshape?
# Is there a way to directly convert a list, or vector, that doesn't have a
# second dimension, into a 1 by length "array" (but really it's still a vector)?
在最常见的情况下,向数组添加额外维度的最简单方法是在添加额外维度的位置进行索引时使用关键字
None
。例如为什么不简单地加上方括号呢?
。。等等,仔细想想,为什么你的切片分配失败了?它不应该:
甚至:
那expand_dims呢?
具有形状
(1,4)
,而具有形状
(4,1)
。相关问题 更多 >
编程相关推荐