首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何分割、训练和测试.mat文件中的数据?

问如何分割、训练和测试.mat文件中的数据?
EN

Stack Overflow用户
提问于 2021-11-10 17:32:33
回答 1查看 516关注 0票数 0

我有一个mnist数据集作为一个.mat文件,并希望分裂训练和测试数据与学习。sklearn按以下方式读取.mat文件:

代码语言:javascript
复制
{'__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)}

怎么做?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-10 22:52:23

我猜您的意思是使用.mat而不是sklearn将该数据文件加载到Python中。从本质上说,可以像这样加载.mat数据文件:

代码语言:javascript
复制
import scipy.io
scipy.io.loadmat('your_dot_mat_file')

scipy将其作为Python字典阅读。所以在你的例子中,你读到的数据被分割成列车:train_fea1,有火车标签的train_gnd1和test_fea1有测试标签的test_gnd1。

要访问您的数据,您可以:

代码语言:javascript
复制
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来拆分您的数据,您可以首先结合您的数据中的特性和标签,然后像这样随机拆分(在加载了上面的数据之后):

代码语言:javascript
复制
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 split
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69917735

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档