无法从统计数据中导入均值

Cannot import mean from statistics

我正在尝试在 python 中导入统计模块。当我执行程序时它给我一个错误信息。

这是我的代码:

from statistics import mean
import numpy as np
import  matplotlib.pyplot as plt

xs = np.array([1,2,3,4,5,6,7,8])
ys = np.array([2,8,5,0,5,7,3,6])
def best_fit_line(xs ,ys):
    m = ( ((mean(xs)* mean(ys))- mean(xs*ys)) /
          (mean(xs)*mean(xs))-(mean(xs*xs)))
    return m
m = best_fit_line(xs,ys)

错误信息:

Traceback (most recent call last):
  File "/home/kudzai/Documents/Machine Learning/LinearRegAlg.py", line 1, in <module>
    from statistics import mean
ImportError: No module named statistics

statistics module 是在 Python 3.4 中添加的。也许您使用的是旧 Python 版本。

如果你因为什么原因不能升级,你也可以只使用numpymean函数:np.mean(xs)等。对于numpy数组,它是可能也更快。