1.sklearn.model_selection.train_test_split隨機劃分訓練集和測試集html
函數原型:dom
X_train,X_test, y_train, y_test =cross_validation.train_test_split(train_data,train_target,test_size=0.4, random_state=0)函數
參數解釋: 學習
train_data:所要劃分的樣本特徵集測試
train_target:所要劃分的樣本結果spa
test_size:樣本佔比,若是是整數的話就是樣本的數量code
random_state:是隨機數的種子。htm
隨機數種子的意義在於,如何區分這個數據集,徹底是按照隨機數種子來決定,至於怎麼決定,咱們其實並不關心,好比你分了兩次,隨機種子都是0,那麼你獲得的兩次劃分也必定是同樣的。blog
1 fromsklearn.cross_validation import train_test_split 2 train= loan_data.iloc[0: 55596, :] 3 test= loan_data.iloc[55596:, :] 4 # 避免過擬合,採用交叉驗證,驗證集佔訓練集20%,固定隨機種子(random_state) 5 train_X,test_X, train_y, test_y = train_test_split(train, 6 target, 7 test_size = 0.2, 8 random_state = 0) 9 train_y= train_y['label'] 10 test_y= test_y['label']
、get
2. kl-fold 劃分
這個方法充分利用了因此樣本,但計算比較繁瑣,須要訓練k次,測試k次
import numpy as np #KFold from sklearn.model_selection import KFold X=np.array([[1,2],[3,4],[5,6],[7,8],[9,10],[11,12]]) y=np.array([1,2,3,4,5,6]) kf=KFold(n_splits=2) #分紅幾個組 kf.get_n_splits(X) print(kf) for train_index,test_index in kf.split(X): print("Train Index:",train_index,",Test Index:",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,y_train,y_test) #KFold(n_splits=2, random_state=None, shuffle=False) #Train Index: [3 4 5] ,Test Index: [0 1 2] #Train Index: [0 1 2] ,Test Index: [3 4 5]
more:http://www.cnblogs.com/nolonely/p/7007432.html