问题描述
下面是我的代码:
我有一个features
数组和一个用于训练model.pkl
I have a features
array, and a labels
array which I use to train the model.pkl
但是当我想在模型中添加single sample
时,会出现warning
波纹管.
But when I want to add a single sample
to the model, I get the warning
bellow.
from sklearn import tree
from sklearn.externals import joblib
features = [[140, 1], [130, 1], [150, 0], [170, 0]]
labels = [0, 0, 1, 1]
# Here I train the model with the above arrays
clf = tree.DecisionTreeClassifier()
clf = clf.fit(features, labels)
joblib.dump(clf, 'model.pkl')
# Now I want to train the model with a new single sample
clf = joblib.load('model.pkl')
clf = clf.fit([130, 1], 0) # WARNING MESSAGE HERE!!
这是warning
:
/usr/local/lib/python2.7/dist-packages/sklearn/utils/validation.py:386:
DeprecationWarning:
Passing 1d arrays as data is deprecated in 0.17 and willraise ValueError in 0.19.
Reshape your data either using X.reshape(-1, 1)
if your data has a single feature or X.reshape(1, -1)
if it contains a single sample. DeprecationWarning)
我已经阅读了此.但是看来我的例子不一样.
I've already read this.But it seems my example is different.
如何每次使用一个样本训练模型?
谢谢
推荐答案
如果阅读错误消息,您会看到很快将不支持传递一维数组.相反,您必须确保单个样本看起来像一个样本列表,其中只有一个.在处理NumPy数组(推荐)时,可以使用reshape(-1, 1)
,但是在使用列表时,将执行以下操作:
If you read the error message you can see that passing single dimensional arrays will soon not be supported. Instead you have to ensure that your single sample looks like a list of samples, in which there is just one. When dealing with NumPy arrays (which is recommended), you can use reshape(-1, 1)
however as you're using lists then the following will do:
clf = clf.fit([[130, 1]], [0])
这篇关于具有单个样本的Sklearn火车模型引发了DeprecationWarning的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!