我目前正在尝试同时遍历三个列表:
list_weight = [0.9,0.3,0.6,0.4]
list_reliability = [0.8,0.5,0.2,0.8]
belief_CRED0 = [create_belief_matrix ('ACBA').iloc[0]]
belief_CRED0
Out[40]:
[1 0.562500
2 0.562500
3 0.391304
4 0.391304
Name: CRED0, dtype: float64]首先,我创建了一个嵌套循环:
for belief in belief_CRED0:
for weight in list_weight:
for reliability in list_reliability:
m = [(1/(1+weight-reliability))*(weight*belief)]
print(m)但结果却完全不同。所以我试着这样做:
for belief, weight, reliability in zip(belief_CRED0, list_weight, list_reliability):
m = [(1/(1+weight-reliability))*(weight*belief)]
print(m)但结果也是错误的:
m
Out[42]:
[1 0.460227
2 0.460227
3 0.320158
4 0.320158
Name: CRED0, dtype: float64]从结果看,循环似乎只使用了相应列表中的第一个权重和可靠性(权重= 0.9,可靠性= 0.8)。
正确的输出应该是:
[1 0.460227
2 0.210937
3 0.16770171
4 0.26086933我该怎么办?
发布于 2017-08-18 05:05:19
如果它们都是pandas.Series或numpy.array,那么您可以直接执行此操作,例如:
>>> weight = pd.Series(list_weight, index=range(1, 5))
>>> reliability = pd.Series(list_reliability, index=range(1, 5))
>>> 1/(1+weight-reliability)*(weight*belief_CRED0)
1 0.460227
2 0.210937
3 0.167702
4 0.260869
dtype: float64与numpy类似
>>> weight = np.array(list_weight)
>>> reliability = np.array(list_reliability)
>>> 1/(1+weight-reliability)*(weight*belief_CRED0)
1 0.460227
2 0.210937
3 0.167702
4 0.260869
Name: CRED0, dtype: float64发布于 2017-08-18 05:05:04
在zip上的for循环中有一个小错误(这是最好的方法)。累积结果..。而不是一直赋值给m。
m = []
for belief, weight, reliability in zip(belief_CRED0, list_weight, list_reliability):
m.append(weight*belief/(1+weight-reliability))
print(m)https://stackoverflow.com/questions/45744693
复制相似问题