简体   繁体   中英

Horizontal Stack 2d Numpy Array with 3d Numpy Array

I'm generating my feature dataset for machine learning, and I have a 2d numpy array X where X.shape = (n, d) - n samples, d features.

Now I generate a new feature with one-hot-encoding - f where f.shape = (n, 1, k) - n samples, k labels.

What would be the best way for me to add this new feature to my existing feature dataset?

The second dimension of the one-hot vector is redundant, so you can drop it and use f as a 2D array of shape (n, k) .
You would do something like:

new_data = np.concatenate((X, f.squeeze()), axis=1)

where the squeeze() function removes all 1-dimensions from you array (ie f.squeeze().shape == (n, k) .

Cheers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM