简体   繁体   English

在 matlab 中直接在 python 中使用 sklearn

[英]Using sklearn directly in python from within matlab

Please do not mark this as a duplicate of how to call python and sklearn from matlab?请不要将此标记为如何从 matlab 调用 python 和 sklearn的重复项 as the question is in my opinion not really answered.因为这个问题在我看来并没有得到真正的回答。
Since Matlab Release R2014b I think, it is possible to directly use python from matlab .我认为从 Matlab Release R2014b 开始,可以直接使用 matlab 中的 python
In short words, you only have to place py in front of the python call.简而言之,您只需要将py放在 python 调用的前面。
I my setup (after giving matlab the python path with the command pyversion('PATH_TO_PYTHON') , that is running fine. I can even use dask multiprocessing. Quite cool. For example, executing py.dask.distributed.Client results in我的设置(在使用命令pyversion('PATH_TO_PYTHON')为 matlab 提供 python 路径后,运行良好。我什dask可以使用dask多处理。非常酷。例如,执行py.dask.distributed.Client结果

  Python Client with properties:

              asynchronous: 0
                   cluster: [1×1 py.distributed.deploy.local.LocalCluster]
         get_futures_error: [1×1 py.method]
                coroutines: [1×1 py.list]
            scheduler_file: [1×1 py.NoneType]
                      loop: [1×1 py.tornado.platform.select.SelectIOLoop]
    recreate_error_locally: [1×1 py.method]
                  refcount: [1×1 py.collections.defaultdict]
                extensions: [1×1 py.dict]
                 scheduler: [1×1 py.distributed.core.rpc]
                       rpc: [1×1 py.distributed.core.ConnectionPool]
                   futures: [1×1 py.dict]
            scheduler_comm: [1×1 py.distributed.batched.BatchedSend]
                    status: [1×7 py.str]
           connection_args: [1×1 py.dict]
                        id: [1×43 py.str]
                generation: [1×1 py.int]
                   io_loop: [1×1 py.tornado.platform.select.SelectIOLoop]
                  security: [1×1 py.distributed.security.Security]

    <Client: scheduler='tcp://127.0.0.1:59795' processes=4 cores=4>

Coming back to the question: I have sklearn installed and can use it from the referenced python Installation.回到问题:我已经安装了 sklearn 并且可以从引用的 python 安装中使用它。 It is working the same way as dask.它的工作方式与 dask 相同。 But MATLAB R2017a is not able to find sklearn.但是 MATLAB R2017a 无法找到 sklearn。
A similiar call to the given above py.sklearn.cluster.dbscan results in对上面给出的py.sklearn.cluster.dbscan类似调用导致

Undefined variable "py" or class "py.sklearn.cluster.dbscan".

Is there any python expert being able to explain?有没有python专家能够解释一下?

I got a solution from the mathworks Support .我从mathworks Support得到了一个解决方案。
It reads the way, that maybe the python environment is not completely setup.它的读取方式可能是python环境没有完全设置好。 I was asked to start matlab from within the Anaconda Prompt which has that complete arranged environment.我被要求从具有完整安排环境的Anaconda Prompt启动 matlab。 Running matlab from there yielded the wanted results thus being able to use for example sklearn.从那里运行 matlab 产生了想要的结果,因此能够使用例如 sklearn。
Further comparing the diffenrences from there showed up, that some more directories from python have to be added to the systems search path.进一步比较那里的差异显示,必须将更多来自 python 的目录添加到系统搜索路径中。

Further I learned, that running py.importlib.import_module(<MODULENAME>) will show details if that python module and its dependencies are available or not.我进一步了解到,运行py.importlib.import_module(<MODULENAME>)将显示该 python 模块及其依赖项是否可用的详细信息。

On a Mac:在 Mac 上:

  • Open a new terminal window;打开一个新的终端窗口;

  • type: which python (to find out where the default version of python is installed);输入: which python (找出which python的默认版本安装在哪里);

  • Restart MATLAB;重启MATLAB;

  • type: pyversion('/anaconda2/bin/python') , in the command line (obviously replace with your path).键入: pyversion('/anaconda2/bin/python') ,在命令行中(显然替换为您的路径)。
  • You can now run all the libraries in your default python installation.您现在可以运行默认 Python 安装中的所有库。

For example:例如:

>>py.sys.version;

>>py.sklearn.cluster.dbscan

You can create a conda environment and use it from MATLAB as follows.您可以创建一个 conda 环境并从 MATLAB 中使用它,如下所示。 Note that it you are debugging Python at the same time, and making changes to some_awesome_python_module , you have to reload it every time (code below starting with clear classes is how to do that):请注意,如果您同时调试 Python,并对some_awesome_python_module进行更改,您必须每次都重新加载它(下面以clear classes开头的代码是如何做到这一点的):

py_root_useFromMATLAB = fileparts(C:\anaconda_3\envs\useFromMATLAB\_conda.exe);
ENV = getenv('PATH');
ENV = strsplit(ENV, ';');
items_to_add_to_path = {
    fullfile(py_root_useFromMATLAB, 'Library', 'mingw-w64', 'bin')
    fullfile(py_root_useFromMATLAB, 'Library', 'usr', 'bin')
    fullfile(py_root_useFromMATLAB, 'Library', 'bin')
    fullfile(py_root_useFromMATLAB, 'Scripts')
    };
ENV = [items_to_add_to_path(:); ENV(:)];
ENV = unique(ENV, 'stable');
ENV = strjoin(ENV, ';');
setenv('PATH', ENV);
clear classes
module_to_load = 'some_awesome_python_module';
python_module_to_use = py.importlib.import_module(module_to_load);
py.importlib.reload(python_module_to_use);

Now you can use it like:现在你可以像这样使用它:

output = py.some_awesome_python_module.some_awesome_python_function(input)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM