Package: kennard-stone
Kennard StoneWhat is this?This is an algorithm for evenly partitioning data in ascikit-learn-like interface.
(SeeReferencesfor details of the algorithm.)How to installPyPIpipinstallkennard-stoneThe project site ishere.Anacondacondainstall-cconda-forgekennard-stoneThe project site ishere.You neednumpy>=1.20andscikit-learnto run.How to useYou can use them likescikit-learn.Seeexamplesfor details.In the following,Xdenotes an arbitrary explanatory variable andyan arbitrary objective variable.
And,estimatorindicates an arbitrary prediction model that conforms to scikit-learn.train_test_splitkennard_stonefromkennard_stoneimporttrain_test_splitX_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.2)scikit-learnfromsklearn.model_selectionimporttrain_test_splitX_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.2,random_state=334)KFoldkennard_stonefromkennard_stoneimportKFold# Always shuffled and uniquely determined for a data set.kf=KFold(n_splits=5)fori_train,i_testinkf.split(X,y):X_train=X[i_train]y_train=y[i_train]X_test=X[i_test]y_test=y[i_test]scikit-learnfromsklearn.model_selectionimportKFoldkf=KFold(n_splits=5,shuffle=True,random_state=334)fori_train,i_testinkf.split(X,y):X_train=X[i_train]y_train=y[i_train]X_test=X[i_test]y_test=y[i_test]Other usagesIf you ever specifycvin scikit-learn, you can assignKFoldobjects to it and apply it to various functions.An example iscross_validate.kennard_stonefromkennard_stoneimportKFoldfromsklearn.model_selectionimportcross_validatekf=KFold(n_splits=5)print(cross_validate(estimator,X,y,cv=kf))scikit-learnfromsklearn.model_selectionimportKFoldfromsklearn.model_selectionimportcross_validatekf=KFold(n_splits=5,shuffle=True,random_state=334)print(cross_validate(estimator,X,y,cv=kf))ORfromsklearn.model_selectionimportcross_validateprint(cross_validate(estimator,X,y,cv=5))NotesThere is no notion ofrandom_stateorshufflebecause the partitioning is determined uniquely for the dataset.
If these arguments are included, they do not cause an error. They simply have no effect on the result. Please be careful.If you want to run the notebook in examples directory,
you will need to additionally installpandas,matplotlib,seaborn,tqdm, andjupyterother than the packages in requirements.txt.Distance metricsSee the documentation ofscipy.spatial.distance.pdisthttps://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.distance.pdist.htmlsklearn.metrics.pairwise_distanceshttps://scikit-learn.org/stable/modules/generated/sklearn.metrics.pairwise_distances.htmlValid values for metric are:From scikit-learn: ['cityblock', 'cosine', 'euclidean', 'l1',
'l2', 'manhattan']. These metrics support sparse matrix inputs.
['nan_euclidean'] but it does not yet support sparse matrices.From scipy.spatial.distance: ['braycurtis', 'canberra',
'chebyshev', 'correlation', 'dice', 'hamming', 'jaccard',
'mahalanobis', 'minkowski', 'rogerstanimoto',
'russellrao', 'seuclidean', 'sokalmichener', 'sokalsneath',
'sqeuclidean', 'yule'] See the documentation for
scipy.spatial.distance for details on these metrics.
These metrics do not support sparse matrix inputs., by default "euclidean"Parallelization (since v2.1.0)This algorithm is very computationally intensive and takes a lot of time.
To solve this problem, I have implemented parallelization and optimized the algorithm since v2.1.0.n_jobscan be specified for parallelization as in the scikit-learn-like api.# parallelization KFoldkf=KFold(n_splits=5,n_jobs=-1)# parallelization train_test_splitX_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.2,n_jobs=-1)The parallelization is used when calculating the distance matrix,
so it doesn't conflict with something likecross_validatein parallel when usingKFold.# OK: does not conflict each othercross_validate(estimator,X,y,cv=KFold(5,n_jobs=-1),n_jobs=-1)Using GPUIf you have a GPU and have installed pytorch,
you can use it to calculate Minkowski distances (Manhattan, Euclidean, and Chebyshev distances).X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.2,device="cuda")LICENSEMIT LicenceCopyright (c) 2021 yu9824ReferencesPapersR. W. Kennard & L. A. Stone (1969) Computer Aided Design of Experiments, Technometrics, 11:1, 137-148, DOI:10.1080/00401706.1969.10490666Siteshttps://datachemeng.com/trainingtestdivision/(Japanese site)Historiesv2.0.0 (deprecated)Define Extended Kennard-Stone algorithm (multi-class) i.e. Improve KFold algorithm.Deletealternateargument inKFold.Delete requirements ofpandas.v2.0.1Fix bug with Python3.7.v2.1.0 (deprecated)Optimize algorithmDeal with Large number of data.parallel calculation when calculating distance (Addn_jobsargument)replacing recursive functions with for-loopsAdd other than "euclidean" calculation methods (Addmetricargument)v2.1.1 (deprecated)Fix bug whenmetric="nan_euclidean".v2.1.2 (deprecated)Fix details.Update docstrings and typings.v2.1.3 (deprecated)Fix details.Update some typings. (You have access to a list of strings that can be used in the metric.)v2.1.4Fix bug when metric=="seuclidean" and "mahalanobis"Add some tests to check all metrics.Add requirements numpy>=1.20v2.1.5Delete "klusinski" metric to support scipy>=1.11v2.1.6Improve typing inkennard_stone.train_test_splitAdd some docstrings.v2.2.0Supports GPU calculations. (when metric is 'euclidean', 'manhattan', 'chebyshev' and 'minkowski')Supports Python 3.12v2.2.1Fix setup.cfgUpdate 'typing'