2016-05-25 16 views
9

numpy ndarrayの要素ごとの平均を計算したいと思います。私が欲しいものpython 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] 

は、ベクトル化の和と分割する以外に、この操作のための任意の内蔵機能は、ありますか?

答えて

14

あなただけの直接np.meanを使用することができます。

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

パンダのデータフレームは、列と行の手段を得るために、業務に組み込まれています。次のコードはあなたを助けるかもしれません:

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 
''' 
関連する問題