最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用python如何將數(shù)據(jù)集劃分為訓(xùn)練集、驗證集和測試集

 更新時間:2023年09月09日 09:15:34   作者:肖申克的陪伴  
這篇文章主要介紹了使用python如何將數(shù)據(jù)集劃分為訓(xùn)練集、驗證集和測試集問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教

python將數(shù)據(jù)集劃分為訓(xùn)練集、驗證集和測試集

劃分?jǐn)?shù)據(jù)集

眾所周知,將一個數(shù)據(jù)集只區(qū)分為訓(xùn)練集和驗證集是不行的,還需要有測試集,本博文針對上一篇沒有分出測試集的不足,重新劃分?jǐn)?shù)據(jù)集

直接上代碼:

#split_data.py
#劃分?jǐn)?shù)據(jù)集flower_data,數(shù)據(jù)集劃分到flower_datas中,訓(xùn)練集:驗證集:測試集比例為6:2:2
import os
import random
from shutil import copy2
# 源文件路徑
file_path = r"D:/other/ClassicalModel/other/flower_data"
# 新文件路徑
new_file_path = r"D:/other/ClassicalModel/other/flower_datas"
# 劃分?jǐn)?shù)據(jù)比例為6:2:2
split_rate = [0.6, 0.2, 0.2]
print("Starting...")
print("Ratio= {}:{}:{}".format(int(split_rate[0] * 10), int(split_rate[1] * 10), int(split_rate[2] * 10)))
class_names = os.listdir(file_path)
# 在目標(biāo)目錄下創(chuàng)建文件夾
split_names = ['train', 'val', 'test']
# 判斷是否存在木匾文件夾
if os.path.isdir(new_file_path):
    pass
else:
    os.mkdir(new_file_path)
for split_name in split_names:
    # split_path = os.path.join(new_file_path, split_name)
    split_path = new_file_path + "/" + split_name
    if os.path.isdir(split_path):
        pass
    else:
        os.mkdir(split_path)
    # 然后在split_path的目錄下創(chuàng)建類別文件夾
    for class_name in class_names:
        class_split_path = os.path.join(split_path, class_name)
        if os.path.isdir(class_split_path):
            pass
        else:
            os.mkdir(class_split_path)
# 按照比例劃分?jǐn)?shù)據(jù)集,并進行數(shù)據(jù)圖片的復(fù)制
# 首先進行分類遍歷
for class_name in class_names:
    current_class_data_path = os.path.join(file_path, class_name)
    current_all_data = os.listdir(current_class_data_path)
    current_data_length = len(current_all_data)
    current_data_index_list = list(range(current_data_length))
    random.shuffle(current_data_index_list)
    train_path = os.path.join(os.path.join(new_file_path, 'train'), class_name)
    val_path = os.path.join(os.path.join(new_file_path, 'val'), class_name)
    test_path = os.path.join(os.path.join(new_file_path, 'test'), class_name)
    train_stop_flag = current_data_length * split_rate[0]
    val_stop_flag = current_data_length * (split_rate[0] + split_rate[1])
    current_idx = 0
    train_num = 0
    val_num = 0
    test_num = 0
    for i in current_data_index_list:
        src_img_path = os.path.join(current_class_data_path, current_all_data[i])
        if current_idx <= train_stop_flag:
            copy2(src_img_path, train_path
            train_num = train_num + 1
        elif (current_idx > train_stop_flag) and (current_idx <= val_stop_flag):
            copy2(src_img_path, val_path)
            val_num = val_num + 1
        else:
            copy2(src_img_path, test_path
            test_num = test_num + 1
        current_idx = current_idx + 1
    print("<{}> has {} pictures,train:val:test={}:{}:{}".format(class_name, current_data_length, train_num, val_num,
                                                              test_num))
print("Done")

輸出結(jié)果:

注意:

只需要修改file_path(源文件夾)和new_file_path(新生成的文件夾)

其次是修改split_rate

python自動劃分訓(xùn)練集和測試集

在進行深度學(xué)習(xí)的模型訓(xùn)練時,我們通常需要將數(shù)據(jù)進行劃分,劃分成訓(xùn)練集和測試集,若數(shù)據(jù)集太大,數(shù)據(jù)劃分花費的時間太多?。。?/p>

不多說,上代碼(python代碼)

代碼

# *_*coding: utf-8 *_*
import os
import random
import shutil
import time
def copyFile(fileDir,origion_path1,class_name):
    name = class_name
    path = origion_path1
    image_list = os.listdir(fileDir) # 獲取圖片的原始路徑
    image_number = len(image_list)
    train_number = int(image_number * train_rate)
    train_sample = random.sample(image_list, train_number) # 從image_list中隨機獲取0.75比例的圖像.
    test_sample = list(set(image_list) - set(train_sample))
    sample = [train_sample, test_sample]
    # 復(fù)制圖像到目標(biāo)文件夾
    for k in range(len(save_dir)):
            if os.path.isdir(save_dir[k]) and os.path.isdir(save_dir1[k]):
                for name in sample[k]:
                    name1 = name.split(".")[0] + '.xml'
                    shutil.copy(os.path.join(fileDir, name), os.path.join(save_dir[k], name))
                    shutil.copy(os.path.join(path, name1), os.path.join(save_dir1[k], name1))
            else:
                os.makedirs(save_dir[k])
                os.makedirs(save_dir1[k])
                for name in sample[k]:
                    name1 = name.split(".")[0] + '.xml'
                    shutil.copy(os.path.join(fileDir, name), os.path.join(save_dir[k], name))
                    shutil.copy(os.path.join(path, name1), os.path.join(save_dir1[k], name1))
if __name__ == '__main__':
    time_start = time.time()
    # 原始數(shù)據(jù)集路徑
    origion_path = './JPEGImages/'
    origion_path1 = './Annotations/'
    # 保存路徑
    save_train_dir = './train/JPEGImages/'
    save_test_dir = './test/JPEGImages/'
    save_train_dir1 = './train/Annotations/'
    save_test_dir1 = './test/Annotations/'
    save_dir = [save_train_dir, save_test_dir]
    save_dir1 = [save_train_dir1, save_test_dir1]
    # 訓(xùn)練集比例
    train_rate = 0.75
    # 數(shù)據(jù)集類別及數(shù)量
    file_list = os.listdir(origion_path)
    num_classes = len(file_list)
    for i in range(num_classes):
        class_name = file_list[i]
    copyFile(origion_path,origion_path1,class_name)
    print('劃分完畢!')
    time_end = time.time()
    print('---------------')
    print('訓(xùn)練集和測試集劃分共耗時%s!' % (time_end - time_start))

1.需要修改的地方

  • origion_path:圖片路徑
  • origion_path1:xml文件路徑
  • train_rate:訓(xùn)練集比例

2.執(zhí)行文件deal.py后生成

  • train-img:訓(xùn)練集圖片數(shù)據(jù)
  • train-xml:訓(xùn)練集xml數(shù)據(jù)
  • test-img:測試集圖片數(shù)據(jù)
  • test-xml:測試及xml數(shù)據(jù)

3.train_rate可以根據(jù)實際情況進行調(diào)整,一般train:test是3:1

注:每次劃分?jǐn)?shù)據(jù)都是隨機的,每次執(zhí)行時將之前劃分好的數(shù)據(jù)保存或者重命名,不然會重復(fù)寫入到4個文件夾中

總結(jié)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 十個Python中常用的pip命令總結(jié)

    十個Python中常用的pip命令總結(jié)

    相信對于大多數(shù)熟悉Python的人來說,一定都聽說并且使用過pip這個工具,但是對它的了解可能還不一定是非常的透徹,今天小編就來為大家介紹10個使用pip的小技巧,相信對大家以后管理和使用Python當(dāng)中的標(biāo)準(zhǔn)庫會有幫助
    2022-07-07
  • python統(tǒng)計函數(shù)被調(diào)用次數(shù)的實現(xiàn)

    python統(tǒng)計函數(shù)被調(diào)用次數(shù)的實現(xiàn)

    本文主要介紹了python如何統(tǒng)計函數(shù)被調(diào)用次數(shù),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-02-02
  • python?dataframe獲得指定行列簡單例子

    python?dataframe獲得指定行列簡單例子

    在DataFrame中取特定行列的數(shù)據(jù)是一個常見的操作,這篇文章主要給大家介紹了關(guān)于python?dataframe獲得指定行列的簡單例子,需要的朋友可以參考下
    2024-03-03
  • python中字符串?dāng)?shù)組逆序排列方法總結(jié)

    python中字符串?dāng)?shù)組逆序排列方法總結(jié)

    在本篇文章里小編給大家整理了關(guān)于python中字符串?dāng)?shù)組如何逆序排列的相關(guān)知識點,需要的朋友們學(xué)習(xí)下。
    2019-06-06
  • 如何利用Python解析超大的json數(shù)據(jù)(GB級別)

    如何利用Python解析超大的json數(shù)據(jù)(GB級別)

    果不想從頭開始創(chuàng)建數(shù)據(jù)格式來存儲數(shù)據(jù),JSON是一個不錯的選擇,下面這篇文章主要給大家介紹了關(guān)于如何利用Python解析超大的json數(shù)據(jù)(GB級別)的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-07-07
  • python版百度語音識別功能

    python版百度語音識別功能

    這篇文章為大家詳細(xì)主要介紹了python版百度語音識別功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-07-07
  • Python softmax實現(xiàn)及數(shù)值穩(wěn)定性詳解

    Python softmax實現(xiàn)及數(shù)值穩(wěn)定性詳解

    這篇文章主要為大家介紹了Python softmax實現(xiàn)及數(shù)值穩(wěn)定性詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-07-07
  • Numpy截取指定范圍內(nèi)的數(shù)據(jù)方法

    Numpy截取指定范圍內(nèi)的數(shù)據(jù)方法

    今天小編就為大家分享一篇Numpy截取指定范圍內(nèi)的數(shù)據(jù)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-11-11
  • Python通過cron或schedule實現(xiàn)爬蟲的自動定時運行

    Python通過cron或schedule實現(xiàn)爬蟲的自動定時運行

    自動定時運行爬蟲是很多數(shù)據(jù)采集項目的基本需求,通過 Python 實現(xiàn)定時任務(wù),可以保證數(shù)據(jù)采集的高效和持續(xù)性,本文將帶大家了解如何在 Python 中使用 cron 和 schedule 來實現(xiàn)爬蟲的自動定時運行,需要的朋友可以參考下
    2024-12-12
  • Python實現(xiàn)發(fā)送聲情并茂的郵件內(nèi)容和附件

    Python實現(xiàn)發(fā)送聲情并茂的郵件內(nèi)容和附件

    Python是一種高級編程語言,它可以用于編寫各種類型的應(yīng)用程序,包括發(fā)送電子郵件。本文就來演示如何使用Python發(fā)送HTML格式的電子郵件,感興趣的可以了解一下
    2023-04-04

最新評論

五常市| 上林县| 湾仔区| 恩施市| 贺兰县| 大邑县| 抚顺县| 彭阳县| 河东区| 桂阳县| 青川县| 沈丘县| 新和县| 东乡族自治县| 梁河县| 福海县| 鄂托克前旗| 洛隆县| 育儿| 广河县| 犍为县| 宣恩县| 阿拉善右旗| 溧水县| 深水埗区| 齐齐哈尔市| 秦安县| 九龙城区| 石屏县| 炉霍县| 凤城市| 开平市| 紫云| 马尔康县| 诸城市| 甘肃省| 景德镇市| 濮阳市| 江城| 根河市| 平遥县|