可以使用train_test_split
函数来分割数据集为训练集和测试集。具体操作如下:
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
其中,X
为特征数据集,y
为标签数据集,test_size
为测试集所占比例(通常取0.2表示20%的数据作为测试集),random_state
为随机种子,可以保证每次运行时划分的训练集和测试集是一样的。分割后,X_train
和y_train
为训练集的特征和标签,X_test
和y_test
为测试集的特征和标签。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:Scikit-learn中怎么导入数据集