我有这四个问题。列表:
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.
我需要x1
和y
的平方差(sse)之和,直到得分为目标。然后,sse
由x2
或x3
和y
计算,具体取决于首先得分的人。然后它会循环回x1
和y
,直到任何一支球队再次对目标进行评分等等......
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
,并在进球时记下。所以我可以返回并添加/减去一些infinitesimal epsilon
到y
值并测量测试......
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
答案 0 :(得分:1)
解决方案是不同的,因为最后一个else语句永远是真的,因为正如@slybloty所说,你将int k
与str
进行比较。所以你的
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)
和
def sum_squared_diff(x1, x2, x3, y, timeslot):
return sum((x3 - y)**2)
做同样的工作
答案 1 :(得分:1)
我想要做的就是遍历您的total timeslot
,如果它与您的Home_Goal
或Away_Goal
匹配,那么就这样计算。
你正在做的是:
int
与str
(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
获得所有结果,而不仅仅是最终结果。在这种情况下,您可以在每个循环结束时添加print
语句并删除增量。
以下是反映变化的代码:
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)