LoginSignup
5
7

More than 5 years have passed since last update.

numpyのarangeとreshapeをよく忘れるので個人的メモ

Last updated at Posted at 2019-01-01

alt

arange

以下の記事から切り出しただけの個人メモ
https://deepage.net/features/numpy-arange.html

np.arrangeは、連番や、同差配列を生成する関数です。
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
のような配列を生成します。

numpy.arange([start, ]stop, [step, ]dtype = None)
パラメータ名
start = (省略可能)生成するとうさ配列の最小の項を設定します。
stop = 生成する等差配列の終点を指定します。
step = (省略可能) 初期値1 生成される数列一つ一つの項間における差を指定します。(公差)


```
import numpy as np
arr1 = np.arange(5)
arr1 #=> array([0, 1, 2, 3, 4])
bar = np.arange(2, 10, 2)
bar => array([2, 4, 6, 8])

なるほど。

reshapeの-1

reshape = 要素数が変わらない範囲でndarrayの形状を変更することが出来る。
特に-1を引数に取るときの挙動を忘れるのでメモ。
第一引数が−1の時は、「第二変数の長さ」の配列を複数生成する。
つまり、
z = np.array([[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12]])
z.reshape(-1)
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
z.reshape(-1, 1)
array([[ 1],
[ 2],
[ 3],
[ 4],
[ 5],
[ 6],
[ 7],
[ 8],
[ 9],
[10],
[11],
[12]])
z.reshape(-1, 2)
array([[ 1, 2],
[ 3, 4],
[ 5, 6],
[ 7, 8],
[ 9, 10],
[11, 12]])

-1が第二引数の時は、列と行が反対になる。
z.reshape(3, -1)
array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12]])

若いもんにはまだまだ負けん。

5
7
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
5
7