每次运行以下代码,得到不同的协方差是否正常?
%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)
最佳答案
完全有道理:您正在发现随机数据的协方差。您每次运行都会获得不同的随机数。如果要获得可重现的结果,则在构建数组之前将随机数生成器设置为恒定值。例如,将此行插入主程序的顶部:
numpy.random.seed(0)