随机划分训练集和测试集
文章目录
- 前言
- 一、无序索引方法
- 1. matlab代码
- 2. python代码
- 二、函数实现方法
- 1. 利用python库中的cross_validation.train_test_split方法
- 2. 利用python库中的KFold方法(K折交叉验证方法)
前言
在机器学习中训练模型时,经常需要按比例对数据集随机划分成训练集和测试集,本文总结了基于matlab和python的划分训练集和测试集的常用方法,仅供大家参考。
一、无序索引方法
1. matlab代码
% 确定随机种子,便于结果复现
rand('seed', 42);
% 生成和样本个数等长的无序索引序列
idx = randperm(length); % length即为样本的总数,idx为生成的无序索引
% 选取80%作为训练集
train_data = data(idx(1:0.8*length), :); % data为样本特征,格式为:样本数*特征数
train_labels = labels(idx(1:0.8*length), 1); % labels为样本标签,的格式为:样本数*1
% 剩余20%作为测试集
test_data = data( idx(0.8*length+1:end), :);
test_labels = labels( idx(0.8*length+1:end), 1);
2. python代码
length = data.shape[1]
# length = len(labels)
# 生成和样本个数等长的无序索引序列
index = randperm(length) # length即为样本的总数,idx为生成的无序索引
# 选取80%作为训练集(假设样本共100个)
idx1 = index[:80]
train_data = data[idx1] # data为样本特征,格式为:样本数*特征数
train_labels = labels[idx1] # labels为样本标签,的格式为:样本数*1
# 剩余20%作为测试集
idx2 = index[80:]
test_data = data[idx2]
test_labels = labels[idx2]
二、函数实现方法
1. 利用python库中的cross_validation.train_test_split方法
from numpy import random
import numpy as np
from sklearn import cross_validation# 其中data为数据的特征矩阵,labels为标签,test_siz为测试的比例
x_train, x_test, y_train, y_test = cross_validation.train_test_split(data, labels, test_size=0.2, random_state=0)
2. 利用python库中的KFold方法(K折交叉验证方法)
from sklearn.model_selection import KFold
from sklearn import svm# 其中data为数据的特征矩阵,labels为标签,train_index, test_index分别为训练集和测试集的样本索引
kf = KFold(n_splits=5)
for train_index, test_index in kf.split(data):x_train, x_test = data[train_index], data[test_index]y_train, y_test = labels[train_index], labels[test_index]clf = svm.SVC(C=0.2, kernel='linear', decision_function_shape='ovo')clf.fit(x_train, y_train)pred_acc = clf.score(x_test, y_test)
随机划分训练集和测试集相关推荐
- Sklearn-train_test_split随机划分训练集和测试集
sklearn.model_selection.train_test_split随机划分训练集和测试集 官网文档:http://scikit-learn.org/stable/modules/gene ...
- sklearn.model_selection.train_test_split随机划分训练集和测试集
1 函数用途 train_test_split()是交叉验证中常用的函数,功能是将数组或矩阵按比例随机划分为训练集和测试集,使用方法为: X_train,X_test, y_train, y_test ...
- train_test_split(),随机划分训练集和测试集的函数
sklearn.model_selection.train_test_split() from sklearn.model_selection import train_test_split x_tr ...
- idea2020.2中@test是怎么测试的_Sklearn 划分训练集和测试集
[从零开始学机器学习第 03 篇] 摘要:手写 Sklearn 的 train_test_split 函数. 之前两篇文章以酒吧的红酒故事引出了 kNN 分类算法,根据已倒好的酒(样本),预测新倒的酒 ...
- python机器学习 train_test_split()函数用法解析及示例 划分训练集和测试集 以鸢尾数据为例 入门级讲解
文章目录 train_test_split()用法 获取数据 划分训练集和测试集 完整代码脚手架 train_test_split()用法 python机器学习中常用 train_test_split ...
- 31,32,33_过拟合、欠拟合的概念、L2正则化,Pytorch过拟合欠拟合,交叉验证-Train-Val-Test划分,划分训练集和测试集,K-fold,Regularization
1.26.过拟合.欠拟合及其解决方案 1.26.1.过拟合.欠拟合的概念 1.26.1.1.训练误差和泛化误差 1.26.1.2.验证数据集与K-fold验证 1.26.1.3.过拟合和欠拟合 1.2 ...
- python按设定比例划分训练集和测试集代码
# *_*coding: utf-8 *_* # Author --LiMing--import os import random import shutil import timedef copyF ...
- 5.sklearn之转换器(划分训练集和测试集、以及标准化、归一化数据会用transform,独热编码也会用到)
文章目录 1. 什么是转换器? 2. 测试集和训练集 2.1 训练集 .测试集.验证集 2.2 拆分训练集测试集有个问题 2.3 代码 3. 标准化 3.1 上离差标准化代码(举一反三就好了,其他几个 ...
- python划分训练集和测试集_杨涛的Python机器学习3:单特征与多特征、训练集与测试集,杨桃...
本人CSDN博客专栏:https://blog.csdn.net/yty_7 Github地址:https://github.com/yot777/ 单特征与多特征 在上一节标签和特征的示例中,我们使 ...
- [机器学习笔记] 将数据拆分成训练集和测试集的几种方法
问题描述: 一般情况下, 我们习惯将原始数据中的80% 作为训练集, 20% 作为测试集(当数据量足够大的时候,也可以将10% 作为测试集. 数据量较小时,如果每次都是随机划分训练集,执行多次训练后, ...
最新文章
- AttributeError: h5py.h5.H5PYConfig‘ has no attribute ‘__reduce_cython__‘
- python第三方库安装-python的第三方库安装
- 同样的电器,为什么官网能比实体店的价格便宜那么多?
- 阿里腾讯面试梳理个人成长经历分享
- 天池 在线编程 数组游戏
- AngularJs入门学习
- jsp 跳到servlet路径_请问如何从jsp中跳到servlet中?
- 微服务下的应用架构设计(COLA架构)
- python预处理后变量情况描述_【数据预处理】python旅游网站数据清洗实践
- 在ASP.NET中创建安全的web站点
- TVS管、稳压管、肖特基二极管
- HTML在列表中加图片,HTML + JS 列表显示图片
- 前沿讲座之计算机图形学
- 架构师日记——VCL介绍和使用
- 基于android的影音设计,基于Android系统的影音播放器设 - 音响技术 - 电子发烧友网...
- convexHull实现
- python交通调查数据处理_GitHub - unlimitbladeworks/traffic-monitor: 基于高德地图的交通数据分析...
- MATLAB群智能开源第十五期-闪电优化算法(LAPO)
- c#语言float转换int,关于C#:在x86上将float转换为int的最快方法是什么
- SpringBoot使用LUA解决Redis库存遗留问题