I want to assign row-specific values to each row in a 2D numpy array. In particular, each row should contain the value of the reciprocal of its row number. In other words, all columns in row 1 should have values 1, all columns in row 2 should be 1/2, all in row 3 should be 1/3, and so on. I tried this:
m = 3
n = 10
train = np.empty([n,m], float)
for curr_n in range(n):
train[curr_n,:] = 1/(curr_n+1)
print train
But the output is:
[[ 1. 1. 1.]
[ 0. 0. 0.]
[ 0. 0. 0.]
[ 0. 0. 0.]
[ 0. 0. 0.]
[ 0. 0. 0.]
[ 0. 0. 0.]
[ 0. 0. 0.]
[ 0. 0. 0.]
[ 0. 0. 0.]]
What am I doing wrong? I used "float" at the beginning, but I'm still getting solid 0 integers for all but the first row.
1/(curr_n+1)
always return 1 or 0 becausecurr_n
is a type of integer. You might want to do like1.0/(curr_n+1)
or1/float(curr_n+1)
. – keimina Apr 21 '14 at 13:51