Python3--我的代码库之Axis(五)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/c_air_c/article/details/81113919

一、Along an Axis

Axes are defined for arrays with more than one dimension.

A 2-dimensional array has two corresponding axes:

The first running vertically downwards across rows (axis 0)
The second running horizontally across columns (axis 1)


解释

  1. 轴这个概念是为不止一个维度的数组准备的;
  2. axis = 0,第一个维度,垂直向下贯穿所有行数据,数据聚合时,注意贯穿,合并数组时,注意垂直向下;
  3. axis = 1,第二个维度,水平贯穿所有列数据,注意的点如上;

For example

a = array([[1, 2, 3],
                  [4, 5, 6]])
b = array([[2, 3, 4],
                  [5, 6, 7]])

np.sum((a),axis=0) # 向下贯穿所有行数据,即按照分别计算所有列的和

Out:

array([5, 7, 9])

np.sum((b),axis=1) # 水平贯穿所有列数据,即按照分别计算所有行的和

Out:

array([ 9, 18])


二、Merge’s Axis


a = array([[1, 2, 3],
                  [4, 5, 6]])
b = array([[2, 3, 4],
                  [5, 6, 7]])
np.concatenate((a,b),axis = 0) #vertically downwards,垂直向下合并

Out:

array([[1, 2, 3],
          [4, 5, 6],
          [2, 3, 4],
          [5, 6, 7]])

np.concatenate((a,b),axis = 1) #horizontally,水平合并
Out[37]:
array([[1, 2, 3, 2, 3, 4],
[4, 5, 6, 5, 6, 7]])


猜你喜欢

转载自blog.csdn.net/c_air_c/article/details/81113919