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


Python Classifier.save方法代码示例

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


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

示例1: main

# 需要导入模块: from classifier import Classifier [as 别名]
# 或者: from classifier.Classifier import save [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

示例2: TestParser

# 需要导入模块: from classifier import Classifier [as 别名]
# 或者: from classifier.Classifier import save [as 别名]
class TestParser(unittest.TestCase):

    def setUp(self):
        #Sentence is : 3 times, This sentence: 2 times - case insensitive
        self.text = "Line1: Sentence is! This sentence isn't no3.\n" \
                    "Line2 starts here. This sentence is 4, ok?\r\n" \
                    "There are two bigrams repeating above"
        self.sentencesWithoutStemming =  \
                    [ "line1 sentence is", "this sentence isnt no3",
                      "line2 starts here", "this sentence is 4 ok",
                      "there are two bigrams repeating above"]
        self.num_bigrams   = 14      #the unique ones
        self.num_sentences = 5
        #Classifier class, encapsulates the 'update_bigrams' method, so:
        self.classifier = Classifier(verbose=True)  
    
    def test_split_sentences(self):
        
        sentences = split_sentences(self.text)
        self.assertEqual(type(sentences).__name__, 'list')
        self.assertEqual(len(sentences), self.num_sentences)

    def test_remove_stemming(self):
        
        sentences = split_sentences(self.text)
        sentencesWithoutStemming = remove_stemming (sentences)
        #print sentencesWithoutStemming
        self.assertItemsEqual (sentencesWithoutStemming, 
                               self.sentencesWithoutStemming)

    def test_make_bigrams(self):
        
        sentences = split_sentences(self.text)
        sentencesWithoutStemming = remove_stemming (sentences)
        allBigrams = defaultdict(int)
        for s in sentencesWithoutStemming:
            self.assertIsInstance(s, str)
            newBigrams = make_bigrams(s)
            #print '\n\n'
            #for k,v in newBigrams.items():
                #print k,v
            self.assertIsInstance(newBigrams, defaultdict)
            merge_and_sum_bigrams(allBigrams, newBigrams)

        self.classifier.update_joint_apriori(allBigrams)
        #for k,v in self.classifier.apriori.items():
            #print k,v
        self.assertEqual (len(allBigrams), self.num_bigrams)
    
    def assertDictEqual(self, d1, d2, msg=None): # assertEqual uses for dicts
        for k,v1 in d1.iteritems():
            self.assertIn(k, d2, msg)
            v2 = d2[k]
            if(isinstance(v1, collections.Iterable) and
               not isinstance(v1, basestring)):
                self.assertItemsEqual(v1, v2, msg)
            else:
                self.assertEqual(v1, v2, msg)
        return True
    
    def test_save_load(self):
        sentences = split_sentences(self.text)
        sentencesWithoutStemming = remove_stemming (sentences)
        allBigrams = defaultdict(int)
        for s in sentencesWithoutStemming:
            newBigrams = make_bigrams(s)
            merge_and_sum_bigrams(allBigrams, newBigrams)

        self.classifier.update_joint_apriori(allBigrams)
        
        for k,v in self.classifier.apriori.items():
            print k,v
        
        self.classifier.save('testC')
        newClassifier = Classifier()
        newClassifier.load('testC')
        #self.assertDictEqual(self.classifier.apriori, newClassifier.apriori)
        print '\nCOMPARE\n'
        for k,v in self.classifier.apriori.items():
            print k,v
        for k,v in newClassifier.apriori.items():
            print k,v
        print '\nEND OF COMPARE\n'
    '''
开发者ID:chefarov,项目名称:predictNextWord,代码行数:86,代码来源:unittests.py


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