0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Numpy~stack and split~

Posted at

配列の結合と分割

  1. 結合
    • concatenate
    • vstack
    • hstack
  2. 分割
    • split
    • vsplit
    • hsplit

import numpy as np
x1 = np.array([1,2,3])
x2 = np.array([4,5,6])

np.concatenate([x1, x2])
# concatenateは鎖状につなぐの意味
# array([1, 2, 3, 4, 5, 6])

np.vstack([x1, x2])
# 縦につなげることが可能
# array([[1, 2, 3],
#        [4, 5, 6]])

np.hstack([x1, x2])
# 横につなげる
# array([1, 2, 3, 4, 5, 6])

## 分割

x = np.arange(10)
# array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

x1, x2, x3 = np.split(x, [1, 2])

# x1: array([0])
# x2: array([1])
# x3: array([2, 3, 4, 5, 6, 7, 8, 9])

y = np.eye(4)
# array([[1., 0., 0., 0.],
#        [0., 1., 0., 0.],
#        [0., 0., 1., 0.],
#        [0., 0., 0., 1.]])
np.vsplit(y, [2])
# [array([[1., 0., 0., 0.],
#        [0., 1., 0., 0.]]), 
# array([[0., 0., 1., 0.],
#        [0., 0., 0., 1.]])]
np.hsplit(y,[2])
# [array([[1., 0.],
#        [0., 1.],
#        [0., 0.],
#        [0., 0.]]), 
# array([[0., 0.],
#        [0., 0.],
#        [1., 0.],
#        [0., 1.]])]
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?