0

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 also need to figure out how to make the lines within the if statement in terms of numpy)

more information about edit-distance function: [![implementing this in terms of numpy functions][2]][2] [2]: https://i.stack.imgur.com/IeJnI.png

    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)```


Moronis2234
  • 57
  • 1
  • 6
  • 1
    What is string1, string2. Also try to explain what you mean with distance. The distance between words? what exactly are you trying to accomplish here? – Luis Alejandro Vargas Ramos Sep 19 '22 at 01:14
  • @LuisAlejandroVargasRamos it's the Levenshtein Edit Distance Algorithm – Moronis2234 Sep 19 '22 at 01:24
  • This link can be useful: https://python-course.eu/applications-python/levenshtein-distance.php. Good luck! – Luis Alejandro Vargas Ramos Sep 19 '22 at 01:32
  • if you're doing this for academic purposes, carry on. If you just want a Levenshtein implementation, this has already been implemented and is probably a lot more performant: https://pypi.org/project/fuzzywuzzy/ – anon01 Sep 19 '22 at 02:17
  • is your question complete? It looks like code may be missing – anon01 Sep 19 '22 at 02:19
  • 1
    Can't help but notice you asked another, almost identical, [question](https://stackoverflow.com/questions/73768077/edit-distance-algorithm-of-two-strings) – Quang Hoang Sep 19 '22 at 04:37
  • Numpy doesn't look like the right tool here. The point of numpy is that it can efficiently perform operations on large arrays of numbers. For string manipulation, it's not particularly useful. – Stef Sep 19 '22 at 08:02

0 Answers0