Python分割训练集和测试集的方法示例

yipeiwu_com5年前Python基础

数据集介绍

使用数据集Wine,来自UCI  。包括178条样本,13个特征。

import pandas as pd
import numpy as np

df_wine = pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/wine/wine.data', header=None)
df_wine.columns = ['Class label', 'Alcohol',
              'Malic acid', 'Ash',
              'Alcalinity of ash', 'Magnesium',
              'Total phenols', 'Flavanoids',
              'Nonflavanoid phenols',
              'Proanthocyanins',
              'Color intensity', 'Hue',
              'OD280/OD315 of diluted wines',
              'Proline']

分割训练集和测试集

随机分割

分为训练集和测试集

方法:使用scikit-learn中model_selection子模块的train_test_split函数

from sklearn.model_selection import train_test_split

X, y = df_wine.ix[:, 1:].values, df_wine.ix[:, 0].values
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=0)#随机选择25%作为测试集,剩余作为训练集

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python控制台中实现进度条功能

python控制台中实现进度条功能

我们大多数人都希望写一些简单的python脚本的同时都想能够在程序运行的过程中实现进度条的功能以便查看程序运行的速度或者进度。今天就和大家探讨这个问题:如何在python控制台中实现进度...

pip命令无法使用的解决方法

今天在学习Python时需要安装Requests    使用命令:pip install requests    &...

用Python代码来绘制彭罗斯点阵的教程

用Python代码来绘制彭罗斯点阵的教程

这里是显示彭罗斯点阵的Python的脚本。是的,这是可以运行的有效Phython代码。 译注:彭罗斯点阵,物理学术语。上世纪70年代英国数学家彭罗斯第一次提出了这个概念,称为彭罗斯点阵(...

python批量复制图片到另一个文件夹

本文实例为大家分享了python批量复制图片到文件夹的具体代码,供大家参考,具体内容如下 直接上代码: # -*- coding: utf-8 -*- """ Created on...

Python2和Python3中urllib库中urlencode的使用注意事项

前言 在Python中,我们通常使用urllib中的urlencode方法将字典编码,用于提交数据给url等操作,但是在Python2和Python3中urllib模块中所提供的urle...