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

yipeiwu_com6年前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+matplotlib实现礼盒柱状图实例代码

python+matplotlib实现礼盒柱状图实例代码

演示结果: 完整代码: import matplotlib.pyplot as plt import numpy as np from matplotlib.image impor...

python中sleep函数用法实例分析

本文实例讲述了python中sleep函数用法。分享给大家供大家参考。具体如下: Python中的sleep用来暂停线程执行,单位为秒 #----------------------...

python Qt5实现窗体跟踪鼠标移动

我就废话不多说了, 直接上代码吧! from PyQt5.Qt import * import sys class Window(QWidget): def __init...

python使用response.read()接收json数据的实例

如下所示: import json result = response.read() result.decode('utf-8') jsonData = json.loads(r...

python数据结构之链表的实例讲解

python数据结构之链表的实例讲解

在程序中,经常需要将⼀组(通常是同为某个类型的)数据元素作为整体 管理和使⽤,需要创建这种元素组,⽤变量记录它们,传进传出函数等。 ҳ...