我正在尝试将.mat数据集加载到我的数据框中。因此,我每次只能使用
从Folder TrainingSet1加载单个文件。 os.chdir('/Users/Ashi/Downloads/TrainingSet2')
data = loadmat('A2001.mat')
而且我能够看到其中的数据,但是我应该如何加载整个TrainingSet1文件夹,以便可以查看整个内容。另外,如何查看.mat文件作为图像?
这里是我的代码,
%reload_ext autoreload
%autoreload 2
%matplotlib inline
from fastai.vision import *
from fastai.metrics import error_rate
from mat4py import loadmat
from pylab import*
import matplotlib
import os
os.chdir('/Users/Ashi/Downloads/TrainingSet2')
data = loadmat('A2001.mat')
data
{'ECG': {'sex': 'Male', 'age': 68,
'data': [[0.009784321006571624,
0.006006033870606647,
...This is roughly how the data looks like
imshow('A2001.mat',[])
---------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-52-23bbdf3a7668> in <module>
----> 1 imshow('A2001.mat',[])...A long error is displayed
TypeError: unhashable type: 'list'
感谢您的帮助
很难从您的帖子中知道什么是输入格式,什么是您想要的输出格式。
[我为您提供了读取文件夹中所有.mat
文件的示例,以及如何将data['data']
显示为图像的示例。
我希望这个例子足以使您自己继续前进。
我使用MATLAB创建了示例数据集'A2001.mat'
,'A2002.mat'
,'A2003.mat'
。如果您已经安装了MATLAB,建议您执行以下代码来创建示例输入(以使Python示例可重现):
ECG.sex = 'Male';
ECG.age = 68;
data = im2double(imread('cameraman.tif')) / 10; % Divide by 10 for simulating range [0, 0.1] instead of [0, 1]
save('A2001.mat', 'ECG', 'data');
ECG.sex = 'Male';
ECG.age = 46;
data = im2double(imread('cell.tif'));
save('A2002.mat', 'ECG', 'data');
ECG.sex = 'Female';
ECG.age = 54;
data = im2double(imread('tire.tif'));
save('A2003.mat', 'ECG', 'data');
Python代码示例执行以下操作:
mat
获取文件夹中所有glob.glob('*.mat')
文件的列表。 mat
文件,从文件中加载数据,并将数据附加到列表中。循环的结果是一个名为alldata
的列表,其中包含来自所有mat
文件的数据。 alldata
并将data['data']
显示为图像。(假设data['data']
是要显示为图像的矩阵)。 这里是代码:
from matplotlib import pyplot as plt
from mat4py import loadmat
import glob
import os
os.chdir('/Users/Ashi/Downloads/TrainingSet2')
# Get a list for .mat files in current folder
mat_files = glob.glob('*.mat')
# List for stroring all the data
alldata = []
# Iterate mat files
for fname in mat_files:
# Load mat file data into data.
data = loadmat(fname)
# Append data to the list
alldata.append(data)
# Iterate alldata elelemts, and show images
for data in alldata:
# Assume image is stored in matrix named data in MATLAB.
# data['data'], access data with string 'data', becuase data is a dictionary
img = data['data']
# Show data as image using matplotlib
plt.imshow(img, cmap='gray')
plt.show(block=True) # Show image with "blocking"
ECG数据不是图像,而是12个数据样本的列表。
[data = loadmat(fname)
之后的数据的内部结构是:
data
的父字典。data
在data['ECG']
中包含一个词典。data['ECG']['data']
是12个列表的列表。 下面的代码迭代mat
文件,并将ECG数据显示为图形:
from matplotlib import pyplot as plt
from mat4py import loadmat
import glob
import os
import numpy as np
os.chdir('/Users/Ashi/Downloads/TrainingSet2')
# Get a list for .mat files in current folder
mat_files = glob.glob('*.mat')
# List for stroring all the data
alldata = []
# Iterate mat files
for fname in mat_files:
# Load mat file data into data.
data = loadmat(fname)
# Append data to the list
alldata.append(data)
# Iterate alldata elelemts, and show images
for data in alldata:
# The internal structure of the data is a dictionary with a dictionary.
ecg = data['ECG']
data = ecg['data'] # Data is a list of lists
# Convert data to NumPy array
ecg_data = np.array(data)
# Show data as image using matplotlib
#plt.imshow(img, cmap='gray')
plt.plot(ecg_data.T) # Plot the data as graph.
plt.show(block=True) # Show image with "blocking"
结果: