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设计】。

相关文章

Tesserocr库的正确安装方式

Tesserocr库的正确安装方式

win10,直接使用 pip install tesserocr 的命令 如果输出如下错误提示: tesserocr.cpp(596): fatal error C1083: 无法打...

Python中random模块生成随机数详解

Python中的random模块用于生成随机数。下面介绍一下random模块中最常用的几个函数。 random.random random.random()用于生成一个0到1的随机符点...

解决Python3下map函数的显示问题

map函数是Python里面比较重要的函数,设计灵感来自于函数式编程。Python官方文档中是这样解释map函数的: map(function, iterable, ...) Retu...

python tkinter界面居中显示的方法

由于tkinter没有直接提供居中显示的api,因此,要想将tk的对话框居中显示,需要用到tk自带的设定位置的方法geometry() nScreenWid, nScreenHei...

Python 过滤字符串的技巧,map与itertools.imap

具体的实例 我们需要在目录中遍历,包括子目录(哈哈),找出所有后缀为:rmvb ,avi ,pmp 的文件。(天哪?!你要干什么?这可是我的隐私啊~~) 复制代码 代码如下:import...