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!

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])
commented: elegant :D +6
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.