1
$\begingroup$

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)
$\endgroup$
4
  • 3
    $\begingroup$ 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: community.wolfram.com/groups/-/m/t/1863194. Also see: mathematica.stackexchange.com/questions/214421/… 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. $\endgroup$ Commented May 12, 2021 at 6:39
  • 1
    $\begingroup$ You can run Python in Mathematica. $\endgroup$ Commented May 12, 2021 at 11:24
  • $\begingroup$ @theorist Thank you so much for the links and your help. It was helpful for me. $\endgroup$ Commented May 12, 2021 at 16:02
  • $\begingroup$ @Alan What a good link! Thank you very much. $\endgroup$ Commented May 12, 2021 at 16:03

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.