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]])