python - 即使设置了种子,LogisticRegressionCV 也会给出不同的答案
问题描述
我有一个在 Logistic RegressionCV 中实现 KFold 的程序。我已经设置了一个种子并在 KFOLD 和 LogisticRegressionCV 中使用它。即使设置了种子,每次重新运行内核时,我都会对所有指标进行不同的测量。这是代码:
rs = random.seed(42)
X_train, X_test, y_train, y_test = train_test_split(X_smt, y_smt, test_size=0.1,
random_state=42)
kf = KFold(n_splits=15, shuffle=flase, random_state=42)
logistic = LogisticRegressionCV(Cs=2, fit_intercept=True, cv=kf, verbose =1, random_state=42)
logistic.fit(X_train, y_train)
print("Train Coefficient:" , logistic.coef_) #weights of each feature
print("Train Intercept:" , logistic.intercept_) #value of intercept
print("\n \n \n ")
logistic.predict(X_test)
test_precision = metrics.precision_score(y_test, logistic.predict(X_test))
test_avg_precision = metrics.average_precision_score(y_test, logistic.predict(X_test))
这可能是什么原因,以及是否有一个简单的解决方案。
解决方案
根据此处的SKlearn文档:Randomized CV splitters may return different results for each call of split. You can make the results identical by setting random_state to an integer
。
但是,它可能只设置折叠随机状态而不是洗牌。尝试设置shuffle=False
,看看你是否仍然得到不同的结果。
推荐阅读
- java - Jacoco Maven 覆盖率报告始终为 0%
- java - 如何从按钮获取文本以编辑文本?
- python - 在通过套接字建立连接时,python 会向 Web 服务器发送有关主机的哪些详细信息?
- android - Android房间从表中选择不同的table.calender
- c++ - 在 C++ 中序列化/反序列化位域结构
- java - 如何创建对象的副本?
- javascript - 根据动态传递的月份设置上一个
- react-native - React Native 中的渲染对
- javascript - 为我的石头剪刀布 JavaScript 游戏实现键盘事件监听器
- vb.net - vb.net帮助,如何跳过未经许可删除文件