Calling regression from Python

6 ビュー (過去 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 Center および File ExchangeCall MATLAB from Python についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by