What's the simplest way to extend a numpy array in 2 dimensions?
The shortest in terms of lines of code i can think of is for the first question.
>>> import numpy as np
>>> p = np.array([[1,2],[3,4]])
>>> p = np.append(p, [[5,6]], 0)
>>> p = np.append(p, [[7],[8],[9]],1)
>>> p
array([[1, 2, 7],
[3, 4, 8],
[5, 6, 9]])
And the for the second question
p = np.array(range(20))
>>> p.shape = (4,5)
>>> p
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19]])
>>> n = 2
>>> p = np.append(p[:n],p[n+1:],0)
>>> p = np.append(p[...,:n],p[...,n+1:],1)
>>> p
array([[ 0, 1, 3, 4],
[ 5, 6, 8, 9],
[15, 16, 18, 19]])
A useful alternative answer to the first question, using the examples from tomeedee’s answer, would be to use numpy’s vstack and column_stack methods:
Given a matrix p,
>>> import numpy as np
>>> p = np.array([ [1,2] , [3,4] ])
an augmented matrix can be generated by:
>>> p = np.vstack( [ p , [5 , 6] ] )
>>> p = np.column_stack( [ p , [ 7 , 8 , 9 ] ] )
>>> p
array([[1, 2, 7],
[3, 4, 8],
[5, 6, 9]])
These methods may be convenient in practice than np.append() as they allow 1D arrays to be appended to a matrix without any modification, in contrast to the following scenario:
>>> p = np.array([ [ 1 , 2 ] , [ 3 , 4 ] , [ 5 , 6 ] ] )
>>> p = np.append( p , [ 7 , 8 , 9 ] , 1 )
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.6/dist-packages/numpy/lib/function_base.py", line 3234, in append
return concatenate((arr, values), axis=axis)
ValueError: arrays must have same number of dimensions
In answer to the second question, a nice way to remove rows and columns is to use logical array indexing as follows:
Given a matrix p,
>>> p = np.arange( 20 ).reshape( ( 4 , 5 ) )
suppose we want to remove row 1 and column 2:
>>> r , c = 1 , 2
>>> p = p [ np.arange( p.shape[0] ) != r , : ]
>>> p = p [ : , np.arange( p.shape[1] ) != c ]
>>> p
array([[ 0, 1, 3, 4],
[10, 11, 13, 14],
[15, 16, 18, 19]])
Note - for reformed Matlab users - if you wanted to do these in a one-liner you need to index twice:
>>> p = np.arange( 20 ).reshape( ( 4 , 5 ) )
>>> p = p [ np.arange( p.shape[0] ) != r , : ] [ : , np.arange( p.shape[1] ) != c ]
This technique can also be extended to remove sets of rows and columns, so if we wanted to remove rows 0 & 2 and columns 1, 2 & 3 we could use numpy's setdiff1d function to generate the desired logical index:
>>> p = np.arange( 20 ).reshape( ( 4 , 5 ) )
>>> r = [ 0 , 2 ]
>>> c = [ 1 , 2 , 3 ]
>>> p = p [ np.setdiff1d( np.arange( p.shape[0] ), r ) , : ]
>>> p = p [ : , np.setdiff1d( np.arange( p.shape[1] ) , c ) ]
>>> p
array([[ 5, 9],
[15, 19]])
Another elegant solution to the first question may be the insert
command:
p = np.array([[1,2],[3,4]])
p = np.insert(p, 2, values=0, axis=1) # insert values before column 2
Leads to:
array([[1, 2, 0],
[3, 4, 0]])
insert
may be slower than append
but allows you to fill the whole row/column with one value easily.
As for the second question, delete
has been suggested before:
p = np.delete(p, 2, axis=1)
Which restores the original array again:
array([[1, 2],
[3, 4]])