Beispiel #1
0
def covariance(x, y):
    '''
    공분산(covariance)
    cov = sum((xi - x_bar)(yi - y_bar)) / (n - 1)

    :param x: 원소가 n개인 (1차원) 리스트
    :param y: 원소가 n개인 (1차원) 리스트
    :return: 공분산
    '''
    n = len(x)
    x_bar = mean(x)
    y_bar = mean(y)
    x_deviations = [x_i - x_bar for x_i in x]
    y_deviations = [y_i - y_bar for y_i in y]
    # x_total = 0
    # y_total = 0
    # for x_i in x:
    #     x_total += (x_i - mean(x))
    # for y_i in y:
    #     y_total += (y_i - mean(y))
    #
    # return ((x_total) * (y_total))/(n-1)
    xy_total = 0
    for x_i, y_i in zip(x, y):
        xy_total += (x_i - mean(x)) * (y_i - mean(y))
    return xy_total / (n - 1)

    sum_of_deviations = dot(x, y)
Beispiel #2
0
def covariance(x, y):
    """
    공분산(covariance)
    Cov = sum((xi - x_bar)(yi - y_bar)) / (n - 1)

    :param x: 원소 n개인 (1차원) 리스트
    :param y: 원소 n개인 (1차원) 리스트
    :return: 공분산
    """
    x_bar = mean(x)  # x의 평균
    y_bar = mean(y)  # y의 평균
    x_deviations = [x_i - x_bar for x_i in x]  # 편차들의 리스트
    y_deviations = [y_i - y_bar for y_i in y]
    sum_of_deviations = dot(x_deviations, y_deviations)
    # sum_of_deviations = 0
    # for xd, yd in zip(x, y):
    #     sum_of_deviations += (xd - x_bar) * (yd - y_bar)
    return sum_of_deviations / (len(x) - 1)
Beispiel #3
0
def covariance(x, y):
    """
    공분산
    Cov = sum((x_i - x_bar)(y_i - y_bar)) / (n-1)

    :param x: 원소가 n개인 1차원 리스트
    :param y: 원소가 n개인 1차원 리스트
    :return: 공분산
    """
    # if len(x) != len(y):
    #     raise ValueError('x와 y의 길이는 같아야 합니다!')
    # de_x = de_mean(x)
    # de_y = de_mean(y)
    # result = []
    # for i, j in zip(de_x, de_y):
    #     result.append(i*j)
    # return sum(result) / (len(x) - 1)

    # if len(x) != len(y):
    #     raise ValueError('x와 y의 길이는 같아야 합니다!')
    # de_x = de_mean(x)
    # de_y = de_mean(y)
    # sum = 0
    # for i, j in zip(de_x, de_y):
    #     sum += (i*j)
    # return sum / (len(x) - 1)

    # x_bar = mean(x)
    # y_bar = mean(y)
    # x_dev = [x_i - x_bar for x_i in x]
    # y_dev = [y_i - y_bar for y_i in y]
    # sum_of_sq = 0
    # for xd, yd in zip(x_dev, y_dev):
    #     sum_of_sq += xd * yd
    # return sum_of_sq / (len(x) - 1)

    x_bar = mean(x)
    y_bar = mean(y)
    x_dev = [x_i - x_bar for x_i in x]
    y_dev = [y_i - y_bar for y_i in y]
    sum_of_dev = dot(x_dev, y_dev)
    return sum_of_dev / (len(x) - 1)
Beispiel #4
0
def variance(x):
    """
    (x1 - mean)**2 + (x2 - mean)***2 + ... + (xn - mean)**2 / n-1
    :param x: 원소가 n개인 1차원 리스트
    :return: 분산
    """
    # x_mean = mean(x)
    # sum = 0
    # for i in x:
    #     sum += (i - x_mean)**2
    # return sum / (len(x)-1)

    # demean = de_mean(x)
    # sum = 0
    # for i in demean:
    #     sum += i**2
    # return sum / (len(x)-1)

    demean = de_mean(x)
    return dot(demean, demean) / (len(x) - 1)
Beispiel #5
0
def variance(x):
    """
    (x1 - mean) ** 2 + (x2 - mean) ** 2 + ... + (xn - mean) ** 2 / (n - 1)
    :param x: 원소 n개인 (1차원) 리스트
    :return: x의 분산 값
    """
    # sum = 0
    # for i in x:
    #     sum += (i - mean(x)) ** 2
    # v = sum / (len(x)-1)

    n = len(x)  # 원소 개수
    # x_bar = mean(x) # 평균
    # return sum([(x_i - x_bar) ** 2 for x_i in x]) / (n-1)

    # 다른방법
    # deviations = de_mean(x) # 편차들의 리스트
    # return sum([d ** 2 for d in deviations]) / (n-1)

    # dot()함수를 import해서 하는 방법
    print('dot 함수 사용')
    deviations = de_mean(x)
    return dot(deviations, deviations) / (n - 1)
Beispiel #6
0
def variance(x):
    print('dot 사용')
    n = len(x)
    deviations = de_mean(x)
    return dot(deviations, deviations) / (n - 1)
Beispiel #7
0
def variance2(x):
    n = len(x)
    deviations = de_mean(x)
    return dot(deviations, deviations) / (n - 1)