如何在 Python 中从 sklearn 多次训练 SVM 分类器? [英] How can i train multiple times an SVM classifier from sklearn in Python?

查看:111
本文介绍了如何在 Python 中从 sklearn 多次训练 SVM 分类器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果有可能在 for 循环中多次使用 Python 中的 sklearn 训练 SVM 分类器,我一直在徘徊.我想到了以下内容:

I was wandering if it possible to train the SVM classifier from sklearn in Python many times inside a for loop. I have in mind something like the following:

for i in range(0,10):
    data = np.load(somedata)
    labels = np.load(somelabels)
    C = SVC()
    C.fit(data, labels)
    joblib.dump(C, 'somefolderpath/Model.pkl')

我希望我的模型针对 10 个数据及其标签中的每一个进行训练.这是可能的,还是我必须将所有数据和标签附加到两个相应的数组中,其中包含来自我的 10 个项目的全部数据和标签?

I want my model to be trained for each one of the 10 data and their labels. Is that possible in that way or do i have to append all the data and labels into two corresponding arrays containing the whole data and labels from my 10 items?

已编辑:如果我想为每个主题训练一个单独的分类器.那么上面的语法会是什么样子呢?我的编辑正确吗?当我想为我的特定主题加载特定的训练分类器时,我可以这样做:

EDITED: If i want to train a separate classifier for each subject. Then how would the above syntax look like? Is my edit correct? And when i want to load the specific trained classifier for my specific subject, can i do:

C = joblib.load('somefolderpath/Model.pkl')
idx = C.predict(data)

?

推荐答案

在任何 scikit-learn estimator 上调用 fit 都会忘记所有之前看到的数据.因此,如果您想使用所有数据(所有 10 位患者)进行预测,则需要先将其连接起来.特别是,如果每个 somelabels 只包含一个标签,则代码没有意义,甚至可能会出错,因为只有一个类.

Calling fit on any scikit-learn estimator will forget all the previously seen data. So if you want to make predictions using all of your data (all ten patients), you need to concatenate it first. In particular, if each somelabels contains only a single label, the code doesn't make sense and might even error because only one class is present.

这篇关于如何在 Python 中从 sklearn 多次训练 SVM 分类器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆