test?
Bill Gatespython - vectorized relabeling of numpy 1-d - Stack Overflow
current community
your communities
Sign up or
log in to customize your list.
more stack exchange communities
- Tour Start here for a quick overview of the site
- Help Center Detailed answers to any questions you might have
- Meta Discuss the workings and policies of this site
- About Us Learn more about Stack Overflow the company
- Business Learn more about hiring developers or posting ads with us
x Dismiss
Join the Stack Overflow Community Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
vectorized relabeling of numpy 1-d
up vote 0
down vote
I have a huge training dataset with 4 classes. These classes are labeled non-consecutively. To be able to apply a sequential neural network the classes have to be relabeled so that the unique values in the classes are consecutive. In addition, at the end of the script I have to relabel them back to their old values.
I know how to relabel them with loops:
def relabel(old_classes, new_classes): indexes=[np.where(old_classes ==np.unique(old_classes)[i]) for i in range(len(new_classes))] for i in range(len(new_classes )): old_classes [indexes[i]]=new_classes[i]>>> old_classes = np.array([0,1,2,6,6,2,6,1,1,0])>>> new_classes = np.arange(len(np.unique(old_classes)))>>> relabel(old_classes,new_classes)array([0, 1, 2, 3, 3, 2, 3, 1, 1, 0])
But this isn't nice coding and it takes quite a lot of time.
Any idea how to vectorize this relabeling?
edited 1 min ago asked 12 mins ago
Wilmar van Ommeren 16910
add a comment |Â
1 Answer 1
up vote 0
down vote
Use optional argument return_inverse with np.unique -
np.unique(old_classes,return_inverse=1)[1]
share answered 7 mins ago
Divakar 86k103575    But can i also use this to relabel them back to their old values? âÂ
Wilmar van Ommeren 5 mins ago    @WilmarvanOmmeren Did you try it? This would re-label same as what you are acheveing with your relabel func, unless you are talking about some other kind of relabeling? âÂ
Divakar 4 mins ago
add a comment |Â
Your Answer
 draft saved draft discarded
Sign up or log in
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Name Email
Post as a guest
Name Email
By posting your answer, you agree to the privacy policy and terms of service.
Not the answer you're looking for? Browse other questions tagged python numpy classification vectorization or ask your own question.
asked
today
viewed
9 times
active
today
Blog
Stack Overflow Podcast #98 - Scott Hanselman Is Better Than Us at Everything
Benefits for Developers from San Francisco to Sweden
Related
How to vectorize finding max value in numpy array with if statement?
Vectorizing a Numpy slice operation
Numpy vectorize and atomic vectors
using numpy broadcasting / vectorization to build new array from other arrays
Numpy vectorized summation with variable number of factors
Assign multiple values to multiple slices of a numpy array at once
Fast random to unique relabeling of numpy 2d regions (without loops)
Vectorized numpy 1-d reclassification
Hot Network Questions
- Alignment of terms horizontally
- Could humanity survive in a closed box?
- BGP send community for individual IP
- Why is 7 the most feared number?
- Primes with prime bit-counts
- Old sci-fi short story where protagonist (a doctor) realizes infant is a serial murderer?
- Executing functions within switch dictionary
- An Intersection function on lists that saves intersection positions
- How can I cut off 1/8 inch from ceramic tile?
- Why don't compilers automatically insert deallocations?
- How can you destroy a small device inside another person's head?
- Should seemingly arbitrary things like "play piano for Church" or "intramural badminton" go on M.S. or PhD applications in sciences/engineering?
- Where to get offline maps for Cairo?
- Why have the Venona materials not yet vindicated McCarthyism?
- Piece Identification
- Is it possible to make an alloy that melts at low temperatures and solidifies at high temperatures?
- I have 97 pages in my book draft. Is it too late to swap to third person from first?
- What is "Hammertime" a reference to?
- Would descendants of Earth people stranded on another planet eventually forget about Earth?
- Add Images in Magento Theme template file
- Have orchestras included any modern instrument lately?
- Can Donald Trump reverse Chelsea Manning's commutation?
- Term to describe the relationship between two people when they share an Alma Mater
- It's time for a clock challenge!
lang-py
mobile
feedback Technology Life / Arts Culture / Recreation Science Other
- Stack Overflow
- Server Fault
- Super User
- Web Applications
- Ask Ubuntu
- Webmasters
- Game Development
- TeX - LaTeX
- Software Engineering
- Unix & Linux
- Ask Different (Apple)
- WordPress Development
- Geographic Information Systems
- Electrical Engineering
- Android Enthusiasts
- Information Security
- Database Administrators
- Drupal Answers
- SharePoint
- User Experience
- Mathematica
- Salesforce
- ExpressionEngine® Answers
- Cryptography
- Photography
- Science Fiction & Fantasy
- Graphic Design
- Movies & TV
- Music: Practice & Theory
- Seasoned Advice (cooking)
- Home Improvement
- Personal Finance & Money
- Academia
- more (8)
- English Language & Usage
- Skeptics
- Mi Yodeya (Judaism)
- Travel
- Christianity
- English Language Learners
- Japanese Language
- Arqade (gaming)
- Bicycles
- Role-playing Games
- Anime & Manga
- Motor Vehicle Maintenance & Repair
- more (17)
- MathOverflow
- Mathematics
- Cross Validated (stats)
- Theoretical Computer Science
- Physics
- Chemistry
- Biology
- Computer Science
- Philosophy
- more (3)
site design / logo © 2017 Stack Exchange Inc; user contributions licensed under
cc by-sa 3.0 with
attribution required rev 2017.1.17.24712 Stack Overflow works best with JavaScript enabled