How do I do conditional array arithmetic on a numpy array?

前端 未结 3 1228
一向
一向 2020-12-06 10:35

I\'m trying to get a better grip on numpy arrays, so I have a sample question to ask about them:

Say I have a numpy array called a. I want to perform an operation on

3条回答
  •  不思量自难忘°
    2020-12-06 11:01

    Just to add to above,In numpy array I wanted to subtract a value based on the ascii value to get a value between 0 to 35 for ascii 0-9 and A-Z and had to write the for loops but the post above showed me how to make it short. So I thought of posting it here as thanks to the post above.

    The code below can be made short

    i = 0
    for y in y_train:
        if y < 58:
            y_train[i] = y_train[i]-48
        else :
            y_train[i] = y_train[i] - 55
        i += 1
    i = 0
    for y in y_test:
        if y < 58:
            y_test[i] = y_test[i]-48
        else :
            y_test[i] = y_test[i] - 55
        i += 1
    # The shortened code is below
    y_train[y_train < 58] -= 48
    y_train[y_train > 64] -= 55
    
    y_test[y_test < 58] -= 48
    y_test[y_test > 64] -= 55
    

提交回复
热议问题