python - 如何使用python根据输入值划分数组
问题描述
我们假设 KNN 中的折叠值为 N,我们需要将数组分成 N 等份,并且对于折叠值的每次迭代,我们需要划分训练并测试这样的方式
example :
fold is 5
1. In First iteration It Consider last means 5th part as test data and rest train data
2. In Second iteration It Consider second last means 4th part as test data and rest train data
3. In third iteration It Consider third last means 3rd part as test data and rest train data
... so on
5. In Firth iteration It Consider first means 1st part as test data and rest train data
我们如何在 Python 中实现这一点你能解释一下吗?
解决方案
我认为您需要 KFold https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.KFold.html
# you can declare number of splits here
kfold = model_selection.KFold(n_splits=5, random_state=42)
# your model goes here.
model = NearestNeighbors(n_neighbors=2, algorithm='ball_tree')
# this will fit your model 5 times and use 1/5 as test data and 4/5 as training data
results = model_selection.cross_val_score(model, X_train, y_train, cv=kfold)
推荐阅读
- git - 无法将文件夹推送到 github
- elasticsearch - Elasticsearch Query:通过比较值列表来选择文档(golang)
- java - 使用 servlet 启用禁用字段集
- xamarin - ReceivedRemoteNotification 和 DidReceiveRemoteNotification 永远不会被调用
- kubernetes - 在 kubernetes 中,POD 内的容器是否有自己的 IP 地址?
- javascript - 如何使用固定的引导导航栏显示不同的引导页面
- php - Laravel 比较数据透视表中的两个属性
- c# - 为什么 C# 发布版本包含调试信息
- git - 如何使用 SSH 添加 git 子模块并在一行中输入密码?
- jquery - 如何让 cookieConsent 导航在移动设备上自动扩展?