【发布时间】:2020-09-21 07:59:45
【问题描述】:
对于我的逻辑回归模型,我想使用交叉验证(例如:5 倍)来评估最佳 L1 正则化强度,而不是单个测试训练集,如下所示:
from sklearn.model_selection import train_test_split
train_x, test_x, train_y, test_y = train_test_split(X_scaled,y, stratify=y, test_size=0.3,
random_state=2)
#Evaluate L1 regularization strengths for reducing features in final model
C = [10, 1, .1, 0.05,.01,.001] # As C decreases, more coefficients go to zero
for c in C:
clf = LogisticRegression(penalty='l1', C=c, solver='liblinear', class_weight="balanced")
clf.fit(train_x, train_y)
pred_y=clf.predict(test_x)
print("Model performance with Inverse Regularization Parameteter, C = 1/λ VALUE: ", c)
cr=metrics.classification_report(test_y, pred_y)
print(cr)
print('')
有人可以告诉我如何使用交叉验证(即,不重复上述代码 5 次和不同的随机状态)在 5 个不同的测试训练集上执行此操作吗?
【问题讨论】:
-
通过“distinct”,您是指将原始
X_scaled数据集随机拆分为不同(但重叠)的训练集,还是将您的x_train进一步细分为 5 个不包含重叠的集实例? -
我的意思是 X 和 y 中的 5 个随机分层分割。也就是说,由于样本是随机拆分的,因此允许重叠。
标签: python scikit-learn cross-validation lasso-regression