简体   繁体   中英

numpy how to remove elements in one array based on boolean conditions from another array

I have an array xi

xi=
[[ 0.49671415 -0.46341769]
[-0.1382643  -0.46572975]
[ 0.64768854  0.24196227]
[ 1.52302986 -1.91328024]
[-0.23415337 -1.72491783]
[-0.23413696 -0.56228753]
[ 1.57921282 -1.01283112]
[ 0.76743473  0.31424733]
[-0.46947439 -0.90802408]
[ 0.54256004 -1.4123037 ]]

then another array yi

[0 0 0 0 0 0 0 0 1 1 1]

how do I slice the xi array into 2 based on 0 and 1 in yi? how can i have

x1=
    [[ 0.49671415 -0.46341769]
    [-0.1382643  -0.46572975]
    [ 0.64768854  0.24196227]
    [ 1.52302986 -1.91328024]
    [-0.23415337 -1.72491783]
    [-0.23413696 -0.56228753]
    [ 1.57921282 -1.01283112]

and

x2=
    [ 0.76743473  0.31424733]
    [-0.46947439 -0.90802408]
    [ 0.54256004 -1.4123037 ]]

Thank you for your help!

You can use:

x1 = xi[~yi.astype(bool)]
x2 = xi[yi.astype(bool)]

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