1

I have a question I try build my own classifier, it's finish and it works perfect but when I try use a cross-validation score I got a error:

  File "/home/webinterpret/workspace/nlp/wi-item-attribute-extraction/attr_extractor.py", line 95, in fit
    print self.fitted_models[attr][len(self.fitted_models[attr]) - 1].cross_validation_score(x_train, y_train, 5, 0.2)
  File "/home/webinterpret/workspace/nlp/wi-item-attribute-extraction/attr_extractor.py", line 163, in cross_validation_score
    cv=self.cv).mean()
  File "/home/webinterpret/.virtualenvs/nlp/local/lib/python2.7/site-packages/sklearn/cross_validation.py", line 1361, in cross_val_score
    for train, test in cv)
  File "/home/webinterpret/.virtualenvs/nlp/local/lib/python2.7/site-packages/sklearn/externals/joblib/parallel.py", line 659, in __call__
    self.dispatch(function, args, kwargs)
  File "/home/webinterpret/.virtualenvs/nlp/local/lib/python2.7/site-packages/sklearn/externals/joblib/parallel.py", line 406, in dispatch
    job = ImmediateApply(func, args, kwargs)
  File "/home/webinterpret/.virtualenvs/nlp/local/lib/python2.7/site-packages/sklearn/externals/joblib/parallel.py", line 140, in __init__
    self.results = func(*args, **kwargs)
  File "/home/webinterpret/.virtualenvs/nlp/local/lib/python2.7/site-packages/sklearn/cross_validation.py", line 1478, in _fit_and_score
    test_score = _score(estimator, X_test, y_test, scorer)
  File "/home/webinterpret/.virtualenvs/nlp/local/lib/python2.7/site-packages/sklearn/cross_validation.py", line 1534, in _score
    score = scorer(estimator, X_test, y_test)
  File "/home/webinterpret/.virtualenvs/nlp/local/lib/python2.7/site-packages/sklearn/metrics/scorer.py", line 201, in _passthrough_scorer
    return estimator.score(*args, **kwargs)
  File "/home/webinterpret/workspace/nlp/wi-item-attribute-extraction/attr_extractor.py", line 198, in score
    return (pd.Series(self.predict(x_test)) == y_test).mean()
  File "/home/webinterpret/workspace/nlp/wi-item-attribute-extraction/attr_extractor.py", line 190, in predict
    result[i] = 1 if self.pattern in item else 0
  File "/home/webinterpret/.virtualenvs/nlp/local/lib/python2.7/site-packages/scipy/sparse/compressed.py", line 216, in __eq__
    if np.isnan(other):
TypeError: Not implemented for this type

My predict function:

result = np.zeros(text.shape[0])
i = 0
for item in text:
    result[i] = 1 if self.pattern in item else 0
    i+=1
return result

The error is in "if self.pattern in item else 0" but I don't know how I can make it in a different way?

Pattern is a text like: "car", text is just a text: "This car is broken."

1
  • For the sake of debugging, can you unfold your statement into several lines: if pattern in item: \n result[i] = 1 \n else: \n result[i]=0 Hopefully that will force the error to focus on one aspect of what is happening. Also, try using the debugger to see what the values of self.pattern and item look like. Commented Aug 5, 2015 at 15:24

1 Answer 1

1

So, scikit-learn really really wants your data to be in a strict matrix form. x_train should be a matrix of numbers, and y_train should be a matrix or vector of numbers. The cross-validation routines array-ify your input to ensure it is in a correct format for the builtin classifiers.

Here, what is happening is the array-ify step is creating a matrix of characters (effectively) which has as many columns as your largest length text. As a result, most lines of text are filling up the remaining columns with "np.nans".

If you want to use your classifier like this, you will need to avoid the builtin Pipeline and Cross Validation routines. You can iterate over a cross validation and build your own scores, like this:

for train,test in StratifiedKFold( target_classes ):
    train_data = data[train]
    test_data = data[test]
    # Train with train, predict with test, score with your favorite scorer
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.