Python實現(xiàn)數(shù)據(jù)集劃分(訓練集和測試集)
前面是分部講解,完整代碼在最后。
導入模塊 :
import os from shutil import copy, rmtree import random
創(chuàng)建文件夾 :
def make_file(file_path: str): if os.path.exists(file_path): rmtree(file_path) os.makedirs(file_path)
劃分數(shù)據(jù)集的比例,本文是0.1:驗證集的數(shù)量占總數(shù)據(jù)集的10%比如填0.1就是驗證集的數(shù)量占總數(shù)據(jù)集的10%
random.seed(0) split_rate = 0.1
數(shù)據(jù)集的存放:新建一個數(shù)據(jù)文件夾,將劃分的數(shù)據(jù)集存放進去
data_path = r'D:\chengxu\data\caodi' # 數(shù)據(jù)集存放的地方 data_root = r'D:\chengxu\data\cd' # 這里是生成的訓練集和驗證集所處的位置,這里設置的是在當前文件夾下。 data_class = [cla for cla in os.listdir(data_path)] print("數(shù)據(jù)的種類分別為:") print(data_class) # 輸出數(shù)據(jù)種類
建立訓練集文件夾:
train_data_root = os.path.join(data_root, "train") # 訓練集的文件夾名稱為 train make_file(train_data_root) for num_class in data_class: make_file(os.path.join(train_data_root, num_class))
建立測試集文件夾:
val_data_root = os.path.join(data_root, "val") # 驗證集的文件夾名稱為 val make_file(val_data_root) for num_class in data_class: make_file(os.path.join(val_data_root, num_class))
劃分數(shù)據(jù):
for num_class in data_class: num_class_path = os.path.join(data_path, num_class) images = os.listdir(num_class_path) num = len(images) val_index = random.sample(images, k=int(num * split_rate)) # 隨機抽取圖片 for index, image in enumerate(images): if image in val_index: # 將劃分到驗證集中的文件復制到相應目錄 data_image_path = os.path.join(num_class_path, image) val_new_path = os.path.join(val_data_root, num_class) copy(data_image_path, val_new_path) else: # 將劃分到訓練集中的文件復制到相應目錄 data_image_path = os.path.join(num_class_path, image) train_new_path = os.path.join(train_data_root, num_class) copy(data_image_path, train_new_path) print("\r[{}] split_rating [{}/{}]".format(num_class, index + 1, num), end="") # processing bar print() print(" ") print(" ") print("劃分成功")
完整代碼:
import os from shutil import copy, rmtree import random def make_file(file_path: str): if os.path.exists(file_path): rmtree(file_path) os.makedirs(file_path) random.seed(0) # 將數(shù)據(jù)集中10%的數(shù)據(jù)劃分到驗證集中 split_rate = 0.1 data_path = r'D:\chengxu\data\caodi' # 數(shù)據(jù)集存放的地方,建議在程序所在的文件夾下新建一個data文件夾,將需要劃分的數(shù)據(jù)集存放進去 data_root = r'D:\chengxu\data\cd' # 這里是生成的訓練集和驗證集所處的位置,這里設置的是在當前文件夾下。 data_class = [cla for cla in os.listdir(data_path)] print("數(shù)據(jù)的種類分別為:") print(data_class) # 建立保存訓練集的文件夾 train_data_root = os.path.join(data_root, "train") # 訓練集的文件夾名稱為 train make_file(train_data_root) for num_class in data_class: # 建立每個類別對應的文件夾 make_file(os.path.join(train_data_root, num_class)) # 建立保存驗證集的文件夾 val_data_root = os.path.join(data_root, "val") # 驗證集的文件夾名稱為 val make_file(val_data_root) for num_class in data_class: # 建立每個類別對應的文件夾 make_file(os.path.join(val_data_root, num_class)) for num_class in data_class: num_class_path = os.path.join(data_path, num_class) images = os.listdir(num_class_path) num = len(images) val_index = random.sample(images, k=int(num * split_rate)) # 隨機抽取圖片 for index, image in enumerate(images): if image in val_index: data_image_path = os.path.join(num_class_path, image) val_new_path = os.path.join(val_data_root, num_class) copy(data_image_path, val_new_path) else: data_image_path = os.path.join(num_class_path, image) train_new_path = os.path.join(train_data_root, num_class) copy(data_image_path, train_new_path) print("\r[{}] split_rating [{}/{}]".format(num_class, index + 1, num), end="") # processing bar print() print(" ") print(" ") print("劃分成功")
到此這篇關于Python實現(xiàn)數(shù)據(jù)集劃分(訓練集和測試集)的文章就介紹到這了,更多相關Python數(shù)據(jù)集劃分內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
TensorFlow可視化工具TensorBoard默認圖與自定義圖
這篇文章主要介紹了TensorFlow可視化工具TensorBoard默認圖與自定義圖的使用操作示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-10-10如何利用opencv訓練自己的模型實現(xiàn)特定物體的識別
在Python中通過OpenCV自己訓練分類器進行特定物體實時識別,下面這篇文章主要給大家介紹了關于如何利用opencv訓練自己的模型實現(xiàn)特定物體的識別,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-10-10Python中的數(shù)據(jù)分組統(tǒng)計、分組運算及透視方式
這篇文章主要介紹了Python中的數(shù)據(jù)分組統(tǒng)計、分組運算及透視方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07pycharm中連接mysql數(shù)據(jù)庫的步驟詳解
在進行Python研發(fā)的時候,pycharm是一個很好的IDE,下面這篇文章主要給大家介紹了pycharm中連接mysql數(shù)據(jù)庫的步驟,文中通過圖文介紹的非常詳細,對大家具有一定的參考價值,需要的朋友們下面來一起看看吧。2017-05-05git查看、創(chuàng)建、刪除、本地、遠程分支方法詳解
這篇文章主要介紹了git查看、創(chuàng)建、刪除、本地、遠程分支方法詳解,需要的朋友可以參考下2020-02-02