当前位置: 首页>>代码示例>>Python>>正文


Python PassiveAggressiveClassifier.decision_function方法代码示例

本文整理汇总了Python中sklearn.linear_model.PassiveAggressiveClassifier.decision_function方法的典型用法代码示例。如果您正苦于以下问题:Python PassiveAggressiveClassifier.decision_function方法的具体用法?Python PassiveAggressiveClassifier.decision_function怎么用?Python PassiveAggressiveClassifier.decision_function使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在sklearn.linear_model.PassiveAggressiveClassifier的用法示例。


在下文中一共展示了PassiveAggressiveClassifier.decision_function方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

# 需要导入模块: from sklearn.linear_model import PassiveAggressiveClassifier [as 别名]
# 或者: from sklearn.linear_model.PassiveAggressiveClassifier import decision_function [as 别名]
class DeployedClassifierFactory:
	def __init__(self, term_doc_matrix, term_doc_matrix_factory, category, nlp=None):
		'''This is a class that enables one to train and save a classification model.

		Parameters
		----------
		term_doc_matrix : TermDocMatrix
		term_doc_matrix_factory : TermDocMatrixFactory
		category : str
			Category name
		nlp : spacy parser
		'''
		self._term_doc_matrix = term_doc_matrix
		self._term_doc_matrix_factory = term_doc_matrix_factory
		assert term_doc_matrix_factory._nlp is None
		assert term_doc_matrix_factory.category_text_iter is None
		self._category = category
		self._clf = None
		self._proba = None

	def passive_aggressive_train(self):
		'''Trains passive aggressive classifier

		'''
		self._clf = PassiveAggressiveClassifier(n_iter=50, C=0.2, n_jobs=-1, random_state=0)
		self._clf.fit(self._term_doc_matrix._X, self._term_doc_matrix._y)
		y_dist = self._clf.decision_function(self._term_doc_matrix._X)
		pos_ecdf = ECDF(y_dist[y_dist >= 0])
		neg_ecdf = ECDF(y_dist[y_dist <= 0])

		def proba_function(distance_from_hyperplane):
			if distance_from_hyperplane > 0:
				return pos_ecdf(distance_from_hyperplane) / 2. + 0.5
			elif distance_from_hyperplane < 0:
				return pos_ecdf(distance_from_hyperplane) / 2.
			return 0.5

		self._proba = proba_function
		return self

	def build(self):
		'''Builds Depoyed Classifier
		'''
		if self._clf is None:
			raise NeedToTrainExceptionBeforeDeployingException()
		return DeployedClassifier(self._category,
		                          self._term_doc_matrix._category_idx_store,
		                          self._term_doc_matrix._term_idx_store,
		                          self._term_doc_matrix_factory)
开发者ID:JasonKessler,项目名称:scattertext,代码行数:51,代码来源:DeployedClassifier.py

示例2: test_main

# 需要导入模块: from sklearn.linear_model import PassiveAggressiveClassifier [as 别名]
# 或者: from sklearn.linear_model.PassiveAggressiveClassifier import decision_function [as 别名]
	def test_main(self):
		categories, documents = get_docs_categories()
		clean_function = lambda text: '' if text.startswith('[') else text
		entity_types = set(['GPE'])
		term_doc_mat = (
			TermDocMatrixFactory(
				category_text_iter=zip(categories, documents),
				clean_function=clean_function,
				nlp=_testing_nlp,
				feats_from_spacy_doc=FeatsFromSpacyDoc(entity_types_to_censor=entity_types)
			).build()
		)
		clf = PassiveAggressiveClassifier(n_iter=5, C=0.5, n_jobs=-1, random_state=0)
		fdc = FeatsFromDoc(term_doc_mat._term_idx_store,
		                   clean_function=clean_function,
		                   feats_from_spacy_doc=FeatsFromSpacyDoc(
			                   entity_types_to_censor=entity_types)).set_nlp(_testing_nlp)
		tfidf = TfidfTransformer(norm='l1')
		X = tfidf.fit_transform(term_doc_mat._X)
		clf.fit(X, term_doc_mat._y)
		X_to_predict = fdc.feats_from_doc('Did sometimes march UNKNOWNWORD')
		pred = clf.predict(tfidf.transform(X_to_predict))
		dec = clf.decision_function(X_to_predict)
开发者ID:JasonKessler,项目名称:scattertext,代码行数:25,代码来源:test_termDocMatrixFactory.py

示例3: run

# 需要导入模块: from sklearn.linear_model import PassiveAggressiveClassifier [as 别名]
# 或者: from sklearn.linear_model.PassiveAggressiveClassifier import decision_function [as 别名]
  def run(self, nFold=3, loss='hinge', iter=10, verbose=1):
    log.debug("PA: run")
    (numx, numy) = self._network.shape

    pp = permutation(numx)
		
    model = PassiveAggressiveClassifier(loss=loss, n_iter=iter, verbose=verbose)
    model.fit(self._network, self._annotation.ravel())
    scores = model.decision_function(self._network)
    self._scores = self._convertScore(scores)

    fold = 0
    offset = 0
    meanroc = []
    labelIx = range(numx)
    while fold < nFold:
      log.debug("NV: ___ fold= %d ___" % fold)
      lastelem = int(min(numx, offset+floor(numx/nFold)))

      ix = []
      for index in pp[offset+1:lastelem]:
        ix.append(index)

      print lastelem

      offset = lastelem

      labeltmp = []
      for value in self._annotation:
        labeltmp.append(float(value))

      for index in ix:
        labeltmp[index] = 0

      model = PassiveAggressiveClassifier(loss=loss, n_iter=iter, verbose=verbose)
      model.fit(self._network, labeltmp)
      scores = model.decision_function(self._network)
      scores = self._convertScore(scores)

      score = []
      label = []
      protein = []
      for index in ix:
        score.append(float(scores[index]))
        label.append(int(self._annotation[index]))
        protein.append(int(self._proteinid[index]))

        self._foldlabels.append(int(self._annotation[index]))
        self._foldscores.append(float(scores[index]))
        self._foldproteins.append(int(self._proteinid[index]))

      auroc = self.AUROC(label, score)
      log.debug("AUROC= %.4f" % auroc)

      meanroc.append(auroc)

      fold += 1

    self._auroc = reduce(lambda x, y: x + y / float(len(meanroc)), meanroc, 0)
    auroc = self.AUROC(self._foldlabels, self._foldscores)

    self._TPR_FPR(self._foldlabels, self._foldscores)
开发者ID:wimverleyen,项目名称:SAPLING,代码行数:64,代码来源:algofact.py


注:本文中的sklearn.linear_model.PassiveAggressiveClassifier.decision_function方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。