相关疑难解决方法(0)

将单个维度添加到NumPy向量的有效方法,以便切片分配起作用

在NumPy中,如何有效地将1-D对象制作成2-D对象,其中单个维度是从当前对象推断出来的(即列表应该是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 …
Run Code Online (Sandbox Code Playgroud)

python numpy

19
推荐指数
2
解决办法
1万
查看次数

标签 统计

numpy ×1

python ×1