python numpy ndarray元素明智的意思

时间:2016-05-25 17:06:13

标签: python numpy vectorization

我想计算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]

除了矢量化求和除以外,是否有任何内置函数用于此操作?

2 个答案:

答案 0 :(得分:31)

您可以直接使用Grid

<Grid>
    <GroupBox Header="Calibracion" Margin="0,0,0,10">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="5*" /> <!-- Or '*', 'Auto', etc -->
                <ColumnDefinition Width="5*" /> <!-- Or '*', 'Auto', etc -->
            </Grid.ColumnDefinitions>
            <GroupBox Header="Equipo" Grid.Column="0" Grid.Row="0">
                <!-- GroupBox contents here -->
            </GroupBox>
            <GroupBox Header="Patron" Grid.Column="1" Grid.Row="0">
                <!-- GroupBox contents here -->
            </GroupBox>
            <GroupBox Header="Condificones" Grid.Column="0" Grid.Row="1">
                <!-- GroupBox contents here -->
            </GroupBox>
        </Grid>
    </GroupBox>
</Grid>

答案 1 :(得分:2)

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
'''