我尝试通过 SGDClassifer.partial_fit 预测新添加的数据的标签,如下所示:
from sklearn import neighbors, linear_model
import numpy as np
def train_predict():
X = [[1, 1], [2, 2.5], [2, 6.8], [4, 7]]
y = [1, 2, 3, 4]
sgd_clf = linear_model.SGDClassifier(loss="log")
sgd_clf.fit(X, y)
X1 = [[6,9]]
y1=[5]
f1 = sgd_clf.partial_fit(X1,y1)
f1.predict([[6,9]])
return f1
if __name__ == "__main__":
clf = train_predict()
fit 完美地预测了标签。但是,部分拟合的预测会导致错误:
in compute_class_weight
raise ValueError("classes should include all valid labels that can be in y")
类似于Sklearn SGDC partial_fit ValueError: classes should include all valid labels that can be in y ,我读了partial_fit手册,http://scikit-learn.org/stable/modules/generated/sklearn.linear_model.SGDClassifier.html#sklearn.linear_model.SGDClassifier.partial_fit
但我仍然无法弄清楚如何设置partial_fit的参数,以便我能够预测动态添加的数据。
有什么引用或想法吗?
最佳答案
潜在的问题似乎是您的部分拟合输入数据不是原始数据的子集(输入到.fit()
)。
该要求至少是我如何解释 partial_fit()
中 X
和 y
的文档:
X : {array-like, sparse matrix}, shape (n_samples, n_features)
Subset of the training data
y : numpy array, shape (n_samples,)
Subset of the target values
当您使用 X1
和 y1
以及 classes = np.unique(y1)
(如文档中建议,结果是:
ValueError: `classes=array([5])` is not the same as on last call to
partial_fit, was: array([1, 2, 3, 4])
这表明 partial_fit
在底层的 fit
中使用。
以下示例有效:
X1 = X[2:3]
y1 = y[2:3]
classes = np.unique(y)
f1 = sgd_clf.partial_fit(X1, y1, classes=classes)
因此请确保 X1
和 y1
包含在您的原始数据集中。
关于python - SGDClassifier.partial_fit 返回错误 "classes should include labels",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48902767/