我正在尝试运行一个 linear_model.SGDClassifier() 并在它分类的每个示例后更新它。我的代码适用于一个小功能文件(10 个功能),但是当我给它一个更大的功能文件(大约 80000 个功能,但非常稀疏)时,它会立即给我错误,第一次调用 partial_fit()。
这就是我在伪代码中所做的:
X, y = load_svmlight_file(train_file)
classifier = linear_model.SGDClassifier()
classifier.fit(X, y)
for every test_line in test file:
test_X, test_y = getFeatures(test_line)
# This gives me a Python list for X
# and an integer label for y
print "prediction: %f" % = classifier.predict([test_X])
classifier.partial_fit(csr_matrix([test_X]),
csr_matrix([Y_GroundTruth])
classes=np.unique(y) )
对于 partial_fit() 行,我不断收到的错误是:
File "/bla/bla/epd/lib/python2.7/site-packages/sklearn/linear_model/stochastic_gradient.py", line 487, in partial_fit
coef_init=None, intercept_init=None)
File "/bla/bla/epd/lib/python2.7/site-packages/sklearn/linear_model/stochastic_gradient.py", line 371, in _partial_fit
sample_weight=sample_weight, n_iter=n_iter)
File "/bla/bla/epd/lib/python2.7/site-packages/sklearn/linear_model/stochastic_gradient.py", line 451, in _fit_multiclass
for i in range(len(self.classes_)))
File "/bla/bla/epd/lib/python2.7/site-packages/sklearn/externals/joblib/parallel.py", line 517, in __call__
self.dispatch(function, args, kwargs)
File "/bla/bla/epd/lib/python2.7/site-packages/sklearn/externals/joblib/parallel.py", line 312, in dispatch
job = ImmediateApply(func, args, kwargs)
File "/bla/bla/epd/lib/python2.7/site-packages/sklearn/externals/joblib/parallel.py", line 136, in __init__
self.results = func(*args, **kwargs)
File "/bla/bla/epd/lib/python2.7/site-packages/sklearn/linear_model/stochastic_gradient.py", line 284, in fit_binary
est.power_t, est.t_, intercept_decay)
File "sgd_fast.pyx", line 327, in sklearn.linear_model.sgd_fast.plain_sgd (sklearn/linear_model/sgd_fast.c:7568)
ValueError: ndarray is not C-contiguous
我还尝试提供 partial.fit() Python 数组或 numpy 数组(我认为默认情况下是 C 连续 (sort=C)),但这给出了相同的结果。classes 属性不是我认为的问题。如果我将其忽略或以硬代码提供正确的类,则会出现相同的错误。
我确实注意到,当我打印分类器的 _coef 数组的标志时,它说:
Flags of coef_ array:
C_CONTIGUOUS : False
F_CONTIGUOUS : True
OWNDATA : True
WRITEABLE : True
ALIGNED : True
UPDATEIFCOPY : False
我确定我做错了什么,但真的,我不明白是什么......
任何帮助表示赞赏!