python 随机打乱 图片和对应的标签方法

如下所示:

# -*- coding: utf-8 -*-
 
import os
import numpy as np
import pandas as pd
import h5py
import pylab
import matplotlib.pyplot as plt
 
 
trainpath = str('C:/Users/49691/Desktop/数据集/train/')
testpath = str('C:/Users/49691/Desktop/数据集/test/')
n_tr = len(os.listdir(trainpath))
print('num of training files: ', n_tr)
 
train_labels = pd.read_csv('C:/Users/49691/Desktop/数据集/sample_submission.csv')
train_labels.head()
 
from skimage import io, transform
 
 
x = np.empty(shape=(n_tr, 224, 224, 3))
y = np.empty(n_tr)
 
labels = train_labels.invasive.values
name = train_labels.name.values
 
permutation=np.random.permutation(name.shape[0])
print(permutation)
print(labels[permutation])
save_data = pd.DataFrame({'name':permutation,'invasive':labels[permutation]})
save_data.to_csv('C:/Users/49691/Desktop/数据集/b.csv')
 
 
for k,v in enumerate(np.random.permutation(n_tr)):
 print(k,v)
 path = '{0}{1}.jpg'.format(trainpath, v)
 tr_im = io.imread(path)
 x[k] = transform.resize(tr_im, output_shape=(224, 224, 3))
 y[k] = float(labels[v-1])

以上这篇python 随机打乱 图片和对应的标签方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持鸟哥教程(niaoge.com)。

声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#niaoge.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。