如何在numpy中使用ones()获得数字数组?

2024-09-29 23:22:35 发布

您现在位置:Python中文网/ 问答频道 /正文

嗨,我有一个在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 2 1 1…..1 2 111…..1 1]

你能告诉我这附近有什么工作吗。你知道吗


Tags: columns代码npones序列数字数组int
2条回答

您可以使用类似的python语法创建一个列表,然后将其转换为numpy数组

import numpy as np

x = [1]*(6*2) + [2] + [1]*(6*2-1) + [2] + [1]*(6*2) + [1]
ans = np.array(x)

如果您想用numpy来完成这一切,您可以使用hstack。你知道吗

np.hstack([np.ones(6*2, int), 2, np.ones(6*2-1, int), 2, np.ones(6*2, int), 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])

相关问题 更多 >

    热门问题