Pergunta

import numpy as np
from sklearn import linear_model

X = np.array([[-1, -1], [-2, -1], [1, 1], [2, 1]])
Y = np.array(['C++', 'C#', 'java','python'])
clf = linear_model.SGDClassifier()
clf.fit(X, Y)
print (clf.predict([[1.7, 0.7]]))
#python

I am trying to predict the values from arrays Y by giving a test case and training it on a training data which is X, Now my problem is that, I want to change the training set X to TF-IDF Feature Vectors, so how can that be possible? Vaguely, I want to do something like this:

import numpy as np
from sklearn import linear_model

X = np.array_str([['abcd', 'efgh'], ['qwert', 'yuiop'], ['xyz','abc'],  ['opi', 'iop']])
Y = np.array(['C++', 'C#', 'java','python'])
clf = linear_model.SGDClassifier()
clf.fit(X, Y)
Foi útil?

Solução

It's useful to do this with a Pipeline:

import numpy as np
from sklearn import linear_model, pipeline, feature_extraction

X = np.array([[-1, -1], [-2, -1], [1, 1], [2, 1]])
Y = np.array(['C++', 'C#', 'java','python'])
clf = pipeline.make_pipeline(
        feature_extraction.text.TfidfTransformer(use_idf=True),
        linear_model.SGDClassifier())
clf.fit(X, Y)
print(clf.predict([[1.7, 0.7]]))

Outras dicas

You can find a nice tutorial how to achieve that on this blog: http://blog.christianperone.com/2011/10/machine-learning-text-feature-extraction-tf-idf-part-ii/

This is acctually a part II. In part I the author discusses how and what is "Term frequency".

Link to part I.: http://blog.christianperone.com/2011/09/machine-learning-text-feature-extraction-tf-idf-part-i/

Licenciado em: CC-BY-SA com atribuição
scroll top