np.c_和np.r_的用法解析

官方解释链接:https://numpy.org/doc/stable/reference/generated/numpy.r_.html

import numpy as np

a = np.array([[1, 2, 3], [7, 8, 9]])

b = np.array([[4, 5, 6], [1, 2, 3]])

d = np.array([7, 8, 9])

e = np.array([1, 2, 3])

def test_c_():
    print("a:")
    print(a)
    print("b:")
    print(b)

    c = np.c_[a, b]
    print("np.c_[a, b]:")
    print(c)

    f = np.c_[d, e]
    print("d:")
    print(d)
    print("e:")
    print(e)
    print("np.c_[d, e]:")
    print(f)

def test_r_():
    print("a:")
    print(a)
    print("b:")
    print(b)

    g = np.r_[a, b]
    print("np.r_[a, b]:")
    print(g)

    print("d:")
    print(d)
    print("e:")
    print(e)

    h = np.r_[d, e]
    print("np.r_[d, e]:")
    print(h)


if __name__ == '__main__':
    test_c_()
    test_r_()

结果:

a:
[[1 2 3]
 [7 8 9]]
b:
[[4 5 6]
 [1 2 3]]
np.c_[a, b]:
[[1 2 3 4 5 6]
 [7 8 9 1 2 3]]
d:
[7 8 9]
e:
[1 2 3]
np.c_[d, e]:
[[7 1]
 [8 2]
 [9 3]]
a:
[[1 2 3]
 [7 8 9]]
b:
[[4 5 6]
 [1 2 3]]
np.r_[a, b]:
[[1 2 3]
 [7 8 9]
 [4 5 6]
 [1 2 3]]
d:
[7 8 9]
e:
[1 2 3]
np.r_[d, e]:
[7 8 9 1 2 3]
文章目录