Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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? 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.
In short words, you only have to place py in front of the python call.
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

  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. It is working the same way as dask. But MATLAB R2017a is not able to find sklearn.
A similiar call to the given above py.sklearn.cluster.dbscan results in

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

Is there any python expert being able to explain?

like image 770
Bastian Ebeling Avatar asked Aug 30 '17 05:08

Bastian Ebeling


1 Answers

I got a solution from the mathworks Support.
It reads the way, that maybe the python environment is not completely setup. I was asked to start matlab from within the Anaconda Prompt which has that complete arranged environment. Running matlab from there yielded the wanted results thus being able to use for example sklearn.
Further comparing the diffenrences from there showed up, that some more directories from python have to be added to the systems search path.

Further I learned, that running py.importlib.import_module(<MODULENAME>) will show details if that python module and its dependencies are available or not.

like image 152
Bastian Ebeling Avatar answered Nov 08 '22 21:11

Bastian Ebeling