python将数据分类并计数_python数据集划分

python将数据分类并计数_python数据集划分在 Python 中 划分数据集通常有以下几种方法 1 使用 train test split 函数 来自 sklearn model selection 模块 pythonfrom sklearn model selection import train test splitX train X test y train y test train test split X

在Python中,划分数据集通常有以下几种方法:

1. 使用`train_test_split`函数(来自`sklearn.model_selection`模块):

python

from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

这里`X`是特征矩阵,`y`是目标向量,`test_size`参数指定测试集所占的比例,`random_state`参数用于设置随机种子。

2. 手动划分数据集:

python

import os

import shutil

def move_files(train_img_dir, train_mask_dir, test_size=0.2):

img_path_dir = os.listdir(train_img_dir)

filenumber = len(img_path_dir)

split_index = int(filenumber * test_size)

for i in range(split_index):

shutil.move(os.path.join(train_img_dir, img_path_dir[i]), os.path.join(train_mask_dir, img_path_dir[i]))

3. 使用`cross_validation`模块中的`train_test_split`函数:

python

from sklearn.cross_validation import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)

4. 划分数据集为训练集、验证集和测试集:

python

import os

import shutil

def split_data(file_path, new_file_path, split_rate):

data_class = [cla for cla in os.listdir(file_path)]

train_path = os.path.join(new_file_path, 'train')

val_path = os.path.join(new_file_path, 'val')

test_path = os.path.join(new_file_path, 'test')

for cla in data_class:

mkfile(os.path.join(train_path, cla))

mkfile(os.path.join(val_path, cla))

mkfile(os.path.join(test_path, cla))

5. 使用`random`模块进行手动划分:

python

import random

def create_image_lists(testing_percentage, validation_percentage):

result = {}

获取所有图片列表

all_files = glob.glob('path_to_image_folder/*')

随机划分图片列表

random.shuffle(all_files)

split_index = int(len(all_files) * (1 - testing_percentage - validation_percentage))

train_files = all_files[:split_index]

val_files = all_files[split_index:split_index + int(len(all_files) * validation_percentage)]

test_files = all_files[split_index + int(len(all_files) * validation_percentage):]

将划分结果存储到字典中

for file in train_files:

根据文件名确定类别

class_name = os.path.basename(os.path.dirname(file))

if class_name not in result:

result[class_name] = {'train': [], 'val': [], 'test': []}

result[class_name]['train'].append(file)

for file in val_files:

class_name = os.path.basename(os.path.dirname(file))

result[class_name]['val'].append(file)

for file in test_files:

class_name = os.path.basename(os.path.dirname(file))

result[class_name]['test'].append(file)

return result

请根据您的具体需求选择合适的方法进行数据集划分。

编程小号
上一篇 2026-05-14 16:12
下一篇 2026-05-14 16:08

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://sigusoft.com/bj/41650.html