• 请求prometheus数据然后使用tensorflow计算正则模型


    使用tensorflow 计算正则模型, 数据来源为Prometheus的数据近7天的数据, 然后计算了90%区间的上下限和当前值的概率密度

    import requests
    import pandas as pd
    import tensorflow as tf
    from datetime import datetime, timedelta
    
    # 定义 Prometheus 查询的参数
    url = "http://{your_path}/api/v1/query_range"
    ## 查询的QL
    query = 'sum(alter_count{group="namespace"})'
    start_time = int((datetime.now() - timedelta(days=7)).timestamp())
    end_time = int(datetime.now().timestamp())
    
    # 构建查询参数
    params = {
        "query": query,
        "start": start_time,
        "end": end_time,
        "step": "1h"
    }
    
    # 发起 GET 请求
    response = requests.get(url, params=params)
    
    # 解析响应数据
    data = response.json()['data']['result']
    
    # 处理数据
    results = []
    for item in data:
        for value in item['values']:
            results.append({
                'timestamp': pd.to_datetime(value[0], unit='s'),
                'value': float(value[1])
            })
    # 将数据转化为 DataFrame
    df = pd.DataFrame(results)
    df.set_index('timestamp', inplace=True)
    
    # 构建 TensorFlow 模型
    mean_value = tf.cast(df['value'].mean(), tf.float32)
    std_dev = tf.cast(df['value'].std(), tf.float32)
    
    # 取最后值, 也就是当前值
    input_data = df.iloc[-1]['value']
    input_data = tf.cast(input_data, tf.float32)  # 将输入数据转换为 float32 类型
    result = tf.exp(-tf.square(input_data - mean_value) / (2 * tf.square(std_dev))) / (std_dev * tf.sqrt(2 * 3.14159))
    print("当前数据 {} 的正态分布概率密度值为: {}".format(input_data.numpy(), result.numpy()))
    # 计算命中 80% 区间的上下界
    lower_bound = mean_value + tf.math.erfinv(-0.45) * std_dev
    upper_bound = mean_value + tf.math.erfinv(0.45) * std_dev
    
    print("命中 90% 区间的上界为: {}".format(upper_bound.numpy()))
    print("命中 90% 区间的下界为: {}".format(lower_bound.numpy()))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
  • 相关阅读:
    实现动态大数结构
    商业合作保密协议 (2)
    TinyEngine 服务端正式开源啦!!!
    TypeScript学习 + 贪吃蛇项目
    常用Python自动化测试框架有哪些?优缺点对比
    mybatis笔记
    数据结构中常见的哈希表,到底是什么?
    elasticsearch
    php的加密方式汇总
    C#学习以及感受
  • 原文地址:https://blog.csdn.net/qq_21047625/article/details/134467478