Question

I'm not sure what I'm doing wrong in building up an array. I do some calculations according to which my completed array, z, should look like this:

[[  -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]]

But my array actually turns out with the entire first column being zeros, like this:

[[  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]]

I don't understand where I'm going wrong. Here's my code, which seems simple enough:

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

As you can see, there's another array that I'm calculating, called weights, but that doesn't seem to have any issues. It has all rows and columns as expected. What's wrong with the way I'm handling z?

Was it helpful?

Solution

You are creating a new array in every j-loop-step. So only the last row will be filled in the final z

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top