ó <¿CVc@sVdZddlmZmZddlmZddlmZddlm Z y$ddl m Z ddl m Z Wnek rƒnXdgZe jd efd „ƒYƒZd „Zed krRdd lmZmZddlmZddlmZedƒeeedeƒƒjdeƒedƒeeeddƒƒjdeƒndS(uè scikit-learn (http://scikit-learn.org) is a machine learning library for Python. It supports many classification algorithms, including SVMs, Naive Bayes, logistic regression (MaxEnt) and decision trees. This package implement a wrapper around scikit-learn classifiers. To use this wrapper, construct a scikit-learn estimator object, then use that to construct a SklearnClassifier. E.g., to wrap a linear SVM with default settings: >>> from sklearn.svm import LinearSVC >>> from nltk.classify.scikitlearn import SklearnClassifier >>> classif = SklearnClassifier(LinearSVC()) A scikit-learn classifier may include preprocessing steps when it's wrapped in a Pipeline object. The following constructs and wraps a Naive Bayes text classifier with tf-idf weighting and chi-square feature selection to get the best 1000 features: >>> from sklearn.feature_extraction.text import TfidfTransformer >>> from sklearn.feature_selection import SelectKBest, chi2 >>> from sklearn.naive_bayes import MultinomialNB >>> from sklearn.pipeline import Pipeline >>> pipeline = Pipeline([('tfidf', TfidfTransformer()), ... ('chi2', SelectKBest(chi2, k=1000)), ... ('nb', MultinomialNB())]) >>> classif = SklearnClassifier(pipeline) iÿÿÿÿ(tprint_functiontunicode_literals(t ClassifierI(tDictionaryProbDist(tcompat(tDictVectorizer(t LabelEncoderuSklearnClassifiertSklearnClassifiercBsSeZdZeed„Zd„Zd„Zd„Zd„Z d„Z d„Z RS(u%Wrapper for scikit-learn classifiers.cCs1||_tƒ|_td|d|ƒ|_dS(uÄ :param estimator: scikit-learn classifier object. :param dtype: data type used when building feature array. scikit-learn estimators work exclusively on numeric data. The default value should be fine for almost all situations. :param sparse: Whether to use sparse matrices internally. The estimator must support these; not all scikit-learn classifiers do (see their respective documentation and look for "sparse matrix"). The default value is True, since most NLP problems involve sparse feature sets. Setting this to False may take a great amount of memory. :type sparse: boolean. tdtypetsparseN(t_clfRt_encoderRt _vectorizer(tselft estimatorRR ((sk/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/classify/scikitlearn.pyt__init__4s  cCs d|jS(Nu(R (R ((sk/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/classify/scikitlearn.pyt__repr__HscCsE|jj|ƒ}|jj}g|jj|ƒD]}||^q1S(uClassify a batch of samples. :param featuresets: An iterable over featuresets, each a dict mapping strings to either numbers, booleans or strings. :return: The predicted class label for each input sample. :rtype: list (R t transformR tclasses_R tpredict(R t featuresetstXtclassesti((sk/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/classify/scikitlearn.pyt classify_manyKs cCsD|jj|ƒ}|jj|ƒ}g|D]}|j|ƒ^q+S(uñCompute per-class probabilities for a batch of samples. :param featuresets: An iterable over featuresets, each a dict mapping strings to either numbers, booleans or strings. :rtype: list of ``ProbDistI`` (R RR t predict_probat_make_probdist(R RRt y_proba_listty_proba((sk/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/classify/scikitlearn.pytprob_classify_manyWscCst|jjƒS(uHThe class labels used by this classifier. :rtype: list (tlistR R(R ((sk/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/classify/scikitlearn.pytlabelsbscCsVttj|Œƒ\}}|jj|ƒ}|jj|ƒ}|jj||ƒ|S(uô Train (fit) the scikit-learn estimator. :param labeled_featuresets: A list of ``(featureset, label)`` where each ``featureset`` is a dict mapping strings to either numbers, booleans or strings. (RRtizipR t fit_transformR R tfit(R tlabeled_featuresetsRty((sk/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/classify/scikitlearn.pyttrainis cs2|jj‰tt‡fd†t|ƒDƒƒƒS(Nc3s%|]\}}ˆ||fVqdS(N((t.0Rtp(R(sk/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/classify/scikitlearn.pys {s(R RRtdictt enumerate(R R((Rsk/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/classify/scikitlearn.pyRys ( t__name__t __module__t__doc__tfloattTrueRRRRRR%R(((sk/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/classify/scikitlearn.pyR0s   cCsDddlm}yddl}Wntk r?|dƒ‚nXdS(Niÿÿÿÿ(tSkipTestuscikit-learn is not installed(tnoseR/tsklearnt ImportError(tmoduleR/R1((sk/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/classify/scikitlearn.pyt setup_module€s  u__main__(t names_demotnames_demo_features(tLogisticRegression(t BernoulliNBuscikit-learn Naive Bayes:tbinarizetfeaturesu# scikit-learn logistic regression:tCièN(R,t __future__RRtnltk.classify.apiRtnltk.probabilityRtnltkRtsklearn.feature_extractionRtsklearn.preprocessingRR2t__all__tpython_2_unicode_compatibleRR4R*tnltk.classify.utilR5R6tsklearn.linear_modelR7tsklearn.naive_bayesR8tprinttFalseR%(((sk/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/classify/scikitlearn.pyt s.   O