使用留一法和逻辑回归寻找最佳样本划分顺序
import numpy as np
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import LeaveOneOut
np.random.seed(10)
X = np.array([[1, 2], [3, 4], [5, 6], [7, 8], [9, 0]])
y = np.random.randint(0, 2, 5).reshape((5, 1))
best_acc = 0
best_order = None
for i in range(X.shape[0]):
loo = LeaveOneOut()
acc = 0
for train_index, test_index in loo.split(X):
X_train, X_test = X[train_index], X[test_index]
y_train, y_test = y[train_index], y[test_index]
model = LogisticRegression()
model.fit(X_train, y_train)
acc += model.score(X_test, y_test)
acc /= X.shape[0]
if acc > best_acc:
best_acc = acc
best_order = i
print('Best order: ', best_order)
print('Best accuracy: {:.2f}'.format(best_acc))
原文地址: https://www.cveoy.top/t/topic/mMAi 著作权归作者所有。请勿转载和采集!