LoginSignup
1
0

More than 3 years have passed since last update.

Python npのreshapeを直感で覚える

Posted at

背景

npのreshapeがなんとなくわからなかった。
しかしコードを書いてみると、何となくわかったので直感で勉強したい人用に共有したいと思いました。

サンプルコード

np_reshape_.py

#1行12列
list_1 = [10.0, 14.0, 23.0, 27.0, 37.0, 58.0, 81.0, 82.0, 135.0, 169.0, 344.0, 319.0]
print(list_1)
#[10.0, 14.0, 23.0, 27.0, 37.0, 58.0, 81.0, 82.0, 135.0, 169.0, 344.0, 319.0]

#3行4列
list_2 = np.array(list_1).reshape(3, 4)
print(list_2)
"""
[[ 10.  14.  23.  27.]
 [ 37.  58.  81.  82.]
 [135. 169. 344. 319.]]
"""

#6行2列
list_3 = np.array(list_1).reshape(6, 2)
print(list_3)
"""
[[ 10.  14.]
 [ 23.  27.]
 [ 37.  58.]
 [ 81.  82.]
 [135. 169.]
 [344. 319.]]
"""

#6行2列 2列にすると座標系
list_4 = np.array(list_1).reshape(-1, 2)
print(list_4)
"""
[[ 10.  14.]
 [ 23.  27.]
 [ 37.  58.]
 [ 81.  82.]
 [135. 169.]
 [344. 319.]]
"""

#6行2列
list_5 = np.array(list_1).reshape(6, -1)
print(list_5)
"""
[[ 10.  14.]
 [ 23.  27.]
 [ 37.  58.]
 [ 81.  82.]
 [135. 169.]
 [344. 319.]]
"""

反省

npのreshapeの-1の使い方が直感的にわかった。

1
0
2

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
1
0