Calling regression from Python

조회 수: 13 (최근 30일)
Vipin Veetil
Vipin Veetil 2020년 5월 23일
답변: Rajani Mishra 2020년 5월 27일
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