Python 中的第 N 个百分位数与 Dynatrace 结果不同

问题描述 投票:0回答:2

我正在尝试根据从 Dynatrace 提取的数据创建报告。

我正在每天提取事件的数据,在我的 Python Django 报告中,我需要显示第 N 个百分位数数据(例如第 30 百分位数、60 百分位数、75 百分位数、90)第 3 个百分位数)。

当我尝试从 Dynatrace 提取数据时,结果如下:

[1563,2731,3586,3966,4174,4971,6055,9175,15667]


对于这个列表,当我使用 numpy.percentile 或 df.quantile 时,我得到一个与百分位值类似的值,就像我在 Excel 中使用的公式一样 然而,Dynatrace PERCENTILE 函数一起显示了不同的值

例如,从 Excel 和 Python 中,我得到的第 75 个百分位为 -

6055 从 Dynatrace 我得到 - 6835

我尝试使用一些在线工具来计算百分位数,但似乎都给出了 6055。 如果有人可以解释

DynaTrace 如何计算这个公式,那将是一个很大的帮助

提前致谢

python excel numpy percentile dynatrace
2个回答
1
投票
这种差异通常是由于

插值方法造成的,当样本很小时非常明显。

但是,6055 恰好是样本中的百分位数 75:

1563 2731 3586 3966 4174 4971 6055 9175 15667 0/8 1/8 2/8 3/8 4/8 5/8 6/8 7/8 8/8 0 0.125 0.25 0.375 0.5 0.625 0.75 0.875 1
因此,Numpy 使用其任何

插值方法(线性、较低、较高、最近、中点)都会产生相同的结果。

Dynatrace 可能使用更复杂的插值方法,例如

这个方法。其中一位作者隶属于 Dynatrace。


0
投票
from flask import Flask, request, render_template_string, jsonify import csv import json import requests from io import StringIO app = Flask(__name__) # HTML template for the UI html_template = """ <!DOCTYPE html> <html> <head> <title>CSV to JSON Converter</title> <script> function executeRequestsSequentially(payloads, index) { if (index >= payloads.length) return; var payload = payloads[index]; var xhr = new XMLHttpRequest(); xhr.open('POST', '/execute', true); xhr.setRequestHeader('Content-Type', 'application/json'); xhr.onreadystatechange = function() { if (xhr.readyState === XMLHttpRequest.DONE) { var response = JSON.parse(xhr.responseText); var responseDiv = document.getElementById('responses'); var responsePre = document.createElement('pre'); if (response.error) { responsePre.textContent = response.error; } else { responsePre.textContent = JSON.stringify(response, null, 2); } responseDiv.appendChild(responsePre); setTimeout(function() { executeRequestsSequentially(payloads, index + 1); }, 5000); } }; xhr.send(payload); } function executeAll() { var payloads = document.querySelectorAll('input[name="json_payload"]'); var payloadList = []; payloads.forEach(function(input) { payloadList.push(input.value); }); document.getElementById('responses').innerHTML = ''; var codeDiv = document.getElementById('execution_code'); codeDiv.innerHTML = ''; // Clear previous code for (var i = 0; i < payloads.length; i++) { var codePre = document.createElement('pre'); codePre.textContent = "import requests\n\n"; codePre.textContent += "payload = " + payloads[i].value + "\n\n"; codePre.textContent += "response = requests.post(payload['script']['requests'][0]['url'], json=payload)\n"; codePre.textContent += "print(response.json())\n"; codeDiv.appendChild(codePre); } executeRequestsSequentially(payloadList, 0); } </script> </head> <body> <h1>Upload CSV</h1> <form action="/" method="post" enctype="multipart/form-data"> <input type="file" name="file" required> <input type="submit" value="Upload"> </form> {% if csv_data %} <h2>CSV Data</h2> <pre>{{ csv_data }}</pre> <h2>JSON Payloads</h2> {% for payload in json_payloads %} <pre>{{ payload }}</pre> <input type="hidden" name="json_payload" value="{{ payload }}"> {% endfor %} <button onclick="executeAll()">Execute All</button> <h2>Execution Code</h2> <div id="execution_code"></div> {% endif %} <div id="responses"></div> </body> </html> """ def update_json_from_csv(csv_row): json_obj = { "name": csv_row["name"], "frequencyMin": int(csv_row["frequencyMin"]), "enabled": csv_row["enabled"], # No .lower() == 'true' conversion "type": csv_row["type"], "createdFrom": csv_row["createdFrom"], "script": { "version": csv_row["script_version"], "requests": [ { "description": csv_row["script_requests_description"], "url": csv_row["script_requests_url"], "method": csv_row["script_requests_method"], "validation": { "rules": [ { "type": csv_row["script_requests_validation_rules_type"], "value": csv_row["script_requests_validation_rules_value"], "passIfFound": csv_row["script_requests_validation_rules_passIfFound"] } ] }, "configuration": { "acceptAnyCertificate": csv_row["script_requests_configuration_acceptAnyCertificate"], "followRedirects": csv_row["script_requests_configuration_followRedirects"], "shouldNotPersistSensitiveData": csv_row["script_requests_configuration_shouldNotPersistSensitiveData"] } } ] }, "locations": csv_row["locations"].split(";"), "tags": json.loads(csv_row["tags"].replace("'", '"')) } return json_obj @app.route('/', methods=['GET', 'POST']) def upload_file(): if request.method == 'POST': file = request.files['file'] if file: csv_data = file.read().decode('utf-8') csv_file = StringIO(csv_data.strip()) csv_reader = csv.DictReader(csv_file) json_payloads = [] for row in csv_reader: updated_json = update_json_from_csv(row) json_payloads.append(json.dumps(updated_json, indent=2)) return render_template_string(html_template, csv_data=csv_data, json_payloads=json_payloads) return render_template_string(html_template) @app.route('/execute', methods=['POST']) def execute_request(): json_payload = request.get_json() try: response = requests.post(json_payload["script"]["requests"][0]["url"], json=json_payload) return jsonify(response.json()) except requests.exceptions.RequestException as e: return jsonify({"error": f"Request failed: {e}"}) except json.JSONDecodeError as e: return jsonify({"error": f"Error decoding JSON response: {e}"}) except Exception as e: return jsonify({"error": str(e)}) if __name__ == '__main__': app.run(debug=True)
    
© www.soinside.com 2019 - 2024. All rights reserved.