当前位置: 首页 > news >正文

「日拱一码」033 机器学习——严格划分

目录

简单随机划分(train_test_split)

分组划分(Group Splitting)

简单分组划分 (Group Splitting)

分层分组划分 (Stratified Group Splitting)

交叉验证法(Cross-Validation)

分组K 折交叉验证(GroupKFold)

留一组法(LeaveOneGroupOut)


简单随机划分(train_test_split)

简单随机分组通过随机分配组到训练集和测试集,确保同一组的数据不会同时出现在训练集和测试集中。这种方法简单易实现,但可能无法保证数据分布的平衡性

## 简单随机划分import pandas as pd
from sklearn.model_selection import train_test_splitdata = {'A': [1.0, 1.0, 2.0, 2.0, 1.0, 1.0],'B': [0.5, 0.5, 1.5, 1.5, 0.5, 0.5],'C': [1.0, 1.0, 2.0, 2.0, 1.0, 1.0],'D': [0.5, 0.5, 1.5, 1.5, 0.5, 0.5],'E': [0.1, 0.2, 0.1, 0.2, 0.3, 0.4],'y': [0, 1, 0, 1, 0, 1]
}
df = pd.DataFrame(data)# 定义分组特征
df['group'] = df[['A', 'B', 'C', 'D']].apply(lambda row: '_'.join(map(str, row)), axis=1)# 获取所有唯一组
unique_groups = df['group'].unique()# 划分组(而非划分行)
train_groups, test_groups = train_test_split(unique_groups,test_size=0.2,  # 测试集比例random_state=42,  # 随机种子stratify=df.groupby('group')['y'].first().values  # 按目标变量分层
)# 根据组划分数据
train_data = df[df['group'].isin(train_groups)]
test_data = df[df['group'].isin(test_groups)]print(f"训练集: {len(train_data)}条(来自{len(train_groups)}个组)")
print(f"测试集: {len(test_data)}条(来自{len(test_groups)}个组)")# 训练集: 2条(来自1个组)
# 测试集: 4条(来自1个组)

分组划分(Group Splitting)

简单分组划分 (Group Splitting)

简单分组划分的核心思想是将具有相同参数组合的数据划分为同一个组,然后基于这些组进行划分,确保训练集和测试集的组互斥

## 简单分组划分import pandas as pd
from sklearn.model_selection import GroupShuffleSplitdata = {'A': [1.0, 1.0, 2.0, 2.0, 1.0, 1.0],'B': [0.5, 0.5, 1.5, 1.5, 0.5, 0.5],'C': [1.0, 1.0, 2.0, 2.0, 1.0, 1.0],'D': [0.5, 0.5, 1.5, 1.5, 0.5, 0.5],'E': [0.1, 0.2, 0.1, 0.2, 0.3, 0.4],'y': [0, 1, 0, 1, 0, 1]
}
df = pd.DataFrame(data)# 定义分组特征
df['group'] = df[['A', 'B', 'C', 'D']].apply(lambda row: '_'.join(map(str, row)), axis=1)# 使用 GroupShuffleSplit 进行分组划分
gss = GroupShuffleSplit(n_splits=1, test_size=0.2, random_state=42)for train_index, test_index in gss.split(df, groups=df['group']):train = df.iloc[train_index]test = df.iloc[test_index]# 分离特征和目标变量
X_train, y_train = train.drop(columns=['y', 'group']), train['y']
X_test, y_test = test.drop(columns=['y', 'group']), test['y']print("训练集:")
print(X_train)
#      A    B    C    D    E
# 0  1.0  0.5  1.0  0.5  0.1
# 1  1.0  0.5  1.0  0.5  0.2
# 4  1.0  0.5  1.0  0.5  0.3
# 5  1.0  0.5  1.0  0.5  0.4
print("测试集:")
print(X_test)
# 2  2.0  1.5  2.0  1.5  0.1
# 3  2.0  1.5  2.0  1.5  0.2

分层分组划分 (Stratified Group Splitting)

分层分组在随机分组的基础上,确保训练集和测试集在某个关键特征(如目标变量  y )的分布上保持一致,能更好地保持数据的分布特性

## 分层分组划分from sklearn.model_selection import StratifiedShuffleSplit
import pandas as pddata = {'A': [1.0, 1.0, 2.0, 2.0, 1.0, 1.0, 1.5, 2, 1.5],'B': [0.5, 0.5, 1.5, 1.5, 0.5, 0.5, 1.5, 2, 1.5],'C': [1.0, 1.0, 2.0, 2.0, 1.0, 1.0, 1.5, 2, 1.5],'D': [0.5, 0.5, 1.5, 1.5, 0.5, 0.5, 1.5, 2, 1.5],'E': [0.1, 0.2, 0.1, 0.2, 0.3, 0.4, 1.5, 2, 1.5],'y': [0, 1, 0, 1, 0, 1, 2, 2, 1]
}
df = pd.DataFrame(data)# 定义分组特征df['group_id'] = df[['A', 'B', 'C', 'D']].astype(str).apply('_'.join, axis=1)group_targets = df.groupby('group_id')['y'].first()
unique_groups = group_targets.index.values# 使用 StratifiedShuffleSplit 进行分层分组划分
sss = StratifiedShuffleSplit(n_splits=1, test_size=0.3, random_state=42)for train_groups_idx, test_groups_idx in sss.split(np.zeros(len(unique_groups)), group_targets):train_groups = unique_groups[train_groups_idx]test_groups = unique_groups[test_groups_idx]train_df = df[df['group_id'].isin(train_groups)]test_df = df[df['group_id'].isin(test_groups)]# 分离特征和目标变量
X_train, y_train = train.drop(columns=['y', 'group']), train['y']
X_test, y_test = test.drop(columns=['y', 'group']), test['y']print("训练集:")
print(X_train)
#      A    B    C    D    E
# 0  1.0  0.5  1.0  0.5  0.1
# 1  1.0  0.5  1.0  0.5  0.2
# 4  1.0  0.5  1.0  0.5  0.3
# 5  1.0  0.5  1.0  0.5  0.4
print("测试集:")
print(X_test)
#      A    B    C    D    E
# 2  2.0  1.5  2.0  1.5  0.1
# 3  2.0  1.5  2.0  1.5  0.2

交叉验证法(Cross-Validation)

交叉验证法通过将数据划分为多个子集(折),每次使用一个子集作为测试集,其余作为训练集,重复多次以评估模型的性能。这种方法充分利用数据,结果更稳定

分组K 折交叉验证(GroupKFold)

GroupKFold  是一种分组 K 折交叉验证方法,它确保每个组(group)的数据完全独立于其他组。这种方法非常适合处理具有明确分组特征的数据

## 分组K折交叉验证
import pandas as pd
from sklearn.model_selection import GroupKFolddata = {'A': [1.0, 1.0, 2.0, 2.0, 1.0, 1.0],'B': [0.5, 0.5, 1.5, 1.5, 0.5, 0.5],'C': [1.0, 1.0, 2.0, 2.0, 1.0, 1.0],'D': [0.5, 0.5, 1.5, 1.5, 0.5, 0.5],'E': [0.1, 0.2, 0.1, 0.2, 0.3, 0.4],'y': [0, 1, 0, 1, 0, 1]
}
df = pd.DataFrame(data)# 定义分组特征
df['group'] = df[['A', 'B', 'C', 'D']].apply(lambda row: '_'.join(map(str, row)), axis=1)
# 获取分组标识符
groups = df['group'].values# 使用 GroupKFold 进行分组 K 折交叉验证
gkf = GroupKFold(n_splits=2)  # 假设我们使用 2 折交叉验证for train_index, test_index in gkf.split(df, groups=groups):train = df.iloc[train_index]test = df.iloc[test_index]X_train, y_train = train.drop(columns=['y', 'group']), train['y']X_test, y_test = test.drop(columns=['y', 'group']), test['y']print("训练集:")print(X_train)     
#     A    B    C    D    E
# 0  1.0  0.5  1.0  0.5  0.1
# 1  1.0  0.5  1.0  0.5  0.2
# 4  1.0  0.5  1.0  0.5  0.3
# 5  1.0  0.5  1.0  0.5  0.4print("测试集:")print(X_test)      
#     A    B    C    D    E
# 2  2.0  1.5  2.0  1.5  0.1
# 3  2.0  1.5  2.0  1.5  0.2

留一组法(LeaveOneGroupOut)

## 留一组法import pandas as pd
from sklearn.model_selection import LeaveOneGroupOutdata = {'A': [1.0, 1.0, 2.0, 2.0, 1.0, 1.0],'B': [0.5, 0.5, 1.5, 1.5, 0.5, 0.5],'C': [1.0, 1.0, 2.0, 2.0, 1.0, 1.0],'D': [0.5, 0.5, 1.5, 1.5, 0.5, 0.5],'E': [0.1, 0.2, 0.1, 0.2, 0.3, 0.4],'y': [0, 1, 0, 1, 0, 1]
}
df = pd.DataFrame(data)# 定义分组特征
df['group'] = df[['A', 'B', 'C', 'D']].apply(lambda row: '_'.join(map(str, row)), axis=1)
# 获取分组标识符
groups = df['group'].values# 使用 LeaveOneGroupOut 进行分组划分
logo = LeaveOneGroupOut()for train_index, test_index in logo.split(df, groups=groups):train = df.iloc[train_index]test = df.iloc[test_index]X_train, y_train = train.drop(columns=['y', 'group']), train['y']X_test, y_test = test.drop(columns=['y', 'group']), test['y']print("训练集:")print(X_train)      
#     A    B    C    D    E
# 0  1.0  0.5  1.0  0.5  0.1
# 1  1.0  0.5  1.0  0.5  0.2
# 4  1.0  0.5  1.0  0.5  0.3
# 5  1.0  0.5  1.0  0.5  0.4print("测试集:")print(X_test) 
#     A    B    C    D    E
# 2  2.0  1.5  2.0  1.5  0.1
# 3  2.0  1.5  2.0  1.5  0.2
http://www.dtcms.com/a/290154.html

相关文章:

  • 【VASP】VASP 机器学习力场(MLFF)实战
  • 机器学习对词法分析、句法分析、浅层语义分析的积极影响
  • Taro 本地存储 API 详解与实用指南
  • 京东疯狂投资具身智能:众擎机器人+千寻智能+逐际动力 | AI早报
  • 从“被动照料”到“主动预防”:智慧养老定义的养老4.0时代
  • 迁移科技3D视觉系统:赋能机器人上下料,开启智能制造高效新纪元
  • Nacos中feign.FeignException$BadGateway: [502 Bad Gateway]
  • 第15次:商品搜索
  • Laravel 系统版本查看及artisan管理员密码找回方法针对各个版本通用方法及原理-优雅草卓伊凡
  • Java-78 深入浅出 RPC Dubbo 负载均衡全解析:策略、配置与自定义实现实战
  • LeetCode - 3274. Check if Two Chessboard Squares Have the Same Color
  • 【Semi笔记】Semisupervised Change Detection With Feature-Prediction Alignment
  • .NET SDK 9.0.200引入对SLNX解决方案文件的支持
  • compser json和lock的作用区别
  • 【qml-3】qml与c++交互第二次尝试(类型方式)
  • 【C++11】哈希表与无序容器:从概念到应用
  • ElasticSearch:不停机更新索引类型(未验证)
  • git switch
  • (LeetCode 面试经典 150 题) 219. 存在重复元素 II (哈希表)
  • taro微信小程序的tsconfig.json文件说明
  • 自动化与安全 - 将 Terraform 集成到 CI/CD
  • 编译支持cuda硬件加速的ffmpeg
  • 数据库和数据仓库的区别
  • day27 力扣332.重新安排行程 力扣51. N皇后 力扣37. 解数独 力扣455.分发饼干 力扣376. 摆动序列 力扣53. 最大子序和
  • 云原生周刊:K8s 中的后量子密码学
  • OpenCV计算机视觉实战(16)——图像分割技术
  • 微服务的编程测评系统-身份认证-管理员登录前端
  • LeetCode|Day21|204. 计数质数|Python刷题笔记
  • 【黑马SpringCloud微服务开发与实战】(四)微服务02
  • 随笔20250721 PostgreSQL实体类生成器