每次运行时协方差都会发生变化

时间:2018-02-06 21:39:42

标签: python machine-learning data-analysis

每次运行以下代码都是正常的,我会得到不同的协方差吗?

%matplotlib inline

import numpy as np
from pylab import *

def de_mean(x):
    xmean = mean(x)
    return [xi - xmean for xi in x]


def covariance(x, y):
    n = len(x)
    return dot(de_mean(x), de_mean(y)) / (n-1)

pageSpeeds = np.random.normal(3.0, 1.0, 1000)
purchaseAmount = np.random.normal(50.0, 10.0, 1000)

scatter(pageSpeeds, purchaseAmount)

covariance (pageSpeeds, purchaseAmount)

1 个答案:

答案 0 :(得分:1)

这很有道理:您正在寻找随机数据的协方差。每次运行时都会得到不同的随机数。如果要获得可重现的结果,请在构建阵列之前将随机数生成器设置为常量值。例如,将此行插入主程序的顶部:

numpy.random.seed(0)