用k折交叉验证LDA(LinearDiscriminantAnalysis)的有效性

数据集:Iris数据共3类,4维,150个数据。

from sklearn.datasets import load_iris
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
lda = LinearDiscriminantAnalysis()
from sklearn.model_selection import KFold, train_test_split, cross_val_score
from sklearn.preprocessing import StandardScaler

# 导入iris数据集并预处理
iris = load_iris()
X = iris.data
y = iris.target
std = StandardScaler()
X = std.fit_transform(X)

# 设置KFold参数
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=0)

# 调参,寻找相对最优
KF = KFold(n_splits=10, random_state=7, shuffle=True)
indexi = -1
indexj = -1
indexk = -1
bestscore = -1

for i in range(5, -18, -2):
    for j in range(-3, 18, 2):
        lda = LinearDiscriminantAnalysis(solver='svd', shrinkage=None, priors=None, n_components=None, store_covariance=False, tol=1e-4)
        score = cross_val_score(lda, X_train, y_train, cv=KF, scoring='accuracy', n_jobs=1).mean()

        if score > bestscore:
            indexi = i
            indexj = j
            bestscore = score
        print(indexi, indexj, bestscore)

# 加入最佳参数训练模型并查看结果
lda.fit(X_train, y_train)
print(lda.score(X_test, y_test))

你可能感兴趣的:(深度学习,机器学习,人工智能)