MATLAB Answers

Calling regression from Python

7 views (last 30 days)
Vipin Veetil
Vipin Veetil on 23 May 2020
Answered: Rajani Mishra on 27 May 2020
I would like to call logistic regression from Python. Seems like Python cannot handle the object returned by Matlab.
My code and error are below:
from __future__ import division
import matlab.engine
xVals = [[1,2,1],[1,3,1],[2,2,1]]
xVals = matlab.double(xVals)
yVals = matlab.double([1,0])
eng = matlab.engine.start_matlab()
[Mdl,FitInfo] = eng.fitclinear(eng.spconvert(xVals),eng.categorical(yVals), 'learner', 'logistic''PostFitBias',true, nargout=2)
Error message
[Mdl,FitInfo] = eng.fitclinear(eng.spconvert(xVals),eng.categorical(yVals), 'learner', 'logistic''PostFitBias',true, nargout=2)
File "/Library/Python/2.7/site-packages/matlab/engine/matlabengine.py", line 71, in __call__
_stderr, feval=True).result()
File "/Library/Python/2.7/site-packages/matlab/engine/futureresult.py", line 67, in result
return self.__future.result(timeout)
File "/Library/Python/2.7/site-packages/matlab/engine/fevalfuture.py", line 82, in result
self._result = pythonengine.getFEvalResult(self._future,self._nargout, None, out=self._out, err=self._err)
TypeError: unsupported data type returned from MATLAB

  0 Comments

Sign in to comment.

Answers (1)

Rajani Mishra
Rajani Mishra on 27 May 2020
When calling a MATLAB function using MATLAB engine, by default the engine returns a single output argument. To have multiple output arguements we use nargout to specify the number of output arguements, which is 2 in this case. So using
t = eng.fitclinear(eng.spconvert(xVals),eng.categorical(yVals), 'learner', 'logistic''PostFitBias',true, nargout=2)
where t will hold both output arguements. As specified in the example on this page (Second Example):

  0 Comments

Sign in to comment.


Translated by