Matlab is a very popular scientific computing platform in academia. Matlab provides powerful data calculation and simulation functions. Datasets are usually saved in .mat format in Matlab. So what if we want to load .mat data in Python? So today I will share with you a method of using python to load .mat data. I will use the Stanford Cars Dataset as an example to demonstrate how to use it.
data set
The Stanford Cars Dataset dataset is a dataset about vehicle image classification. The dataset is saved in .mat format. The data and download address are:
https://ai.stanford.edu/~jkrause/cars/car_dataset.html
Load .mat file
Scipy is a very popular python library for scientific computing, and naturally, they have a way for you to read in .mat files. Reading them is definitely an easy task. You can do it in one line of code:
format data
After loading the data through the loadmat method, a data structure of a Python dictionary will be returned. We can view the data keywords. The code is as follows:
The following is the document about the description of the dataset, from which we can view the data and more detailed descriptions, and can also verify the pass Is the data correct after Python loads.
As you can see from the documentation, the annotations variable contains the structural data we want, including labels, image filenames, and image bounding box information, so we just need to process the annotations variable and extract the information we want from it.
The data extracted from .mat is stored in numpy.ndarray format, and the data type of the items in this array is numpy.void.
Next, we extract the annotations variable information in the dictionary by looping and store them in a list:
Convert data to Pandas Dataframe
Now we use python to load the matlab data file. For the convenience of subsequent processing, we convert the data to pandas format. The conversion process is very simple, the specific code is as follows: The
converted data format is as follows:
Click here to join a group to find the administrator to receive free data