中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

python 的numpy庫中的mean()函數用法介紹

發布時間:2020-10-02 19:42:58 來源:腳本之家 閱讀:745 作者:饕餮爭鋒 欄目:開發技術

1. mean() 函數定義:

numpy.mean(a, axis=None, dtype=None, out=None, keepdims=<class numpy._globals._NoValue at 0x40b6a26c>)[source]
Compute the arithmetic mean along the specified axis.

Returns the average of the array elements. The average is taken over the flattened array by default, otherwise over the specified axis. float64intermediate and return values are used for integer inputs.

Parameters:

a : array_like

Array containing numbers whose mean is desired. If a is not an array, a conversion is attempted.

axis : None or int or tuple of ints, optional

Axis or axes along which the means are computed. The default is to compute the mean of the flattened array.

New in version 1.7.0.

If this is a tuple of ints, a mean is performed over multiple axes, instead of a single axis or all the axes as before.

dtype : data-type, optional

Type to use in computing the mean. For integer inputs, the default is float64; for floating point inputs, it is the same as the input dtype.

out : ndarray, optional

Alternate output array in which to place the result. The default is None; if provided, it must have the same shape as the expected output, but the type will be cast if necessary. See doc.ufuncs for details.

keepdims : bool, optional

If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.

If the default value is passed, then keepdims will not be passed through to the mean method of sub-classes of ndarray, however any non-default value will be. If the sub-classes sum method does not implement keepdims any exceptions will be raised.

Returns:

m : ndarray, see dtype parameter above

If out=None, returns a new array containing the mean values, otherwise a reference to the output array is returned.

2 mean()函數功能:求取均值

經常操作的參數為axis,以m * n矩陣舉例:

axis 不設置值,對 m*n 個數求均值,返回一個實數

axis = 0:壓縮行,對各列求均值,返回 1* n 矩陣

axis =1 :壓縮列,對各行求均值,返回 m *1 矩陣

舉例:

>>> import numpy as np

>>> num1 = np.array([[1,2,3],[2,3,4],[3,4,5],[4,5,6]])
>>> now2 = np.mat(num1)
>>> now2
matrix([[1, 2, 3],
  [2, 3, 4],
  [3, 4, 5],
  [4, 5, 6]])


>>> np.mean(now2) # 對所有元素求均值
3.5


>>> np.mean(now2,0) # 壓縮行,對各列求均值
matrix([[ 2.5, 3.5, 4.5]])


>>> np.mean(now2,1) # 壓縮列,對各行求均值
matrix([[ 2.],
  [ 3.],
  [ 4.],
  [ 5.]])

補充拓展:numpy的np.nanmax和np.max區別(坑)

numpy的np.nanmax和np.array([1,2,3,np.nan]).max()的區別(坑)

numpy中numpy.nanmax的官方文檔

原理

在計算dataframe最大值時,最先用到的一定是Series對象的max()方法(),最終結果是4。

s1 = pd.Series([1,2,3,4,np.nan])
s1_max = s1.max()

但是筆者由于數據量巨大,列數較多,于是為了加快計算速度,采用numpy進行最大值的計算,但正如以下代碼,最終結果得到的是nan,而非4。發現,采用這種方式計算最大值,nan也會包含進去,并最終結果為nan。

s1 = pd.Series([1,2,3,4,np.nan])
s1_max = s1.values.max()
>>>nan

通過閱讀numpy的文檔發現,存在np.nanmax的函數,可以將np.nan排除進行最大值的計算,并得到想要的正確結果。

當然不止是max,min 、std、mean 均會存在列中含有np.nan時,s1.values.min /std/mean ()返回nan的情況。

速度區別

速度由快到慢依次:

s1 = pd.Series([1,2,3,4,5,np.nan])
#速度由快至慢
np.nanmax(s1.values) > np.nanmax(s1) > s1.max() 

以上這篇python 的numpy庫中的mean()函數用法介紹就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

柘城县| 霞浦县| 广昌县| 恩平市| 青龙| 泸州市| 固阳县| 阳新县| 吴忠市| 丹东市| 甘洛县| 江口县| 阳泉市| 乌海市| 河北区| 乌兰察布市| 桃江县| 琼结县| 尼木县| 哈尔滨市| 西和县| 宁化县| 陇西县| 大悟县| 无锡市| 德江县| 南雄市| 枣强县| 嵩明县| 林口县| 芷江| 长顺县| 黔东| 常宁市| 牟定县| 崇仁县| 梅州市| 萨嘎县| 奎屯市| 富裕县| 永嘉县|