Python Forum
Replace elements of array with elements from another array based on a third array
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Replace elements of array with elements from another array based on a third array
#1
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!
Reply
#2
Show what you have tried.
Reply
#3
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.
Reply
#4
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?
Reply
#5
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]
Reply
#6
(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]
Reply
#7
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Elegant way to apply each element of an array to a dataframe? sawtooth500 3 115 6 hours ago
Last Post: deanhystad
  Concatenate array for 3D plotting armanditod 1 189 Mar-21-2024, 08:08 PM
Last Post: deanhystad
  Convert numpy array to image without loading it into RAM. DreamingInsanity 7 5,722 Feb-08-2024, 09:38 AM
Last Post: paul18fr
  unable to remove all elements from list based on a condition sg_python 3 371 Jan-27-2024, 04:03 PM
Last Post: deanhystad
  How Write Part of a Binary Array? Assembler 1 305 Jan-14-2024, 11:35 PM
Last Post: Gribouillis
  Loop over an an array of array Chendipeter 1 524 Nov-28-2023, 06:37 PM
Last Post: deanhystad
  How to remove some elements from an array in python? gohanhango 9 980 Nov-28-2023, 08:35 AM
Last Post: Gribouillis
  IPython errors for numpy array min/max methods muelaner 1 506 Nov-04-2023, 09:22 PM
Last Post: snippsat
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 426 Oct-17-2023, 09:46 AM
Last Post: tomciodev
  Convert np Array A to networkx G IanAnderson 2 625 Jul-05-2023, 11:42 AM
Last Post: IanAnderson

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020