Numpy implementation of Edit Distance Algorithm

Issue

This Content is from Stack Overflow. Question asked by Zoxze

I am new to numpy(and python) and working on making the edit distance Algorithm with numpy. This is my code so far. I have an error for the first line after the else: . The error says: “index 3 is out of bounds for axis 0 with size 2”. I’m very confused as to what this means. I also have a question about how I’m initializing my T array – am I doing it in the best way? It took me a while to figure out how to make an empty 2d array that are 1 column and 1 row wide. Any guidance would be super helpful!

    i = len(string1) - 1
    j = len(string2) - 1
    T = np.array([[1,1],[1,1]])

    if string1[i] == string2[j]:
      T[i][j] = T[i-1][j-1]

    else:
      T[i][j] = 1 + min(T[i-1][j], T[i-1][j-1], T[i][j-1])
      

assert(edit_distance('hello', 'relevant') == 6)
assert(edit_distance('elephant', 'relevant') == 3)
assert(edit_distance('statistics', 'mathematics') == 6)
assert(edit_distance('numpy', 'alexa') == 5)```



Solution

This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?