test?

test?

Bill Gates

python - 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

company blog

Stack Exchange

Inbox

Reputation and Badges

sign up

log in

tour

help

Stack Overflow

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:

Sign up

vectorized relabeling of numpy 1-d

up vote 0

down vote

favorite

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?

python

numpy

classification

vectorization

share|

improve this question

edited 1 min ago asked 12 mins ago

Wilmar van Ommeren 16910

add a comment | 

1 Answer 1

active

oldest

votes

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

discard

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

0

NumPy vectorized functions

5

Vectorizing loops in NumPy

5

How to vectorize finding max value in numpy array with if statement?

4

Vectorizing a Numpy slice operation

1

Numpy vectorize and atomic vectors

2

using numpy broadcasting / vectorization to build new array from other arrays

2

Numpy vectorized summation with variable number of factors

1

Assign multiple values to multiple slices of a numpy array at once

2

Fast random to unique relabeling of numpy 2d regions (without loops)

1

Vectorized numpy 1-d reclassification

Hot Network Questions

more hot questions

question feed

lang-py

about us

tour

help

blog

chat

data

legal

privacy policy

work here

advertising info

mobile

contact us

feedback Technology Life / Arts Culture / Recreation Science Other

  1. Stack Overflow
  2. Server Fault
  3. Super User
  4. Web Applications
  5. Ask Ubuntu
  6. Webmasters
  7. Game Development
  8. TeX - LaTeX
  9. Software Engineering
  10. Unix & Linux
  11. Ask Different (Apple)
  12. WordPress Development
  1. Geographic Information Systems
  2. Electrical Engineering
  3. Android Enthusiasts
  4. Information Security
  5. Database Administrators
  6. Drupal Answers
  7. SharePoint
  8. User Experience
  9. Mathematica
  10. Salesforce
  11. ExpressionEngine® Answers
  12. Cryptography
  1. Code Review
  2. Magento
  3. Signal Processing
  4. Raspberry Pi
  5. Programming Puzzles & Code Golf
  6. more (7)
  1. Photography
  2. Science Fiction & Fantasy
  3. Graphic Design
  4. Movies & TV
  5. Music: Practice & Theory
  6. Seasoned Advice (cooking)
  7. Home Improvement
  8. Personal Finance & Money
  9. Academia
  10. more (8)
  1. English Language & Usage
  2. Skeptics
  3. Mi Yodeya (Judaism)
  4. Travel
  5. Christianity
  6. English Language Learners
  7. Japanese Language
  8. Arqade (gaming)
  9. Bicycles
  10. Role-playing Games
  11. Anime & Manga
  12. Motor Vehicle Maintenance & Repair
  13. more (17)
  1. MathOverflow
  2. Mathematics
  3. Cross Validated (stats)
  4. Theoretical Computer Science
  5. Physics
  6. Chemistry
  7. Biology
  8. Computer Science
  9. Philosophy
  10. more (3)
  1. Meta Stack Exchange
  2. Stack Apps
  3. Area 51
  4. Stack Overflow Talent

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

Report Page