Sklearn train model with single sample raises a De

2019-09-02 04:03发布

So here is my code bellow:

I have a features array, and a labels array which I use to train the model.pkl

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!!

This is the 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.

How can I train a model with a single sample each time?

Thank you

1条回答
可以哭但决不认输i
2楼-- · 2019-09-02 04:33

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])

查看更多
登录 后发表回答