数据集介绍
使用数据集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%作为测试集,剩余作为训练集