Python numpy数组根据行为每列分配值

时间:2021-10-25 21:23:01

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:

我想为2D numpy数组中的每一行指定特定于行的值。特别是,每行应包含其行号倒数的值。换句话说,第1行中的所有列应具有值1,第2行中的所有列应为1/2,第3行中的所有列应为1/3,依此类推。我试过这个:

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.

我究竟做错了什么?我在开始时使用了“float”,但除了第一行之外,我仍然可以获得整数0整数。

2 个解决方案

#1


5  

Implicit type conversion has been added in Python 3, so your original code will then work, as is:

Python 3中添加了隐式类型转换,因此您的原始代码将按原样运行:

from __future__ import division

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

Some information on the __future__ module can be seen in the official docs future module

有关__future__模块的一些信息可以在官方文档未来模块中看到

Or, as sshashank124 put in his answer and I put in the comment, you can use 1. or 1.0 to force float behavior.

或者,当sshashank124输入他的答案并放入评论时,您可以使用1.或1.0来强制浮动行为。

#2


3  

You have a simple type problem. Use 1.0 instead of 1 in your reciprocation. The following works:

你有一个简单的类型问题。在您的互惠中使用1.0而不是1。以下作品:

m = 3
n = 10

train = np.empty([n,m])

for curr_n in range(n):
    train[curr_n,:] = 1.0/(curr_n+1)   #converted 1 to 1.0

print train

Explanation

Although you might think that numpy deals with floats by default, in this case, the numpy array is getting assigned the value after python has had the time to calculate the inverses. It is then that python truncates your floats to an int and numpy innocently converts that sneaky int to a float just as it is supposed to and ends up taking all the blame.

虽然你可能认为numpy默认处理浮点数,但在这种情况下,在python有时间计算反转之后,numpy数组会被赋值。然后python将你的浮点数截断为一个int,并且numpy无辜地将那个偷偷摸摸的int转换为浮点数就像它应该的那样并最终承担所有的责任。

#1


5  

Implicit type conversion has been added in Python 3, so your original code will then work, as is:

Python 3中添加了隐式类型转换,因此您的原始代码将按原样运行:

from __future__ import division

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

Some information on the __future__ module can be seen in the official docs future module

有关__future__模块的一些信息可以在官方文档未来模块中看到

Or, as sshashank124 put in his answer and I put in the comment, you can use 1. or 1.0 to force float behavior.

或者,当sshashank124输入他的答案并放入评论时,您可以使用1.或1.0来强制浮动行为。

#2


3  

You have a simple type problem. Use 1.0 instead of 1 in your reciprocation. The following works:

你有一个简单的类型问题。在您的互惠中使用1.0而不是1。以下作品:

m = 3
n = 10

train = np.empty([n,m])

for curr_n in range(n):
    train[curr_n,:] = 1.0/(curr_n+1)   #converted 1 to 1.0

print train

Explanation

Although you might think that numpy deals with floats by default, in this case, the numpy array is getting assigned the value after python has had the time to calculate the inverses. It is then that python truncates your floats to an int and numpy innocently converts that sneaky int to a float just as it is supposed to and ends up taking all the blame.

虽然你可能认为numpy默认处理浮点数,但在这种情况下,在python有时间计算反转之后,numpy数组会被赋值。然后python将你的浮点数截断为一个int,并且numpy无辜地将那个偷偷摸摸的int转换为浮点数就像它应该的那样并最终承担所有的责任。