Python Forum
Replace elements of array with elements from another array based on a third array - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Replace elements of array with elements from another array based on a third array (/thread-37214.html)



Replace elements of array with elements from another array based on a third array - Cola_Reb - May-13-2022

I hope I can explain my current problem in a understandable way (besides from the possible confusing title Rolleyes):

I need to replace some elements of Array 1 with corresponding elements from Array 2, depending on what Array 3 indicates by its input (because it is based on a dropdown selection in excel) but I fail to find a proper formulation of the function that would do this.

Example:

Target Array / Array 1:

array([[0.05, 0.07 , 0.04, 0.03, 0.05,
        0.72],
       [0.08, 0.083, 0.40 , 0.038, 0.05,
        0.70],
       [0.40, 0.17, 0.07, 0.04, 0.03,
        0.31],
       [0.16, 0.14, 0.08, 0.40, 0.08,
        0.40]])
Array for replacements / Array 2:

array([[1, 0, 0, 0],
       [0, 1, 0, 0],
       [0, 0, 1, 0],
       [0, 0, 0, 1]])
Input Array / Array 3:

array(['No change', 'Change second element to 1',
       'Change first element to 1', 'Change fourth element to 1'],
      dtype='<U27')
Desired Output:

array([[0.05, 0.07 , 0.04, 0.03, 0.05,
        0.72],
       [0, 1, 0, 0],
       [1, 0, 0, 0],
      [0, 0, 0, 1]])
So basically everytime the input Array 3 says "Change x element", the corresponding element of Array 1 should be replaced with the row of Array 2 so that one value becomes 1 an all others 0.

I already tried several ways to interpret the Input Array and then iterate over the target array, but there was no succesful attempts so far. Maybe you got an idea. Huh

Thanks in advance!


RE: Replace elements of array with elements from another array based on a third array - deanhystad - May-13-2022

Show what you have tried.


RE: Replace elements of array with elements from another array based on a third array - Cola_Reb - May-13-2022

I tried something similiar to this:

for input_value in np.nditer(inputarray):
        if input_value == 'Change second element to 1':
                targetarray = replacement_array[1]
....
But this approach does not take into account that I would check the conditions line by line from the input and then replace the right line in the target array.


RE: Replace elements of array with elements from another array based on a third array - deanhystad - May-13-2022

Is there a pattern to the statements? It looks like they will either be "No change" or "Change x element to y". How can you convert "x" and "y" to indices?


RE: Replace elements of array with elements from another array based on a third array - ibreeden - May-13-2022

It seems you are presenting a part of a mysterious algorithm. Smile
You can do it the way you showed, but of course you must extend the "if" with a number of "elifs".

If I understand it correctly, the first array (input) and the third (control) must have the same number of items. Am I right?

The following construction is not easy to digest for a computer:
array(['No change', 'Change second element to 1',
       'Change first element to 1', 'Change fourth element to 1'],
      dtype='<U27')
I hope you can change this to something more suitable like I did in the following example.
input_list = [[0.05, 0.07 , 0.04, 0.03, 0.05, 0.72],
       [0.08, 0.083, 0.40 , 0.038, 0.05, 0.70],
       [0.40, 0.17, 0.07, 0.04, 0.03, 0.31],
       [0.16, 0.14, 0.08, 0.40, 0.08, 0.40]]

replacement = [[1, 0, 0, 0],
       [0, 1, 0, 0],
       [0, 0, 1, 0],
       [0, 0, 0, 1]]

# array(['No change', 'Change second element to 1',
#       'Change first element to 1', 'Change fourth element to 1'],
#       dtype='<U27')
# This array has to be coded in a useful manner. (What is dtype='<U27'? I ignore it.)
# I call it "control" because it controls the way "input_list" is
#     converted to "target".
# A value of "-1" means: 'No change'.
# Elements count from 0. So first element is 0, second element is 1 etc.
# Then the previous array looks like this:
control = [-1, 1, 0, 3]

target = []

if len(input_list) != len(control):
    raise Exception("ERROR: input_list does not have the same number of elements as control list.")

for i in range(len(input_list)):
    if control[i] == -1 :
        target.append(input_list[i])
    else:
        target.append(replacement[control[i]])

# Show result.
for item in target:
    print(item)
Output:
[0.05, 0.07, 0.04, 0.03, 0.05, 0.72] [0, 1, 0, 0] [1, 0, 0, 0] [0, 0, 0, 1]



RE: Replace elements of array with elements from another array based on a third array - Cola_Reb - May-13-2022

(May-13-2022, 04:55 PM)deanhystad Wrote: Is there a pattern to the statements? It looks like they will either be "No change" or "Change x element to y". How can you convert "x" and "y" to indices?

Yes, the pattern could only consist of 'no change' or the choice of changing a specific element to 1 and all others to 0:

Input should be rplaced by :
'No change' Arrow no change
'Change first element to 1', Arrow [1, 0, 0, 0]
'Change second element to 1', Arrow [0, 1, 0, 0]
'Change third element to 1', Arrow [0, 0, 1, 0]
'Change fourth element to 1' Arrow [0, 0, 0, 1]


RE: Replace elements of array with elements from another array based on a third array - deanhystad - May-13-2022

That is a brute force ugly way to do it. Look at my post again and think about this problem in terms of x and y. x will be things like "first", "second", "third" and y will be things like "0" or "1". How do you change "first" to an array index? How do you change "0" to a number.

This assumes you have to use the clubby "No change" or "Change third element to 1" strings to enter changes. This isn't too difficult to parse, but it would be better if no parsing was required.