在Python中实现分布之间的距离和距离度。
项目描述
Python中实现的描述为字典的离散分布之间的距离和距离度。
这些用作计算离散分布之间的距离和距离度的快速解决方案,特别是在两个分布包含大量概率为零的事件时,这些事件在字典中未描述。
我如何安装此软件包?
像往常一样,只需使用pip下载即可
pip install dictances
可用的度量标准
提供了一些距离和距离度
距离 |
方法 |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
使用点的示例
假设您有一个由my_first_dictionary描述的点,另一个由my_second_dictionary描述的点
from dictances import cosine
my_first_dictionary = {
"a": 56,
"b": 34,
"c": 89
}
my_second_dictionary = {
"a": 21,
"d": 51,
"e": 74
}
cosine(my_first_dictionary, my_second_dictionary)
#>>> 0.8847005261889619
使用分布的示例
假设您有一个由my_first_dictionary描述的点,另一个由my_second_dictionary描述的点
from dictances import bhattacharyya, bhattacharyya_coefficient
a = {
"event_1": 0.4,
"event_2": 0.1,
"event_3": 0.2,
"event_4": 0.3,
}
b = {
"event_1": 0.1,
"event_2": 0.2,
"event_5": 0.2,
"event_9": 0.5,
}
bhattacharyya_coefficient(a, b)
#>>> 0.3414213562373095
bhattacharyya(a, b)
#>>> 1.07463791569453
处理嵌套字典
如果您需要计算两个嵌套字典之间的距离,可以使用deflate_dict,如下所示
from dictances import cosine
from deflate_dict import deflate
my_first_dictionary = {
"a": 8,
"b": {
"c": 3,
"d": 6
}
}
my_second_dictionary = {
"b": {
"c": 8,
"d": 1
},
"y": 3,
}
cosine(deflate(my_first_dictionary), deflate(my_second_dictionary))