【问题标题】:Using sklearn directly in python from within matlab在 matlab 中直接在 python 中使用 sklearn
【发布时间】:2020-08-31 19:57:50
【问题描述】:

请不要将此标记为 how to call python and sklearn from matlab? 的重复项,因为我认为这个问题没有得到真正的回答。
我认为自从 Matlab 发布 R2014b 以来,directly use python from matlab 是可能的。
简而言之,您只需将py 放在python 调用前面。
我的设置(在使用命令pyversion('PATH_TO_PYTHON') 为matlab 提供python 路径后,运行良好。我什至可以使用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>

回到问题:我已经安装了 sklearn,可以从引用的 python 安装中使用它。它的工作方式与 dask 相同。但是 MATLAB R2017a 找不到 sklearn。
对上述py.sklearn.cluster.dbscan 的类似调用会导致

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

有没有python高手可以解释一下?

【问题讨论】:

  • 无法复制。 MATLAB R2017a 在这里与 sklearn 完美配合。无需额外设置,只需正确设置pyversion。您是否 100% 确定您在控制台和 MATLAB 中使用相同的 python 版本?您可以尝试使用 sklearn 创建一个新的虚拟环境并在 MATLAB 中使用它吗?
  • @hbaderts:感谢您的测试。我运行 Windows 并且在计算机上只有一个 python。我完全确定运行/测试相同的 pyhton 版本(Anaconda 3 64 位)。您正在运行哪个安装程序?

标签: python matlab scikit-learn


【解决方案1】:

我从the mathworks Support得到了解决方案。
它读取方式,可能python环境没有完全设置。我被要求从具有完整安排环境的Anaconda Prompt 中启动 matlab。从那里运行 matlab 产生了想要的结果,因此可以使用例如 sklearn。
进一步比较从那里出现的差异,必须将更多来自 python 的目录添加到系统搜索路径中。

我进一步了解到,运行 py.importlib.import_module(&lt;MODULENAME&gt;) 将显示该 python 模块及其依赖项是否可用的详细信息。

【讨论】:

    【解决方案2】:

    在 Mac 上:

    • 打开一个新的终端窗口;

    • 输入:which python(查找默认python版本的安装位置);

    • 重启 MATLAB;

    • 键入:pyversion('/anaconda2/bin/python'),在命令行中(显然替换为您的路径)。
    • 您现在可以运行默认 python 安装中的所有库。

    例如:

    >>py.sys.version;
    
    >>py.sklearn.cluster.dbscan
    

    【讨论】:

      【解决方案3】:

      您可以创建一个 conda 环境并从 MATLAB 中使用它,如下所示。请注意,如果您同时调试 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);
      

      现在你可以像这样使用它:

      output = py.some_awesome_python_module.some_awesome_python_function(input)
      

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-27
      • 2022-10-31
      • 2011-10-14
      • 2022-06-11
      • 1970-01-01
      • 2018-09-15
      相关资源
      最近更新 更多