Import numpy and scipy (maybe through Anaconda) in Matlab
6 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
(1) Introduction.
I am trying to run the following Python function (which is saved as anderson_darling.py) on Matlab:
def ADtest(a,b):
rng = np.random.default_rng()
method = stats.PermutationMethod(n_resamples=9999, random_state=rng)
res = stats.anderson_ksamp([a,b], method=method)
print(a)
print(b)
print(res.statistic)
print(res.critical_values)
print(res.pvalue)
if __name__ == "__main__":
import sys
import numpy as np
from scipy import stats
input_list1 = list(map(int, sys.argv[1].strip('[]').split(',')))
input_list2 = list(map(int, sys.argv[2].strip('[]').split(',')))
a = np.array(input_list1)
b = np.array(input_list2)
ADtest(a,b)
To run this file, I open a Terminal (throgh Visual Studio Code, but this is not relevant), and I give the following command:
(base) xyz@xyz myfolder % python3 anderson_darling.py "[1,3,5,6,4,6,7,1,2,7]" "[1,3,5,6,4,6,7,1,2,7]"
The result, that appears on the Terminal is the following:
[1 3 5 6 4 6 7 1 2 7] % <-- array a
[1 3 5 6 4 6 7 1 2 7] % <-- array b
-1.4363560826434034 % <-- res.statistic
[0.325 1.226 1.961 2.718 3.752 4.592 6.546] % <-- res.critical_values
1.0 % <-- res.pvalue
(2) Problem.
Now, when I go to the editor of Matlab, I try to follow some indications of @Pavan Sahith, from Call a Python function inside a MATLAB loop:
a = [1,3,5,6,4,6,7,1,2,7];
b = [1,3,5,6,4,6,7,1,2,7];
% Convert MATLAB arrays to Python lists
py_a = py.list(a);
py_b = py.list(b);
py.anderson_darling.ADtest(py_a,py_b)
However, I get the following error on the Matlab Command Window:
Error using anderson_darling>ADtest
Python Error: NameError: name 'np' is not defined
(3) Potential source/reason of the problem & Question
I guess the problem is related to the way of importing "numpy" and "scipy" to Matlab. I found the following discussions on the Matlab forum, but it looks like they are related to Windows:
I instead use Mac and I do not know what to do.
Therefore, how can I successfully import and use "numpy" and "scipy" to Matlab?
(4) Additional information on my system.
If it can help, my Python, Anaconda (the "numpy" and "scipy" packages are contained in (Ana)conda) and Matlab versions are the following ones:
From Terminal:
(base) xyz@xyz ~ % python3 --version
Python 3.12.4
(base) xyz@xyz ~ % conda --version
conda 24.7.1
(base) xyz@xyz ~ % conda info --envs
# conda environments:
#
base * /opt/anaconda3
/usr/local/anaconda3
From the Matlab's Command Window:
% Python Version for your system (inside Matlab)
% https://ch.mathworks.com/help/matlab/ref/pyenv.html
>> pyenv
ans =
PythonEnvironment with properties:
Version: "3.9"
Executable: "/Library/Developer/CommandLineTools/usr/bin/python3"
Library: "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/libpython3.9.dylib"
Home: "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9"
Status: Loaded
ExecutionMode: InProcess
ProcessID: "4128"
ProcessName: "MATLAB"
% Matlab version
>> version
ans =
'23.2.0.2515942 (R2023b) Update 7'
0 comentarios
Respuestas (1)
dpb
el 7 de Ag. de 2024
No joy regarding Python, but to get to the desired end result, see the <FEX Submittal> that returned the identical test statistic your example does...much simpler/quicker route it would seem (although the function needs to be edited to return the results in a struct rather than or in lieu of the present long narrative dumped to the command window, that would be a pretty trivial exercise).
Ver también
Categorías
Más información sobre Call Python from MATLAB en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!