How to prepend an n-by-1 column of ones to a matrix in python using numpy ? (machine learning)

Daidalos November 14, 2019

Examples of how to prepend an n-by-1 column of ones to a matrix of training input data:

Prepend an n-by-1 column of ones to a training dataset

Let's consider the following matrix X of training data X (with n=6 observations and d=3 features):

X = \left( \begin{array}{ccc}
x_{11} & x_{12} & x_{13} \\
x_{21} & x_{22} & x_{23} \\
x_{31} & x_{32} & x_{33} \\
x_{41} & x_{42} & x_{43} \\
x_{51} & x_{52} & x_{53} \\
x_{61} & x_{62} & x_{63}
\end{array}\right)

the goal is to prepend an n-by-1 column of ones to the matrix X:

X = \left( \begin{array}{ccc}
1 & x_{11} & x_{12} & x_{13} \\
1 & x_{21} & x_{22} & x_{23} \\
1 & x_{31} & x_{32} & x_{33} \\
1 & x_{41} & x_{42} & x_{43} \\
1 & x_{51} & x_{52} & x_{53} \\
1 & x_{61} & x_{62} & x_{63}
\end{array}\right)

As an example, let's create a matrix X with random element:

>>> import numpy as np
>>> X = np.random.randint(100, size=(6,3))
>>> X
array([[46, 16, 11],
       [79, 95, 54],
       [35,  3, 90],
       [62, 71, 63],
       [14, 61, 80],
       [92, 69, 57]])


to create a matrix of ones of size (6-by-1) a solution is to use the numpy finction ones():

>>> b = np.ones(X.shape[0])
>>> b.shape
(6,)


than can be concatenated to the matrix X using the numpy function numpy.c_ , illustration:

>>> X = np.c_[np.ones(X.shape[0]),X]
>>> X
array([[  1.,  46.,  16.,  11.],
       [  1.,  79.,  95.,  54.],
       [  1.,  35.,   3.,  90.],
       [  1.,  62.,  71.,  63.],
       [  1.,  14.,  61.,  80.],
       [  1.,  92.,  69.,  57.]])


Note: check if d < n else transpose the matrix:

>>> X
array([[46, 79, 35, 62, 14, 92],
       [16, 95,  3, 71, 61, 69],
       [11, 54, 90, 63, 80, 57]])
>>> if X.shape[0] < X.shape[1]: X = X.T 
... 
>>> X
array([[46, 16, 11],
       [79, 95, 54],
       [35,  3, 90],
       [62, 71, 63],
       [14, 61, 80],
       [92, 69, 57]])


Add a column of 1 to a 1D matrix

To make a prediction it is necessary to add a 1 as well, example:

>>> Y_new = np.array([11.,  64.,  20.])
>>> Y_new
array([ 11.,  64.,  20.])


A solution is to use the numpy function numpy.concatenate:

>>> Y_new = np.concatenate([np.ones(1),Y_new])
>>> Y_new
array([  1.,  11.,  64.,  20.])


References

numpy.c_ scipy doc
numpy.concatenate scipy doc
numpy.transpose scipy doc
ones() scipy doc

Licence