简体   繁体   English

numpy 如何根据 boolean 条件从另一个数组中删除一个数组中的元素

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

I have an array xi我有一个数组 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然后另一个数组 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?如何根据 yi 中的 0 和 1 将 xi 数组分割成 2? 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)]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Numpy arrays; 如何根据条件用另一个数组替换元素? - Numpy arrays; How to replace elements with another array based on conditions? 如何使用 numpy 方法根据另一个 np 数组的条件对一个 np 数组的某些行执行操作? - How to perform operations on certain rows of one np array based on conditions of another np array using numpy methods? 如何根据另一个 numpy 数组的元素设置 numpy 数组的元素 - How to set elemnts of numpy array based on elements of another numpy array 基于另一个数组的元素从 numpy 数组中删除元素 - removing elements from a numpy array based on elements of another array 如果存在于另一个数组中,则从一个数组中删除元素,保留重复项 - NumPy / Python - Remove elements from one array if present in another array, keep duplicates - NumPy / Python 如何根据多个条件从numpy数组中删除行? - How do I remove rows from a numpy array based on multiple conditions? 基于另一个数组替换 numpy 数组中的元素 - Replacing elements in numpy array based on another array 如何基于另一个数组删除或屏蔽numpy数组中的值 - How to remove or mask values in a numpy array based on another array 如何有效地从另一个数组中删除一个数组的元素 - How to efficiently remove elements of one array from another 如何从 numpy 数组中删除在另一个数组中等于零的所有元素? - How do I remove all elements from a numpy array that are equal to zero in another array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM