{"id":5508,"date":"2023-01-05T12:51:27","date_gmt":"2023-01-05T04:51:27","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=5508"},"modified":"2023-01-05T12:51:27","modified_gmt":"2023-01-05T04:51:27","slug":"%e5%88%a9%e7%94%a8typescript%e6%b1%82%e5%8f%96%e9%99%a3%e5%88%97%e7%9a%84%e7%9b%b8%e9%97%9c%e7%b3%bb%e6%95%b8","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/typescript\/%e5%88%a9%e7%94%a8typescript%e6%b1%82%e5%8f%96%e9%99%a3%e5%88%97%e7%9a%84%e7%9b%b8%e9%97%9c%e7%b3%bb%e6%95%b8\/","title":{"rendered":"\u5229\u7528TypeScript\u6c42\u53d6\u9663\u5217\u7684\u76f8\u95dc\u7cfb\u6578"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"TypeScript, \u9663\u5217, \u76f8\u95dc\u7cfb\u6578, correlation\"><\/p>\n<h1>\u4f7f\u7528 TypeScript \u9663\u5217\u6c42\u76f8\u95dc\u7cfb\u6578(correlation)<\/h1>\n<p>\u5728\u8cc7\u6599\u5206\u6790\u4e2d\uff0c\u76f8\u95dc\u7cfb\u6578(correlation)\u662f\u4e00\u500b\u91cd\u8981\u7684\u6982\u5ff5\uff0c\u5b83\u53ef\u4ee5\u7528\u4f86\u6e2c\u91cf\u5169\u500b\u8b8a\u6578\u4e4b\u9593\u7684\u7dda\u6027\u95dc\u4fc2\u3002\u5728 TypeScript \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u9663\u5217\u4f86\u8a08\u7b97\u76f8\u95dc\u7cfb\u6578\uff0c\u4e26\u4e14\u53ef\u4ee5\u5f97\u5230\u4e00\u500b\u6578\u503c\uff0c\u4ee5\u63cf\u8ff0\u5169\u500b\u8b8a\u6578\u4e4b\u9593\u7684\u7dda\u6027\u95dc\u4fc2\u3002<\/p>\n<p>\u5728 TypeScript \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>Array.prototype.reduce()<\/code> \u65b9\u6cd5\u4f86\u8a08\u7b97\u76f8\u95dc\u7cfb\u6578\u3002\u8a72\u65b9\u6cd5\u6703\u5c0d\u9663\u5217\u4e2d\u7684\u6bcf\u500b\u5143\u7d20\u57f7\u884c\u6307\u5b9a\u7684\u51fd\u6578\uff0c\u4e26\u5c07\u7d50\u679c\u7d2f\u52a0\u8d77\u4f86\uff0c\u6700\u5f8c\u8fd4\u56de\u4e00\u500b\u55ae\u4e00\u7684\u503c\u3002<\/p>\n<pre class=\"brush: typescript\">\n\/\/ \u8a08\u7b97\u5169\u500b\u9663\u5217\u7684\u76f8\u95dc\u7cfb\u6578\nfunction correlation(x: number[], y: number[]) {\n  const xMean = x.reduce((a, b) => a + b) \/ x.length;\n  const yMean = y.reduce((a, b) => a + b) \/ y.length;\n\n  const xDiff = x.map(x => x - xMean);\n  const yDiff = y.map(y => y - yMean);\n\n  const xDiff2 = xDiff.map(x => x * x);\n  const yDiff2 = yDiff.map(y => y * y);\n\n  const xyDiff = xDiff.map((x, i) => x * yDiff[i]);\n\n  const numerator = xyDiff.reduce((a, b) => a + b);\n  const denominator = Math.sqrt(\n    xDiff2.reduce((a, b) => a + b) * yDiff2.reduce((a, b) => a + b)\n  );\n\n  return numerator \/ denominator;\n}\n<\/pre>\n<p>\u4e0a\u9762\u7684\u51fd\u6578\u6703\u63a5\u53d7\u5169\u500b\u9663\u5217\u4f5c\u70ba\u53c3\u6578\uff0c\u4e26\u8a08\u7b97\u5b83\u5011\u4e4b\u9593\u7684\u76f8\u95dc\u7cfb\u6578\u3002\u9996\u5148\uff0c\u5b83\u6703\u8a08\u7b97\u5169\u500b\u9663\u5217\u7684\u5e73\u5747\u503c\uff0c\u7136\u5f8c\u8a08\u7b97\u6bcf\u500b\u5143\u7d20\u8207\u5e73\u5747\u503c\u4e4b\u9593\u7684\u5dee\u503c\uff0c\u4e26\u5c07\u5b83\u5011\u5e73\u65b9\u3002\u63a5\u8457\uff0c\u5b83\u6703\u5c07\u5169\u500b\u9663\u5217\u7684\u5dee\u503c\u76f8\u4e58\uff0c\u7136\u5f8c\u5c07\u7d50\u679c\u7d2f\u52a0\u8d77\u4f86\uff0c\u6700\u5f8c\u9664\u4ee5\u5169\u500b\u9663\u5217\u7684\u5dee\u503c\u5e73\u65b9\u548c\u7684\u5e73\u65b9\u6839\uff0c\u4ee5\u5f97\u5230\u6700\u7d42\u7684\u76f8\u95dc\u7cfb\u6578\u3002<\/p>\n<p>\u73fe\u5728\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528\u4e0a\u9762\u7684\u51fd\u6578\u4f86\u8a08\u7b97\u5169\u500b\u9663\u5217\u4e4b\u9593\u7684\u76f8\u95dc\u7cfb\u6578\uff1a<\/p>\n<pre class=\"brush: typescript\">\nconst x = [1, 2, 3, 4, 5];\nconst y = [2, 4, 6, 8, 10];\n\nconst correlation = correlation(x, y);\nconsole.log(correlation); \/\/ 1\n<\/pre>\n<p>\u4e0a\u9762\u7684\u7a0b\u5f0f\u78bc\u6703\u8a08\u7b97\u5169\u500b\u9663\u5217\u4e4b\u9593\u7684\u76f8\u95dc\u7cfb\u6578\uff0c\u4e26\u5c07\u7d50\u679c\u8f38\u51fa\u5230\u63a7\u5236\u53f0\u3002\u7531\u65bc\u5169\u500b\u9663\u5217\u4e4b\u9593\u5b58\u5728\u5b8c\u5168\u7dda\u6027\u95dc\u4fc2\uff0c\u56e0\u6b64\u6700\u7d42\u7684\u76f8\u95dc\u7cfb\u6578\u70ba 1\u3002<\/p>\n<p><!--more--><\/p>\n<p>\u5728 TypeScript \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u9663\u5217\u4f86\u8a08\u7b97\u76f8\u95dc\u7cfb\u6578\uff0c\u4e26\u5f97\u5230\u4e00\u500b\u6578\u503c\uff0c\u4ee5\u63cf\u8ff0\u5169\u500b\u8b8a\u6578\u4e4b\u9593\u7684\u7dda\u6027\u95dc\u4fc2\u3002\u4f7f\u7528 <code>Array.prototype.reduce()<\/code> \u65b9\u6cd5\u53ef\u4ee5\u5c0d\u9663\u5217\u4e2d\u7684\u6bcf\u500b\u5143\u7d20\u57f7\u884c\u6307\u5b9a\u7684\u51fd\u6578\uff0c\u4e26\u5c07\u7d50\u679c\u7d2f\u52a0\u8d77\u4f86\uff0c\u6700\u5f8c\u8fd4\u56de\u4e00\u500b\u55ae\u4e00\u7684\u503c\u3002<\/p>\n<p>\u672c\u6587\u4ecb\u7d39\u4e86\u5982\u4f55\u4f7f\u7528 TypeScript \u9663\u5217\u4f86\u8a08\u7b97\u76f8\u95dc\u7cfb\u6578\uff0c\u4e26\u7d66\u51fa\u4e86\u4e00\u500b\u7c21\u55ae\u7684\u793a\u4f8b\u7a0b\u5f0f\u78bc\uff0c\u53ef\u4ee5\u7528\u4f86\u8a08\u7b97\u5169\u500b\u9663\u5217\u4e4b\u9593\u7684\u76f8\u95dc\u7cfb\u6578\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528TypeScript\u4f86\u6c42\u53d6\u9663\u5217\u7684\u76f8\u95dc\u7cfb\u6578\uff0c\u4e26\u8a73\u7d30\u89e3\u91cb\u5982\u4f55\u4f7f\u7528TypeScript\u4e2d\u7684\u51fd\u6578\u4f86\u8a08\u7b97\u9663\u5217\u7684\u76f8\u95dc\u7cfb\u6578\uff0c\u4ee5\u53ca\u5982\u4f55\u5229\u7528\u76f8\u95dc\u7cfb\u6578\u4f86\u5206\u6790\u8cc7\u6599\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-5508","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-1qQ","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5508","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=5508"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5508\/revisions"}],"predecessor-version":[{"id":5509,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5508\/revisions\/5509"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=5508"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=5508"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=5508"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}