gpt4 book ai didi

python - 如何在函数中循环遍历 pandas 数据框中的列表

转载 作者:太空宇宙 更新时间:2023-11-04 09:42:59 25 4
gpt4 key购买 nike

这是我的数据框,

 df = pd.DataFrame({'Id': [102,103,104,303,305],'ExpG_Home':[1.8,1.5,1.6,1.8,2.9],
'ExpG_Away':[2.2,1.3,1.2,2.8,0.8],
'HomeG_Time':[[93, 109, 187],[169], [31, 159],[176],[16, 48, 66, 128]],
'AwayG_Time':[[90, 177],[],[],[123,136],[40]]})

首先,我需要创建一个数组 y,对于给定的 ID 号,它从同一行(ExpG_Home & ExpG_Away)获取值。

y = [1 - (ExpG_Home + ExpG_Away), ExpG_Home, ExpG_Away]

其次,我发现这更难,对于用于创建 y 的 Id,下面的函数从 HomeG_Time & AwayG_Time 中获取相应的列表> 并创建一个数组。不幸的是,我的函数一次只占用一行。我需要为大型数据集执行此操作。

x1 = [1,0,0]    
x2 = [0,1,0]
x3 = [0,0,1]
total_timeslot = 200 # number of timeslot per game.
k = 1 # constant



#For Id=102 with ExpG_Home=2.2 and ExpG_Away=1.8
HomeG_Time = [93, 109, 187]
AwayG_Time = [90, 177]
y = np.array([1-(2.2 + 1.8)/k, 2.2/k, 1.8/k])
# output of y = [0.98 , 0.011, 0.009]

def squared_diff(x1, x2, x3, y):
ssd = []
for k in range(total_timeslot):
if k in HomeG_Time:
ssd.append(sum((x2 - y) ** 2))
elif k in AwayG_Time:
ssd.append(sum((x3 - y) ** 2))
else:
ssd.append(sum((x1 - y) ** 2))
return ssd

sum(squared_diff(x1, x2, x3, y))
Out[37]: 7.880400000000012

此输出仅适用于第一行。

最佳答案

这是给出的完整片段,

>>> import numpy as np
>>> x1 = np.array( [1,0,0] )
>>> x2 = np.array( [0,1,0] )
>>> x3 = np.array( [0,0,1] )
>>> total_timeslot = 200
>>> HomeG_Time = [93, 109, 187]
>>> AwayG_Time = [90, 177]
>>> ExpG_Home=2.2
>>> ExpG_Away=1.8
>>> y = np.array( [1 - (ExpG_Home + ExpG_Away), ExpG_Home, ExpG_Away] )
>>> def squared_diff(x1, x2, x3, y):
... ssd = []
... for k in range(total_timeslot):
... if k in HomeG_Time:
... ssd.append(sum((x2 - y) ** 2))
... elif k in AwayG_Time:
... ssd.append(sum((x3 - y) ** 2))
... else:
... ssd.append(sum((x1 - y) ** 2))
... return ssd
...
>>> sum(squared_diff(x1, x2, x3, y))
4765.599999999989

假设这个。使用 pandas.DataFrame.apply 将 y 计算为 (N,3)

>>> y = np.array( df.apply(lambda row: [1 - (row.ExpG_Home + row.ExpG_Away),
... row.ExpG_Home, row.ExpG_Away ],
... axis=1).tolist() )
>>> y.shape
(5, 3)

现在计算给定 x 的平方误差

>>> def squared_diff(x, y):
... return np.sum( np.square(x - y), axis=1)

在你的例子中,如果 error2squared_diff(x2,y) 你添加的是 HomeG_Time 的出现次数

>>> n3 = df.AwayG_Time.apply(len)
>>> n2 = df.HomeG_Time.apply(len)
>>> n1 = 200 - (n2 + n3)

最终的误差平方和是(根据你的计算)

>>> squared_diff(x1, y) * n1 + squared_diff(x2, y) * n2 + squared_diff(x3, y) * n3
0 4766.4
1 2349.4
2 2354.4
3 6411.6
4 4496.2
dtype: float64
>>>

关于python - 如何在函数中循环遍历 pandas 数据框中的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50965660/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com