Scipy io savemat : How to use in Python?

Scipy io savemat featured image

Scipy io savemat is actually a scipy.io.savemat() function. It exports the data into Matlab file format specially NumPy arrays to “.mat” file. In this article, We will see examples to convert the NumPy array into “.mat” file.  We will keep this implementation stepwise. So let’s start.

 

Scipy io savemat Implementation –

Let’s create a sample NumPy array and export it into “export.mat” file. Here are the steps.

Step 1: Importing packages-

Firstly, We basically need scipy package and NumPy package.Let’s import them. Here we need to make sure we have scipy package installed in the system.

from scipy import io
import numpy as np

 

Step 2: Numpy array creation

Secondly, In order to demonstrate the savemat() function. We need to create the dummy Numpy Array.

nympy_arr = np.arange(15)

It will create numpy array with random numbers.

 

Step 3: invoking savemat() function –

io.savemat('export.mat', {"data": nympy_arr })

After that in this section, We are using the savemat() function. Which accepts the below parameters –

 

  1. filename –  the filename with full path.
  2. mdict – the python dict object which contains the data.
  3. do_compression – It an optional parameter with default value as False. If we set it True, It will compress the exported data. In the default scenario False, it will save into the original format.

Complete code –

Lets put all the code together and run it.

from scipy import io
import numpy as np
nympy_arr = np.arange(15)
io.savemat('export.mat', {"data": nympy_arr })
scipy io savemat
scipy io savemat

This export.mat file is generated at the same directory where we run this script. We can also parameterize the location as per our convenience.

Conclusion –

Exporting Python objects into various File Format is very easy. In some real-time situation, we need to export the arrays in Matlab files. In this article, We have shown how easy is to use savemat() function.  We have also explained each step with examples. If you are fine with this implementation, please let us know. Also, If you have any doubt,  please comment below in the comment box.  We love to interact with our readers, We also appreciate you for reading this article till the end section.

 

Thanks 

Data Science Learner Team  

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Abhishek ( Chief Editor) , a data scientist with major expertise in NLP and Text Analytics. He has worked on various projects involving text data and have been able to achieve great results. He is currently manages Datasciencelearner.com, where he and his team share knowledge and help others learn more about data science.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner