我正在尝试使用下面的代码生成一组数组,我也将尝试解释我所做的工作
首先:
example = np.zeros(8,dtype=int)
print(example)
这给了我输出:[0 0 0 0 0 0 0 0]
然后:
input=np.array([],int)
for i in range(0,8):
if i <8:
example[i-1]=0
example[i]=1
print(example)
input = np.append(input,example)
print(input)
然后它给了我:
[0 1 0 0 0 0 0 0]
[0 0 1 0 0 0 0 0]
[0 0 0 1 0 0 0 0]
[0 0 0 0 1 0 0 0]
[0 0 0 0 0 1 0 0]
[0 0 0 0 0 0 1 0]
[0 0 0 0 0 0 0 1]
最后我做了这个input = np.append(input,example)
,它给了我:[1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1]
,但这是我想要的方式:
[[1 0 0 0 0 0 0 0]
[0 1 0 0 0 0 0 0]
[0 0 1 0 0 0 0 0]
[0 0 0 1 0 0 0 0]
[0 0 0 0 1 0 0 0]
[0 0 0 0 0 1 0 0]
[0 0 0 0 0 0 1 0]
[0 0 0 0 0 0 0 1]]
或者类似的东西。现在,我试着搜索,我得到了错误的任何我try.hope我得到尽快。
发布于 2020-10-31 23:45:16
如果我没理解错的话,你想要的是单位矩阵。
X = np.identity(8, dtype=int)
发布于 2020-10-31 23:45:36
您可以使用.reshape()
重塑数组(不要使用input
作为变量名,此处的myInput
应为您的input
变量):
myInput = myInput.reshape(8,8)
你也可以使用np.identity
来缩短它
myInput = np.identity(8, dtype=int)
发布于 2020-10-31 23:45:39
要完成您的尝试,您应该编写input = np.append(input,example).reshape(8,8)
。或者,也可以使用out = np.diag(np.ones(8))
直接生成所需的输出
https://stackoverflow.com/questions/64623453
复制相似问题