代码:
#可视化层次k折划分,k=3
#层次k折划分,每次选其中一折作为测试集,其它为训练集,确保在每次划分时不同类别的样本在测试集与训练集中比例划分,分层根据标签y实现,y为类别标签。
skf_cv=skf.split(X_oversampled,y_oversampled)
kk=[k*j for k,j in enumerate(y_oversampled) if j!=0]
k=1
plt.figure(figsize=(16,3))
for train_indices, test_indices in skf_cv:
plt.scatter(train_indices,[k]*len(train_indices),marker=".",c="r")
plt.scatter(test_indices,[k]*len(test_indices),marker="v",c="b")
print("训练集索引-%d"%k,train_indices)
print("测试集索引-%d"%k,test_indices)
k=k+1
plt.scatter(kk,[k]*len(kk),marker="+",c="b")
plt.yticks([1,2,3,4,5,6],["1","2","3","4","5","label"])
plt.ylabel("iteration")
plt.xlabel("index")
输出:
问题:1、这个图片应该怎么解释?为什么label中0和1的比例看起来是1:1?(该数据集不平衡率1:100)
2、怎么使用划分后的测试集和训练集?其代码是什么?(因为感觉看上去并没有像X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)一样输出了数据集)