最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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è)置值,對 m*n 個(gè)數(shù)求均值,返回一個(gè)實(shí)數(shù)

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.]])

補(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對象的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

通過閱讀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庫中的mean()函數(shù)用法介紹就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 教你如何在pycharm中使用less

    教你如何在pycharm中使用less

    這篇文章主要介紹了如何在pycharm中使用less,操作步驟真的很簡單,本文通過圖文并茂的形式給大家詳細(xì)介紹,需要的朋友可以參考下
    2021-10-10
  • python3 QT5 端口轉(zhuǎn)發(fā)工具兩種場景分析

    python3 QT5 端口轉(zhuǎn)發(fā)工具兩種場景分析

    這篇文章主要介紹了python3 QT5 端口轉(zhuǎn)發(fā)工具,功能是打開本機(jī)端口,映射到指定IP的端口,接下來通過兩種場景給大家詳細(xì)介紹,感興趣的朋友一起看看吧
    2022-01-01
  • 一篇文章帶你了解幾個(gè)好用的Python技巧

    一篇文章帶你了解幾個(gè)好用的Python技巧

    這篇文章主要介紹了幾個(gè)Python小技巧,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下,希望能夠給你帶來幫助
    2021-10-10
  • Python中time與datetime模塊使用方法詳解

    Python中time與datetime模塊使用方法詳解

    這篇文章主要為大家詳細(xì)介紹了Python中time與datetime模塊使用方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03
  • python的多重繼承的理解

    python的多重繼承的理解

    這篇文章主要介紹了python的多重繼承的理解的相關(guān)資料,多重繼承不是多容易理解,這里舉例說明幫助大家學(xué)習(xí)參考,需要的朋友可以參考下
    2017-08-08
  • Python彈球小游戲的項(xiàng)目代碼

    Python彈球小游戲的項(xiàng)目代碼

    本文主要介紹了Python彈球小游戲的項(xiàng)目代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • Python編寫萬花尺圖案實(shí)例

    Python編寫萬花尺圖案實(shí)例

    在本篇文章里小編給大家分享的是一篇關(guān)于Python編寫萬花尺圖案實(shí)例的內(nèi)容,有興趣的朋友們可以參考下。
    2021-01-01
  • Python實(shí)現(xiàn)簡易的圖書管理系統(tǒng)

    Python實(shí)現(xiàn)簡易的圖書管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了Python實(shí)現(xiàn)簡易的圖書管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • 淺談tensorflow使用張量時(shí)的一些注意點(diǎn)tf.concat,tf.reshape,tf.stack

    淺談tensorflow使用張量時(shí)的一些注意點(diǎn)tf.concat,tf.reshape,tf.stack

    這篇文章主要介紹了淺談tensorflow使用張量時(shí)的一些注意點(diǎn)tf.concat,tf.reshape,tf.stack,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-06-06
  • Python函數(shù)基礎(chǔ)

    Python函數(shù)基礎(chǔ)

    這篇文章主要從函數(shù)開始介紹展開Python函數(shù),以最基本的函數(shù)定義方法描述,需要的朋友可以參考下文簡單的介紹
    2021-08-08

最新評(píng)論

郁南县| 于田县| 高要市| 静海县| 南平市| 阜南县| 志丹县| 南靖县| 佛冈县| 江川县| 方山县| 琼结县| 中牟县| 鹰潭市| 灵山县| 桦川县| 麻江县| 沅陵县| 昌吉市| 荔波县| 松原市| 交口县| 若尔盖县| 栾川县| 明溪县| 德保县| 江永县| 沙湾县| 库尔勒市| 永康市| 盐边县| 济源市| 故城县| 怀安县| 库尔勒市| 汶川县| 沅江市| 卢龙县| 图木舒克市| 呼和浩特市| 石泉县|