本文整理汇总了Python中nltk.classify.scikitlearn.SklearnClassifier._convert方法的典型用法代码示例。如果您正苦于以下问题:Python SklearnClassifier._convert方法的具体用法?Python SklearnClassifier._convert怎么用?Python SklearnClassifier._convert使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类nltk.classify.scikitlearn.SklearnClassifier
的用法示例。
在下文中一共展示了SklearnClassifier._convert方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: zip
# 需要导入模块: from nltk.classify.scikitlearn import SklearnClassifier [as 别名]
# 或者: from nltk.classify.scikitlearn.SklearnClassifier import _convert [as 别名]
#build, train, and test classifiers
from sklearn.svm import LinearSVC
from nltk.classify.scikitlearn import SklearnClassifier
sv=SklearnClassifier(LinearSVC())
sv.train(train)
#note that train performance matches tmp.sum()
pred_train_sv=sv.batch_classify(train_feat)
nltk.ConfusionMatrix(train_tag,pred_train_sv)
#also test performance matches tmp_test.sum()
pred_sv=sv.batch_classify(test_feat)
#confusion matrices
cmsv=nltk.ConfusionMatrix(test_tag,pred_sv)
print cmsv.pp(sort_by_count=True, show_percents=False, truncate=5)
#some SklearnClassifier internals
featsets, labs = zip(*train)
X = sv._convert(featsets)
import numpy
y=numpy.array([sv._label_index[l] for l in labs])
#then to train one would use sv._clf.fit(X,y)
#-------------------------------------
#To vectorize/classify all in sklearn
#-------------------------------------
porter=nltk.PorterStemmer()
#for use with sklearn
def myparser(s):
punc='[!"#$%&\'()*+,-./:;<=>[email protected][\\]^_`{|}~\n ]' #all punc+whtspc+newline
np=[a for a in re.split(punc,s) if a not in string.punctuation]
low=[a.lower() for a in np if len(a)>2] #only two-lett words lowered
nostop=[a for a in low if a not in stopwords.words('english')]
return [porter.stem(a) for a in nostop if re.findall(r"[^\W\d]",a)]