午夜剧场伦理_日本一道高清_国产又黄又硬_91黄色网战_女同久久另类69精品国产_妹妹的朋友在线

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

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

瀏覽:149日期:2022-08-04 14:00:54

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)>>> now2matrix([[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è)參考,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 中文字幕在线观看网站 | 久久性网| 亚洲+小说+欧美+激情+另类 | av伦理在线 | 麻豆视频免费在线 | 九九热这里都是精品 | 成人在线播放视频 | 成人在线视频网址 | 免费超碰在线 | 亚洲综合欧美 | 国产黄色精品网站 | 国产精品50页 | 亚洲人在线| 亚洲成年人专区 | 免费的黄色av | 播五月综合 | 国产sm在线观看 | 亚洲图片自拍偷拍 | 国产精品大全 | japanese在线视频 | 深夜久久久 | 欧美成人免费在线 | 一二三四区在线 | 国产福利小视频在线观看 | 在线中文视频 | 日韩视频在线观看免费视频 | 亚洲天堂欧美 | 成人免费播放视频 | 欧美成人手机在线 | 日韩av免费在线看 | 在线看v| 黄色一级片网站 | 蜜臀久久99精品久久久久久宅男 | 久久伊人国产 | 久久神马影院 | 欧美三级视频在线观看 | 久久久成人免费视频 | 国产麻豆免费视频 | v片在线观看 | 久久高清 | 欧美色资源|