Matlab Engine in Python for pretrained Matlab machine learning model doesn't work

This is my code in IntelliJ (IDE) in Python:
import matlab.engine
eng = matlab.engine.start_matlab()
pre_input = [24.0188969443267, 0.0293602407565970, 0.00128042250976566, 1.16376422874023]
theta_prd = eng.predictTheta2021(pre_input)
print(theta_prd)
////////////////////////
This is my predictTheta2021.m file:
function Theta = predictTheta2021(data)
mdl = load("Pdct_Wen13_2ps.mat"); % this loads pretrained Matlab prediction model which works in Matlab
model = mdl.theta_model; % extracting one (theta model) of the three models in that mat file
Theta = model.predictFcn(data); % calling prediction function by sending input data and output to Theta variable
end
This is what I see in the output window in IntelliJ:
Error using classreg.learning.internal.table2PredictMatrix>makeXMatrix (line 96)
Table variable column_1 is not a valid predictor.
Error in classreg.learning.internal.table2PredictMatrix (line 47)
Xout = makeXMatrix(X,CategoricalPredictors,vrange,pnames);
Error in classreg.learning.regr.CompactRegressionGP/predict (line 432)
X = classreg.learning.internal.table2PredictMatrix(X,[],[],...
Error in ThetaGPRTrain>@(x)predict(regressionGP,x) (line 62)
gpPredictFcn = @(x) predict(regressionGP, x);
Error in ThetaGPRTrain>@(x)gpPredictFcn(predictorExtractionFcn(x)) (line 63)
trainedModel.predictFcn = @(x) gpPredictFcn(predictorExtractionFcn(x));
Error in predictTheta2021 (line 8)
Theta = model.predictFcn(data);
Traceback (most recent call last):
File "C:/Users/vadim.sivetskiy/IdeaProjects/PyTest/Test.py", line 6, in <module>
theta_prd = eng.predictTheta2021(pre_input)
File "C:\Users\vadim.sivetskiy\AppData\Local\Programs\Python\Python36\lib\site-packages\matlab\engine\matlabengine.py", line 71, in __call__
_stderr, feval=True).result()
File "C:\Users\vadim.sivetskiy\AppData\Local\Programs\Python\Python36\lib\site-packages\matlab\engine\futureresult.py", line 67, in result
return self.__future.result(timeout)
File "C:\Users\vadim.sivetskiy\AppData\Local\Programs\Python\Python36\lib\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)
matlab.engine.MatlabExecutionError:
File C:\Program Files\MATLAB\R2019b\toolbox\stats\classreg\+classreg\+learning\+internal\table2PredictMatrix.m, line 96, in makeXMatrix
File C:\Program Files\MATLAB\R2019b\toolbox\stats\classreg\+classreg\+learning\+internal\table2PredictMatrix.m, line 47, in table2PredictMatrix
File C:\Program Files\MATLAB\R2019b\toolbox\stats\classreg\+classreg\+learning\+regr\CompactRegressionGP.m, line 432, in CompactRegressionGP.predict
File C:\Users\vadim.sivetskiy\IdeaProjects\PyTest\ThetaGPRTrain.m, line 62, in @(x)predict(regressionGP,x)
File C:\Users\vadim.sivetskiy\IdeaProjects\PyTest\ThetaGPRTrain.m, line 63, in @(x)gpPredictFcn(predictorExtractionFcn(x))
File C:\Users\vadim.sivetskiy\IdeaProjects\PyTest\predictTheta2021.m, line 8, in predictTheta2021
Table variable column_1 is not a valid predictor.
////////////////////////////////////////////////////////////////////////////////////////////////
The model uses Gaussian Process Regression (GPR).
My questions is:
is it even possible to load pretrained Matlab model in Python (IntelliJ IDEA)? If it is, could you give me any feedback on the errors I observe? Maybe some additional conversions required between Matlab and Python besides importing Matlab engine?
Thank you.

5 Comments

Seems like your python code just passes some data to your matlab code. Why don't you just use matlab code purely to verify the whole thing works first?
It works fine in Matlab. I get output for Theta using pre-trained model without any issues.
Okay, I tried something else - I hardcoded input in that Matlab file instead of sending it from Python and I received correct value returned in Python...not sure why it didn't like me sending input from Python though.
Print the data out in your matlab code. What you sent from python might not be what you get in matlab.
Thanks, it helped. Apparently, it was converted into a cell array instead of a matrix. All I did to fix this, cell2mat function for the input received from Python and it returned correct output back to Python.

Sign in to comment.

Answers (0)

Asked:

on 20 Jul 2021

Commented:

on 21 Jul 2021

Community Treasure Hunt

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

Start Hunting!