Exemplo n.º 1
0
def pearson_coefficient(sample_x, sample_y):
    """
    皮尔森相关性系数
    :param sample_x: x样本数据变量集合
    :param sample_y: y样本数据变量集合
    :return:x和y的相关系数
    """
    ana = AnalysisMath()  # 数学分析模块
    numerator = cov(sample_x, sample_y)  # 分子是协方差
    denominator = pow(ana.variance(sample_x) * ana.variance(sample_y), 0.5)  # 分母是两个方差的积开平方
    return numerator / denominator
Exemplo n.º 2
0
def mahalanobis_distance(matrix, coordinate):
    """
    求解马氏距离
    :param matrix: 矩阵列表
    :param coordinate: 坐标
    :return result: 两个点的马氏距离
    """
    # 马氏距离要求样本数要大于维数,否则无法求协方差矩阵
    # 此处进行转置,表示10个样本,每个样本2维
    s = Matrix(matrix).t
    ana = AnalysisMath()
    mean, c, distance = [], [], []
    for index, i in enumerate(s):
        c.append([cov(i, j) for j in s])
    s_i = Matrix(c).i  # 协方差矩阵的逆矩阵
    for data in s:
        mean.append(ana.expect(data))
    distance.append([i - j for i, j in zip(coordinate, mean)])
    ma = Matrix(Matrix(distance) * s_i) * Matrix(distance).t
    return pow(ma[0][0], 0.5)