Calling regression from Python

6 次查看(过去 30 天)
Vipin Veetil
Vipin Veetil 2020-5-23
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

回答(1 个)

Rajani Mishra
Rajani Mishra 2020-5-27
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):

类别

Help CenterFile Exchange 中查找有关 Call MATLAB from Python 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by