【发布时间】:2016-02-27 00:02:33
【问题描述】:
我在 Windows 上的 Python 2.7 中运行以下代码。
当我运行代码时(在帖子底部),我收到一个错误:
Traceback (most recent call last):
File "<ipython-input-27-f7c0cd1d93c7>", line 75, in <module>
if r_val_path == 0:
File "C:\Anaconda\lib\site-packages\pandas\core\generic.py", line 698, in __nonzero__
.format(self.__class__.__name__))
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
运行代码后,我意识到r_val、r_val_edge 和r_val_path 都是numpy timeseries',长度等于1 或0,而不是浮点数。我什至在代码的开头明确指定了 r_val = 0
谁能告诉我为什么会在我的代码中出现这种情况?
import pandas as pd
import networkx as nx
from itertools import permutations
import numpy as np
'''import dataframes '''
df = pd.DataFrame({'fld1': ['a', 'a', 'b', 'c', 'c', 'g', 'd', 'd', 'e', 'e', 'f']
, 'fld2': ['b', 'c', 'f', 'd', 'g', 'd', 'e', 'b', 'c', 'f', 'b']
, 'r_val': [0.1, 0.9, 1, 0.5, 0.5, 1, 0.8, 0.2, 0.2, 0.8, 1]})
##df of all relationships to build
flds = pd.Series(df.fld1.unique())
flds = pd.Series(flds.append(pd.Series(df.fld2.unique())).unique())
combos = []
for L in range(0, len(flds)+1):
for subset in permutations(flds, L):
if len(subset) == 2:
combos.append(subset)
if len(subset) > 2:
break
rel_df = pd.DataFrame.from_records(data = combos, columns = ['fld1','fld2'])
rel_df['relationship'] = 0
'''build graph '''
w_edges= map(list, df.values)
DG=nx.DiGraph()
DG.add_weighted_edges_from(w_edges)
''' iterator '''
#iterate through each row of the rel_df
for index, row in rel_df.iterrows():
#pull source and target
fld1_val = rel_df.fld1[index]
fld2_val = rel_df.fld2[index]
#pull original r_val, vlookup both fields in df
try:
r_val = df.loc[(df['fld1'] == fld1_val) &
(df['fld2'] == fld2_val)]['r_val']
except:
r_val = 0
#iterate through each path
for path in nx.all_simple_paths(DG, source= fld1_val, target= fld2_val):
path_holder = path
r_val_path = 0
r_val_path = r_val_path
#iterate through each edge in each path
for e in np.arange(0,len(path_holder)):
r_val_edge = 0
#grab nodes in pairs
if (e < len(path_holder)) & (
(path_holder[e - 1 ] <> fld1_val) &
(path_holder[e] <> fld2_val)):
#grab pair of nodes
node1 = path_holder[e - 1]
node2 = path_holder[e]
#find r_val_edge from table
r_val_edge = df.loc[(df['fld1'] == node1) &
(df['fld2'] == node2)]['r_val']
r_val_edge = r_val_edge
#add r_val_edge to r_val_path
if r_val_path == 0:
r_val_path = r_val_edge
else:
r_val_path = r_val_path * r_val_edge
else:
#path is done or path is direct connection
# move onto the next path
pass
r_val += r_val_path
#if the r_val for the path is less than threshold then quit
if r_val < .00000001:
pass
#if the r_val for the path is less than threshold then quit
if r_val < .00000001:
r_val = 0
pass
#add r_val to rel_df
rel_df.loc[(rel_df.fld1 == fld1_val) & (
rel_df.fld2 == fld2_val),'relationship'] = r_val
【问题讨论】:
标签: python-2.7 numpy pandas networkx