gpt4 book ai didi

python - 如何在 numpy 中使用 ones() 获取数字数组?

转载 作者:太空宇宙 更新时间:2023-11-04 08:26:51 24 4
gpt4 key购买 nike

您好,我在 Matlab 中有一段代码生成以下序列。

[ones(1,6*2) 2 ones(1,6*2-1) 2 ones(1,6*2) 1]

ans =

Columns 1 through 18
1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1

Columns 19 through 36
1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1

Columns 37 through 38
1 1

我想在 Python 中生成类似的数字数组。

我尝试生成如下。

ConvStride = [np.ones((12,),dtype=int),2,np.ones((11,),dtype=int),2,np.ones((12,),dtype=int),1]

Ans= [1 1 1 1 1... 1],2,[1 1 1 ... 1],2,[1 1 1 1....1],1

ConvStride = [np.ones((12,),dtype=int),2,np.ones((11,),dtype=int),2,np.ones((12,),dtype=int),1]

需要

[ 1 1 1 .....1 2 1 1 1 .....1 2 111 ....1 1]

你能告诉我这里的工作吗。

最佳答案

使用np.r_:

np.r_[np.ones(12,int),2,np.ones(11,int),2,np.ones(12,int)]
# array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1,
# 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])

关于python - 如何在 numpy 中使用 ones() 获取数字数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56248030/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com