欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

用pandas劃分?jǐn)?shù)據(jù)集實(shí)現(xiàn)訓(xùn)練集和測試集

 更新時間:2020年07月20日 11:02:10   作者:MDbabyface  
這篇文章主要介紹了用pandas劃分?jǐn)?shù)據(jù)集實(shí)現(xiàn)訓(xùn)練集和測試集,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

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)文章

最新評論