{"id":4110,"date":"2018-10-14T09:42:01","date_gmt":"2018-10-14T01:42:01","guid":{"rendered":"https:\/\/ixyzero.com\/blog\/?p=4110"},"modified":"2018-10-14T09:42:01","modified_gmt":"2018-10-14T01:42:01","slug":"%e7%94%a8python%e5%ae%9e%e7%8e%b0tp99%e7%ad%89%e4%b8%ad%e4%bd%8d%e6%95%b0%e7%bb%9f%e8%ae%a1%e7%9a%84%e5%8a%9f%e8%83%bd","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/4110.html","title":{"rendered":"\u7528Python\u5b9e\u73b0tp99\u7b49\u4e2d\u4f4d\u6570\u7edf\u8ba1\u7684\u529f\u80fd"},"content":{"rendered":"<p>=Start=<\/p>\n<h4 id=\"id-\u6a21\u677f-\u7f18\u7531\uff1a\">\u7f18\u7531\uff1a<\/h4>\n<p>\u4e4b\u524d\u5728\u535a\u5ba2\u4e2d\u8bb0\u5f55\u8fc7\u4e00\u7bc7\u6587\u7ae0\u300c<a href=\"https:\/\/ixyzero.com\/blog\/archives\/3888.html\" target=\"_blank\" rel=\"noopener\">\u4e3a\u4ec0\u4e48\u6027\u80fd\u76d1\u63a7\u6307\u6807\u7528TP50\/90\/99\u7b49\u767e\u5206\u4f4d\u6570\u800c\u975e\u5e73\u5747\u6570<\/a>\u300d\uff0c\u7b80\u5355\u8bf4\u660e\u4e86tp90\/tp99\u7b49\u6307\u6807\u7684\u542b\u4e49\u548c\u4f5c\u7528\uff0c\u4f46\u662f\u6ca1\u6709\u7528\u4ee3\u7801\u5b9e\u73b0\u76f8\u5e94\u7684\u529f\u80fd\uff0c\u524d\u6bb5\u65f6\u95f4\u521a\u597d\u6709\u54cd\u5e94\u65f6\u95f4\u7edf\u8ba1\u7684\u9700\u8981\uff0c\u6839\u636e\u7f51\u4e0a\u7684\u4e00\u4e9b\u4ee3\u7801\u6574\u7406\u4e86\u4e00\u4e0b\uff0c\u653e\u5728\u8fd9\u91cc\uff0c\u65b9\u4fbf\u4ee5\u540e\u4f7f\u7528\u3002<\/p>\n<h4 id=\"id-\u6a21\u677f-\u6b63\u6587\uff1a\">\u6b63\u6587\uff1a<\/h4>\n<h5 id=\"id-\u6a21\u677f-\u53c2\u8003\u89e3\u7b54\uff1a\">\u53c2\u8003\u89e3\u7b54\uff1a<\/h5>\n<pre class=\"lang:default decode:true\"># \u65b9\u6cd5\u4e00\r\ndef percentile(N, percent, key=lambda x:x):\r\n    \"\"\"\r\n    Find the percentile of a list of values.\r\n\r\n    @parameter N - is a list of values. Note N MUST BE already sorted.\r\n    @parameter percent - a float value from 0.0 to 1.0.\r\n    @parameter key - optional key function to compute value from each element of N.\r\n\r\n    @return - the percentile of the values\r\n    \"\"\"\r\n    if not N:\r\n        return None\r\n    k = (len(N)-1) * percent\r\n    f = math.floor(k)\r\n    c = math.ceil(k)\r\n    if f == c:\r\n        return key(N[int(k)])\r\n    d0 = key(N[int(f)]) * (c-k)\r\n    d1 = key(N[int(c)]) * (k-f)\r\n    return d0+d1\r\n\r\n# \u6d4b\u8bd5demo\r\ntime_list = [10, 8, 3, 5, 4, 9, 6, 2, 1, 7]\r\ntime_list = sorted(time_list)\r\ntime_tp90 = percentile(time_list, 0.9)\r\ntime_tp99 = percentile(time_list, 0.99)\r\ntime_tp999 = percentile(time_list, 0.999)\r\n<\/pre>\n<p>&amp;&amp;<\/p>\n<pre class=\"lang:default decode:true \"># \u65b9\u6cd5\u4e8c\r\nimport numpy as np\r\na = np.array([1,2,3,4,5])\r\np = np.percentile(a, 50) # \u4e2d\u4f4d\u6570\r\nprint p\r\n<\/pre>\n<p>==<\/p>\n<ul>\n<li><a href=\"https:\/\/stackoverflow.com\/questions\/9039961\/finding-the-average-of-a-list\">Python\u4e2d\u5982\u4f55\u5feb\u901f\u8ba1\u7b97\u4e00\u4e2a\u6574\u6570\/\u6d6e\u70b9\u6570 list \u7684\u5e73\u5747\u503c<\/a><\/li>\n<\/ul>\n<pre class=\"lang:default decode:true \">l = [15, 18, 2, 36, 12, 78, 5, 6, 9]\r\n\r\n# \u65b9\u6cd5\u4e00\r\nsum(l) \/ float(len(l))\r\n# \u65b9\u6cd5\u4e8c\r\nprint reduce(lambda x, y: x + y, l) \/ float(len(l))\r\n# \u65b9\u6cd5\u4e09\r\nimport numpy as np\r\nprint np.mean(l)<\/pre>\n<p>&nbsp;<\/p>\n<h5 id=\"id-\u6a21\u677f-\u53c2\u8003\u94fe\u63a5\uff1a\">\u53c2\u8003\u94fe\u63a5\uff1a<\/h5>\n<ul>\n<li><a href=\"https:\/\/stackoverflow.com\/questions\/2374640\/how-do-i-calculate-percentiles-with-python-numpy\">https:\/\/stackoverflow.com\/questions\/2374640\/how-do-i-calculate-percentiles-with-python-numpy<\/a><\/li>\n<li><a href=\"https:\/\/code.activestate.com\/recipes\/511478\/\">https:\/\/code.activestate.com\/recipes\/511478\/<\/a><\/li>\n<li><a href=\"https:\/\/www.cnblogs.com\/guodongdidi\/p\/6018356.html\">https:\/\/www.cnblogs.com\/guodongdidi\/p\/6018356.html<\/a><\/li>\n<\/ul>\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u4e4b\u524d\u5728\u535a\u5ba2\u4e2d\u8bb0\u5f55\u8fc7\u4e00\u7bc7\u6587\u7ae0\u300c\u4e3a\u4ec0\u4e48\u6027\u80fd\u76d1\u63a7\u6307\u6807\u7528TP50\/90\/99\u7b49\u767e\u5206\u4f4d\u6570\u800c\u975e\u5e73\u5747 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,7],"tags":[1223,8,1104,1105],"class_list":["post-4110","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-programing","tag-numpy","tag-python","tag-tp90","tag-tp99"],"views":7670,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4110","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=4110"}],"version-history":[{"count":1,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4110\/revisions"}],"predecessor-version":[{"id":4111,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4110\/revisions\/4111"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=4110"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=4110"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=4110"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}