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

标签 python arrays numpy

我不确定在构建数组时我做错了什么。 我做了一些计算,根据这些计算,我完成的数组 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/

相关文章:

Python Scikit Learn GridSearchCV 与 TF IDF 存在问题 - JobLib ValueError?

Python:使用模块 laspy 的列表理解问题

python - 如何在 macOS 上使用 freeTDS 连接本地 mysql

Python-从长度不等的列表列表中获取所有唯一组合并进行替换

c - 通过数组的最有效方法?

datetime - 在 Pandas 中绘制 TimeDeltas

python - 嵌入本地镜像的 HTML 电子邮件正文

arrays - 数组中数组的和

c - 如果用户输入多行/空格,我如何计算字数?

python - 使用 Scipy.optimize 方法 ='SLSQP' 返回初始猜测