本文整理汇总了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
示例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()))