Python numpy array assign values to each column according to row -
I want to specify line-specific values in each row in the 2D numerical array. Specifically, each row should have the value of reciprocal of its line number. In other words, all the columns in line 1 should have a value of 1, all columns in line 2 should be 1/2, all should be 1/3 in row 3, and so on. I tried this: For curr_n in category (n):
m = 3 n = 10 train = np.empty ([n, m], float): train [curr_n,: ] = 1 / (curr_n + 1) print train but the output is:
[[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.]] What am I doing wrong? I used "float" at the beginning, but I am still getting a solid 0 integer for all but the first line.
Full type of conversion has been added to Python 3, so your original code will work, As:
__future__ from import partition = 3 n = 10 train = np.empty [n, m], float) for curr_n in category (n): train [curr_n,:] = 1 / (curr_n + 1) Print Train __future___ module can be viewed in official docs Or, as in their reply sshashank124 and I put a comment Minister, you can use to apply the float behavior 1 or 1.0.
Comments
Post a Comment