简体   繁体   English

如何使用python根据输入值划分数组

[英]How to divide the array based on Input value using python

We have the lets say fold value in KNN is N we need to divide the array in N equal part and for each iteration of fold value we need to divide the train and test such way that我们可以说 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

How we can achieve this in Python Can you please explain this .我们如何在 Python 中实现这一点你能解释一下吗?

I think you need the KFold https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.KFold.html我认为你需要 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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python-根据输入从数组中减去值 - Python - Subtracting a value from an array based on input 输入一个 function,将数组分成等份并打印具有最高值的子数组 Python 3.6 - Take input in a function, divide the array in equal parts and print child array with highest value Python 3.6 使用python如何向图像/数组添加足够的行和列,以便我可以始终将输入图像分成所需形状的片段? - Using python how to add enough rows and columns to an image/array, so that I can always divide the input image in to segments of required shape? 如何与 python/pandas 交叉和合并文件以根据原始输入文件将重叠划分为子区域? - How to intersect and merge files with python/pandas to divide overlap into subregions based on original input file? 如何在不使用 // 的情况下划分 python? - How to divide in python without using //? Python-使用取决于输入值的数组索引 - Python - Using an array index that depends on an input value 根据值划分行 - Divide rows based on value Python(熊猫):如何根据列中的值将每一行除以“绝对”行 - Python (pandas): How to divide each row by an 'absolute' row based on value in column 根据python中的bin大小将数组划分为多个(单个)数组 - Divide a array into multiple (individual) arrays based on a bin size in python 如何使用 pandas 根据某个列中的值合并/划分数据框中的行? - How to consolidate/divide rows within a data frame based on a value within a certain column using pandas?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM