gpt4 book ai didi

python - 构建 numpy 数组的列 - 为什么第一列仍然全是零?

转载 作者:太空宇宙 更新时间:2023-11-03 18:23:55 24 4
gpt4 key购买 nike

我不确定在构建数组时我做错了什么。我做了一些计算,根据这些计算,我完成的数组 z 应该如下所示:

[[  -2.00000000e+000   2.00000000e+000]
[ -2.00000000e+000 2.00000000e+000]
[ 2.00000000e+000 -2.00000000e+000]
[ -2.00000000e+000 2.00000000e+000]
[ 2.00000000e+000 -2.00000000e+000]
[ -2.00000000e+000 2.00000000e+000]
[ -2.00000000e+000 2.00000000e+000]
[ 2.00000000e+000 -2.00000000e+000]
[ 2.00000000e+000 -2.00000000e+000]
[ -2.00000000e+000 2.00000000e+000]
[ -2.00000000e+000 2.00000000e+000]
[ -2.00000000e+000 2.00000000e+000]
[ 2.00000000e+000 -2.00000000e+000]
[ 2.00000000e+000 -2.00000000e+000]
[ 2.00000000e+000 -2.00000000e+000]
[ 2.00000000e+000 -2.00000000e+000]
[ 2.00000000e+000 -2.00000000e+000]
[ 2.00000000e+000 -2.00000000e+000]
[ -2.00000000e+000 2.00000000e+000]
[ -2.00000000e+000 2.00000000e+000]]

但我的数组实际上结果是整个第一列为零,如下所示:

[[  5.28650241e-322   2.00000000e+000]
[ 0.00000000e+000 2.00000000e+000]
[ 0.00000000e+000 -2.00000000e+000]
[ 0.00000000e+000 2.00000000e+000]
[ 0.00000000e+000 -2.00000000e+000]
[ 0.00000000e+000 2.00000000e+000]
[ 0.00000000e+000 2.00000000e+000]
[ 0.00000000e+000 -2.00000000e+000]
[ 0.00000000e+000 -2.00000000e+000]
[ 0.00000000e+000 2.00000000e+000]
[ 0.00000000e+000 2.00000000e+000]
[ 0.00000000e+000 2.00000000e+000]
[ 0.00000000e+000 -2.00000000e+000]
[ 0.00000000e+000 -2.00000000e+000]
[ 0.00000000e+000 -2.00000000e+000]
[ 0.00000000e+000 -2.00000000e+000]
[ 0.00000000e+000 -2.00000000e+000]
[ 0.00000000e+000 -2.00000000e+000]
[ 0.00000000e+000 2.00000000e+000]
[ 0.00000000e+000 2.00000000e+000]]

我不明白我哪里错了。这是我的代码,看起来很简单:

import sys
import numpy as np

def is_class(j, c):
if j == c:
return 1
else:
return 0

n = 20
m = 2

weights = np.empty([n,m])
for curr_n in range(n):
weights[curr_n,:] = 1.0/(n)

beta = np.empty([m,n])
for curr_beta in range(m):
beta[curr_beta,:] = 1./m

y = np.array([1, 1, 0, 1, 0, 1, 1, 0, 0, 1,1,1,0,0,0,0,0,0,1,1])

# BELOW IS THE PART WHERE THERE MUST BE SOME PROBLEM

for j in range(m): # computing working responses, z
z = np.empty([n,m])
for i in range(n):
# the calculation below is tested and works correctly
i_class = y[i]
z[i][j] = (is_class(j,i_class) - beta[j][i])/((beta[j][i])*(1. - beta[j][i]))
weights[i][j] = beta[j][i]*(1. - beta[j][i])
print z[i,j] # this gives the right value that it ought to have
# these values can be compared with the first matrix
# which I put up at the beginning of the question

print "Working responses, z:"
print z # now when I print it out, the first column is suddenly full of zeros

正如您所看到的,我正在计算另一个数组,称为权重,但这似乎没有任何问题。它具有预期的所有行和列。我处理 z 的方式有什么问题?

最佳答案

您将在每个 j 循环步骤中创建一个新数组。所以最后的z只会填最后一行

关于python - 构建 numpy 数组的列 - 为什么第一列仍然全是零?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23579446/

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