Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Constructing array from elements of another array neatly

I am wondering how to do the following more cleanly and in the general case. I’m constructing the array final_array_transpose with elements from another array, imb_data (currently has length 5 but can have any length). Here’s what I have for the len=5 case.

final_array_transpose = np.array([ 
        imb_data[0].copy(), 
        imb_data[1].copy(), 
        imb_data[2].copy(), 
        imb_data[3].copy(), 
        imb_data[4].copy(), 
        bin_label_str, 
        range(len(imb_data[0])), 
        bin_label_str,
        bin_label,
        bin_label],       
    dtype = object)

(Ignore everything past imb_data[4].copy())

How do I generalize this for any length of imb_data (imb_data is a 2D array)?
Thanks!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Copy the whole array and use unpacking:

final_array_transpose = np.array([ 
        *imb_data.copy(), 
        bin_label_str, 
        range(len(imb_data[0])), 
        bin_label_str,
        bin_label,
        bin_label],       
    dtype = object)

If you want the entire array in imb_data.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading