numpy 如何在python中进行动态计算

rqenqsqc  于 2023-06-06  发布在  Python
关注(0)|答案(5)|浏览(161)

如何使它像这样,所以奇数索引将做(-),偶数索引将做(+),最大迭代是6。迭代1 + 10、迭代2 - 20、迭代3 + 30、迭代4 - 40、迭代5 + 50、迭代6 - 60

AA = np.array([[9.27914]+10,
              [9.33246]-20,
              [9.26303]+30,
              [9.30597]-40,
              [9.6594 ]+50,
              [9.04283]-60,
              [8.88866]+10,
              [8.89956]-20])

预期结果:

AA=np.array([
    [19.27914],
    [-10.66754],
    [39.26303],
    [-30.69403],
    [59.6594],
    [-50.95717],
    [18.88866],
    [-11.10044],
])

我尝试使用此代码,但不工作

max_iter = 6
iter = 0
for i in range(len(AA)):
    if i % 2 == 0:
        AA[i][0] = AA[i][0] + (iter % max_iter)
    else:
       AA[i][0] = AA[i][0] - (iter % max_iter)
    iter += 10
nwlqm0z1

nwlqm0z11#

这是numpy,不要循环,使用矢量代码。
模运算符(%)对于重新开始序列非常有用。

max_iter = 6

a = np.arange(len(AA))[:,None]

out = AA + (a%max_iter+1)*10 * np.where(a%2, -1, 1)

输出:

array([[ 19.27914],
       [-10.66754],
       [ 39.26303],
       [-30.69403],
       [ 59.6594 ],
       [-50.95717],
       [ 18.88866],
       [-11.10044]])

使用的输入:

AA = np.array([[9.27914],
               [9.33246],
               [9.26303],
               [9.30597],
               [9.6594 ],
               [9.04283],
               [8.88866],
               [8.89956]])

中间体:

(a%6+1)*10

array([[10],
       [20],
       [30],
       [40],
       [50],
       [60],
       [10],
       [20]])

np.where(a%2, -1, 1)

array([[ 1],
       [-1],
       [ 1],
       [-1],
       [ 1],
       [-1],
       [ 1],
       [-1]])
hec6srdp

hec6srdp2#

你们很亲密。只需要做三个小的改变。我在括号内添加了一个+1,为每个数组操作添加了 *10,并将iter += 10更改为array += 1

max_iter = 6
iter = 0
for i in range(len(AA)):
    if i % 2 == 0:
        AA[i][0] = AA[i][0] + (iter % max_iter+1)*10
    else:
        AA[i][0] = AA[i][0] - (iter % max_iter+1)*10
    iter += 1

实际上,如果使用以下代码,可以删除if else语句并在一行中完成:

AA[i][0] = AA[i][0] +(-1)**(i)* (iter % max_iter+1)*10
j8ag8udp

j8ag8udp3#

您遇到的根本问题是混淆了变量iter的含义。有时你似乎想成为10的倍数,有时是1到6之间的数字。让我们把它分离出来,稍微清理一下代码。

给出:

import numpy
AA = numpy.array([
    [9.27914],
    [9.33246],
    [9.26303],
    [9.30597],
    [9.6594 ],
    [9.04283],
    [8.88866],
    [8.89956]
])

然后我们可以:

max_iter = 6
iter = 1
for i in range(len(AA)):
    delta = 10 * (iter % max_iter)
    sign = 1 if i % 2 == 0 else -1
    AA[i][0] += sign * delta
    iter += 1

print(AA)

获取:

[
 [ 19.27914]
 [-10.66754]
 [ 39.26303]
 [-30.69403]
 [ 59.6594 ]
 [  9.04283]
 [ 18.88866]
 [-11.10044]
]

或者如果你喜欢更接近你开始的东西:

max_iter = 6
iter = 1
for i in range(len(AA)):
    if i % 2 == 0:
        AA[i][0] += 10 * (iter % max_iter)
    else:
        AA[i][0] -= 10 * (iter % max_iter)
    iter += 1

print(AA)
p1tboqfb

p1tboqfb4#

试试这个:

AA = [
    9.27914,
    9.33246,
    9.26303,
    9.30597,
    9.6594,
    9.04283,
    8.88866,
    8.89956
]

BB=[]

iter = 10
for i in range(len(AA)):
    if i % 2 == 0:
        var = AA[i] + (iter)
    else:
       var = AA[i] - (iter)
    BB.append(var)
    iter += 10
    
for i in range(len(BB)):
    print(BB[i])

注意:省略了最大迭代次数…我会尽快修好的。

relj7zay

relj7zay5#

我为你写了一个函数,没有循环,并给予你想要的结果。

AA = np.array([[9.27914],
          [9.33246],
          [9.26303],
          [9.30597],
          [9.6594 ],
          [9.04283],
          [8.88866],
          [8.89956]])

expected_output = np.array([[19.27914],
          [-10.66754],
          [39.26303],
          [-30.69403],
          [59.6594],
          [-50.95717],
          [18.88866],
          [-11.10044],
])

def test(AA, iterstep, maxiter):
    aa_shape = AA.shape
    arr1 = np.tile(np.arange(1, max_iter+1), (aa_shape[0] // maxiter) + 1)[:aa_shape[0]] 
    sign_arr = np.tile(np.array([1, -1]), (aa_shape[0] // 2) + 1)[:aa_shape[0]]
    _ = (arr1*sign_arr)*10
    _=_.reshape(AA.shape)
    return AA + _

res = test(AA, 10, 6)

# Test 
assert (np.all(np.isclose(res, expected_output)))

相关问题