python row_stack_Python之numpy数组学习(二)

#-*- coding:utf-8 -*-

#stacking.py

import numpy as np

#创建数组

a = np.arange(9).reshape(3,3)

print(a)

#Out:

#array([[0, 1, 2],

# [3, 4, 5],

# [6, 7, 8]])

b = 2 * a

print (b)

#Out:

#array([[ 0, 2, 4],

# [ 6, 8, 10],

# [12, 14, 16]])

#水平叠加

print (np.hstack((a, b)))

#Out:

#array([[ 0, 1, 2, 0, 2, 4],

# [ 3, 4, 5, 6, 8, 10],

# [ 6, 7, 8, 12, 14, 16]])

print (np.concatenate((a, b), axis=1))

#Out:

#array([[ 0, 1, 2, 0, 2, 4],

# [ 3, 4, 5, 6, 8, 10],

# [ 6, 7, 8, 12, 14, 16]])

#垂直叠加

print (np.vstack((a, b)))

#Out:

#array([[ 0, 1, 2],

# [ 3, 4, 5],

# [ 6, 7, 8],

# [ 0, 2, 4],

# [ 6, 8, 10],

# [12, 14, 16]])

print (np.concatenate((a, b), axis=0))

#Out:

#array([[ 0, 1, 2],

# [ 3, 4, 5],

# [ 6, 7, 8],

# [ 0, 2, 4],

# [ 6, 8, 10],

# [12, 14, 16]])

#深度叠加

print (np.dstack((a, b)))

#Out:

#array([[[ 0, 0],

# [ 1, 2],

# [ 2, 4]],

#

# [[ 3, 6],

# [ 4, 8],

# [ 5, 10]],

#

# [[ 6, 12],

# [ 7, 14],

# [ 8, 16]]])

oned = np.arange(2)

print (oned)

#Out: array([0, 1])

twice_oned = 2 * oned

print (twice_oned)

#Out: array([0, 2])

print (np.column_stack((oned, twice_oned)))

#Out:

#array([[0, 0],

# [1, 2]])

print (np.column_stack((a, b)))

#Out:

#array([[ 0, 1, 2, 0, 2, 4],

# [ 3, 4, 5, 6, 8, 10],

# [ 6, 7, 8, 12, 14, 16]])

#数组对比

print (np.column_stack((a, b)) == np.hstack((a, b)))

#Out:

#array([[ True, True, True, True, True, True],

# [ True, True, True, True, True, True],

# [ True, True, True, True, True, True]], dtype=bool)

#列式堆叠

print (np.row_stack((oned, twice_oned)))

#Out:

#array([[0, 1],

# [0, 2]])

print (np.row_stack((a, b)))

#Out:

#array([[ 0, 1, 2],

# [ 3, 4, 5],

# [ 6, 7, 8],

# [ 0, 2, 4],

# [ 6, 8, 10],

# [12, 14, 16]])

print (np.row_stack((a,b)) == np.vstack((a, b)))

#Out:

#array([[ True, True, True],

# [ True, True, True],

# [ True, True, True],

# [ True, True, True],

# [ True, True, True],

# [ True, True, True]], dtype=bool)

你可能感兴趣的:(python,row_stack)