numpy的:语法/成语投(N,)阵列为(N,1)阵列? [英] numpy: syntax/idiom to cast (n,) array to a (n, 1) array?
本文介绍了numpy的:语法/成语投(N,)阵列为(N,1)阵列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想投numpy的 ndarray
形状的物体(的 N 的,)到具有一个形状( N ,1)。我拿出最好的是推出自己的_to_col功能:
I'd like to cast a numpy ndarray
object of shape (n,) into one having shape (n, 1). The best I've come up with is to roll my own _to_col function:
def _to_col(a):
return a.reshape((a.size, 1))
但是,我很难相信这样一个无处不在的操作是不是已经内置到numpy的语法。我想,我只是一直没能想出正确的谷歌搜索找到它。
But it is hard for me to believe that such a ubiquitous operation is not already built into numpy's syntax. I figure that I just have not been able to hit upon the right Google search to find it.
推荐答案
我会用以下内容:
a[:,np.newaxis]
这是另一种(但也许稍微不太清楚)的方式来写同样的事情是:
An alternative (but perhaps slightly less clear) way to write the same thing is:
a[:,None]
以上所有的(包括你的版本)恒定时操作。
All of the above (including your version) are constant-time operations.
这篇关于numpy的:语法/成语投(N,)阵列为(N,1)阵列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文