0

Hello,

I have an one dimension numpy array with 1 to 5 numbers. I need to map these values to corresponding numbers between 0.76 to 1.24 with 0.12 interval.
Then the problem is the find exact values in one array and fill the exact index in the second array.
for example:
array([1,2,1,3,4,5])
Should be:
array([0.76,0.88,0.76,1,1.12,1.24])

I did this using if,elif but I though there has to be a elegent way to do this.

def ClassConv(classNumVec):
    relativeMilk = np.empty([size])

    for i in range(size):
        if classNumVec[i] == 1:
            relative[i] = 0.76
        elif classNumVec[i] == 2:
            relative[i] = 0.88
        elif classNumVec[i] == 3:
            relative[i] = 1
        elif classNumVec[i] == 4:
            relative[i] = 1.12
        else:
            relative[i] = 1.24 

    return relative

My searches resulted to numpy.where and biesct functions but could not do this.
Thanks in advance!

2
Contributors
1
Reply
28
Views
2 Years
Discussion Span
Last Post by Gribouillis
2

Why not this ?

>>> import numpy as np
>>> a = np.array([1,2,1,3,4,5])
>>> 0.64 + 0.12 * a
array([ 0.76,  0.88,  0.76,  1.  ,  1.12,  1.24])
Votes + Comments
elegant :D
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.