Shape Manipulation¶
Changing the shape of any array¶
>>> a = np.floor(10*np.random.random((3,4)))
>>> a
array([[ 2., 8., 0., 6.],
[ 4., 5., 1., 1.],
[ 8., 9., 3., 6.]])
>>> a.shape
(3, 4)
>>> a.ravel() # returns the array, flattened
array([ 2., 8., 0., 6., 4., 5., 1., 1., 8., 9., 3., 6.])
>>> a.reshape(6,2) # returns the array with a modified shape
array([[ 2., 8.],
[ 0., 6.],
[ 4., 5.],
[ 1., 1.],
[ 8., 9.],
[ 3., 6.]])
>>> a.T # returns the array, transposed
array([[ 2., 4., 8.],
[ 8., 5., 9.],
[ 0., 1., 3.],
[ 6., 1., 6.]])
>>> a.T.shape
(4, 3)
>>> a.shape
(3, 4)
>>> import Numeric.LinearAlgebra.HMatrix (Seed, RandDist(..), randomVector, rand)
>>> let rand' seed r c = reshape c (randomVector seed Uniform (r*c))
>>> let a = cmap (floor . (*10)) (rand' 1 3 4) :: Matrix Z
>>> a
(3><4)
[ 2, 8, 0, 6
, 4, 5, 1, 1
, 8, 9, 3, 6 ]
>>> size a
(3,4)
>>> flatten a
[2,8,0,6,4,5,1,1,8,9,3,6]
>>> ((reshape 2) . flatten) a
(6><2)
[ 2, 8
, 0, 6
, 4, 5
, 1, 1
, 8, 9
, 3, 6 ]
>>> tr a
(4><3)
[ 2, 4, 8
, 8, 5, 9
, 0, 1, 3
, 6, 1, 6 ]
>>> size (tr a)
(4,3)
>>> size a
(3,4)
>>> a
array([[ 2., 8., 0., 6.],
[ 4., 5., 1., 1.],
[ 8., 9., 3., 6.]])
>>> a.resize((2,6))
>>> a
array([[ 2., 8., 0., 6., 4., 5.],
[ 1., 1., 8., 9., 3., 6.]])
>>> a.reshape(3,-1)
array([[ 2., 8., 0., 6.],
[ 4., 5., 1., 1.],
[ 8., 9., 3., 6.]])
>>> a
(3><4)
[ 2, 8, 0, 6
, 4, 5, 1, 1
, 8, 9, 3, 6 ]
>>> let a' = ((reshape 6) . flatten) a
>>> a'
(2><6)
[ 2, 8, 0, 6, 4, 5
, 1, 1, 8, 9, 3, 6 ]
>>> (tr . (reshape 3) . flatten . tr) a
(3><4)
[ 2, 8, 0, 6
, 4, 5, 1, 1
, 8, 9, 3, 6 ]
Stacking together different arrays¶
>>> a = np.floor(10*np.random.random((2,2)))
>>> a
array([[ 8., 8.],
[ 0., 0.]])
>>> b = np.floor(10*np.random.random((2,2)))
>>> b
array([[ 1., 8.],
[ 0., 4.]])
>>> np.vstack((a,b))
array([[ 8., 8.],
[ 0., 0.],
[ 1., 8.],
[ 0., 4.]])
>>> np.hstack((a,b))
array([[ 8., 8., 1., 8.],
[ 0., 0., 0., 4.]])
>>> import Numeric.LinearAlgebra.HMatrix (Seed, RandDist(..), randomVector, rand)
>>> let rand' seed r c = reshape c (randomVector seed Uniform (r*c))
>>> let a = cmap (floor . (*10)) (rand' 1 2 2) :: Matrix Z
>>> a
(2><2)
[ 8, 8
, 0, 0 ]
>>> let b = cmap (floor . (*10)) (rand' 2 2 2) :: Matrix Z
>>> b
(2><2)
[ 1, 8
, 0, 4 ]
>>> a === b
(4><2)
[ 8, 8
, 0, 0
, 1, 8
, 0, 4 ]
>>> a ||| b
(2><4)
[ 8, 8, 1, 8
, 0, 0, 0, 4 ]
>>> from numpy import newaxis
>>> np.column_stack((a,b)) # with 2D arrays
array([[ 8., 8., 1., 8.],
[ 0., 0., 0., 4.]])
>>> a = np.array([4.,2.])
>>> b = np.array([3.,8.])
>>> np.column_stack((a,b)) # returns a 2D array
array([[ 4., 3.],
[ 2., 8.]])
>>> np.hstack((a,b)) # the result is different
array([ 4., 2., 3., 8.])
>>> a[:,newaxis] # this allows to have a 2D columns vector
array([[ 4.],
[ 2.]])
>>> np.column_stack((a[:,newaxis],b[:,newaxis]))
array([[ 4., 3.],
[ 2., 8.]])
>>> np.hstack((a[:,newaxis],b[:,newaxis])) # the result is the same
array([[ 4., 3.],
[ 2., 8.]])
>>> np.r_[1:4,0,4]
array([1, 2, 3, 0, 4])
>>> let a = fromList [4,2] :: Vector Z
>>> let b = fromList [3,8] :: Vector Z
>>> fromColumns [a,b]
(2><2)
[ 4, 3
, 2, 8 ]
>>> vjoin [a,b]
[4,2,3,8]
>>> asColumn a
(2><1)
[ 4
, 2 ]
>>> fromBlocks [fmap asColumn [a,b]]
(2><2)
[ 4, 3
, 2, 8 ]
>>> fromList ([1..3] ++ [0] ++ [4]) :: Vector Z
[1,2,3,0,4]
Splitting one array into several smaller ones¶
>>> a = np.floor(10*np.random.random((2,12)))
>>> a
array([[ 9., 5., 6., 3., 6., 8., 0., 7., 9., 7., 2., 7.],
[ 1., 4., 9., 2., 2., 1., 0., 6., 2., 2., 4., 0.]])
>>> np.hsplit(a,3) # Split a into 3
[array([[ 9., 5., 6., 3.],
[ 1., 4., 9., 2.]]), array([[ 6., 8., 0., 7.],
[ 2., 1., 0., 6.]]), array([[ 9., 7., 2., 7.],
[ 2., 2., 4., 0.]])]
>>> np.hsplit(a,(3,4)) # Split a after the third and the fourth column
[array([[ 9., 5., 6.],
[ 1., 4., 9.]]), array([[ 3.],
[ 2.]]), array([[ 6., 8., 0., 7., 9., 7., 2., 7.],
[ 2., 1., 0., 6., 2., 2., 4., 0.]])]
>>> import Numeric.LinearAlgebra.HMatrix (Seed, RandDist(..), randomVector, rand)
>>> let rand' seed r c = reshape c (randomVector seed Uniform (r*c))
>>> let a = cmap (floor . (*10)) (rand' 1 2 12) :: Matrix Z
>>> a
(2><12)
[ 9, 5, 6, 3, 6, 8, 0, 7, 9, 7, 2, 7
, 1, 4, 9, 2, 2, 1, 0, 6, 2, 2, 4, 0 ]
>>> toBlocksEvery 2 4 a -- we could do @toBlocksEvery (rows a) ((floor . (/3) . fromIntegral . cols) a) a@
[[(2><4)
[ 9, 5, 6, 3
, 1, 4, 9, 2 ],(2><4)
[ 6, 8, 0, 7
, 2, 1, 0, 6 ],(2><4)
[ 9, 7, 2, 7
, 2, 2, 4, 0 ]]]
>>> toBlocks [2] [3, 4-3, cols a - 4] a
[[(2><3)
[ 9, 5, 6
, 1, 4, 9 ],(2><1)
[ 3
, 2 ],(2><8)
[ 6, 8, 0, 7, 9, 7, 2, 7
, 2, 1, 0, 6, 2, 2, 4, 0 ]]]