How to import a pre trained linear regression model from python to matlab?

If I trained a model using python. Is there any way to import or use that model in matlab? Finally I want to genarate a c code after adding something to that in matlab.

3 Comments

What format is your model in? To generate code, you'll need the MATLAB Code Toolbox.
First of all Thank you so much for quick responce.
I have a model in ".SAV" formate right now. I want to import that into matlab and genarate C code for that.
Please let me know if there is any way to do that.
Could you please tell me do you mean MATLAB Coder Toolbox?
If you have any referance links or documents regarding this process please suggest me. Those will help me a lot.
could you please let me know what are all formates of ML models that are developed and trained using python,
Can be accepted by matlab to import ?
I have a doubt here.
if we are converting matlab code from .m to .c, If we have any function which is calling a python method from a .py file then, Is our tool box going to convert the packages that are being called by the python function in .py file?
please let me know if the question is not clear.
I will try to reconstruct or try to send a picture on my question.

Sign in to comment.

Answers (1)

To import from Python to MATLAB, save the model in a format MATLAB can import:
or a simpler format, like text or csv.
To generate C code from the imported model, use MATLAB Coder or GPU Coder:

5 Comments

Thanks for your quick reply.
1.
IN PYTHON:
from sklearn.linear_model import LogisticRegression
import pickle
model = LogisticRegression()
model.fit(X_train, Y_train)
pickle.dump(model, open('D:\python_exp\mfinalized_model.mat', 'wb'))
I have tried to save my model in matlab formate(.mat) in python and I was succesfully saved that.
but when I am trying to load into matlab it is showing some error.
IN MATLAB:
>> load('mfinalized_model.mat')
Error using load
Unable to read MAT-file D:\python_exp\mfinalized_model.mat. Not a binary MAT-file. Try load -ASCII to read as text.
2.
In the link that you have suggested I came to know the ways to import neural neworks. but unable to find way to import simple reggession models.
Please let me know If I am going in wrong direction.
Ah, my mistake assuming you were using a neural network.
For simple regression models, the coefficients are usually just one vector, right?
For logistic regression, a few lines of MATLAB can use the coefficient vector and input to produce output, similar to the mymodelfun function in this example:
For getting the data from python into MATLAB, it sounds like pickle uses its own binary format. With your command, it named the file with a ".mat" extension, but doesn't follow the MathWorks ".mat" format.
I'd recommend saving the vector of coefficients as a text or csv file, and importing to MATLAB via readmatrix or importdata.
Hi David, I have a similar question. I have a model trained in python and have the model saved in csv. How could I import the model in matlab and then convert it into a simulink block ?
Basically what I need to do is the same as what the gensim function does to a trained matlab network.
Could you please assist me with this.
Hi Shayne,
Here is an example that shows how to call GoogLeNet into Simulink using the MATLAB function block:
Also, there are the following blocks that support calling popular model types directly:
Hi David,
Thanks for your advise.
I tried this, but I was able to directly get a .h5 file of model weights from keras.
So importkeraslayers(model file) worked well.
But unfortunately the gensim(net) function does not operate on the 'net' file generated from the importkeraslayers function. It shows an error of incorrect argument datatype or missing argument in call to function 'gensim'
Can you provide me with some insight on this. Thanks a ton.
Shayne

Sign in to comment.

Edited:

on 14 May 2021

Community Treasure Hunt

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

Start Hunting!