0

I've written the following code in Python. I want to write this code in Mathematica but I don't know Mathematica. How can I convert this code to a code in Mathematica?

Thanks in advance.

    # Python program to read an excel file
# import openpyxl module
import openpyxl
import easygui
import pandas as pd
import math
path=easygui.fileopenbox('Choose your excel file')
n=int(input('Enter the number of points: '))
m=int(input('Enter the number of dimensions: '))
k=input('Enter new point: ')
e=float(input('Enter Eta: '))
k=k.split()
k=[int(i) for i in k]
df = pd.read_excel(path, sheet_name='Sheet1')
t=0
for j in range(1,m+1) :
    t=t+pow(k[j-1]-df.loc[0,j],2)
min=round(math.sqrt(t),3)
for i in range(1,n):
    t=0
    for j in range(1,m+1) :
        t=t+pow(k[j-1]-df.loc[i,j],2)
    t=round(math.sqrt(t),3)
    if t < min :
        min=t
T=e*min
counter=0
Y=0
for i in range(n):
    t = 0
    for j in range(1, m + 1):
        t = t + pow(k[j-1] - df.loc[i, j], 2)
    t = round(math.sqrt(t), 3)
    if t <= T :
        counter +=1
        Y=Y+df.loc[i,'y']
print(Y/counter)
M.Ramana
  • 101
  • 3
    I don't know of a way to automatically convert an entire program from Python to Mathematica. But you might want to take a look at this, which discusses options for converting individual lines of Python code: https://community.wolfram.com/groups/-/m/t/1863194. Also see: https://mathematica.stackexchange.com/questions/214421/convert-python-code-to-mathematica Regarding the "for" loops in your code: you'll want to learn to convert them to a functional coding structure, using commands like Do, While, Table, Fold, etc.—Mathematica is not designed to work well with a procedural approach. – theorist May 12 '21 at 06:39
  • 1
  • @theorist Thank you so much for the links and your help. It was helpful for me. – M.Ramana May 12 '21 at 16:02
  • @Alan What a good link! Thank you very much. – M.Ramana May 12 '21 at 16:03

0 Answers0