python numpy mean out,Python numpy.mean()用法及代碼示例

numpy.mean(arr,axis = None):計算沿指定軸的給定數據(數組元素)的算術平均值(平均值)。

參數:

arr :[數組]輸入數組。

axis :我們要沿其計算算術平均值的[int或int元組]。否則,它將考慮將arr展平(適用於所有

軸)。 axis = 0表示沿列,而axis = 1表示沿行。

out :[ndarray,可選]我們要在其中放置結果的不同數組。數組必須具有與預期輸出相同的尺寸。

dtype :[數據類型,可選]我們在計算均值時需要的類型。

結果:數組的算術平均值(如果軸不存在,則為標量值)或具有沿指定軸的平均值的數組的算術平均值。

代碼1:

# Python Program illustrating

# numpy.mean() method

import numpy as np

# 1D array

arr = [20, 2, 7, 1, 34]

print("arr : ", arr)

print("mean of arr : ", np.mean(arr))

輸出:

arr : [20, 2, 7, 1, 34]

mean of arr : 12.8

代碼2:

# Python Program illustrating

# numpy.mean() method

import numpy as np

# 2D array

arr = [[14, 17, 12, 33, 44],

[15, 6, 27, 8, 19],

[23, 2, 54, 1, 4, ]]

# mean of the flattened array

print("\nmean of arr, axis = None : ", np.mean(arr))

# mean along the axis = 0

print("\nmean of arr, axis = 0 : ", np.mean(arr, axis = 0))

# mean along the axis = 1

print("\nmean of arr, axis = 1 : ", np.mean(arr, axis = 1))

out_arr = np.arange(3)

print("\nout_arr : ", out_arr)

print("mean of arr, axis = 1 : ",

np.mean(arr, axis = 1, out = out_arr))

輸出:

mean of arr, axis = None : 18.6

mean of arr, axis = 0 : [17.33333333 8.33333333 31. 14. 22.33333333]

mean of arr, axis = 1 : [24. 15. 16.8]

out_arr : [0 1 2]

mean of arr, axis = 1 : [24 15 16]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值