簡體   English   中英

python numpy ndarray元素明智的意思

[英]python numpy ndarray element-wise mean

我想計算numpy ndarray的元素平均值。

In [56]: a = np.array([10, 20, 30])

In [57]: b = np.array([30, 20, 20])

In [58]: c = np.array([50, 20, 40])

我想要的是:

[30, 20, 30]

除了矢量化和和除以外,是否有任何內置函數用於此操作?

你可以直接使用np.mean

>>> np.mean([a, b, c], axis=0)
array([ 30.,  20.,  30.])

Pandas DataFrames內置了操作來獲取列和行的方法。 以下代碼可以幫助您:

import pandas and numpy
import pandas as pd
import numpy as np

# Define a DataFrame
df = pd.DataFrame([
np.arange(1,5), 
np.arange(6,10),
np.arange(11,15)
])

# Get column means by adding the '.mean' argument
# to the name of your pandas Data Frame
# and specifying the axis

column_means = df.mean(axis = 0)

'''
print(column_means)

0    6.0
1    7.0
2    8.0
3    9.0
dtype: float64
'''   

# Get row means by adding the '.mean' argument
# to the name of your pandas Data Frame
# and specifying the axis

row_means = df.mean(axis = 1)
'''
print(row_means)

0     2.5
1     7.5
2    12.5
dtype: float64
'''

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM