import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import os
from matplotlib.font_manager import FontProperties
fonts=FontProperties(fname=os.path.join(os.getcwd(),"data","方正粗黑宋简体.ttf"))
from sklearn import metrics
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_iris
Iris=load_iris()print(Iris.data.shape)print(Iris.target)'''K折交叉验证''''''k-fold随机抽样'''
from sklearn.model_selection import KFold
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
Ifkf=KFold(n_splits=10,random_state=2)
LDA_clf=LinearDiscriminantAnalysis(n_components=2)
scores=[]for ii,(train_index,test_index) in enumerate(Ifkf.split(Iris.data)):
LDA_clf.fit(Iris.data[train_index],Iris.target[train_index])
prey=LDA_clf.predict(Iris.data[test_index])
acc=metrics.accuracy_score(Iris.target[test_index],prey)print("Fold:",ii+1,"Acc:",np.round(acc,4))
scores.append(acc)print("平均Acc:",np.mean(scores))'''Stratified k_fold分层交叉验证'''
from sklearn.model_selection import StratifiedKFold
Skf_ir=StratifiedKFold(n_splits=3,random_state=2)
scores=[]for ii,(train_index,test_index) in enumerate(Skf_ir.split(Iris.data,Iris.target)):
LDA_clf.fit(Iris.data[train_index],Iris.target[train_index])
prey=LDA_clf.predict(Iris.data[test_index])
acc=metrics.accuracy_score(Iris.target[test_index],prey)print("Fold:",ii+1,"Acc:",np.round(acc,4))
scores.append(acc)print("平均Acc:",np.mean(scores))
K折线交叉验证import numpy as npimport pandas as pdimport matplotlib.pyplot as pltimport seaborn as snsimport osfrom matplotlib.font_manager import FontPropertiesfonts=FontProperties(fname=os.path.join(os.getcwd(),"data","方正粗黑宋简体.ttf"))from sklearn import