Run pickle file in matlab

297 visualizaciones (últimos 30 días)
Abdulrahman Mohamed
Abdulrahman Mohamed el 12 de Jun. de 2022
Comentada: Aymane ahajjam el 5 de Abr. de 2024
I have machines learning model developed by python and saved as pickle file how can i run it in matlab
  1 comentario
Jan
Jan el 12 de Jun. de 2022
The questions contains too few information to be answered. What exactly is your problem?

Iniciar sesión para comentar.

Respuestas (1)

Al Danial
Al Danial el 13 de Jun. de 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 comentario
Aymane ahajjam
Aymane ahajjam el 5 de Abr. de 2024
@Al Danial, can you help with running the pickle-loaded machine learning model in matlab then?
Thank you eitherway!

Iniciar sesión para comentar.

Categorías

Más información sobre Call Python from MATLAB en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by