python - 在不同的循环python中迭代四个列表

标签 python loops iteration

我有这四个问题。列表:

x1 = [1,0,0] ; prob. of No goal scored per timeslot.
x2 = [0,1,0] ; prob. of Home Team scoring per timeslot.
x3 = [0,0,1] ; prob. of Away Team scoring per timeslot.
y = [0.97, 0.02, 0.01]; constant prob. per timeslot.

在进球之前,我想要 x1y 的平方差 (sse) 之和。然后 ssex2x3y 计算,具体取决于谁先得分。然后它循环回到 x1y 直到任一团队再次进球,依此类推...

Here is what I have attempted so far.

Total timeslot = 15.
Order of timeslot
Home_Goal_timeslot = 8th, 14th. i.e. at two different timeslots.
Away_Goal_timeslot = 11th
No_Goal = 12. i.e. number of timeslots 12


x1 = np.array([1,0,0])
x2 = np.array([0,1,0])
x3 = np.array([0,0,1])
y = np.array([0.97, 0.02, 0.01])

def sum_squared_diff(x1, x2, x3, y, k):
    for k in range(15):
        if k == 'No_Goal':
           return sum((x1 - y)**2)
        elif k == 'Home_Goal':    
            return  sum((x2 - y)**2)
        else:
            return  sum((x3 - y)**2)





sum_squared_diff(x1, x2, x3, y, k=15)

Out[1912]: 1.9213999999999998     

print(sum((x1 - y)**2)**12, sum((x2 - y)**2)**2, sum((x3 - y)**2)**1)

    5.669391237529683e-35 3.6153219599999997 1.9213999999999998
add up to 5.5367219599999995

为什么上述解决方案不同? 我更喜欢在每个时间段之后添加 sse 并在进球时做一个记录。所以我可以返回并向 y 值添加/减去一些 infinitesimal epsilon 并测量测试...

This is the out come i want

 0.0014000000000000017,
 0.0014000000000000017,
 0.0014000000000000017,
 0.0014000000000000017,
 0.0014000000000000017,
 0.0014000000000000017,
 0.0014000000000000017,
 0.0014000000000000017,
 1.9014,
 0.0014000000000000017,
 0.0014000000000000017,
 1.9213999999999998,
 0.0014000000000000017,
 0.0014000000000000017,
 1.9014

最佳答案

在我看来,您尝试做的是遍历您的 total timeslot,如果它与您的 Home_GoalAway_Goal 匹配> 然后计算。
你在做什么是这样的:

  • 比较 intstr (k == 'No_Goal') 你会得到 False几乎一直都是。
  • 在计算后使用 return 在第一次迭代时结束循环。

根据我的理解,这就是您要查找的内容:

total_timeslot = 15
Home_Goal = [8, 14]
Away_Goal = [11]

def sum_squared_diff(x1, x2, x3, y):
    r1, r2, r3 = 0, 0, 0
    for k in range(total_timeslot):
        if k in Home_Goal:
            r2 += sum((x2 - y)**2)
        elif k in Away_Goal:
            r3 += sum((x3 - y)**2)
        else:
            r1 += sum((x1 - y)**2)
    return r1, r2, r3

更新:

在您更新问题后,您似乎希望在每次迭代后打印 所有结果,而不仅仅是最终结果。在这种情况下,您可以在每个循环的末尾添加一个 print 语句并删除增量。

更新 2:

这是反射(reflect)更改的代码:

def sum_squared_diff(x1, x2, x3, y):
    r1, r2, r3 = 0, 0, 0
    for k in range(total_timeslot):
        if k in Home_Goal:
            r2 = sum((x2 - y)**2)
        elif k in Away_Goal:
            r3 = sum((x3 - y)**2)
        else:
            r1 = sum((x1 - y)**2)
    print(r1, r2, r3)

关于python - 在不同的循环python中迭代四个列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50722037/

相关文章:

Python 相当于 PowerShell Get-EventLog

c++ - 在切换循环周期结束时未显示的游戏总数

python - 解析 Python 的输出

python - 如何使用 Django 过早地拒绝请求?

python - 'is' 运算符在 Python 中有什么作用?

javascript - 用 `for` 循环替换 `forEach` 循环

python - 迭代迭代器对象中的 "1 or more"元素?

java - 在多个线程中使用 forEach() 或使用 forEach() 和 lambda 进行集合迭代

python - 在 For 循环中制作列表列表

python - 用逗号分隔值