我有一个mnist数据集作为一个.mat文件,并希望分裂训练和测试数据与学习。sklearn按以下方式读取.mat文件:
{'__header__': b'MATLAB 5.0 MAT-file, Platform: GLNXA64, Created on: Sat Oct 8 18:13:47 2016',
'__version__': '1.0',
'__globals__': [],
'train_fea1': array([[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
...,
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0]], dtype=uint8),
'train_gnd1': array([[ 1],
[ 1],
[ 1],
...,
[10],
[10],
[10]], dtype=uint8),
'test_fea1': array([[ 0, 0, 0, ..., 0, 0, 0],
[ 0, 0, 0, ..., 0, 0, 0],
[ 0, 0, 0, ..., 0, 0, 0],
...,
[ 0, 0, 0, ..., 0, 0, 0],
[ 0, 0, 0, ..., 64, 0, 0],
[ 0, 0, 0, ..., 25, 0, 0]], dtype=uint8),
'test_gnd1': array([[ 1],
[ 1],
[ 1],
...,
[10],
[10],
[10]], dtype=uint8)}怎么做?
发布于 2021-11-10 22:52:23
我猜您的意思是使用.mat而不是sklearn将该数据文件加载到Python中。从本质上说,可以像这样加载.mat数据文件:
import scipy.io
scipy.io.loadmat('your_dot_mat_file')scipy将其作为Python字典阅读。所以在你的例子中,你读到的数据被分割成列车:train_fea1,有火车标签的train_gnd1和test_fea1有测试标签的test_gnd1。
要访问您的数据,您可以:
import scipy.io as sio
data = sio.loadmat('filename.mat')
train = data['train_fea1']
trainlabel = data['train_gnd1']
test = data['test_fea1']
testlabel = data['test_gnd1']但是,如果您使用sklearn的train-test-split来拆分您的数据,您可以首先结合您的数据中的特性和标签,然后像这样随机拆分(在加载了上面的数据之后):
import numpy as np
from sklearn.model_selection import train_test_split
X = np.vstack((train,test))
y = np.vstack((trainlabel, testlabel))
X_train, X_test, y_train, y_test = train_test_split(X, y, \
test_size=0.2, random_state=42) #random seed for reproducible splithttps://stackoverflow.com/questions/69917735
复制相似问题