用pandas劃分?jǐn)?shù)據(jù)集實(shí)現(xiàn)訓(xùn)練集和測試集
1、使用model_select子模塊中的train_test_split函數(shù)進(jìn)行劃分
數(shù)據(jù):使用kaggle上Titanic數(shù)據(jù)集
劃分方法:隨機(jī)劃分
# 導(dǎo)入pandas模塊,sklearn中model_select模塊 import pandas as pd from sklearn.model_select import train_test_split # 讀取數(shù)據(jù) data = pd.read_csv('.../titanic_dataset/train.csv') # 將特征劃分到 X 中,標(biāo)簽劃分到 Y 中 x = data.iloc[:, 2:] y = data.loc['Survived'] # 使用train_test_split函數(shù)劃分?jǐn)?shù)據(jù)集(訓(xùn)練集占75%,測試集占25%)
x_train, x_test, y_train,y_test = train_test_split(x, y, test_size=0.25, ramdon_state=0)
缺點(diǎn):1、數(shù)據(jù)浪費(fèi)嚴(yán)重,只對部分?jǐn)?shù)據(jù)進(jìn)行了驗(yàn)證
2、容易過擬合
2、k折交叉驗(yàn)證(kfold)
原理:將數(shù)據(jù)集劃分成n個不相交的子集,每次選擇其中一個作為測試集,剩余n-1個子集作為 訓(xùn)練集,共生成 n 組數(shù)據(jù)
使用方法:sklearn.model_select.KFold(n_splits=5,shuffle=False,random_state=0)
參數(shù)說明:n_splits:數(shù)據(jù)集劃分的份數(shù),
shuffle:每次劃分前是否重新洗牌 ,False表示劃分前不洗牌,每次劃分結(jié)果一樣,True表示劃分前洗牌,每次劃分結(jié)果不同
random_state:隨機(jī)種子數(shù)
(1)shuffle=False 情況下數(shù)據(jù)劃分情況
# 不洗牌模式下數(shù)據(jù)劃分情況 import numpy as np from sklearn.model_selection import KFold x = np.arange(46).reshape(23,2) kf = KFold(n_splits=5,shuffle=False) for train_index, test_index in kf.split(x): print(train_index,test_index) [ 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22] [0 1 2 3 4] [ 0 1 2 3 4 10 11 12 13 14 15 16 17 18 19 20 21 22] [5 6 7 8 9] [ 0 1 2 3 4 5 6 7 8 9 15 16 17 18 19 20 21 22] [10 11 12 13 14] [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 19 20 21 22] [15 16 17 18] [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18] [19 20 21 22]
(2)shuffle=True 情況下數(shù)據(jù)劃分情況
import numpy as np from sklearn.model_selection import KFold x = np.arange(46).reshape(23,2) kf = KFold(n_splits=5,shuffle=True) for train_index, test_index in kf.split(x): print(train_index,test_index) [ 0 3 4 5 6 7 8 9 10 11 12 14 15 16 17 19 20 21] [ 1 2 13 18 22] [ 0 1 2 3 5 6 7 10 11 13 15 16 17 18 19 20 21 22] [ 4 8 9 12 14] [ 0 1 2 3 4 7 8 9 10 12 13 14 15 16 17 18 19 22] [ 5 6 11 20 21] [ 1 2 3 4 5 6 8 9 10 11 12 13 14 15 18 19 20 21 22] [ 0 7 16 17] [ 0 1 2 4 5 6 7 8 9 11 12 13 14 16 17 18 20 21 22] [ 3 10 15 19]
總結(jié):從數(shù)據(jù)中可以看出shuffle=True情況下數(shù)據(jù)的劃分是打亂的,而shuffle=False情況下數(shù)據(jù)的劃分是有序的
到此這篇關(guān)于用pandas劃分?jǐn)?shù)據(jù)集實(shí)現(xiàn)訓(xùn)練集和測試集的文章就介紹到這了,更多相關(guān)pandas劃分?jǐn)?shù)據(jù)集 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python實(shí)現(xiàn)多進(jìn)程代碼示例
Python中大部分情況下都需要使用多進(jìn)程,Python中提供了multiprocessing這個包實(shí)現(xiàn)多進(jìn)程。multiprocessing支持子進(jìn)程、進(jìn)程間的同步與通信,本文就詳細(xì)的介紹一下2018-10-10Python使用reportlab將目錄下所有的文本文件打印成pdf的方法
這篇文章主要介紹了Python使用reportlab將目錄下所有的文本文件打印成pdf的方法,涉及reportlab模塊操作pdf文件的相關(guān)技巧,需要的朋友可以參考下2015-05-05django admin 根據(jù)choice字段選擇的不同來顯示不同的頁面方式
這篇文章主要介紹了django admin 根據(jù)choice字段選擇的不同來顯示不同的頁面方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05Python讀取含url圖片鏈接的txt文檔方法小結(jié)
這篇文章主要為大家詳細(xì)介紹了三種Python讀取含url圖片鏈接的txt文檔方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-04-04Python3實(shí)現(xiàn)配置文件差異對比腳本
這篇文章主要介紹了Python3實(shí)現(xiàn)配置文件差異對比腳本,本文通過案例場景分析給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-11-11python基礎(chǔ)入門學(xué)習(xí)筆記(Python環(huán)境搭建)
這篇文章主要介紹了python基礎(chǔ)入門學(xué)習(xí)筆記,這是開啟學(xué)習(xí)python基礎(chǔ)知識的第一篇,夯實(shí)Python基礎(chǔ),才能走的更遠(yuǎn),感興趣的小伙伴們可以參考一下2016-01-01