question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Np.Select add additional input type such as dict

See original GitHub issue

Hi, I’m a huge fan of np.select() - it makes it very easy to categorize records in a table when there are a lot of potential categories. The only issue I have with it is that you have two separate lists to track, conditions and choices. This isnt so bad when dealing with only a handful of conditions but as the number grows it becomes difficult to track which list item in conditions corresponds to which list item in choices. I end up having to add a comment for each line of my conditions to say which choice it generates. I was wondering if we could alternately supply a dictionary/tuple (or any type that allows you to see the condition and choice in proximity to each other) where the keys are the choice and the values are the conditions.

Reproducing code example:

import numpy as np

#Current
conditions = [x == 1, x == 2, x == 3]
choices = ["One", "Two", "Three"] 
np.select(conditions, choices)

#Proposed
logic = {
    "One": x==1,
    "Two": x==2,
    "Three": x==3
}

np.select(logic)

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
oliver-polacommented, Jul 16, 2020

@matthewmturner If your problem is “end up having to add a comment for each line of my conditions to say which choice it generates”, you could use that dictionary for nice code in that part, and then call select like this?

np.select(logic.values(), logic.keys())
0reactions
matthewmturnercommented, Jul 17, 2020

@oliver-pola you’re totally right, thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Translate every element in numpy array according to key
addendum - i found this one to be the fastest (compared to vectoring dictionary.get and iterating through keys)! ymmv... – william_grisaitis.
Read more >
Python | Add new keys to a dictionary - GeeksforGeeks
In this article, we will cover how to add a new Key to a Dictionary in Python. We will use 7 different methods...
Read more >
Structured arrays — NumPy v1.23 Manual
Accessing Multiple Fields. One can index and assign to a structured array with a multi-field index, where the index is a list of...
Read more >
pandas.Series.replace — pandas 1.5.2 documentation
Dicts can be used to specify different replacement values for different existing values. · For a DataFrame a dict can specify that different...
Read more >
Chapter 4. NumPy Basics: Arrays and Vectorized Computation
Selection from Python for Data Analysis [Book] ... array, Convert input data (list, tuple, array, or other sequence type) to an ndarray either...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found