模型評估:數據切分
順序切分
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.4, random_state=0)
ShuffleSplit
from sklearn.model_selection import ShuffleSplit
sample = pd.DataFrame()
rs = ShuffleSplit(n_splits=5, test_size=0.2, random_state=0)
for train_indices, test_indices in rs.split(sample):
#共循環5次
#每次循環拿到打亂順序的訓練集和驗證集的索引值,訓練集數量為80%,測試集數量為20%
這種數據切分方式經常與CV結合使用
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import ShuffleSplit
cv = ShuffleSplit(n_splits=3, test_size=0.3, random_state=0)
cross_val_score(clf, iris.data, iris.target, cv=cv)
GroupShuffleSplit
from sklearn.model_selection import GroupShuffleSplit
sample = pd.DataFrame()
rs = GroupShuffleSplit(n_splits=4, test_size=0.25, random_state=0)
#獲取組標記
_, driver_indices = np.unique(np.array(driver_ids), return_inverse=True)
driver_indices = [0 0 1 1 1 2 2 2 2 3]
#這裏是按照groups所給列的類別進行數據集的劃分
for train_indices, test_indices in rs.split(x_train_names_all, y_train_labels_all, groups=driver_indices):
#共循環4次
#假設groups列中有4個類別,每次循環,從中選擇75%的類別數,屬於這75%的類別數的所有數據集作為訓練集,其余
#為驗證集。
模型評估:數據切分