python - 无法在 Python 中定义术语

标签 python numpy

这是第一篇文章,所以我会尽量具体。

我正在尝试在 Python 中定义术语 ybar1。我打算将 ybar1 设为列表 y1

的平均值
where y1 = [1, 2, 3, 4, 5]

我首先要做的是:

import numpy as np

然后定义一个新函数

def funk(y1):
     print np.sum(y1)/len(y1)
     return;

现在,当我计算 funk(y1) 时,我得到一个数字 3

现在事情变得奇怪了。我试着说:

ybar1 = funk(y1)

,返回值 3

但是当我单独输入 ybar1 时,我没有得到任何输出。

想法?

最佳答案

试试这个:

def funk(y1):
  return np.sum(y1)/len(y1)

您实际上并没有从函数返回值,只是在控制台上打印结果。此外,还有一种更好的方法来使用 numpy 计算平均值:

def funk(y1):
  return np.mean(y1)

关于python - 无法在 Python 中定义术语,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32262496/

相关文章:

python - Tensorflow,选择操作给出错误

python - 基维 : BoxLayout containing horizontal BoxLayout

python - NumPy:填充数组中 1 周围的字段

python - 使用索引手动切片列表,Python

python - 神经网络不产生结果

python - numpy 例程和 ndarray 方法之间的区别?

python - 从 PIL 图像转换后,Numpy 数组不显示灰度图像的颜色尺寸

python - cv2.VideoWriter 的输出不正确。更快

Python:在 OS X 中激活窗口

python - 如何在python中配置gRPC HTTP/2流量控制