Adding a column of numbers 1 to a numpy array

Asked

Viewed 442 times

1

Let X be the numpy array below:

array([ 6.1101,  5.5277,  8.5186,  7.0032,  5.8598,  8.3829,  7.4764,
        8.5781,  6.4862,  5.0546,  5.7107, 14.164 ,  5.734 ,  8.4084,
        5.6407,  5.3794,  6.3654,  5.1301,  6.4296,  7.0708,  6.1891,
       20.27  ,  5.4901,  6.3261,  5.5649, 18.945 , 12.828 , 10.957 ,
       13.176 , 22.203 ,  5.2524,  6.5894,  9.2482,  5.8918,  8.2111,
        7.9334,  8.0959,  5.6063, 12.836 ,  6.3534,  5.4069,  6.8825,
       11.708 ,  5.7737,  7.8247,  7.0931,  5.0702,  5.8014, 11.7   ,
        5.5416,  7.5402,  5.3077,  7.4239,  7.6031,  6.3328,  6.3589,
        6.2742,  5.6397,  9.3102,  9.4536,  8.8254,  5.1793, 21.279 ,
       14.908 , 18.959 ,  7.2182,  8.2951, 10.236 ,  5.4994, 20.341 ,
       10.136 ,  7.3345,  6.0062,  7.2259,  5.0269,  6.5479,  7.5386,
        5.0365, 10.274 ,  5.1077,  5.7292,  5.1884,  6.3557,  9.7687,
        6.5159,  8.5172,  9.1802,  6.002 ,  5.5204,  5.0594,  5.7077,
        7.6366,  5.8707,  5.3054,  8.2934, 13.394 ,  5.4369])

I cannot understand the operation of the following code: (I only realized that created a column of 1 s)

 X = np.c_[np.ones((X.shape[0],1)), X]

The result is:

array([[ 1.    ,  6.1101],
       [ 1.    ,  5.5277],
       [ 1.    ,  8.5186],
       [ 1.    ,  7.0032],
       [ 1.    ,  5.8598],
       [ 1.    ,  8.3829],
       [ 1.    ,  7.4764],
       [ 1.    ,  8.5781],
       [ 1.    ,  6.4862],
       [ 1.    ,  5.0546],
       [ 1.    ,  5.7107],
       [ 1.    , 14.164 ],
       [ 1.    ,  5.734 ],
       [ 1.    ,  8.4084],
       [ 1.    ,  5.6407],
       [ 1.    ,  5.3794],
       [ 1.    ,  6.3654],
       [ 1.    ,  5.1301],
       [ 1.    ,  6.4296],
       [ 1.    ,  7.0708],
       [ 1.    ,  6.1891],
       [ 1.    , 20.27  ],
       [ 1.    ,  5.4901],
       [ 1.    ,  6.3261],
       [ 1.    ,  5.5649],
       [ 1.    , 18.945 ],
       [ 1.    , 12.828 ],
       [ 1.    , 10.957 ],
       [ 1.    , 13.176 ],
       [ 1.    , 22.203 ],
       [ 1.    ,  5.2524],
       [ 1.    ,  6.5894],
       [ 1.    ,  9.2482],
       [ 1.    ,  5.8918],
       [ 1.    ,  8.2111],
       [ 1.    ,  7.9334],
       [ 1.    ,  8.0959],
       [ 1.    ,  5.6063],
       [ 1.    , 12.836 ],
       [ 1.    ,  6.3534],
       [ 1.    ,  5.4069],
       [ 1.    ,  6.8825],
       [ 1.    , 11.708 ],
       [ 1.    ,  5.7737],
       [ 1.    ,  7.8247],
       [ 1.    ,  7.0931],
       [ 1.    ,  5.0702],
       [ 1.    ,  5.8014],
       [ 1.    , 11.7   ],
       [ 1.    ,  5.5416],
       [ 1.    ,  7.5402],
       [ 1.    ,  5.3077],
       [ 1.    ,  7.4239],
       [ 1.    ,  7.6031],
       [ 1.    ,  6.3328],
       [ 1.    ,  6.3589],
       [ 1.    ,  6.2742],
       [ 1.    ,  5.6397],
       [ 1.    ,  9.3102],
       [ 1.    ,  9.4536],
       [ 1.    ,  8.8254],
       [ 1.    ,  5.1793],
       [ 1.    , 21.279 ],
       [ 1.    , 14.908 ],
       [ 1.    , 18.959 ],
       [ 1.    ,  7.2182],
       [ 1.    ,  8.2951],
       [ 1.    , 10.236 ],
       [ 1.    ,  5.4994],
       [ 1.    , 20.341 ],
       [ 1.    , 10.136 ],
       [ 1.    ,  7.3345],
       [ 1.    ,  6.0062],
       [ 1.    ,  7.2259],
       [ 1.    ,  5.0269],
       [ 1.    ,  6.5479],
       [ 1.    ,  7.5386],
       [ 1.    ,  5.0365],
       [ 1.    , 10.274 ],
       [ 1.    ,  5.1077],
       [ 1.    ,  5.7292],
       [ 1.    ,  5.1884],
       [ 1.    ,  6.3557],
       [ 1.    ,  9.7687],
       [ 1.    ,  6.5159],
       [ 1.    ,  8.5172],
       [ 1.    ,  9.1802],
       [ 1.    ,  6.002 ],
       [ 1.    ,  5.5204],
       [ 1.    ,  5.0594],
       [ 1.    ,  5.7077],
       [ 1.    ,  7.6366],
       [ 1.    ,  5.8707],
       [ 1.    ,  5.3054],
       [ 1.    ,  8.2934],
       [ 1.    , 13.394 ],
       [ 1.    ,  5.4369]])

Could you enlighten me on how the code works?

1 answer

5

The np. c_function basically creates two columns with the arrays informed: if you inform:

np.c_[np.array([1,2,3]), np.array([4,5,6])]

he creates:

array([[1, 4],
       [2, 5],
       [3, 6]])

The np.ones() function creates an array of 1’s.

The function array.Shape modifies the shape of the array, as yours is originally horizontal, it is transforming into vertical array.

np.ones((X.shape[0],1))

This is creating an array of 1’s, which is based on its column array, it has become a column because of the function . Shape, the array is a column.

np.c_[np.ones((X.shape[0],1)), X]

Here it is merging the two arrays, the 1’s array you created in the previous step to column, with your X array, creating one.

X = np.c_[np.ones((X.shape[0],1)), X]

Finally it assigns to the same variable, its X array.

info’s: https://docs.scipy.org/doc/numpy/reference/generated/numpy.c_.html https://docs.scipy.org/doc/numpy/reference/generated/numpy.ones.html

Browser other questions tagged

You are not signed in. Login or sign up in order to post.