An array has a shape given by the number of elements along each axis (配列は、各軸に沿った要素の数によって与えられた shape をもつ):
import numpy as np
a = np.floor(10 * np.random.random((3, 4)))
a
array([[ 0., 6., 9., 3.], [ 8., 3., 3., 2.], [ 4., 4., 2., 9.]])
a.shape
(3, 4)
The shape of an array can be changed with various commands 配列のshape は、様々なコマンドで変更できる:
a.ravel() # flatten the array (配列を解きほぐして(ravel)平らにする)
array([ 0., 6., 9., 3., 8., 3., 3., 2., 4., 4., 2., 9.])
a.shape = (6, 2) # 6×2行列
a
array([[ 0., 6.], [ 9., 3.], [ 8., 3.], [ 3., 2.], [ 4., 4.], [ 2., 9.]])
a.T # 転置行列 2 ×6
array([[ 0., 9., 8., 3., 4., 2.], [ 6., 3., 3., 2., 4., 9.]])
The order of the elements in the array resulting from ravel()
is normally “C-style”, that is, the rightmost index “changes the fastest”,
so the element after a[0, 0]
is a[0, 1]
.
(ravel()
からの結果として得られる配列中の要素の順番は、普通に "C-style"、
即ち、一番右のindex が「最初に変わる」ので、
a[0, 0]
の次に来る要素は a[0, 1]
である。)
If the array is reshaped to some other shape, again the array is treated as “C-style”. (もし、その配列が、他のshape にreshape されても、配列は再び "C-style" として扱われる。)
Numpy normally creates arrays stored in this order,
so ravel()
will usually not need to copy its argument,
but if the array was made by taking slices of another array or created with unusual options,
it may need to be copied.
(Numpyは、普通ravel()
が引数(順序)をコピーしなくてもいいように、この順序で格納して配列を作成するが、
もし、その配列が他の配列をスライスして作られたものであったり、
一般的でないオプションで作られたものである場合、
コピーが必要になるかもしれない。)
The functions ravel()
and reshape()
can also be instructed, using an optional argument, to use FORTRAN-style arrays,
in which the leftmost index changes the fastest.
(関数 ravel()
, reshape()
は、オプション引数を使うことで、
FORTRAN形式の配列を使うこともできる。
その場合、一番左の添字が最初に変わる。)
The reshape
function returns its argument with a modified shape,
whereas the ndarray.resize
method modifies the array itself
(reshape
関数は、その引数を修正されたshape (新しい配列)で返す
一方で、ndarray.resize
メソッドは配列そのものを修正する):
a
array([[ 0., 6.], [ 9., 3.], [ 8., 3.], [ 3., 2.], [ 4., 4.], [ 2., 9.]])
a.resize((2, 6))
a
array([[ 0., 6., 9., 3., 8., 3.], [ 3., 2., 4., 4., 2., 9.]])
If a dimension is given as -1
in a reshaping operation,
the other dimensions are automatically calculated
(もし、次元が、reshape操作において、-1で与えられた場合、
他の次元は自動的に計算される):
a.reshape(3, -1)
array([[ 0., 6., 9., 3.], [ 8., 3., 3., 2.], [ 4., 4., 2., 9.]])
See also:
Several arrays can be stacked together along different axes (それぞれの配列は、異なる軸に沿って一緒にスタックされる):
a = np.floor(10 * np.random.random((2, 2)))
a
array([[ 0., 4.], [ 1., 3.]])
b = np.floor(10 * np.random.random((2, 2)))
b
array([[ 2., 9.], [ 1., 1.]])
np.vstack((a,b)) # vertical stack
array([[ 0., 4.], [ 1., 3.], [ 2., 9.], [ 1., 1.]])
np.hstack((a,b)) # horizontal stack
array([[ 0., 4., 2., 9.], [ 1., 3., 1., 1.]])
The function column_stack
stacks 1D arrays as columns into a 2D array.(column_stack
関数は1次元配列を列として積み上げて2次元配列を作る。)
It is equivalent to vstack
(hstackの間違い?)only for 1D arrays(1次元配列の場合に限れば、これはvstack
と等価である):
np.column_stack((a,b)) # With 2D arrays
array([[ 0., 4., 2., 9.], [ 1., 3., 1., 1.]])
a = np.array([4., 1.])
b = np.array([2., 8.])
a
array([ 4., 1.])
a[:, np.newaxis]# This allows to have a 2D columns vector (2Dの列ベクトルをもつ)
array([[ 4.], [ 1.]])
np.column_stack((a, b))
array([[ 4., 2.], [ 1., 8.]])
np.column_stack((a[:, np.newaxis], b[:, np.newaxis]))
array([[ 4., 2.], [ 1., 8.]])
np.vstack((a, b))
array([[ 4., 1.], [ 2., 8.]])
np.vstack((a[:, np.newaxis], b[:, np.newaxis])) # The behavior of vstack is different
array([[ 4.], [ 1.], [ 2.], [ 8.]])
np.hstack((a[:, np.newaxis], b[:, np.newaxis]))
array([[ 4., 2.], [ 1., 8.]])
For arrays of with more than two dimensions, hstack
stacks along their second axes,
vstack
stacks along their first axes,
and concatenate
allows for an optional arguments giving the number of the axis along which the concatenation should happen.
(
hstack
は2番目の軸(第1軸)に沿ってスタックし、vstack
は1番目の軸(第0軸)に沿ってスタックし、concatenate
はどの軸に沿って結合が起こるべきかを示す数をオプション引数に与えることができる。)In complex cases, r_
and c_
are useful for creating arrays by stacking numbers along one axis.
(複雑なケースにおいては、r_
と c_
が、ある軸に沿って配列を作る際に 便利。
They allow the use of range literals (”:”)
(これらは、範囲を表すリテラル (":")
を使うことができる) :
np.r_[1:4, 0, 4] # 行ベクトル(配列)
array([1, 2, 3, 0, 4])
b = np.array([[1, 2, 3]]), 0, 0, np.array([[4, 5, 6]])
b
(array([[1, 2, 3]]), 0, 0, array([[4, 5, 6]]))
np.c_[b] # Translates slice objects to concatenation along the second axis.
array([[1, 2, 3, 0, 0, 4, 5, 6]])
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
np.stack((a, b))
array([[1, 2, 3], [4, 5, 6]])
np.stack((a, b), axis=0)
array([[1, 2, 3], [4, 5, 6]])
np.stack((a, b), axis=1)
array([[1, 4], [2, 5], [3, 6]])
Stack arrays in sequence vertically (row wise).
Stack arrays in sequence depth wise (along third axis).
a = np.array((1, 2, 3))
b = np.array((4, 5, 6))
np.dstack((a, b)).shape # third軸目に積むので2
(1, 3, 2)
np.dstack((a, b))
array([[[1, 4], [2, 5], [3, 6]]])
Join a sequence of arrays along an existing axis.
a = np.array([
[1, 2],
[3, 4]
])
b = np.array([
[5, 6]
])
a.shape
(2, 2)
b.shape
(1, 2)
np.concatenate((a, b), axis=0)
array([[1, 2], [3, 4], [5, 6]])
np.concatenate((a, b), axis=1) # bが1*2行列なので積めない
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-33-7d73420ac2d6> in <module>() ----> 1 np.concatenate((a, b), axis=1) # bが1*2行列なので積めない ValueError: all the input array dimensions except for the concatenation axis must match exactly
np.concatenate((a, b.T), axis=1)
array([[1, 2, 5], [3, 4, 6]])
Using hsplit
,
you can split an array along its horizontal axis,
either by specifying the number of equally shaped arrays to return,
or by specifying the columns after which the division should occur
(hsplit
を用いれば、同じshape の配列をいくつ返すか、
あるいはどの列で分割が起こるべきかを指定して、配列を水平軸沿いに分割できる:):
a = np.floor(10 * np.random.random((2, 12)))
a
array([[ 6., 7., 0., 5., 0., 3., 7., 6., 9., 6., 0., 8.], [ 1., 8., 8., 4., 8., 5., 5., 2., 2., 3., 3., 8.]])
np.hsplit(a, 3) # Split a into 3, 2×4 3つ
[array([[ 6., 7., 0., 5.], [ 1., 8., 8., 4.]]), array([[ 0., 3., 7., 6.], [ 8., 5., 5., 2.]]), array([[ 9., 6., 0., 8.], [ 2., 3., 3., 8.]])]
np.hsplit(a, (3, 4)) # Split a after the third and the fourth column 3列目と4列目に分割
[array([[ 6., 7., 0.], [ 1., 8., 8.]]), array([[ 5.], [ 4.]]), array([[ 0., 3., 7., 6., 9., 6., 0., 8.], [ 8., 5., 5., 2., 2., 3., 3., 8.]])]
vsplit
splits along the vertical axis,
(vsplit
は垂直軸沿いに分割し、
x = np.arange(16.).reshape(4, 4)
x
array([[ 0., 1., 2., 3.], [ 4., 5., 6., 7.], [ 8., 9., 10., 11.], [ 12., 13., 14., 15.]])
np.vsplit(x, 2)
[array([[ 0., 1., 2., 3.], [ 4., 5., 6., 7.]]), array([[ 8., 9., 10., 11.], [ 12., 13., 14., 15.]])]
np.vsplit(x, (3, 6))
[array([[ 0., 1., 2., 3.], [ 4., 5., 6., 7.], [ 8., 9., 10., 11.]]), array([[ 12., 13., 14., 15.]]), array([], shape=(0, 4), dtype=float64)]
and array_split
allows one to specify along which axis to split.
(array_split
は、どの軸沿いに分割するかを指定できる。)
x = np.arange(8.)
x
array([ 0., 1., 2., 3., 4., 5., 6., 7.])
np.array_split(x, 3)
[array([ 0., 1., 2.]), array([ 3., 4., 5.]), array([ 6., 7.])]