日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

Python實(shí)現(xiàn)K折交叉驗(yàn)證法的方法步驟

系統(tǒng) 1877 0

學(xué)習(xí)器在測(cè)試集上的誤差我們通常稱作“泛化誤差”。要想得到“泛化誤差”首先得將數(shù)據(jù)集劃分為訓(xùn)練集和測(cè)試集。那么怎么劃分呢?常用的方法有兩種,k折交叉驗(yàn)證法和自助法。介紹這兩種方法的資料有很多。下面是k折交叉驗(yàn)證法的python實(shí)現(xiàn)。

            
##一個(gè)簡(jiǎn)單的2折交叉驗(yàn)證
from sklearn.model_selection import KFold
import numpy as np
X=np.array([[1,2],[3,4],[1,3],[3,5]])
Y=np.array([1,2,3,4])
KF=KFold(n_splits=2) #建立4折交叉驗(yàn)證方法 查一下KFold函數(shù)的參數(shù)
for train_index,test_index in KF.split(X):
  print("TRAIN:",train_index,"TEST:",test_index)
  X_train,X_test=X[train_index],X[test_index]
  Y_train,Y_test=Y[train_index],Y[test_index]
  print(X_train,X_test)
  print(Y_train,Y_test)
#小結(jié):KFold這個(gè)包 劃分k折交叉驗(yàn)證的時(shí)候,是以TEST集的順序?yàn)橹鞯?,舉例來(lái)說(shuō),如果劃分4折交叉驗(yàn)證,那么TEST選取的順序?yàn)閇0].[1],[2],[3]。

#提升
import numpy as np
from sklearn.model_selection import KFold
#Sample=np.random.rand(50,15) #建立一個(gè)50行12列的隨機(jī)數(shù)組
Sam=np.array(np.random.randn(1000)) #1000個(gè)隨機(jī)數(shù)
New_sam=KFold(n_splits=5)
for train_index,test_index in New_sam.split(Sam): #對(duì)Sam數(shù)據(jù)建立5折交叉驗(yàn)證的劃分
#for test_index,train_index in New_sam.split(Sam): #默認(rèn)第一個(gè)參數(shù)是訓(xùn)練集,第二個(gè)參數(shù)是測(cè)試集
  #print(train_index,test_index)
  Sam_train,Sam_test=Sam[train_index],Sam[test_index]
  print('訓(xùn)練集數(shù)量:',Sam_train.shape,'測(cè)試集數(shù)量:',Sam_test.shape) #結(jié)果表明每次劃分的數(shù)量


#Stratified k-fold 按照百分比劃分?jǐn)?shù)據(jù)
from sklearn.model_selection import StratifiedKFold
import numpy as np
m=np.array([[1,2],[3,5],[2,4],[5,7],[3,4],[2,7]])
n=np.array([0,0,0,1,1,1])
skf=StratifiedKFold(n_splits=3)
for train_index,test_index in skf.split(m,n):
  print("train",train_index,"test",test_index)
  x_train,x_test=m[train_index],m[test_index]
#Stratified k-fold 按照百分比劃分?jǐn)?shù)據(jù)
from sklearn.model_selection import StratifiedKFold
import numpy as np
y1=np.array(range(10))
y2=np.array(range(20,30))
y3=np.array(np.random.randn(10))
m=np.append(y1,y2) #生成1000個(gè)隨機(jī)數(shù)
m1=np.append(m,y3)
n=[i//10 for i in range(30)] #生成25個(gè)重復(fù)數(shù)據(jù)

skf=StratifiedKFold(n_splits=5)
for train_index,test_index in skf.split(m1,n):
  print("train",train_index,"test",test_index)
  x_train,x_test=m1[train_index],m1[test_index]


          

Python中貌似沒(méi)有自助法(Bootstrap)現(xiàn)成的包,可能是因?yàn)樽灾ㄔ聿浑y,所以自主實(shí)現(xiàn)難度不大。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 商丘市| 盐津县| 峡江县| 县级市| 保德县| 新巴尔虎右旗| 新宾| 交城县| 陕西省| 房产| 望江县| 连州市| 安塞县| 新源县| 安阳县| 延吉市| 苏州市| 印江| 外汇| 淮阳县| 疏附县| 施秉县| 佛坪县| 卓资县| 公主岭市| 茌平县| 凭祥市| 静海县| 桐柏县| 阜新市| 时尚| 赤城县| 会同县| 武威市| 怀安县| 丹江口市| 盐边县| 晋州市| 南乐县| 湖口县| 宁城县|