numpy select fixed amount of values among duplicate values in array

Fabio Lamanna

Starting from a simple array with duplicate values:

a = np.array([2,3,2,2,3,3,2,1])

I'm trying to select a maximum of 2 unique values from this. The resulting array would appear as:

b = np.array([2,3,2,3,1])

no matter the order of the items. So far I tried to find unique values with:

In [20]: c = np.unique(a,return_counts=True)

In [21]: c
Out[21]: (array([1, 2, 3]), array([1, 4, 3]))

which is useful because it returns the frequency of values as well, but I'm stucked in filtering by frequency.

Kasravnd

You can use a list comprehension within np.concatenate() and limit the number of items by slicing:

>>> np.concatenate([a[a==i][:2] for i in np.unique(a)])
array([1, 2, 2, 3, 3])

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to select values from Numpy array based on fuzzy conditions?

From Dev

perform operation on select indices of a numpy array based on values in another array

From Dev

Merge values of array by duplicate keys

From Dev

Check if an array contains duplicate values

From Dev

How do I duplicate the values for the last dimension in a numpy array?

From Dev

array with only non duplicate values

From Dev

Convert numpy.array to order of elements when duplicate values are present

From Dev

Array of hermite values in numpy

From Dev

Checking for and indexing non-unique/duplicate values in a numpy array

From Dev

Removes duplicate values from an array

From Dev

Rails Select Box for fixed values

From Dev

Assigning values to a NumPy array

From Dev

In PHP Add the duplicate values in array

From Dev

Processing a select range of values in a numpy array

From Dev

Finding duplicate values in a MongoDB array

From Dev

Calculating the Duplicate Values in an Array

From Dev

Count of duplicate values in array php

From Dev

Array merge and removal of duplicate values

From Dev

Merge duplicate values in array

From Dev

Rendering array with duplicate values in vuejs

From Dev

Find duplicate values among multiple columns across different rows

From Dev

Checking for and indexing non-unique/duplicate values in a numpy array

From Dev

modifying values in numpy array

From Dev

How to check divisibility among integer values in a array

From Dev

Masking values in numpy array

From Dev

Count the occurrences of duplicate values among columns of a table

From Dev

Change select values with array values

From Dev

Select subset of numpy.ndarray based on other array's values

From Dev

PHP Array Unknown Amount of Values

Related Related

  1. 1

    How to select values from Numpy array based on fuzzy conditions?

  2. 2

    perform operation on select indices of a numpy array based on values in another array

  3. 3

    Merge values of array by duplicate keys

  4. 4

    Check if an array contains duplicate values

  5. 5

    How do I duplicate the values for the last dimension in a numpy array?

  6. 6

    array with only non duplicate values

  7. 7

    Convert numpy.array to order of elements when duplicate values are present

  8. 8

    Array of hermite values in numpy

  9. 9

    Checking for and indexing non-unique/duplicate values in a numpy array

  10. 10

    Removes duplicate values from an array

  11. 11

    Rails Select Box for fixed values

  12. 12

    Assigning values to a NumPy array

  13. 13

    In PHP Add the duplicate values in array

  14. 14

    Processing a select range of values in a numpy array

  15. 15

    Finding duplicate values in a MongoDB array

  16. 16

    Calculating the Duplicate Values in an Array

  17. 17

    Count of duplicate values in array php

  18. 18

    Array merge and removal of duplicate values

  19. 19

    Merge duplicate values in array

  20. 20

    Rendering array with duplicate values in vuejs

  21. 21

    Find duplicate values among multiple columns across different rows

  22. 22

    Checking for and indexing non-unique/duplicate values in a numpy array

  23. 23

    modifying values in numpy array

  24. 24

    How to check divisibility among integer values in a array

  25. 25

    Masking values in numpy array

  26. 26

    Count the occurrences of duplicate values among columns of a table

  27. 27

    Change select values with array values

  28. 28

    Select subset of numpy.ndarray based on other array's values

  29. 29

    PHP Array Unknown Amount of Values

HotTag

Archive