2017-06-12 117 views
0

我正試圖在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 

回答

2

statistics module在Python 3.4加入。也許你正在使用一個較老的Python版本。

如果因爲任何原因無法升級,您也可以使用numpymean功能:np.mean(xs)等。對於numpy陣列,它可能也會更快。