{"id":5254,"date":"2023-01-05T12:18:25","date_gmt":"2023-01-05T04:18:25","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=5254"},"modified":"2023-01-05T12:18:25","modified_gmt":"2023-01-05T04:18:25","slug":"%e5%ad%b8%e7%bf%92%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8typescript%e6%b1%82%e6%a8%99%e6%ba%96%e5%b7%aestandarddeviation","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/typescript\/%e5%ad%b8%e7%bf%92%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8typescript%e6%b1%82%e6%a8%99%e6%ba%96%e5%b7%aestandarddeviation\/","title":{"rendered":"\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528TypeScript\u6c42\u6a19\u6e96\u5dee(standardDeviation)"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"TypeScript, \u9663\u5217, \u6a19\u6e96\u5dee, standardDeviation\"><\/p>\n<h1>TypeScript \u9663\u5217\u7684\u6c42\u6a19\u6e96\u5dee(standardDeviation)<\/h1>\n<p>\u6a19\u6e96\u5dee\u662f\u4e00\u500b\u7d71\u8a08\u5b78\u4e2d\u5e38\u7528\u7684\u6307\u6a19\uff0c\u5b83\u53ef\u4ee5\u8861\u91cf\u4e00\u7d44\u6578\u64da\u7684\u5206\u6563\u7a0b\u5ea6\u3002\u5728 TypeScript \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>Array.prototype.reduce()<\/code> \u4f86\u8a08\u7b97\u6a19\u6e96\u5dee\u3002<\/p>\n<p>\u6a19\u6e96\u5dee\u7684\u8a08\u7b97\u516c\u5f0f\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush: javascript\">\n\/\/ \u6a19\u6e96\u5dee\u516c\u5f0f\nstandardDeviation = sqrt(sum(x - mean)^2 \/ n)\n<\/pre>\n<p>\u5176\u4e2d\uff0c<code>x<\/code> \u662f\u6578\u7d44\u4e2d\u7684\u6bcf\u500b\u5143\u7d20\uff0c<code>mean<\/code> \u662f\u6578\u7d44\u7684\u5e73\u5747\u503c\uff0c<code>n<\/code> \u662f\u6578\u7d44\u4e2d\u5143\u7d20\u7684\u500b\u6578\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u4e00\u500b\u7c21\u55ae\u7684 TypeScript \u51fd\u6578\uff0c\u53ef\u4ee5\u7528\u4f86\u8a08\u7b97\u6a19\u6e96\u5dee\uff1a<\/p>\n<pre class=\"brush: javascript\">\n\/\/ \u8a08\u7b97\u6a19\u6e96\u5dee\nfunction standardDeviation(arr: number[]) {\n  const mean = arr.reduce((a, b) => a + b) \/ arr.length;\n  return Math.sqrt(\n    arr.reduce((sq, n) => sq + Math.pow(n - mean, 2), 0) \/ arr.length\n  );\n}\n<\/pre>\n<p>\u8a72\u51fd\u6578\u7684\u5de5\u4f5c\u539f\u7406\u662f\uff1a\u9996\u5148\u4f7f\u7528 <code>Array.prototype.reduce()<\/code> \u8a08\u7b97\u6578\u7d44\u4e2d\u5143\u7d20\u7684\u5e73\u5747\u503c\uff0c\u7136\u5f8c\u518d\u4f7f\u7528 <code>Array.prototype.reduce()<\/code> \u8a08\u7b97\u6bcf\u500b\u5143\u7d20\u8207\u5e73\u5747\u503c\u4e4b\u9593\u7684\u5dee\u503c\u7684\u5e73\u65b9\u548c\uff0c\u6700\u5f8c\u518d\u5c07\u8a72\u548c\u9664\u4ee5\u6578\u7d44\u4e2d\u5143\u7d20\u7684\u500b\u6578\uff0c\u4e26\u5c07\u7d50\u679c\u958b\u6839\u865f\uff0c\u5373\u53ef\u5f97\u5230\u6a19\u6e96\u5dee\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u5c0d\u65bc\u6578\u7d44 <code>[1, 2, 3, 4, 5]<\/code>\uff0c\u6a19\u6e96\u5dee\u70ba <code>1.5811388300841898<\/code>\uff1a<\/p>\n<pre class=\"brush: javascript\">\nstandardDeviation([1, 2, 3, 4, 5]); \/\/ 1.5811388300841898\n<\/pre>\n<p>\u56e0\u6b64\uff0c\u53ef\u4ee5\u4f7f\u7528 TypeScript \u4e2d\u7684 <code>Array.prototype.reduce()<\/code> \u4f86\u8a08\u7b97\u6a19\u6e96\u5dee\u3002<\/p>\n<p><!--more--><\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u5b78\u7fd2\u4e86\u5982\u4f55\u4f7f\u7528 TypeScript \u4e2d\u7684 <code>Array.prototype.reduce()<\/code> \u4f86\u8a08\u7b97\u6a19\u6e96\u5dee\u3002\u8a72\u51fd\u6578\u7684\u5de5\u4f5c\u539f\u7406\u662f\uff1a\u9996\u5148\u4f7f\u7528 <code>Array.prototype.reduce()<\/code> \u8a08\u7b97\u6578\u7d44\u4e2d\u5143\u7d20\u7684\u5e73\u5747\u503c\uff0c\u7136\u5f8c\u518d\u4f7f\u7528 <code>Array.prototype.reduce()<\/code> \u8a08\u7b97\u6bcf\u500b\u5143\u7d20\u8207\u5e73\u5747\u503c\u4e4b\u9593\u7684\u5dee\u503c\u7684\u5e73\u65b9\u548c\uff0c\u6700\u5f8c\u518d\u5c07\u8a72\u548c\u9664\u4ee5\u6578\u7d44\u4e2d\u5143\u7d20\u7684\u500b\u6578\uff0c\u4e26\u5c07\u7d50\u679c\u958b\u6839\u865f\uff0c\u5373\u53ef\u5f97\u5230\u6a19\u6e96\u5dee\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528TypeScript\u6c42\u6a19\u6e96\u5dee(standardDeviation)\uff0c\u8b93\u5b78\u751f\u53ef\u4ee5\u66f4\u8f15\u9b06\u5730\u4e86\u89e3\u5982\u4f55\u4f7f\u7528TypeScript\u7684\u9663\u5217\u51fd\u6578\u4f86\u6c42\u6a19\u6e96\u5dee\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[187],"tags":[186],"class_list":["post-5254","post","type-post","status-publish","format-standard","hentry","category-typescript","tag-typescript"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1mK","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5254","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=5254"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5254\/revisions"}],"predecessor-version":[{"id":5255,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5254\/revisions\/5255"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=5254"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=5254"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=5254"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}