D-S证据融合(Dempster组合规则)——Python3代码实现

import numpy as np


def py_intersect(string_1, string_2):
    """
    :param string_1: 字符串
    :param string_2: 字符串
    :return: 两字符串的交集
    """
    result = ''
    for char in string_1:
        if char in string_2 and char not in result:
            result += char
    return result


def dempster(mp1, mp2, P):
    """
    :param mp1: 证据源1,numpy数组,存储信度
    :param mp2: 证据源2,numpy数组,存储信度
    :param P: 辨识框架
    :return: 返回融合信度和冲突因子
    """
    l = len(P)  # 幂集长度
    mp = np.zeros((1, l), 'float64')  # 初始化最终结果mp
    k_matrix = np.zeros((l, l))  # 冲突因子乘子
    for k in range(l):
        tmp = P[k]
        f_matrix = np.zeros((l, l))  # 融合乘子
        for i in range(l):
            for j in range(l):
                tmp_ij = py_intersect(P[i], P[j])  # 有无交集
                if not tmp_ij:  # 若空集
                    k_matrix[i][j] = 1
                if tmp_ij == tmp:  # 若交集等于P[k]
                    f_matrix[i][j] = 1
        mp[0][k] = sum(sum(np.dot(mp1.T, mp2) * f_matrix))
    k = sum(sum(np.dot(mp1.T, mp2) * k_matrix))
    mp = mp / (1 - k)
    return mp, k


m1 = np.array([[0.3243, 0, 0, 0, 0, 0, 0.6857]])
m2 = np.array([[0, 0, 0.4696, 0, 0, 0.3054, 0.2250]])

P = ['S', 'E', 'V', 'SE', 'SV', 'EV', 'SEV']

mp, k = dempster(m1, m2, P)
print(f'融合后信度为{mp}')
print(f'冲突因子k = {k}')

你可能感兴趣的:(python,机器学习)