-2

i'm new to python and am looking for a way to compare the above and below arrays so I can have a computer make a decision based upon them, and then update the array. I am open to different methods if this is a bad way to go about this.

import numpy as np
import matplotlib.pylab as plt

    return 1/(1+np.exp(-x))
    #the array
X = np.array([[5,5,5,5,5,5,5,5,5,5,5,5],
            [5,4,1,0,1,1,1,0,1,1,1,5],
            [5,0,1,0,1,0,1,0,1,0,1,5],
            [5,0,1,0,1,0,1,0,1,0,1,5],
            [5,0,1,0,1,0,1,0,1,0,1,5],
            [5,0,1,0,1,0,1,0,1,0,1,5],
            [5,0,1,2,1,2,1,2,1,2,1,5],
            [5,0,1,0,1,0,1,0,1,0,1,5],
            [5,0,1,0,1,0,1,0,1,0,1,5],
            [5,0,1,0,1,0,1,0,1,0,1,5],
            [5,0,1,0,1,0,1,0,1,0,3,5],
            [5,5,5,5,5,5,5,5,5,5,5,5]])

print(X)

This outputs

[[5 5 5 5 5 5 5 5 5 5 5 5]
 [5 4 1 0 1 1 1 0 1 1 1 5]
 [5 0 1 0 1 0 1 0 1 0 1 5]
 [5 0 1 0 1 0 1 0 1 0 1 5]
 [5 0 1 0 1 0 1 0 1 0 1 5]
 [5 0 1 0 1 0 1 0 1 0 1 5]
 [5 0 1 2 1 2 1 2 1 2 1 5]
 [5 0 1 0 1 0 1 0 1 0 1 5]
 [5 0 1 0 1 0 1 0 1 0 1 5]
 [5 0 1 0 1 0 1 0 1 0 1 5]
 [5 0 1 0 1 0 1 0 1 0 3 5]
 [5 5 5 5 5 5 5 5 5 5 5 5]]

As an example, comparing the 4 to the above 5 and below 0 so it can make a decision based upon the neighboring numbers. I also need to compare the ones to the left and right, so re formatting the array 90 degrees wouldn't work.

martineau
  • 119,623
  • 25
  • 170
  • 301
  • 1
    Your question is missing an explanation _how_ do you want to compare the arrays. What kind of neighbor-dependent computation do you plan, and why? Your code sample is clearly missing some code, too. Even better, explain the high-level problem you're trying to solve. Please edit the question to make it answerable. – 9000 Jun 07 '18 at 16:59
  • This seems duplicate.. please check this question https://stackoverflow.com/questions/10580676/comparing-two-numpy-arrays-for-equality-element-wise – Eddwin Paz Jun 07 '18 at 17:20

2 Answers2

0

The best way to do such thing is using scipy.signal.convolve2d:

You can make a second matrix with desired numerical pattern such as

[[0,-1,0],[-1,1,-1],[0,-1,0]]

This will return a new matrix with the center minus the sum of the surroundings.

Ernie Yang
  • 114
  • 5
0

Source: https://docs.scipy.org/doc/numpy/reference/generated/numpy.array_equal.html

import numpy as np
p = np.array_equal([1, 2], [1, 2])
print(p) # True 
c = np.array_equal([1, 2], [1, 2, 3])
print(c) # False
Eddwin Paz
  • 2,842
  • 4
  • 28
  • 48