Issue
I have numpy
array, and I do want to map the ranges of the array values into specific values:
import numpy as np
array = [
[328, 124, 146, 117, 147, 109, 139, 100, 140, 98, 128, 101, 121, 95, 111, 89, 114, 82, 106, 77, 97, 71, 273],
[132, 121, 121, 117, 114, 110, 99, 163, 89, 81, 83, 81, 91, 91, 89, 144, 89, 81, 81, 75, 69, 68, 68],
[164, 114, 117, 110, 105, 105, 97, 91, 142, 80, 131, 77, 122, 81, 108, 74, 77, 75, 69, 71, 68, 63, 87],
[121, 116, 110, 103, 104, 99, 90, 84, 77, 137, 77, 125, 99, 128, 79, 73, 69, 71, 68, 70, 60, 53, 64],
[151, 100, 103, 99, 379, 88, 264, 78, 135, 77, 121, 97, 113, 75, 100, 62, 232, 65, 350, 54, 56, 50, 75],
[101, 93, 121, 90, 84, 92, 86, 78, 74, 113, 66, 115, 72, 100, 63, 67, 60, 57, 57, 47, 47, 44, 43],
[146, 89, 116, 86, 87, 85, 79, 73, 101, 59, 98, 63, 107, 64, 62, 56, 56, 52, 48, 50, 46, 38, 68],
[89, 113, 108, 83, 82, 78, 74, 125, 69, 67, 57, 65, 67, 56, 58, 105, 49, 52, 69, 59, 63, 34, 43],
[127, 87, 78, 79, 105, 74, 163, 64, 61, 61, 56, 235, 61, 48, 51, 43, 134, 40, 63, 28, 31, 26, 55],
[76, 79, 75, 70, 72, 69, 62, 58, 53, 52, 49, 50, 53, 43, 39, 37, 41, 36, 31, 29, 26, 18, 17],
[274, 73, 96, 71, 92, 64, 87, 57, 82, 53, 81, 30, 73, 41, 66, 37, 52, 31, 49, 22, 50, 15, 211]]
array = np.array(array)
def mapVal(val, table = [140, 195, 250, 350, 450]):
"""
Function: mapVal, to quantify depth values manually.
---
Parameters:
@param: val, nd-array.
@param: table, list, mapping table.
---
@return: None.
"""
val[val <= table[0]] = 0
val[val > table[0] & val <= table[1]] = 1
val[val > table[1] & val <= table[2]] = 2
val[val > table[2] & val <= table[3]] = 3
val[val > table[3] & val <= table[4]] = 4
val[val > table[4]] = 5
But I'm getting this Error:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
I'm awar that I can use inefficient loops, but I do like to ask if there is a better solution, thanks in advance.
Solution
As mentioned in the comments, first solve your ValueError
by using parenthesis around your conditional checks to avoid having the bit-wise and operator &
be evaluated first, which is the default behaviour.
For example:
val[(val > table[0]) & (val <= table[1])]
Next, for your search for a more efficient method, you can use the built-in np.digitize
function to help you do what you're doing:
np.digitize(array, table)
Answered By - rcshon
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.