亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時(shí)間:2020年03月03日 16:08:04   作者:饕餮爭(zhēng)鋒  
這篇文章主要介紹了python 的numpy庫(kù)中的mean()函數(shù)用法介紹,具有很好對(duì)參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

1. mean() 函數(shù)定義:

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()函數(shù)功能:求取均值

經(jīng)常操作的參數(shù)為axis,以m * n矩陣舉例:

axis 不設(shè)置值,對(duì) m*n 個(gè)數(shù)求均值,返回一個(gè)實(shí)數(shù)

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

axis =1 :壓縮列,對(duì)各行求均值,返回 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) # 對(duì)所有元素求均值
3.5


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


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

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

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

numpy中numpy.nanmax的官方文檔

原理

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

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

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

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

通過(guò)閱讀numpy的文檔發(fā)現(xiàn),存在np.nanmax的函數(shù),可以將np.nan排除進(jìn)行最大值的計(jì)算,并得到想要的正確結(jié)果。

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

速度區(qū)別

速度由快到慢依次:

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

以上這篇python 的numpy庫(kù)中的mean()函數(shù)用法介紹就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • python中with語(yǔ)句結(jié)合上下文管理器操作詳解

    python中with語(yǔ)句結(jié)合上下文管理器操作詳解

    這篇文章主要給大家介紹了關(guān)于python中with語(yǔ)句結(jié)合上下文管理器操作的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用python具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • 舉例講解Python中的死鎖、可重入鎖和互斥鎖

    舉例講解Python中的死鎖、可重入鎖和互斥鎖

    這篇文章主要介紹了舉例講解Python中的死鎖、可重入鎖和互斥鎖,盡管線程編程方面Python的GIL問(wèn)題老生常談...需要的朋友可以參考下
    2015-11-11
  • python使用Matplotlib畫(huà)餅圖

    python使用Matplotlib畫(huà)餅圖

    這篇文章主要介紹了python使用Matplotlib畫(huà)餅圖,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-09-09
  • Python設(shè)計(jì)模式之代理模式實(shí)例詳解

    Python設(shè)計(jì)模式之代理模式實(shí)例詳解

    這篇文章主要介紹了Python設(shè)計(jì)模式之代理模式,結(jié)合實(shí)例形式較為詳細(xì)的分析了代理模式的概念、原理及Python定義、使用代理模式相關(guān)操作技巧,需要的朋友可以參考下
    2019-01-01
  • Pytorch使用Visdom進(jìn)行數(shù)據(jù)可視化的示例代碼

    Pytorch使用Visdom進(jìn)行數(shù)據(jù)可視化的示例代碼

    pytorch Visdom可視化,是一個(gè)靈活的工具,用于創(chuàng)建,組織和共享實(shí)時(shí)豐富數(shù)據(jù)的可視化,這個(gè)博客簡(jiǎn)要介紹一下在使用Pytorch進(jìn)行數(shù)據(jù)可視化的一些內(nèi)容,感興趣的朋友可以參考下
    2023-12-12
  • Python文件處理、os模塊、glob模塊

    Python文件處理、os模塊、glob模塊

    這篇文章介紹了Python處理文件的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-05-05
  • python百行代碼自制電腦端網(wǎng)速懸浮窗的實(shí)現(xiàn)

    python百行代碼自制電腦端網(wǎng)速懸浮窗的實(shí)現(xiàn)

    這篇文章主要介紹了python百行代碼自制電腦端網(wǎng)速懸浮窗的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05
  • python計(jì)算圓周長(zhǎng)、面積、球體體積并畫(huà)出圓

    python計(jì)算圓周長(zhǎng)、面積、球體體積并畫(huà)出圓

    這篇文章主要介紹了python計(jì)算圓周長(zhǎng)、面積、球體體積并畫(huà)出圓(python3+PyObject+Gtk實(shí)現(xiàn)界面聯(lián)動(dòng)),需要的朋友可以參考下
    2014-04-04
  • Python sort 自定義函數(shù)排序問(wèn)題

    Python sort 自定義函數(shù)排序問(wèn)題

    這篇文章主要介紹了Python sort 自定義函數(shù)排序問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • Python實(shí)現(xiàn)抓取騰訊視頻所有電影的示例代碼

    Python實(shí)現(xiàn)抓取騰訊視頻所有電影的示例代碼

    這篇文章主要為大家介紹了如何使用python實(shí)現(xiàn)抓取騰訊視頻所有電影,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-04-04

最新評(píng)論