Matlab是学术界非常受欢迎的科学计算平台,matlab提供强大的数据计算以及仿真功能。在Matlab中数据集通常保存为.mat格式。那么如果我们想要在Python中加载.mat数据应该怎么办呢?所以今天就给大家分享一个使用python加载.mat数据的方法。我将使用Stanford Cars Dataset数据集作为例子为大家演示使用方法。

加载.mat文件

Scipy是一个非常流行的用于科学计算的python库,很自然地,它们有一种方法可以让你读入.mat文件。阅读它们绝对是一件容易的事。您可以在一行代码中完成它:from scipy.io import loadmat

annots = loadmat('cars_train_annos.mat')

格式化数据

通过loadmat方法加载数据后会返回一个Python字典的数据结构,我们可以查看数据关键字,代码如下:annots.keys()

> dict_keys(['__header__', '__version__', '__globals__', 'annotations'])

下边是关于数据集描述的文档,从中我们可以查看关于数据及更详细的描述,也可以验证通过Python加载后数据是否正确。This file gives documentation for the cars 196 dataset.

(http://ai.stanford.edu/~jkrause/cars/car_dataset.html)

— — — — — — — — — — — — — — — — — — — —

Metadata/Annotations

— — — — — — — — — — — — — — — — — — — —

Descriptions of the files are as follows:

-cars_meta.mat:

Contains a cell array of class names, one for each class.

-cars_train_annos.mat:

Contains the variable ‘annotations’, which is a struct array of length

num_images and where each element has the fields:

bbox_x1: Min x-value of the bounding box, in pixels

bbox_x2: Max x-value of the bounding box, in pixels

bbox_y1: Min y-value of the bounding box, in pixels

bbox_y2: Max y-value of the bounding box, in pixels

class: Integral id of the class the image belongs to.

fname: Filename of the image within the folder of images.

-cars_test_annos.mat:

Same format as ‘cars_train_annos.mat’, except the class is not provided.

— — — — — — — — — — — — — — — — — — — —

Submission file format

— — — — — — — — — — — — — — — — — — — —

Files for submission should be .txt files with the class prediction for

image M on line M. Note that image M corresponds to the Mth annotation in

the provided annotation file. An example of a file in this format is

train_perfect_preds.txt

Included in the devkit are a script for evaluating training accuracy,

eval_train.m. Usage is:

(in MATLAB)

>> [accuracy, confusion_matrix] = eval_train(‘train_perfect_preds.txt’)

If your training predictions work with this function then your testing

predictions should be good to go for the evaluation server, assuming

that they’re in the same format as your training predictions.

从文档中可以看到,annotations变量中包含我们想要的结构数据,包括标签、图像文件名以及图像边界框信息,因此我们只需处理annotations变量并从中提取我们想要的信息。type(annots[‘annotations’]),annots[‘annotations’].shape

>(numpy.ndarray, (1, 8144))

type(annots['annotations'][0][0]),annots['annotations'][0][0].shape

>(numpy.void, ())

从.mat中提取的数据以numpy.ndarray格式存储,此数组中的项的数据类型是numpy.void。annots[‘annotations’][0][0][‘bbox_x1’], annots[‘annotations’][0][0][‘fname’]

> (array([[39]], dtype=uint8), array(['00001.jpg'], dtype='

接下来我们通过循环将字典中的annotations变量信息提取出来,并将它们存储在列表中:[item.flat[0] for item in annots[‘annotations’][0][0]]

> [39, 116, 569, 375, 14, '00001.jpg']

将数据转换成Pandas Dataframe

现在我们用python加载好matlab数据文件,为方便后续的处理,我们将数据转换为pandas格式。转换过程十分简单,具体代码如下:data = [[row.flat[0] for row in line] for line in annots[‘annotations’][0]]

columns = [‘bbox_x1’, ‘bbox_y1’, ‘bbox_x2’, ‘bbox_y2’, ‘class’, ‘fname’]

df_train = pd.DataFrame(data, columns=columns)

转换后数据形式如下:

python读取matlab数据_两分钟搞定Python读取matlab的.mat数据相关推荐

  1. python网络编程自学_五分钟搞定Python网络编程实现TCP和UDP连接

    Python网络编程实现TCP和UDP连接, 使用socket模块, 所有代码在python3下测试通过. 实现TCP#!/usr/bin/env python3 # -*- coding: utf- ...

  2. matlab读取.mb15格式数据,两分钟搞定Python读取matlab的.mat数据

    Matlab是学术界非常受欢迎的科学计算平台,matlab提供强大的数据计算以及仿真功能.在Matlab中数据集通常保存为.mat格式.那么如果我们想要在Python中加载.mat数据应该怎么办呢?所 ...

  3. python 逆向生成正则表达式_一篇搞定Python正则表达式

    1. 正则表达式语法 1.1 字符与字符类 1 特殊字符:.^$?+*{}[]()| 以上特殊字符要想使用字面值,必须使用进行转义 2 字符类 1. 包含在[]中的一个或者多个字符被称为字符类,字符类 ...

  4. 关于python论文2000字_一篇文章搞定Python全部基础知识

    前言: 1.Python软件安装 第一章.字符串及数字变量 1.变量 2.数字型数据 要点提炼:这下面那张图就行,至于其它的,就是文本转字数(int),数字转文本(Str) 3.字符串 要点提炼:字符 ...

  5. Python自动化(十二):一分钟搞定几百个Excel中查找的数据

    一.需求说明 首先我们来看下今天的需求,有一份档案记录总表的Excel工作簿, 每天会根据当天日期建立新表,每天的表格内包含所有档案信息,同时也有可能会添加新的档案名.同个年度的总表在年末可能会有两. ...

  6. Spring Boot 返回 XML 数据,一分钟搞定!

    2019独角兽企业重金招聘Python工程师标准>>> Spring Boot 返回 XML 数据,前提必须已经搭建了 Spring Boot 项目,所以这一块代码就不贴了,可以点击 ...

  7. 正则表达式里转义字符_五分钟搞定正则表达式,如果没搞定,再加两分钟

    五分钟搞定正则表达式,如果没搞定,再加两分钟 [这是 ZY 第 18 篇原创文章] 文章概览 一.正则表达式介绍 正则表达式,又称规则表达式.(英语:Regular Expression,在代码中常简 ...

  8. 逗号后面统一加空格_十分钟搞定字幕,教你做加字幕的“快手菜”

    平台上许多同学有疑问:做视频是否有加字幕的必要呢? 其实除了外语需要翻译.语速过快加字幕方便理解.普通话不标准等情况之外,还是建议有余力的同学可以加上字幕,提升用户的观看体验. 那么问题来了,存在以下 ...

  9. 深入浅出 Python 装饰器:16 步轻松搞定 Python 装饰器

    2019独角兽企业重金招聘Python工程师标准>>> Python的装饰器的英文名叫Decorator,当你看到这个英文名的时候,你可能会把其跟Design Pattern里的De ...

最新文章

  1. 2021年就业力排名TOP10的英国大学
  2. 微信小程序 用户权限部分
  3. 升级Springboot版本异常:HateoasConfiguration required a single bean, but 3 were found
  4. (1)Uni-App入门
  5. 产生随机小数_如果取到小数区间内的任一数字?
  6. 后台启动_Linux系统后台运行应用三板斧
  7. 小计C/C++问题(1)
  8. unity 特效shader下载_Unity shader消融特效——(1)逻辑节点篇
  9. jclasslib修改jar包中class文件 IDEA
  10. 机械师笔记本电脑使用小常识
  11. Duplicate entry for key 'PRIMARY'
  12. 什么是BPM系统?BPM流程管理系统介绍
  13. flutter doctor --android-licenses命令之后出现JAVA_HOME错误
  14. 精心收集的95个超实用的JavaScript代码片段(ES6 +编写)
  15. html5妇女节游戏,html5开发三八女王节表白神器
  16. vue绑定类名 禁用样式
  17. 宽带拨号密码查看工具
  18. Lazy and Hungry
  19. 046 中值定理之型三(ξ与a,b不可分离;凑微法);型四(ξ η多个中值之case1:找三点 两次拉格朗日)
  20. 使用所学Spring知识,实现简易的图书查询系统功能。实现查询全部图书。 根据书籍编号查询信息。 根据书名查询书籍信息。 根据状态查询书籍信息。

热门文章

  1. 基于JAVA+SpringMVC+Mybatis+MYSQL的学籍管理系统
  2. 基于JAVA+SpringBoot+Mybatis+MYSQL的停车场管理系统
  3. 基于JAVA+Servlet+JSP+MYSQL的教室预订管理系统
  4. bzoj 4006 管道连接 —— 斯坦纳树+状压DP
  5. Python中单引号,双引号,三个单引号,外双单引号内双引号,外双引号内单引号的区别...
  6. 基于OneAPM的Web系统性能监测
  7. 说说WeakReference弱引用
  8. html文档不是本地电脑,电脑浏览器打不开本地html文件
  9. android html拦截广告,广告见鬼去!两招让安卓告别网页广告
  10. 机器学习模型_如何口述机器学习模型原理