I got the following two part coding challenge, where I needed to write a function and a theory description for an alternative solution using a neural network:

You are given a data set where each point has 3 values - X, Y, C, where X is the x coordinate, Y is the y coordinate and C is a color (coded either as 0 or 1).

Function

You are asked to write a function that will take three values (xi,yi,r), where xi, yi are some influence coordinates and r is the influence radius. The function should return a float value between 0 and 1, which tells you the probability that the given input coordinate xi, yi has a color 0 based on the various samples radius r around xiyi.

For example given the following dataset:

2.00.001.03.003.01.002.01.000.02.000.02.007.07.016.06.018.08.017.07.017.06.016.08.01

Examples:

  • If we use xi=2.0, yi=2.0 and r=2.0, the probability should be 1.0 since there are 3 nodes of color 0 and 0 nodes of color 1 in the defined radius.
  • If we use xi=7.0, yi=7.0 and r=2.0, the probability should be 0.0 since there are 0 nodes of color 0 and 6 nodes of color 1 in the defined radius.
  • If we use xi=4.0, yi=4.0 and r=4.0, the probability should be 0.6 since there are 3 nodes of color 0 and 2 nodes of color 1 in the defined radius.
import math

def get_dist(x1, y1, x2, y2):
    return math.hypot(x2 - x1, y2 - y1)


def findProbability(X, Y, C, xpos, ypos, radius):
    color = []
    for k, v in enumerate(X):
        dist = get_dist(X[k], Y[k], xpos, ypos)
        if dist <= radius:
            color.append(C[k])

    result = sum(color)/float(len(color))
    print result



X = [2.0, 1.0, 3.0, 2.0, 0.0, 0.0, 7.0, 6.0, 8.0, 7.0, 7.0, 6.0]
Y = [0.0, 3.0, 1.0, 1.0, 2.0, 2.0, 7.0, 6.0, 8.0, 7.0, 6.0, 8.0]
C = [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1]
xpos = 2.0
ypos = 2.0
radius = 2.0
findProbability(X, Y, C, xpos, ypos, radius)

Neural Network

The given data set here is small but you could imagine an extremely large data set such as locations on a map, or vertices in a 3D scene. Using a brute force approach still might prove expensive. Neural Networks on the other hand give a quick (?) feed forward look up or answer. How you would design a neural network to solve this problem? For a specific data set with points having 3 values (x, y, c) you want to design a network that takes three inputs xi, yi, r and output a probability of the color being 0.

Explain what training data you would use, what kind of a network and layers you would build.

…to be continued here