How to import the MinMaxScaler created in Python into MATLAB

13 views (last 30 days)
Hello everyone! I need to import my neural networks from Python into MATLAB. I am able to do that using the importTensorFlowNetworks command, and use the "predict" function to make predictions in MATLAB with the pre-trained networks on different datasets. The problem here is that the predicted outputs remain in the scaled format, which is understandable since the data which went into the neural network originally in Python was scaled between [-1, 1] for obvious reasons. I tried using different methods in MATLAB to unscale the predicted values and bring them to their original domain. But it is not working accurately. Only if I export these values from MATLAB back into Python again and use the "scaler.inverse_transform" function of the MinMaxScaler(), I am getting accurate results. Does anyone know if I can import this MinMaxScaler() from Python into MATLAB so that I can perform the unscaling in MATLAB itself without having to import the data from MATLAB into Python? Or if anyone has a better alternative, I am open to suggestions.
Regards,
Akshay

Answers (1)

Yash
Yash on 6 Oct 2023
Edited: Yash on 6 Oct 2023
I understand that you are unable to rescale the predicted values back to their original domain. I suspect this is because sklearn.preprocessing.MinMaxScaler "scaler" was fit on the training data and then transformed before being fed to the neural network. The desired results are obtained when using scaler.inverse_transform in Python, since "scaler" object has access to minimum and maximum values of the original data, which the MATLAB code doesn't.
Consider storing the minimum and maximum value of the original data using the below code and import these values into the MATLAB code.
minValue = scaler.data_min_[0].astype(np.float64)
maxValue = scaler.data_max_[0].astype(np.float64)
Now use MATLAB's rescale function to scale the output of the neural network back to the original domain.
scaledOutput = rescale(neuralNetOutput, minValue, maxValue)
Refer here for more information on rescale function: https://www.mathworks.com/help/matlab/ref/rescale.html
I hope this helps!

Categories

Find more on Image Data Workflows in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!