查看 numpy OrderedDict 中的值在另一个 OrderedDict 中是否相同的最快方法是什么?
What is the fastest way to see whether values in a numpy OrderedDict are the same in another OrderedDict?
我正在尝试确定不同 numpy orderdict 对象中保存的两个值是否相同。
这两个词典都是使用 turbodbc
中的 fetchallnumpy()
选项创建的,由两个键组成。第一个键是一个 id 字段,第二个键是一个可变长度的字符串值。我想查看第一组字典项中的字符串值是否存在于第二组字典项中。
可能值得注意的是,两个字典对象在每个键下都包含大约 6000 万个值。
到目前为止我已经尝试了几种方法:-
np.isin(dict1[str_col],dict2[str_col])
作为一个函数,但这非常慢,大概是因为字符串值存储为 dtype
对象。
我尝试将两个字典对象转换为 numpy
数组,显式字符串类型为 np.asarray(dict1[str_col], dtype='S500')
,然后尝试使用 isin
和 in1d
函数。此时系统 运行 内存不足。已将 'S500' 换成 dtype=np.string_
,但仍然得到 MemoryError
。 (ar=np.concatenate((ar1,ar2)))
同时执行 isin
功能。
我也试过for循环。
[r in dict2[str_col] for r in dict1[str_col]]
同样,这非常慢。
My aim is to have a relatively quick way of testing the two string
columns without running out of memory.
额外位
在漫长的 运行 中,我将 运行 进行不止一次检查,因为我正在尝试识别 > 新值和已更改的值。
字典 A = 当前数据 ['ID': [int,int,int]]
字典 B = 历史数据 ['record':[str,str,str]]
所以我感兴趣的是:-
- A != B(当前记录与历史记录不同)
- A 不存在于 B 中(新记录添加到数据库中)
- B 不存在于 A 中(记录需要编辑)
最后两个元素是我迄今为止发现的最快方法,它是将 id 列传递给包含 np.isin(arr1,arr2) 的函数。比较数据平均需要 15 秒。
您可以使用 np.searchsorted
进行更快的搜索:
ar1 = dict1[str_col]
ar2 = dict2[str_col]
sorter = np.argsort(ar2)
idx = np.searchsorted(ar2, ar1, sorter=sorter)
if idx.max() >= len(ar2):
return False
return np.all(ar1 == ar2[sorter[idx]])
仍然不完全清楚您要实现的目标(请参阅我的评论)。但这是我的短裤。
Pandas 可能提供更有效的替代方法来比较字符串列表。我自己还没有对大块数据进行测试。
尝试以下操作:
import pandas as pd
s1 = pd.Series(dict1[str_col])
s2 = pd.Series(dict2[str_col])
print(s1.isin(s2).all())
或者如果您无论如何都需要遍历所有列,您可以将完整的字典转换为数据框:
df1 = pd.DataFrame(dict1)
df2 = pd.DataFrame(dict2)
for col in df1:
print(df1[col].isin(df2[col]).all())
如果你想测试完整的DataFrame是否相等,你可以使用pandas' assert_frame_equal
。例如:
pd.util.testing.assert_frame_equal(df1, df2)
# ...or if the ordering is not the same.
pd.util.testing.assert_frame_equal(df1, df2, check_like=True)
显然,可以将 turbodbc 数据直接转储到 pandas 对象中 (to_pandas()
)。看这里:turbodbc documentation, advanced usage
我正在尝试确定不同 numpy orderdict 对象中保存的两个值是否相同。
这两个词典都是使用 turbodbc
中的 fetchallnumpy()
选项创建的,由两个键组成。第一个键是一个 id 字段,第二个键是一个可变长度的字符串值。我想查看第一组字典项中的字符串值是否存在于第二组字典项中。
可能值得注意的是,两个字典对象在每个键下都包含大约 6000 万个值。
到目前为止我已经尝试了几种方法:-
np.isin(dict1[str_col],dict2[str_col])
作为一个函数,但这非常慢,大概是因为字符串值存储为
dtype
对象。我尝试将两个字典对象转换为
numpy
数组,显式字符串类型为np.asarray(dict1[str_col], dtype='S500')
,然后尝试使用isin
和in1d
函数。此时系统 运行 内存不足。已将 'S500' 换成dtype=np.string_
,但仍然得到MemoryError
。(ar=np.concatenate((ar1,ar2)))
同时执行isin
功能。我也试过for循环。
[r in dict2[str_col] for r in dict1[str_col]]
同样,这非常慢。
My aim is to have a relatively quick way of testing the two string columns without running out of memory.
额外位 在漫长的 运行 中,我将 运行 进行不止一次检查,因为我正在尝试识别 > 新值和已更改的值。
字典 A = 当前数据 ['ID': [int,int,int]] 字典 B = 历史数据 ['record':[str,str,str]]
所以我感兴趣的是:-
- A != B(当前记录与历史记录不同)
- A 不存在于 B 中(新记录添加到数据库中)
- B 不存在于 A 中(记录需要编辑)
最后两个元素是我迄今为止发现的最快方法,它是将 id 列传递给包含 np.isin(arr1,arr2) 的函数。比较数据平均需要 15 秒。
您可以使用 np.searchsorted
进行更快的搜索:
ar1 = dict1[str_col]
ar2 = dict2[str_col]
sorter = np.argsort(ar2)
idx = np.searchsorted(ar2, ar1, sorter=sorter)
if idx.max() >= len(ar2):
return False
return np.all(ar1 == ar2[sorter[idx]])
仍然不完全清楚您要实现的目标(请参阅我的评论)。但这是我的短裤。
Pandas 可能提供更有效的替代方法来比较字符串列表。我自己还没有对大块数据进行测试。
尝试以下操作:
import pandas as pd
s1 = pd.Series(dict1[str_col])
s2 = pd.Series(dict2[str_col])
print(s1.isin(s2).all())
或者如果您无论如何都需要遍历所有列,您可以将完整的字典转换为数据框:
df1 = pd.DataFrame(dict1)
df2 = pd.DataFrame(dict2)
for col in df1:
print(df1[col].isin(df2[col]).all())
如果你想测试完整的DataFrame是否相等,你可以使用pandas' assert_frame_equal
。例如:
pd.util.testing.assert_frame_equal(df1, df2)
# ...or if the ordering is not the same.
pd.util.testing.assert_frame_equal(df1, df2, check_like=True)
显然,可以将 turbodbc 数据直接转储到 pandas 对象中 (to_pandas()
)。看这里:turbodbc documentation, advanced usage