using matlab with python - mathworks · python note the syntax differences when calling matlab from...

19
© 2020 The MathWorks, Inc. Using MATLAB with Python Twitter: @HeatherGorr Instagram: @heather.codes Heather Gorr, PhD Senior Product Manager, MATLAB MathWorks

Upload: others

Post on 30-Mar-2021

38 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Using MATLAB with Python - MathWorks · Python Note the syntax differences when calling MATLAB from Python. >> C = A + B >>> C = eng.plus(A,B) >> foo(x) >>> eng.foo(x,nargout=0) MATLAB

© 2020 The MathWorks, Inc.

Using MATLAB with Python

Twitter: @HeatherGorr

Instagram: @heather.codes

Heather Gorr, PhD

Senior Product Manager, MATLAB

MathWorks

Page 2: Using MATLAB with Python - MathWorks · Python Note the syntax differences when calling MATLAB from Python. >> C = A + B >>> C = eng.plus(A,B) >> foo(x) >>> eng.foo(x,nargout=0) MATLAB

Amazing applications are built using MATLAB!

https://www.mathworks.com/company/user_stories.html

Page 3: Using MATLAB with Python - MathWorks · Python Note the syntax differences when calling MATLAB from Python. >> C = A + B >>> C = eng.plus(A,B) >> foo(x) >>> eng.foo(x,nargout=0) MATLAB

.NET

Java

C/ C++

Python HTTP

https://www.mathworks.com/help/matlab/external-language-interfaces.html

MATLAB provides flexible integration with multiple languages

Page 4: Using MATLAB with Python - MathWorks · Python Note the syntax differences when calling MATLAB from Python. >> C = A + B >>> C = eng.plus(A,B) >> foo(x) >>> eng.foo(x,nargout=0) MATLAB

Using MATLAB with Python

▪ Example overview

▪ Calling Python libraries from MATLAB

▪ Calling MATLAB from Python

– via MATLAB Engine API

– via MATLAB Runtime (MATLAB Compiler SDK)

– via MATLAB Production Server

▪ Additional info

– Data management

– Deep learning

– Troubleshooting

▪ Resources

Page 5: Using MATLAB with Python - MathWorks · Python Note the syntax differences when calling MATLAB from Python. >> C = A + B >>> C = eng.plus(A,B) >> foo(x) >>> eng.foo(x,nargout=0) MATLAB

Sentiment Analysis Example

Example files:

https://github.com/mathworks/matlab-with-python

“That movie

was terrible!”

Page 6: Using MATLAB with Python - MathWorks · Python Note the syntax differences when calling MATLAB from Python. >> C = A + B >>> C = eng.plus(A,B) >> foo(x) >>> eng.foo(x,nargout=0) MATLAB

Sentiment Analysis Example

Page 7: Using MATLAB with Python - MathWorks · Python Note the syntax differences when calling MATLAB from Python. >> C = A + B >>> C = eng.plus(A,B) >> foo(x) >>> eng.foo(x,nargout=0) MATLAB

Sentiment Analysis Example

MATLABPython

Page 8: Using MATLAB with Python - MathWorks · Python Note the syntax differences when calling MATLAB from Python. >> C = A + B >>> C = eng.plus(A,B) >> foo(x) >>> eng.foo(x,nargout=0) MATLAB

Sentiment Analysis Example: Calling Python from MATLAB

MATLAB

Python

Page 9: Using MATLAB with Python - MathWorks · Python Note the syntax differences when calling MATLAB from Python. >> C = A + B >>> C = eng.plus(A,B) >> foo(x) >>> eng.foo(x,nargout=0) MATLAB

Sentiment Analysis Example: Calling MATLAB from Python

MATLAB

Python

Page 10: Using MATLAB with Python - MathWorks · Python Note the syntax differences when calling MATLAB from Python. >> C = A + B >>> C = eng.plus(A,B) >> foo(x) >>> eng.foo(x,nargout=0) MATLAB

Using MATLAB with Python

▪ Calling libraries written in Python from MATLAB– MATLAB

▪ Calling MATLAB from Python– MATLAB

▪ Packaging MATLAB programs for

scalable deployment with Python

– MATLAB Compiler SDK

– MATLAB Production Server

Page 11: Using MATLAB with Python - MathWorks · Python Note the syntax differences when calling MATLAB from Python. >> C = A + B >>> C = eng.plus(A,B) >> foo(x) >>> eng.foo(x,nargout=0) MATLAB

MATLAB Type Example Python Type Example

Scalar double 9.0 Float 9.0

Char 'A' Str 'A'

Cell array {'apple',2,1} List ['apple',2,1]

1-by-n double [1.0, 2.0, 3.0] array.array(‘d’) [1.0, 2.0, 3.0]

m-by-n matrix [ 1.0, 2.0

3.0, 4.0 ]

MATLAB Type Example Python Type Example

Scalar double 9.0 Float 9.0

Char 'A' Str 'A'

Cell array {'apple',2,1} List ['apple',2,1]

1-by-n double [1.0, 2.0, 3.0] array.array(‘d’) [1.0, 2.0, 3.0]

m-by-n matrix [ 1.0, 2.0

3.0, 4.0 ]memoryview object

MATLAB Type Example Python Type Example

Scalar double 9.0 Float 9.0

Char 'A' Str 'A'

Cell array {'apple',2,1} List ['apple',2,1]

1-by-n double [1.0, 2.0, 3.0] array.array(‘d’) [1.0, 2.0, 3.0]

MATLAB Type Example Python Type Example

Scalar double 9.0 Float 9.0

MATLAB Type Example Python Type Example

Scalar double 9.0 Float 9.0

Char 'A' Str 'A'

MATLAB Type Example Python Type Example

Scalar double 9.0 Float 9.0

Char 'A' Str 'A'

Cell array {'apple',2,1} Tuple ['apple',2,1]

MATLAB Converts Core Python Types

>> x = 9.0;

>> a = py.math.sqrt(x);

>> class(a)

'double'

https://www.mathworks.com/help/matlab/python-data-types.html

Page 12: Using MATLAB with Python - MathWorks · Python Note the syntax differences when calling MATLAB from Python. >> C = A + B >>> C = eng.plus(A,B) >> foo(x) >>> eng.foo(x,nargout=0) MATLAB

>> py.print('hello','world',...

pyargs('sep',', '))

>> py.math.sqrt(42)

MATLAB

>>> import math

>>> math.sqrt(42)

Python

>>> print('hello','world',sep=', ')

Note the syntax differences when calling Python from MATLAB

Page 13: Using MATLAB with Python - MathWorks · Python Note the syntax differences when calling MATLAB from Python. >> C = A + B >>> C = eng.plus(A,B) >> foo(x) >>> eng.foo(x,nargout=0) MATLAB

>> [s,sidx] = sort(x)

MATLAB

>>> s = eng.sort(x,nargout=2)

Python

Note the syntax differences when calling MATLAB from Python

>> C = A + B >>> C = eng.plus(A,B)

>> foo(x) >>> eng.foo(x,nargout=0)

Page 14: Using MATLAB with Python - MathWorks · Python Note the syntax differences when calling MATLAB from Python. >> C = A + B >>> C = eng.plus(A,B) >> foo(x) >>> eng.foo(x,nargout=0) MATLAB

MATLAB Production Server manages multiple MATLAB Runtime

versions simultaneously

▪ RESTful API:

– https://www.mathworks.com/help/mps/restful-api-and-json.html

▪ Python Client:

– https://www.mathworks.com/help/mps/python/create-a-matlab-production-server-python-

client.html

Page 15: Using MATLAB with Python - MathWorks · Python Note the syntax differences when calling MATLAB from Python. >> C = A + B >>> C = eng.plus(A,B) >> foo(x) >>> eng.foo(x,nargout=0) MATLAB

Using MATLAB with Python for Deep Learning

▪ Import and export networks directly using ONNX and Tensorflow-Keras

importers:

▪ https://www.mathworks.com/solutions/deep-learning/models.html

Page 16: Using MATLAB with Python - MathWorks · Python Note the syntax differences when calling MATLAB from Python. >> C = A + B >>> C = eng.plus(A,B) >> foo(x) >>> eng.foo(x,nargout=0) MATLAB

Use Apache Parquet to store and transfer tabular data between

MATLAB and Python

▪ Working with Parquet files:

▪ https://www.mathworks.com/help/matlab/parquet-files.html

▪ MATLAB library for Apache Arrow on GitHub:

▪ https://github.com/apache/arrow/tree/master/matlab

Page 17: Using MATLAB with Python - MathWorks · Python Note the syntax differences when calling MATLAB from Python. >> C = A + B >>> C = eng.plus(A,B) >> foo(x) >>> eng.foo(x,nargout=0) MATLAB

Troubleshooting

▪ Sophisticated exception handling

options exist

▪ See the documentation

▪ Call tech support!

▪ MATLAB Answers, community

https://www.mathworks.com/help/matlab/getting-started-with-python.html

https://www.mathworks.com/help/matlab/matlab-engine-for-python.html

Page 18: Using MATLAB with Python - MathWorks · Python Note the syntax differences when calling MATLAB from Python. >> C = A + B >>> C = eng.plus(A,B) >> foo(x) >>> eng.foo(x,nargout=0) MATLAB

Resources

▪ General:

▪ https://www.mathworks.com/products/matlab/matlab-and-python.html

▪ Python from MATLAB:

▪ https://www.mathworks.com/help/matlab/call-python-libraries.html

▪ MATLAB from Python:

– MATLAB Engine API:

– https://www.mathworks.com/help/matlab/matlab-engine-for-python.html

– MATLAB Compiler SDK:

– https://www.mathworks.com/help/compiler_sdk/python_packages.html

– Data type conversions:

– https://www.mathworks.com/help/matlab/python-data-types.html

▪ Example:

▪ https://github.com/mathworks/matlab-with-python

Page 19: Using MATLAB with Python - MathWorks · Python Note the syntax differences when calling MATLAB from Python. >> C = A + B >>> C = eng.plus(A,B) >> foo(x) >>> eng.foo(x,nargout=0) MATLAB

Using MATLAB with Python

Python

MATLAB

+

@HeatherGorr

@heather.codes

Heather Gorr, PhD