Run pickle file in matlab (2024)

329 views (last 30 days)

Show older comments

I have machines learning model developed by python and saved as pickle file how can i run it in matlab

Answers (1)

Run pickle file in matlab (3)

Al Danial on 13 Jun 2022

This shows how to load a pickle file into MATLAB:

pickle = py.importlib.import_module('pickle');

fh = py.open('data.pkl', 'rb')

P = pickle.load(fh); % pickle file loaded to Python variable

fh.close();

mP = py2mat(P); % pickle data converted to MATLAB native variable

The last line uses the py2mat.m utility. I tested this with the file data.pkl , created with this Python program,

#!/usr/bin/env python3

import pickle

import numpy as np

cm = np.array([[2., 3],[0, 1]]) - np.eye(2)*1j

a_list = ['this', 'is', 'a', 'complex', 'matrix', cm]

a_dict = { 1 : 1, 2 : 'two', 'three' : 3}

an_int = 42

some_bytes = b'1ee50ffe2fb5104144142f001a8ca94ae56b90cf'

X = np.arange(12,dtype=np.float16).reshape(3,4)

P = {

'a_list' : a_list,

'a_dict' : a_dict,

'an_int' : an_int,

'some_bytes' : some_bytes,

'X' : X,

}

with open('data.pkl', 'wb') as fh:

pickle.dump(P, fh, pickle.HIGHEST_PROTOCOL)

which comes from Section 7.13, "Pickle Files", from my book Python for MATLAB Development.

However this merely loads the contents of the pickle file. Running the machine learning model in MATLAB is something else entirely.

1 Comment

Show -1 older commentsHide -1 older comments

Run pickle file in matlab (4)

Aymane ahajjam on 5 Apr 2024

@Al Danial, can you help with running the pickle-loaded machine learning model in matlab then?

Thank you eitherway!

Sign in to comment.

See Also

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


Run pickle file in matlab (5)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

Europe

Asia Pacific

Contact your local office

Run pickle file in matlab (2024)
Top Articles
Latest Posts
Article information

Author: Horacio Brakus JD

Last Updated:

Views: 6067

Rating: 4 / 5 (51 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Horacio Brakus JD

Birthday: 1999-08-21

Address: Apt. 524 43384 Minnie Prairie, South Edda, MA 62804

Phone: +5931039998219

Job: Sales Strategist

Hobby: Sculling, Kitesurfing, Orienteering, Painting, Computer programming, Creative writing, Scuba diving

Introduction: My name is Horacio Brakus JD, I am a lively, splendid, jolly, vivacious, vast, cheerful, agreeable person who loves writing and wants to share my knowledge and understanding with you.