简体   繁体   English

如何使用 sklearn 的交叉验证 (Kfold) 预测标签

[英]How to predict labels using cross-validation (Kfold) with sklearn

I would like to compare the predictions of the same classifier.我想比较同一分类器的预测。 As an example , I picked the Linear Discriminant Analysis classifier.例如,我选择了线性判别分析分类器。

Therefore, I took a look in the documentation of sklearn.因此,我查看了sklearn的文档。 I found these two websites: Link 1 Link 2我找到了这两个网站: Link 1 Link 2

I would like to link them together: prediction of labels with the help of cross-validation (for example Kfold).我想将它们联系在一起:借助交叉验证(例如Kfold)预测标签。

However, I cannot manage to make my code work.但是,我无法使我的代码正常工作。

from sklearn.model_selection import cross_val_predict, KFold
from sklearn.model_selection import train_test_split
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis

X = np.array([[1, 2], [2, 4], [3, 2], [4, 4], [5, 2], [6, 4], [7, 2], [8, 4]])
Y = np.array([1, 2, 3, 4, 5, 6, 7, 8])

Xtrain,Xtest,Ytrain,Ytest = train_test_split(X,Y,train_size=0.5,random_state=1)

clf = LinearDiscriminantAnalysis()
clf.fit(Xtrain, Ytrain)
LinearDiscriminantAnalysis(n_components=None, priors=None, shrinkage='auto', solver='lsqr', store_covariance=False, tol=0.001)

# without cross-valdidation
prediction = clf.predict(Xtest)

# with cross-valdidation
cv = KFold(n_splits=2)
prediction_cv = cross_val_predict(clf, X, Y, cv=cv)

Hopefully, someone can help me.希望有人可以帮助我。

EDIT:编辑:

I think I need to explain more.我想我需要解释更多。 At the moment, I have 232 datapoints (X).目前,我有 232 个数据点 (X)。 Each point consists of 16 values and is assigned to a specific class.每个点由 16 个值组成,并分配给一个特定的类。 I hope that I can improve the predictions (=less classification mistakes for unseen data points), when I am using cross-validation, like Kfold or Leave One Out .我希望在使用交叉验证(例如KfoldLeave One Out )时,可以改进预测(= 减少看不见的数据点的分类错误)。

With the line cross_val_predict(clf, X, Y, cv=cv) , Python does a Kfold cross-validation.使用cross_val_predict(clf, X, Y, cv=cv) ,Python 进行了 Kfold 交叉验证。

Now, let's say, I get new datapoints ( X_new ).现在,假设我得到了新的数据点 ( X_new )。 How can I classify them?我该如何对它们进行分类?

I presume you are getting a Traceback when you run your code that looks similar to this:我想当您运行与此类似的代码时,您会得到一个回溯:

    376         # avoid division by zero in normalization
    377         std[std == 0] = 1.
--> 378         fac = 1. / (n_samples - n_classes)
    379 
    380         # 2) Within variance scaling

ZeroDivisionError: float division by zero

That's what I get when I run your code.这就是我运行你的代码时得到的。 The reason this happens is because you have 1 data point for each class, and so n_samples - n_classes will equal zero.发生这种情况的原因是每个类都有 1 个数据点,因此n_samples - n_classes将等于 0。

You can alleviate this by populating more examples or reducing the number of classes:您可以通过填充更多示例或减少类数量来缓解这种情况:

import numpy as np

from sklearn.model_selection import cross_val_predict, KFold
from sklearn.model_selection import train_test_split
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis

X = np.array([[1, 2], [2, 4], [3, 2], [4, 4], [5, 2], [6, 4], [7, 2], [8, 4], [1, 2], [2, 4], [3, 2], [4, 4], [5, 2], [6, 4], [7, 2], [8, 4]])
Y = np.array([1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8])

Xtrain,Xtest,Ytrain,Ytest = train_test_split(X,Y,train_size=0.5,random_state=1)

clf = LinearDiscriminantAnalysis()
clf.fit(X, Y)

# without cross-valdidation
prediction = clf.predict(Xtest)

# with cross-valdidation
cv = KFold(n_splits=2)
prediction_cv = cross_val_predict(clf, X, Y, cv=cv)

If you have another problem, update your question.如果您有其他问题,请更新您的问题。

EDIT :编辑

For your updated question, it is a duplicate of this question: Using cross_val_predict against test data set对于您更新的问题,它是这个问题的重复: 针对测试数据集使用 cross_val_predict

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

相关问题 与Kfold交叉验证 - cross-validation with Kfold 如何真正理解 sklearn 中的 kfold 交叉验证 - how to really understand kfold cross validation in sklearn 使用 sklearn.cross_validation.KFold() 在 Python 中使用 train_index、test_index 的 Kfold 交叉验证创建 K 数据帧 - Creating K dataframe using train_index, test_index of Kfold cross validation in Python using sklearn.cross_validation.KFold() 使用 sklearn 在嵌套交叉验证中使用 GroupKFold - Use GroupKFold in nested cross-validation using sklearn 如何使用 keras model.predict() 使用来自交叉验证的最佳 model Z78E62681F6398F4CE365 - how to use keras model.predict() using the best model from a cross-validation output? 如何在 sklearn 中使用自定义估计器进行交叉验证? - How to use cross-validation with custom estimator in sklearn? 如何在sklearn中编写自定义估算器并对其进行交叉验证? - How to write a custom estimator in sklearn and use cross-validation on it? 如何使用Sklearn的管道进行参数调整/交叉验证? - How do to parameter tuning/cross-validation with Sklearn's pipeline? 如何将KerasClassifier,Hyperopt和Sklearn交叉验证放在一起 - How to put KerasClassifier, Hyperopt and Sklearn cross-validation together 交叉验证如何在学习曲线中发挥作用? Python sklearn - how does the cross-validation work in learning curve? Python sklearn
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM