• Appscanner实验还原code2


    import _pickle as pickle
    from sklearn import svm, ensemble
    import random
    from sklearn.metrics import accuracy_score, f1_score, precision_score, recall_score, classification_report, confusion_matrix
    import numpy as np
    
    ##########
    ##########
        
    #TRAINING_PICKLE = 'motog-old-110-noisefree-statistical.p'     # 1
    TRAINING_PICKLE = 'trunc-dataset1a-noisefree-statistical.p'     # 1
    #TESTING_PICKLE  = 'lg-new-new-110-noisefree-statistical.p'    # 5
    TESTING_PICKLE  = 'trunc-dataset2-noisefree-statistical.p'    # 5
    
    print('Loading pickles...')
    trainingflowlist = pickle.load(open(TRAINING_PICKLE, 'rb'),encoding='iso-8859-1')
    testingflowlist = pickle.load(open(TESTING_PICKLE, 'rb'),encoding='iso-8859-1')
    print('Done...')
    print('')
    
    print('Training with ' + TRAINING_PICKLE + ': ' + str(len(trainingflowlist)))
    print('Testing with ' + TESTING_PICKLE + ': ' + str(len(testingflowlist)))
    print('')
    
    p = []
    r = []
    f = []
    a = []
    
    for i in range(10):
        ########## PREPARE STUFF
        trainingexamples = []
        #classifier = svm.SVC(gamma=0.001, C=100, probability=True)
        classifier = ensemble.RandomForestClassifier()
    
    
        ########## GET FLOWS
        for package, time, flow in trainingflowlist:
            trainingexamples.append((flow, package))
    
    
        ########## SHUFFLE DATA to ensure classes are "evenly" distributed
        random.shuffle(trainingexamples)
    
    
        ########## TRAINING
        X_train = []
        y_train = []
    
        for flow, package in trainingexamples:       
            X_train.append(flow)
            y_train.append(package)
    
        print('Fitting classifier...')
        classifier.fit(X_train, y_train)
        print('Classifier fitted!')
        print('')
    
                
        ########## TESTING
    
        X_test = []
        y_test = []
    
        for package, time, flow in testingflowlist:
            X_test.append(flow)
            y_test.append(package)
    
        y_pred = classifier.predict(X_test)
    
        print((precision_score(y_test, y_pred, average="macro")))
        print((recall_score(y_test, y_pred, average="macro")))
        print((f1_score(y_test, y_pred, average="macro")))
        print((accuracy_score(y_test, y_pred)))
        print('')
    
        p.append(precision_score(y_test, y_pred, average="macro"))
        r.append(recall_score(y_test, y_pred, average="macro"))
        f.append(f1_score(y_test, y_pred, average="macro"))
        a.append(accuracy_score(y_test, y_pred))
    
    
    print(p)
    print(r)
    print(f)
    print(a)
    print('')
    
    print(np.mean(p))
    print(np.mean(r))
    print(np.mean(f))
    print(np.mean(a))
    做一枚奔跑的老少年!
  • 相关阅读:
    Eclipse配置SVN的几种方法及使用详情
    python爬虫实战:基础爬虫(使用BeautifulSoup4等)
    MySQL中case when的基本用法总结
    SQL常见的一些面试题(太有用啦)
    Python应用——自定义排序全套方案
    Hadoop运维
    图形化查看maven的dependency依赖
    mac os x 10.10.3 安装protoc
    创业方向:O2O及移动社交 from 沈博阳
    手动编译安装docker环境,以及偶尔出现的bug
  • 原文地址:https://www.cnblogs.com/xiaoshayu520ly/p/10469415.html
Copyright © 2020-2023  润新知