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


Python Classifier.predict_proba方法代码示例

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


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

示例1: EmojiRecommender

# 需要导入模块: from classifier import Classifier [as 别名]
# 或者: from classifier.Classifier import predict_proba [as 别名]
class EmojiRecommender():
	def __init__(self, fname_model, fname_embed, fname_dataset):
		print >> sys.stderr, 'EmojiRecommender: [info] loading word index...'
		self.windexer = WordIndexer.load(fname_embed)
	
		print >> sys.stderr, 'EmojiRecommender: [info] loading model...'		
		self.clf = Classifier()
		self.clf.load_model(fname_model)

		print >> sys.stderr, 'EmojiRecommender: [info] loading emojis...'
		ecode_split = cPickle.load(open(fname_dataset, 'r'))
		self.emojis = [emo for emo, split in ecode_split]

		self.ydim = len(self.emojis)

		print >> sys.stderr, 'EmojiRecommender: [info] initialization done'

	def preprocess(self, text):
		text = text.decode('utf8')
		seq = zhtokenizer.tokenize(text)
		idxs = self.windexer.seq2idx(seq)

		return idxs

	def predict_proba(self, text):
		idxs = self.preprocess(text)
		
		if len(idxs) == 0:
			return None
		else:
			return self.clf.predict_proba(idxs)

	def recommend(self, text, n = 5):
		proba = self.predict_proba(text)

		if proba is None:
			eids = [i for i in range(n)]
			scores = [0. for i in range(n)]
		else:
			ranks = [(i, proba[i]) for i in range(self.ydim)]
			ranks = sorted(ranks, key = lambda k:-k[1])

			eids = [ranks[i][0] for i in range(n)]
			scores = [ranks[i][1] for i in range(n)]

		res = [{'emoji':self.emojis[eid], 'score':'%.2f'%(score)} for eid, score in zip(eids, scores)]

		return res
开发者ID:liangxh,项目名称:emozh,代码行数:50,代码来源:recomia.py

示例2: main

# 需要导入模块: from classifier import Classifier [as 别名]
# 或者: from classifier.Classifier import predict_proba [as 别名]
def main():
    print("started at %s"%str(time.localtime()))
    if dumped in os.listdir():
        with open(dumped,"rb") as f:
            clf = pickle.load(f)
    else:
        clf = Classifier()
        
        dataNameList = os.listdir(dataPath)
        
        trainModels = [Model(name.split(".")[0], True) for name in dataNameList]

        data = np.concatenate([m.feature for m in trainModels],axis=0)
        label = np.concatenate([m.labels for m in trainModels],axis=0)
        
        data = clf.preprocess(data, True)
        
        clf.fit(data, label)
        print("training time end at %s"%str(time.localtime()))
        clf.save()
        
    testNameList = os.listdir(testPath)
    testModels = [Model(name.split(".")[0]) for name in testNameList]
    for m in testModels:
        data = m.feature
        data = clf.preprocess(data)
        
        print("compute probability.")
        proba = clf.predict_proba(data)
        print("saving prob")
        np.savetxt("./result_prob/%s.prob"%m.name,proba)
        
        print("saving mid-res.")
        np.savetxt("./result_mid/%s.seg"%m.name,np.argmax(proba,axis=-1),fmt="%d")
        
        print("cutting")
        WG = build_weight_graph(m.faceGraph,clf.transform(data, threshold="median"))
        gList = build_s_t_graph_list(WG, proba)
        
        result = cut_and_label(gList)
        print( 'saving result.')
        np.savetxt('./result/%s.seg'%m.name,result,fmt='%d') 
        
    print("end at %s"%str(time.localtime()))
开发者ID:zwy1135,项目名称:segmentor,代码行数:46,代码来源:segmentor.py


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