机器学习测试(docker)获取可行性结果

数据结构
{“port”:“80”,“ip”:“78.132.233.232”,“banner”:“HTTP/1.1 200 OK\r\nServer: Router Webserver\r\nConnection: close\r\nWWW-Authenticate: Basic realm=“TP-LINK Wireless N Gigabit Router WR1043ND”\r\nContent-Type: text/html”}
{“port”:“80”,“ip”:“108.167.206.174”,“banner”:“HTTP/1.0 401 Unauthorized\r\nServer: thttpd/2.25b 29dec2003\r\nContent-Type: text/html; charset=iso-8859-1\r\nDate: \r\nLast-Modified: \r\nAccept-Ranges: bytes\r\nConnection: close\r\nCache-Control: no-cache,no-store\r\nWWW-Authenticate: Basic realm=”.""}

import json
import requests

class Predict:
    def __init__(self):
        self.base_url = 'http://10.10.10.199:8888/extract'
        self.headers = {'content-type': 'application/json'}


    def predict(self, request_data):
        request_data_json = json.dumps(request_data).encode('utf-8')
        response = requests.post(self.base_url, data=request_data_json, headers=self.headers)
        response_data = json.loads(response.text)
        response_data['predict_product'] = response_data['product']
        response_data['product'] = request_data['product']
        # print(response_data)
        return response_data


    def file_predict(self, file_path):
        response_datas = []
        with open(file_path, encoding='utf-8', mode='r') as data_file:
            for line in data_file:
                line = line.strip()
                request_data= json.loads(line)
                response_data = self.predict(request_data)
                response_datas.append(response_data)
        return response_datas


    def evaluate(self, datas):
        standard_count = 0
        predict_count = 0
        common_count = 0
        for data in datas:
            if data['product']:
                standard_count += 1
            if data['predict_product']:
                predict_count += 1
            if data['product'] and data['predict_product'] and data['product'] == data['predict_product']:
                common_count += 1
        min_num = 0.0000000000001
        precision_score = common_count / (predict_count + min_num)
        recall_score = common_count / (standard_count + min_num)
        f_score = precision_score * recall_score * 2 / (precision_score + recall_score + min_num)
        print('[precision: {}, recall: {}, f_score: {}]'.format(str(precision_score), str(recall_score), str(f_score)))


    def write_file(self, datas, file_path):
        with open(file_path, encoding='utf-8', mode='w') as data_file:
            for data in datas:
                data_json = json.dumps(data, ensure_ascii=False)
                data_file.write(data_json + '\n')


def main():
    predict = Predict()
    response_datas = predict.file_predict('E:/20190929/title.json')
    predict.evaluate(response_datas)
    predict.write_file(response_datas, 'E:/20190929/title14.json')


if __name__ == '__main__':
    main()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值