{"id":2089,"date":"2025-04-10T07:58:00","date_gmt":"2025-04-09T23:58:00","guid":{"rendered":"https:\/\/blog.laoyulaoyu.top\/?p=2089"},"modified":"2025-03-20T14:54:37","modified_gmt":"2025-03-20T06:54:37","slug":"python-%e9%87%91%e8%9e%8d%e9%87%8f%e5%8c%96%e5%ae%9e%e6%88%98%e5%b9%b2%e8%b4%a7%ef%bc%9a%e8%bf%99%e5%87%a0%e7%a7%8d%e5%9b%9e%e5%bd%92%e6%96%b9%e6%b3%95%e5%92%8c%e6%8a%80%e6%9c%af%e4%b8%80%e5%ae%9a","status":"publish","type":"post","link":"https:\/\/laoyulaoyu.com\/index.php\/2025\/04\/10\/python-%e9%87%91%e8%9e%8d%e9%87%8f%e5%8c%96%e5%ae%9e%e6%88%98%e5%b9%b2%e8%b4%a7%ef%bc%9a%e8%bf%99%e5%87%a0%e7%a7%8d%e5%9b%9e%e5%bd%92%e6%96%b9%e6%b3%95%e5%92%8c%e6%8a%80%e6%9c%af%e4%b8%80%e5%ae%9a\/","title":{"rendered":"Python \u91d1\u878d\u91cf\u5316\u5b9e\u6218\u5e72\u8d27\uff1a\u8fd9\u51e0\u79cd\u56de\u5f52\u65b9\u6cd5\u548c\u6280\u672f\u4e00\u5b9a\u8981\u638c\u63e1\uff08\u9644\u6e90\u7801\uff09"},"content":{"rendered":"\n<p>\u4f5c\u8005\uff1a<a href=\"https:\/\/www.laoyulaoyu.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">\u8001\u4f59\u635e\u9c7c<\/a><\/p>\n\n\n\n<p><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-cyan-bluish-gray-color\">\u539f\u521b\u4e0d\u6613\uff0c\u8f6c\u8f7d\u8bf7\u6807\u660e\u51fa\u5904\u53ca\u539f\u4f5c\u8005\u3002<\/mark><\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.laoyulaoyu.com\/wp-content\/uploads\/2025\/03\/0.png\" alt=\"\" class=\"wp-image-4622\"\/><\/figure>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<pre class=\"wp-block-verse\"><strong>\u5199\u5728\u524d\u9762\u7684\u8bdd\uff1a<\/strong>\u6295\u8d44\u5c31\u50cf\u4e00\u573a\u523a\u6fc0\u7684\u5192\u9669\uff0c\u8c01\u90fd\u60f3\u5728\u91d1\u878d\u5e02\u573a\u91cc\u5927\u8d5a\u4e00\u7b14\u3002\u53ef\u73b0\u5b9e\u5f80\u5f80\u5f88\u6b8b\u9177\uff0c\u5e02\u573a\u6ce2\u52a8\u5927\uff0c\u5404\u79cd\u56e0\u7d20\u9519\u7efc\u590d\u6742\uff0c\u4e00\u4e0d\u5c0f\u5fc3\u5c31\u5bb9\u6613\u8e29\u5751\u3002<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">\u4eca\u5929\u6211\u6316\u5230\u4e86\u8d85\u5b9e\u7528\u7684\u56de\u5f52\u6280\u672f\u5e72\u8d27\uff0c\u5fc5\u987b\u5206\u4eab\u7ed9\u5927\u5bb6\uff01<\/mark>\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u6295\u8d44\u4e4b\u8def\u80fd\u5c11\u8d70\u597d\u591a\u5f2f\u8def\uff0c\u5c0f\u767d\u4e5f\u80fd\u9006\u88ad\u53d8\u9ad8\u624b\uff0c\u8d76\u7d27\u6765\u770b\u770b\uff01<\/pre>\n<\/blockquote>\n\n\n\n<p>\u5728\u91d1\u878d\u6295\u8d44\u9886\u57df\uff0c\u8981\u60f3\u7cbe\u51c6\u9884\u6d4b\u6295\u8d44\u6536\u76ca\uff0c\u79bb\u4e0d\u5f00\u5404\u79cd\u5206\u6790\u65b9\u6cd5\u3002\u5176\u4e2d\u6709\u4e00\u7c7b\u65b9\u6cd5\u5c31\u50cf\u7ed9\u6295\u8d44\u88c5\u4e0a\u4e86 \u201c\u7a33\u5b9a\u5668\u201d\uff0c\u80fd\u8ba9\u6211\u4eec\u7684\u6295\u8d44\u51b3\u7b56\u66f4\u9760\u8c31\uff0c\u8fd9\u5c31\u662f\u54b1\u4eec\u4eca\u5929\u8981\u8bf4\u7684\u56de\u5f52\u6280\u672f\u3002<\/p>\n\n\n\n<p>\u5728\u666e\u901a\u7684\u591a\u5143\u7ebf\u6027\u56de\u5f52\u6a21\u578b\u91cc\uff0c\u6211\u4eec\u7684\u76ee\u6807\u662f\u627e\u5230\u6700\u5408\u9002\u7684\u7cfb\u6570\uff0c\u8ba9\u9884\u6d4b\u8bef\u5dee\u6700\u5c0f\uff0c\u4e5f\u5c31\u662f\u8ba9\u5747\u65b9\u8bef\u5dee\u6700\u5c0f\u3002\u4f46\u5728\u5b9e\u9645\u7684\u91d1\u878d\u6295\u8d44\u573a\u666f\u4e2d\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u5c31\u6709\u70b9 \u201c\u6c34\u571f\u4e0d\u670d\u201d \u4e86\u3002\u56e0\u4e3a\u91d1\u878d\u6570\u636e\u91cc\u6709\u5f88\u591a\u56e0\u7d20\u4e4b\u95f4\u76f8\u4e92\u5173\u8054\uff0c\u8fd8\u6709\u4e9b\u56e0\u7d20\u5bf9\u6295\u8d44\u6536\u76ca\u7684\u9884\u6d4b\u6ca1\u4ec0\u4e48\u5e2e\u52a9\uff0c\u8fd9\u4e9b\u90fd\u4f1a\u8ba9\u9884\u6d4b\u7ed3\u679c\u53d8\u5f97\u5f88\u4e0d\u7a33\u5b9a\u3002<\/p>\n\n\n\n<p>\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u4f18\u5316\u8c03\u6574\u65b9\u6cd5\u5c31\u6d3e\u4e0a\u7528\u573a\u5566\uff01\u5b83\u5c31\u50cf\u7ed9\u6a21\u578b\u52a0\u4e0a\u4e86 \u201c\u7d27\u7b8d\u5492\u201d\uff0c\u901a\u8fc7\u5728\u8ba1\u7b97\u8bef\u5dee\u7684\u516c\u5f0f\u91cc\u52a0\u4e2a\u60e9\u7f5a\u9879\uff0c\u6765\u8c03\u6574\u90a3\u4e9b\u7cfb\u6570\uff0c\u8ba9\u6a21\u578b\u53d8\u5f97\u66f4\u7a33\u5b9a\uff0c\u9884\u6d4b\u4e5f\u66f4\u51c6\u786e\u3002\u4e0b\u9762\u5c31\u7ed9\u5927\u5bb6\u8be6\u7ec6\u4ecb\u7ecd\u51e0\u79cd\u8d85\u5b9e\u7528\u7684\u56de\u5f52\u6280\u672f\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u4e00\u3001\u5cad\u56de\u5f52\uff1a\u6295\u8d44\u7ec4\u5408\u7684 \u201c\u7a33\u5b9a\u5668\u201d<\/strong><\/h2>\n\n\n\n<p>\u5cad\u56de\u5f52\uff08Ridge Regression<strong>&nbsp;<\/strong>\uff09\u7684\u4e3b\u8981\u76ee\u7684\u662f\u901a\u8fc7\u5f15\u5165\u6b63\u5219\u5316\u9879\u6765\u51cf\u5c11\u6a21\u578b\u7684\u590d\u6742\u6027\uff0c\u4ece\u800c\u9632\u6b62\u8fc7\u62df\u5408\u3002\u5b83\u7ed9\u6a21\u578b\u52a0\u4e0a\u4e86 L2 \u60e9\u7f5a\u9879\uff0c\u8fd9\u4e2a\u60e9\u7f5a\u9879\u7684\u4f5c\u7528\u5c31\u662f\u4e0d\u8ba9\u7cfb\u6570\u53d8\u5f97\u592a\u5927\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.laoyulaoyu.com\/wp-content\/uploads\/2025\/03\/image-85.png\" alt=\"\" class=\"wp-image-4623\"\/><\/figure>\n\n\n\n<p>\u5728\u91d1\u878d\u6295\u8d44\u91cc\uff0c\u5cad\u56de\u5f52\u7684\u7528\u5904\u53ef\u5927\u4e86\u53bb\u4e86\uff01\u800c\u4e14\u968f\u7740\u91d1\u878d\u6570\u636e\u7684\u4e0d\u65ad\u589e\u957f\u548c\u590d\u6742\u6027\u589e\u52a0\uff0c\u5cad\u56de\u5f52\u5c06\u7ee7\u7eed\u6210\u4e3a\u91d1\u878d\u5206\u6790\u5e08\u548c\u7814\u7a76\u4eba\u5458\u7684\u91cd\u8981\u5de5\u5177\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.laoyulaoyu.com\/wp-content\/uploads\/2025\/03\/image-86.png\" alt=\"\" class=\"wp-image-4624\"\/><\/figure>\n\n\n\n<p>\u5176\u5728\u91d1\u878d\u9886\u57df\u7684\u5e94\u7528\u5c55\u73b0\u4e86\u5176\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u80fd\u529b\u548c\u6a21\u578b\u7a33\u5b9a\u6027\u3002\u901a\u8fc7\u6709\u6548\u5730\u5904\u7406\u591a\u91cd\u5171\u7ebf\u6027\u95ee\u9898\uff0c\u5cad\u56de\u5f52\u4e0d\u4ec5\u63d0\u9ad8\u4e86\u6a21\u578b\u7684\u9884\u6d4b\u80fd\u529b\uff0c\u8fd8\u5728\u98ce\u9669\u7ba1\u7406\u548c\u6295\u8d44\u7ec4\u5408\u4f18\u5316\u4e2d\u53d1\u6325\u4e86\u91cd\u8981\u4f5c\u7528\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u4e8c\u3001Lasso \u56de\u5f52\uff1a\u7cbe\u51c6\u7b5b\u9009\u6295\u8d44\u56e0\u7d20\u7684 \u201c\u795e\u5668\u201d<\/strong><\/h2>\n\n\n\n<p>Lasso \u56de\u5f52\uff08Lasso Regression\uff09\u548c\u5cad\u56de\u5f52\u6709\u70b9\u4e0d\u4e00\u6837\uff0c\u5b83\u80fd\u76f4\u63a5\u628a\u4e00\u4e9b\u4e0d\u592a\u91cd\u8981\u7684\u5f71\u54cd\u56e0\u7d20\u4ece\u6a21\u578b\u91cc \u201c\u8e22\u201d \u51fa\u53bb\uff0c\u53ea\u7559\u4e0b\u90a3\u4e9b\u5bf9\u6295\u8d44\u6536\u76ca\u5f71\u54cd\u5927\u7684\u5173\u952e\u56e0\u7d20\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u6a21\u578b\u5c31\u53d8\u5f97\u66f4\u7b80\u5355\u3001\u66f4\u7cbe\u51c6\u4e86\u3002<\/p>\n\n\n\n<p>\u5728\u5b9e\u9645\u6295\u8d44\u4e2d\uff0cLasso \u56de\u5f52\u5c55\u73b0\u4e86\u5176\u5f3a\u5927\u7684\u7279\u5f81\u9009\u62e9\u80fd\u529b\u548c\u6a21\u578b\u4f18\u5316\u4f18\u52bf\u3002\u65e0\u8bba\u662f\u5728\u8bbe\u8ba1\u4ea4\u6613\u6a21\u578b\u3001\u9884\u6d4b\u7ecf\u6d4e\u8d70\u52bf\u8fd8\u662f\u6295\u8d44\u7ec4\u5408\u4f18\u5316\u4e2d\uff0cLasso \u56de\u5f52\u90fd\u80fd\u4e3a\u91d1\u878d\u5206\u6790\u63d0\u4f9b\u6709\u529b\u7684\u652f\u6301\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.laoyulaoyu.com\/wp-content\/uploads\/2025\/03\/Lasso-\u56de\u5f52\uff08L1-\u6b63\u5219\u5316\uff09\u5728\u91d1\u878d\u9886\u57df\u7684\u5e94\u7528-visual-selection.png\" alt=\"\" class=\"wp-image-4626\"\/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728\u8bbe\u8ba1\u4ea4\u6613\u6a21\u578b\u65f6\uff0c\u5b83\u80fd\u5e2e\u6211\u4eec\u4ece\u4e00\u5806\u5f71\u54cd\u8d44\u4ea7\u6536\u76ca\u7684\u56e0\u7d20\u91cc\uff0c\u5feb\u901f\u627e\u51fa\u6700\u91cd\u8981\u7684\u90a3\u4e9b\uff0c\u628a\u6ca1\u7528\u7684\u56e0\u7d20\u90fd\u53bb\u6389\u3002<\/li>\n\n\n\n<li>\u5728\u6784\u5efa\u6295\u8d44\u7ec4\u5408\u7684\u65f6\u5019\uff0cLasso \u56de\u5f52\u8fd8\u80fd\u8ba9\u7ec4\u5408\u91cc\u7684\u8d44\u4ea7\u6570\u91cf\u53d8\u5c11\uff0c\u8fd9\u6837\u6211\u4eec\u4e70\u5356\u8d44\u4ea7\u65f6\u8981\u4ea4\u7684\u624b\u7eed\u8d39\u7b49\u4ea4\u6613\u6210\u672c\u4e5f\u4f1a\u964d\u4f4e\u3002<\/li>\n\n\n\n<li>\u5728\u9884\u6d4b\u5b8f\u89c2\u7ecf\u6d4e\u8d70\u52bf\uff0c\u5206\u6790\u5404\u79cd\u7ecf\u6d4e\u6307\u6807\u5bf9\u6295\u8d44\u7684\u5f71\u54cd\u65f6\uff0c\u5b83\u4e5f\u80fd\u5e2e\u6211\u4eec\u9009\u51fa\u6700\u5173\u952e\u7684\u7ecf\u6d4e\u6307\u6807\uff0c\u8ba9\u9884\u6d4b\u66f4\u51c6\u786e\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u4e09\u3001\u5f39\u6027\u7f51\u7edc\u56de\u5f52\uff1a\u96c6\u5927\u6210\u7684\u6295\u8d44 \u201c\u591a\u9762\u624b\u201d<\/strong><\/h2>\n\n\n\n<p>\u5f39\u6027\u7f51\u7edc\u56de\u5f52\uff08Elastic Net\uff09\u5c31\u50cf\u662f\u628a\u5cad\u56de\u5f52\u548c Lasso \u56de\u5f52\u7684\u4f18\u70b9\u90fd\u96c6\u5408\u5728\u4e00\u8d77\u4e86\u3002\u5b83\u65e2\u80fd\u50cf\u5cad\u56de\u5f52\u4e00\u6837\u8ba9\u6a21\u578b\u66f4\u7a33\u5b9a\uff0c\u53c8\u80fd\u50cf Lasso \u56de\u5f52\u90a3\u6837\u7b5b\u9009\u5173\u952e\u56e0\u7d20\u3002<\/p>\n\n\n\n<p>\u901a\u8fc7\u8c03\u6574\u4e00\u4e2a\u53eb \u03b1 \u7684\u53c2\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u63a7\u5236\u5b83\u66f4\u50cf\u5cad\u56de\u5f52\u8fd8\u662f\u66f4\u50cf Lasso \u56de\u5f52\u3002\u5f53 \u03b1 \u7b49\u4e8e 1 \u7684\u65f6\u5019\uff0c\u5b83\u5c31\u548c Lasso \u56de\u5f52\u4e00\u6837\uff1b\u5f53 \u03b1 \u7b49\u4e8e 0 \u7684\u65f6\u5019\uff0c\u5c31\u548c\u5cad\u56de\u5f52\u4e00\u6837\u3002<\/p>\n\n\n\n<p>\u5728\u91d1\u878d\u6295\u8d44\u91cc\uff0c\u5f39\u6027\u7f51\u7edc\u56de\u5f52\u4e5f\u6709\u5f88\u591a\u7528\u6b66\u4e4b\u5730\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.laoyulaoyu.com\/wp-content\/uploads\/2025\/03\/Lasso-\u56de\u5f52\uff08L1-\u6b63\u5219\u5316\uff09\u5728\u91d1\u878d\u9886\u57df\u7684\u5e94\u7528-visual-selection-1.png\" alt=\"\" class=\"wp-image-4627\"\/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728\u8fdb\u884c\u56e0\u5b50\u6295\u8d44\uff0c\u6311\u9009\u5bf9\u80a1\u7968\u6da8\u8dcc\u6709\u91cd\u8981\u5f71\u54cd\u7684\u56e0\u5b50\u7ec4\u5408\u65f6\uff0c\u5b83\u80fd\u53d1\u6325\u5927\u4f5c\u7528\u3002\u5728\u5206\u6790\u5927\u91cf\u80a1\u7968\u6570\u636e\uff0c<\/li>\n\n\n\n<li>\u50cf\u5206\u6790\u793e\u4ea4\u5a92\u4f53\u4e0a\u5927\u5bb6\u5bf9\u67d0\u4e2a\u516c\u53f8\u7684\u8bc4\u4ef7\u8fd9\u79cd\u7279\u6b8a\u6570\u636e\u65f6\uff0c\u5b83\u80fd\u5e2e\u52a9\u6211\u4eec\u5efa\u7acb\u66f4\u51c6\u786e\u7684\u8d44\u4ea7\u5b9a\u4ef7\u6a21\u578b\u3002<\/li>\n\n\n\n<li>\u5728\u9884\u6d4b\u8d37\u6b3e\u8fdd\u7ea6\u98ce\u9669\uff0c\u505a\u4fe1\u7528\u98ce\u9669\u5efa\u6a21\u7684\u65f6\u5019\uff0c\u5b83\u8fd8\u80fd\u9632\u6b62\u6a21\u578b\u51fa\u73b0\u8fc7\u5ea6\u62df\u5408\u7684\u60c5\u51b5\uff0c\u8ba9\u9884\u6d4b\u7ed3\u679c\u66f4\u53ef\u9760\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u56db\u3001\u4ee3\u7801\u5b9e\u73b0<\/strong><\/h2>\n\n\n\n<p>\u5149\u8bf4\u7406\u8bba\u53ef\u80fd\u6709\u70b9\u62bd\u8c61\uff0c\u63a5\u4e0b\u6765\u54b1\u4eec\u7528 Python \u4ee3\u7801\u5b9e\u64cd\u4e00\u4e0b\uff0c\u770b\u770b\u8fd9\u4e9b\u56de\u5f52\u65b9\u6cd5\u5728\u5b9e\u9645\u6295\u8d44\u91cc\u8868\u73b0\u548b\u6837\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.laoyulaoyu.com\/wp-content\/uploads\/2025\/03\/image-88.png\" alt=\"\" class=\"wp-image-4629\"\/><\/figure>\n\n\n\n<p>\u6211\u4eec\u8fd8\u662f\u4ee5\u7f8e\u80a1\u201c\u4e03\u5de8\u5934\u201d \u80a1\u7968 \u201cMagnificent Seven\u201d\u7684\u7b49\u6743\u91cd\u6295\u8d44\u7ec4\u5408\u7684\u6570\u636e\u4e3a\u4f8b\u6765\u5206\u6790\uff0c\u5bf9\u6bd4\u5cad\u56de\u5f52\u3001Lasso \u56de\u5f52\u3001\u5f39\u6027\u7f51\u7edc\u56de\u5f52\u548c\u5206\u5c42\u98ce\u9669\u5e73\u4ef7\uff08HRP\uff09\u7b56\u7565\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4.1 \u6570\u636e\u4e0e\u6295\u8d44\u7ec4\u5408\u6784\u5efa<\/strong><\/h3>\n\n\n\n<p>\u5148\u5b89\u88c5\u9700\u8981\u7684\u5de5\u5177\u5305\uff0c\u4ee3\u7801\u662f\uff1a<\/p>\n\n\n\n<p><code>pip install yfinance PyPortfolioOpt scikit - learn matplotlib<\/code>&nbsp;\u3002<\/p>\n\n\n\n<p>\u4e3a\u4e86\u65b9\u4fbf\u4e2d\u56fd\u4eba\u4f7f\u7528\uff0c\u5b89\u88c5\u4e2d\u6587\u5b57\u4f53\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>!apt-get -qq install -y fonts-wqy-zenhei fonts-noto-cjk<br>!fc-cache -fv # \u5237\u65b0\u5b57\u4f53\u7f13\u5b58<\/code><\/pre>\n\n\n\n<p>\u7136\u540e\uff0c\u5bfc\u5165\u8981\u7528\u7684\u5404\u79cd\u5e93\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import numpy as np\nimport pandas as pd\nimport yfinance as yf\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom datetime import datetime, timedelta\nfrom sklearn.linear_model import Ridge, Lasso, ElasticNet\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.model_selection import TimeSeriesSplit\nfrom pypfopt.hierarchical_portfolio import HRPOpt\nfrom pypfopt import risk_models, expected_returns\nimport warnings\nwarnings.filterwarnings('ignore')<\/code><\/pre>\n\n\n\n<p>\u8bbe\u7f6e\u4e00\u4e0b\u56fe\u8868\u7684\u98ce\u683c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>plt.style.use('seaborn-v0_8-darkgrid')\nsns.set_palette(\"Set2\")<\/code><\/pre>\n\n\n\n<p>\u786e\u5b9a\u4e00\u4e0b\u83b7\u53d6\u6570\u636e\u7684\u65f6\u95f4\u8303\u56f4\uff0c\u8fc7\u53bb 5 \u5e74\u7684\u6570\u636e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>end_date = datetime.now()\nstart_date = end_date - timedelta(days=5*365)<\/code><\/pre>\n\n\n\n<p>\u9009\u597d\u8981\u5206\u6790\u7684\u80a1\u7968\u4ee3\u7801\uff0c\u201cMagnificent Seven\u201d \u662f<code>['AAPL', 'MSFT', 'AMZN', 'GOOGL', 'META', 'TSLA', 'NVDA']<\/code>&nbsp;\uff0c\u518d\u52a0\u4e0a\u4e00\u4e2a\u57fa\u51c6<code>['QQQ']<\/code>&nbsp;\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4.2 \u6570\u636e\u8ba1\u7b97\u53ca\u5206\u7c7b<\/strong><\/h3>\n\n\n\n<p>\u4e0b\u8f7d\u5386\u53f2\u6570\u636e\uff0c\u8ba1\u7b97\u65e5\u6536\u76ca\u7387\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tickers = mag_seven + benchmark\ndata = yf.download(tickers, start=start_date, end=end_date)&#91;'Close']\nreturns = data.pct_change().dropna()<\/code><\/pre>\n\n\n\n<p>\u628a\u6570\u636e\u5206\u6210\u8bad\u7ec3\u96c6\uff0870%\uff09\u548c\u6d4b\u8bd5\u96c6\uff0830%\uff09\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>train_size = int(len(returns) * 0.7)\nreturns_train = returns.iloc&#91;:train_size]\nreturns_test = returns.iloc&#91;train_size:]\nprint(f\"\u8bad\u7ec3\u96c6\u65f6\u95f4\u8303\u56f4: {returns_train.index&#91;0]} \u5230 {returns_train.index&#91;-1]}\")\nprint(f\"\u6d4b\u8bd5\u96c6\u65f6\u95f4\u8303\u56f4: {returns_test.index&#91;0]} \u5230 {returns_test.index&#91;-1]}\")<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4.3 \u7b56\u7565\u5b9e\u73b0<\/strong><\/h3>\n\n\n\n<p>\u63a5\u4e0b\u6765\uff0c\u5206\u522b\u5b9e\u73b0\u4e0d\u540c\u7684\u7b56\u7565\u3002\u5148\u770b\u5206\u5c42\u98ce\u9669\u5e73\u4ef7\uff08HRP\uff09\u7b56\u7565\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>hrp_opt = HRPOpt(returns_train&#91;mag_seven])\nhrp_weights = hrp_opt.optimize()<\/code><\/pre>\n\n\n\n<p>\u518d\u51c6\u5907\u56de\u5f52\u6a21\u578b\u7684\u6570\u636e\uff0c\u5e76\u4e14\u8fdb\u884c\u6807\u51c6\u5316\u5904\u7406\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>X_train = returns_train&#91;mag_seven].values\ny_train = returns_train&#91;'QQQ'].values\nscaler = StandardScaler()\nX_train_scaled = scaler.fit_transform(X_train)<\/code><\/pre>\n\n\n\n<p>\u5199\u4e2a\u51fd\u6570\uff0c\u7528\u6765\u8bad\u7ec3\u6a21\u578b\u5e76\u5f97\u5230\u6295\u8d44\u7ec4\u5408\u7684\u6743\u91cd\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def get_reg_weights(model, X, y, assets):\n    model.fit(X, y)\n    coefs = np.abs(model.coef_)\n    if np.sum(coefs) == 0:\n        weights = np.ones(len(assets)) \/ len(assets)\n    else:\n        weights = coefs \/ np.sum(coefs)\n    return pd.Series(weights, index=assets)<\/code><\/pre>\n\n\n\n<p>\u521b\u5efa\u5cad\u56de\u5f52\u3001Lasso \u56de\u5f52\u548c\u5f39\u6027\u7f51\u7edc\u56de\u5f52\u7684\u6a21\u578b\u5b9e\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ridge = Ridge(alpha=1.0)\nlasso = Lasso(alpha=0.01)\nelastic = ElasticNet(alpha=0.01, l1_ratio=0.5)<\/code><\/pre>\n\n\n\n<p>\u5f97\u5230\u6bcf\u4e2a\u6a21\u578b\u7684\u6295\u8d44\u7ec4\u5408\u6743\u91cd\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ridge_weights = get_reg_weights(ridge, X_train_scaled, y_train, mag_seven)\nlasso_weights = get_reg_weights(lasso, X_train_scaled, y_train, mag_seven)\nelastic_weights = get_reg_weights(elastic, X_train_scaled, y_train, mag_seven)<\/code><\/pre>\n\n\n\n<p>\u628a\u4e09\u79cd\u56de\u5f52\u6a21\u578b\u7684\u7ed3\u679c\u7ec4\u5408\u8d77\u6765\uff0c\u5f62\u6210\u4e00\u4e2a\u65b0\u7684\u6295\u8d44\u7ec4\u5408\u3002\u5148\u8ba1\u7b97\u6bcf\u4e2a\u6a21\u578b\u5728\u8bad\u7ec3\u96c6\u4e0a\u7684\u8868\u73b0\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def calculate_portfolio_performance(returns, weights):\n    portfolio_returns = (returns * weights).sum(axis=1)\n    cumulative_returns = (1 + portfolio_returns).cumprod()\n    sharpe_ratio = portfolio_returns.mean() \/ portfolio_returns.std() * np.sqrt(252)  \n    volatility = portfolio_returns.std() * np.sqrt(252)  \n    return portfolio_returns, cumulative_returns, sharpe_ratio, volatility<\/code><\/pre>\n\n\n\n<p>\u6839\u636e\u590f\u666e\u6bd4\u7387\u7ed9\u6bcf\u4e2a\u6a21\u578b\u5206\u914d\u6743\u91cd\uff0c\u5f62\u6210\u6295\u8d44\u7ec4\u5408\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>model_names = &#91;'\u5cad\u56de\u5f52', 'Lasso\u56de\u5f52', '\u5f39\u6027\u7f51\u7edc\u56de\u5f52']\nmodel_weights = &#91;ridge_weights, lasso_weights, elastic_weights]\nmodel_sharpes = &#91;]\n\n# \u7bc7\u5e45\u539f\u56e0\uff0c\u6b64\u5904\u7f29\u5199\uff0c\u5b8c\u6574\u7248\u5230colab\u7533\u8bf7\u4e0b\u8f7d\n\nprint(\"\\n\u6839\u636e\u590f\u666e\u6bd4\u7387\u7684\u6a21\u578b\u6743\u91cd:\")\nfor name, importance in zip(model_names, model_importance):\n    print(f\"{name}: {importance:.4f}\")\ncombined_weights = pd.Series(0, index=mag_seven)\nfor i, weights in enumerate(model_weights):\n    combined_weights += weights * model_importance&#91;i]\ncombined_weights = combined_weights \/ combined_weights.sum()<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.laoyulaoyu.com\/wp-content\/uploads\/2025\/03\/1742441142534.png\" alt=\"\" class=\"wp-image-4630\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><br><strong>4.4 <strong>\u7ed3\u679c\u5c55\u793a\u4e0e\u5206\u6790<\/strong><\/strong><\/h3>\n\n\n\n<p>\u6700\u540e\uff0c\u5728\u6d4b\u8bd5\u96c6\u4e0a\u8bc4\u4f30\u6240\u6709\u6295\u8d44\u7ec4\u5408\u7684\u8868\u73b0\uff0c\u5305\u62ec HRP\u3001\u4e09\u79cd\u56de\u5f52\u6a21\u578b\u4ee5\u53ca\u6295\u8d44\u7ec4\u5408\uff0c\u8fd8\u6709\u57fa\u51c6<code>QQQ<\/code>&nbsp;\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>all_weights = {\n    'HRP': hrp_weights,\n    '\u5cad\u56de\u5f52': ridge_weights,\n    'Lasso\u56de\u5f52': lasso_weights,\n    '\u5f39\u6027\u7f51\u7edc\u56de\u5f52': elastic_weights,\n    '\u7ec4\u5408\u6295\u8d44\u7ec4\u5408': combined_weights,\n}\nprint(\"\\n\u6bcf\u4e2a\u6295\u8d44\u7ec4\u5408\u7684\u6743\u91cd\u5206\u5e03:\")\nweights_df = pd.DataFrame(all_weights)\n\n# \u7bc7\u5e45\u539f\u56e0\uff0c\u6b64\u5904\u7f29\u5199\uff0c\u5b8c\u6574\u7248\u5230colab\u7533\u8bf7\u4e0b\u8f7d\n\nresults&#91;'QQQ'] = {\n   'returns': benchmark_returns,\n    'cumulative': benchmark_cum_returns,\n   'sharpe': benchmark_sharpe,\n    'volatility': benchmark_vol,\n    'weights': pd.Series(1.0, index=&#91;'QQQ'])\n}<\/code><\/pre>\n\n\n\n<p>\u4e3a\u4e86\u66f4\u76f4\u89c2\u5730\u770b\u5230\u7ed3\u679c\uff0c\u6211\u4eec\u8fd8\u505a\u4e86\u5f88\u591a\u53ef\u89c6\u5316\u56fe\u8868\u3002<\/p>\n\n\n\n<p><strong>\u7d2f\u8ba1\u6536\u76ca\u7387\u56fe\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.laoyulaoyu.com\/wp-content\/uploads\/2025\/03\/1-1024x508.png\" alt=\"\" class=\"wp-image-4631\"\/><\/figure>\n\n\n\n<p>\u5b9e\u73b0\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>plt.figure(figsize=(12, 6))\nfor name, result in results.items():\n    plt.plot(result&#91;'cumulative'], label=name)\nplt.title('\u6d4b\u8bd5\u671f\u5185\u6295\u8d44\u7ec4\u5408\u4e0eQQQ\u7684\u7d2f\u8ba1\u6536\u76ca\u7387')\nplt.xlabel('\u65e5\u671f')\nplt.ylabel('\u7d2f\u8ba1\u6536\u76ca\u7387')\nplt.legend()\nplt.tight_layout()\nplt.savefig('cumulative_returns_test.png')\nplt.show()<\/code><\/pre>\n\n\n\n<p><strong>\u7ee9\u6548\u6307\u6807\u5bf9\u6bd4\u8868\u683c\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u5404\u6295\u8d44\u7ec4\u5408\u7684\u6743\u91cd\u5206\u5e03:\n            HRP     Ridge     Lasso  ElasticNet  Combinada\nAAPL   0.192454  0.209846  0.320533    0.287006   0.269942\nAMZN   0.178445  0.115885  0.000000    0.094346   0.073031\nGOOGL  0.163813  0.120615  0.000000    0.120223   0.083471\nMETA   0.113203  0.086448  0.000000    0.000000   0.030644\nMSFT   0.190025  0.186661  0.665312    0.307939   0.374608\nNVDA   0.067490  0.174206  0.014155    0.190487   0.130608\nTSLA   0.094570  0.106339  0.000000    0.000000   0.037696<\/code><\/pre>\n\n\n\n<p>\u5b9e\u73b0\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>metrics = pd.DataFrame({\n    '\u590f\u666e\u6bd4\u7387': &#91;results&#91;model]&#91;'sharpe'] for model in results],\n    '\u5e74\u5316\u6ce2\u52a8\u5e45\u5ea6(%)': &#91;results&#91;model]&#91;'volatility'] * 100 for model in results],\n    '\u6700\u7ec8\u6536\u76ca\u7387(%)': &#91;(results&#91;model]&#91;'cumulative'].iloc&#91;-1] - 1) * 100 for model in results]\n}, index=results.keys())\nprint(\"\\n\u6d4b\u8bd5\u671f\u5185\u7ee9\u6548\u6307\u6807\u5bf9\u6bd4:\")\nprint(metrics)<\/code><\/pre>\n\n\n\n<p><strong>\u6295\u8d44\u7ec4\u5408\u6743\u91cd\u5206\u5e03\u67f1\u72b6\u56fe\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.laoyulaoyu.com\/wp-content\/uploads\/2025\/03\/2.png\" alt=\"\" class=\"wp-image-4632\"\/><\/figure>\n\n\n\n<p>\u5b9e\u73b0\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>metrics = pd.DataFrame({\n    '\u590f\u666e\u6bd4\u7387': &#91;results&#91;model]&#91;'sharpe'] for model in results],\n    '\u5e74\u5316\u6ce2\u52a8\u5e45\u5ea6(%)': &#91;results&#91;model]&#91;'volatility'] * 100 for model in results],\n    '\u6700\u7ec8\u6536\u76ca\u7387(%)': &#91;(results&#91;model]&#91;'cumulative'].iloc&#91;-1] - 1) * 100 for model in results]\n}, index=results.keys())\nprint(\"\\n\u6d4b\u8bd5\u671f\u5185\u7ee9\u6548\u6307\u6807\u5bf9\u6bd4:\")\nprint(metrics)<\/code><\/pre>\n\n\n\n<p><strong>\u6295\u8d44\u7ec4\u5408\u6536\u76ca\u7387\u76f8\u5173\u6027\u70ed\u529b\u56fe\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.laoyulaoyu.com\/wp-content\/uploads\/2025\/03\/3.png\" alt=\"\" class=\"wp-image-4633\"\/><\/figure>\n\n\n\n<p>\u5b9e\u73b0\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>correlation_matrix = pd.DataFrame({\n    name: result&#91;'returns'] for name, result in results.items()\n}).corr()\nplt.figure(figsize=(10, 8))\nsns.heatmap(correlation_matrix, annot=True, cmap='coolwarm', vmin=-1, vmax=1)\nplt.title('\u6295\u8d44\u7ec4\u5408\u6536\u76ca\u7387\u4e4b\u95f4\u7684\u76f8\u5173\u6027')\nplt.tight_layout()\nplt.savefig('correlation_heatmap.png')\nplt.show()<\/code><\/pre>\n\n\n\n<p><strong>\u56de\u64a4\u5206\u6790\u56fe\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.laoyulaoyu.com\/wp-content\/uploads\/2025\/03\/4-1024x507.png\" alt=\"\" class=\"wp-image-4634\"\/><\/figure>\n\n\n\n<p>\u5b9e\u73b0\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def calculate_drawdowns(returns):\n    cum_returns = (1 + returns).cumprod()\n    running_max = cum_returns.cummax()\n    drawdowns = (cum_returns \/ running_max) - 1\n    return drawdowns\nplt.figure(figsize=(12, 6))\nfor name, result in results.items():\n    drawdowns = calculate_drawdowns(result&#91;'returns'])\n    plt.plot(drawdowns, label=name)\nplt.title('\u6d4b\u8bd5\u671f\u5185\u5404\u7b56\u7565\u7684\u56de\u64a4\u60c5\u51b5')\nplt.xlabel('\u65e5\u671f')\nplt.ylabel('\u56de\u64a4(%)')\nplt.legend(loc='upper center', bbox_to_anchor=(0.5, -0.1), ncol=3)\nplt.tight_layout()\nplt.savefig('drawdowns.png')\nplt.show()<\/code><\/pre>\n\n\n\n<p><strong>\u6eda\u52a8\u6ce2\u52a8\u7387\u5206\u6790\u56fe\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.laoyulaoyu.com\/wp-content\/uploads\/2025\/03\/5.png\" alt=\"\" class=\"wp-image-4635\"\/><\/figure>\n\n\n\n<p>\u5b9e\u73b0\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>window = 30  \nrolling_vol = pd.DataFrame({\n    name: result&#91;'returns'].rolling(window=window).std() * np.sqrt(252) for name, result in results.items()\n})\nplt.figure(figsize=(12, 6))\nrolling_vol&#91;&#91;'HRP', '\u7ec4\u5408\u6295\u8d44\u7ec4\u5408', 'QQQ']].plot()\nplt.title(f'{window}\u5929\u6eda\u52a8\u6ce2\u52a8\u7387: HRP\u3001\u7ec4\u5408\u6295\u8d44\u7ec4\u5408\u4e0eQQQ\u5bf9\u6bd4')\nplt.xlabel('\u65e5\u671f')\nplt.ylabel('\u5e74\u5316\u6ce2\u52a8\u5e45\u5ea6')\nplt.legend()\nplt.tight_layout()\nplt.savefig('rolling_volatility_comparison.png')\nplt.show()<\/code><\/pre>\n\n\n\n<p><strong>\u6700\u540e\u603b\u7ed3\u4e00\u4e0b\u7ed3\u679c\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.laoyulaoyu.com\/wp-content\/uploads\/2025\/03\/6.png\" alt=\"\" class=\"wp-image-4636\"\/><\/figure>\n\n\n\n<p>\u5b9e\u73b0\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(\"\\n\u6700\u7ec8\u603b\u7ed3:\")\nprint(f\"\u6d4b\u8bd5\u671f: {returns_test.index&#91;0].strftime('%Y-%m-%d')} \u5230 {returns_test.index&#91;-1].strftime('%Y-%m-%d')}\")\nprint(f\"\u6700\u9ad8\u590f\u666e\u6bd4\u7387: {metrics&#91;'\u590f\u666e\u6bd4\u7387'].idxmax()} ({metrics&#91;'\u590f\u666e\u6bd4\u7387'].max():.2f})\")\nprint(f\"\u6700\u4f4e\u6ce2\u52a8\u5e45\u5ea6: {metrics&#91;'\u5e74\u5316\u6ce2\u52a8\u5e45\u5ea6(%)'].idxmin()} ({metrics&#91;'\u5e74\u5316\u6ce2\u52a8\u5e45\u5ea6(%)'].min():.2f}%)\")\nprint(f\"\u6700\u9ad8\u6536\u76ca\u7387: {metrics&#91;'\u6700\u7ec8\u6536\u76ca\u7387(%)'].idxmax()} ({metrics&#91;'\u6700\u7ec8\u6536\u76ca\u7387(%)']\")<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4.5 \u6e90\u4ee3\u7801\u4e0b\u8f7d<\/strong><\/h3>\n\n\n\n<p>\u4ee5\u4e0a\u7684\u6e90\u4ee3\u7801\u8bf7\u5728\u6211\u7684Google Colab\u4e2d\u7533\u8bf7\u4e0b\u8f7d\uff0c\u5185\u9644\u4e2d\u6587\u6784\u5efa\u8bf4\u660e\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.laoyulaoyu.com\/wp-content\/uploads\/2025\/03\/1742443023624.png\" alt=\"\" class=\"wp-image-4639\"\/><\/figure>\n\n\n\n<p>\u5982\u89c9\u5f97\u5bf9\u60a8\u6709\u6240\u5e2e\u52a9\uff0c\u8bf7\u7559\u8a00\u7ed9\u6211\u7d22\u8981\uff0c\u540c\u65f6\u8bf7\u6211\u559d\u676f\u5496\u5561\u5c31\u597d\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u4e94\u3001\u89c2\u70b9\u603b\u7ed3<\/strong><\/h2>\n\n\n\n<p>\u5bb6\u4eba\u4eec\uff0c\u770b\u5b8c\u8fd9\u4e9b\u662f\u4e0d\u662f\u6536\u83b7\u6ee1\u6ee1\uff01\u6295\u8d44\u7684\u95e8\u9053\u5f88\u591a\uff0c\u5cad\u56de\u5f52\u3001Lasso \u56de\u5f52\u548c\u5f39\u6027\u7f51\u7edc\u56de\u5f52\u4e09\u79cd\u56de\u5f52\u6280\u672f\u53ea\u662f\u5176\u4e2d\u4e00\u90e8\u5206\u3002\u4f46\u638c\u63e1\u4e86\u5b83\u4eec\uff0c\u4f60\u5c31\u6bd4\u522b\u4eba\u591a\u4e86\u51e0\u5206\u80dc\u7b97\u3002\u5927\u5bb6\u8d76\u7d27\u52a8\u624b\u5b9e\u8df5\u4e00\u4e0b\uff0c\u628a\u8fd9\u4e9b\u65b9\u6cd5\u7528\u8d77\u6765\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u56de\u5f52\u6280\u672f\u539f\u7406<\/strong>\uff1a\u4e3a\u89e3\u51b3\u91d1\u878d\u6570\u636e\u4e2d\u56e0\u7d20\u5173\u8054\u548c\u65e0\u7528\u56e0\u7d20\u5e72\u6270\u95ee\u9898\uff0c\u901a\u8fc7\u5728\u8ba1\u7b97\u8bef\u5dee\u516c\u5f0f\u91cc\u6dfb\u52a0\u60e9\u7f5a\u9879\u8c03\u6574\u7cfb\u6570\uff0c\u8ba9\u6a21\u578b\u66f4\u7a33\u5b9a\u3001\u9884\u6d4b\u66f4\u51c6\u786e\u3002\u5cad\u56de\u5f52\u63a7\u5236\u7cfb\u6570\u5927\u5c0f\uff0cLasso \u56de\u5f52\u7b5b\u9009\u5173\u952e\u56e0\u7d20\uff0c\u5f39\u6027\u7f51\u7edc\u56de\u5f52\u517c\u5177\u4e24\u8005\u4f18\u52bf\u3002<\/li>\n\n\n\n<li><strong>Python \u5b9e\u64cd\u8fc7\u7a0b<\/strong>\uff1a\u4ee5 \u7f8e\u80a17\u96c4\u80a1\u7968\u6570\u636e\u4e3a\u4f8b\uff0c\u7ecf\u5b89\u88c5\u5de5\u5177\u5305\u3001\u5bfc\u5165\u5e93\u3001\u786e\u5b9a\u6570\u636e\u65f6\u95f4\u8303\u56f4\u3001\u83b7\u53d6\u6570\u636e\u3001\u8ba1\u7b97\u6536\u76ca\u7387\u3001\u5212\u5206\u6570\u636e\u96c6\u3001\u5b9e\u73b0\u4e0d\u540c\u7b56\u7565\u3001\u8bad\u7ec3\u6a21\u578b\u3001\u7ec4\u5408\u6a21\u578b\u7ed3\u679c\u7b49\u6b65\u9aa4\uff0c\u5f97\u5230\u5404\u6295\u8d44\u7ec4\u5408\u6743\u91cd\u3002<\/li>\n\n\n\n<li><strong>\u7ed3\u679c\u5c55\u793a\u4e0e\u5206\u6790<\/strong>\uff1a\u901a\u8fc7\u7d2f\u8ba1\u6536\u76ca\u7387\u56fe\u8868\u3001\u7ee9\u6548\u6307\u6807\u5bf9\u6bd4\u8868\u683c\u3001\u6743\u91cd\u5206\u5e03\u67f1\u72b6\u56fe\u7b49\u591a\u79cd\u53ef\u89c6\u5316\u65b9\u5f0f\uff0c\u5206\u6790\u4e0d\u540c\u6295\u8d44\u7ec4\u5408\u5728\u6d4b\u8bd5\u96c6\u4e2d\u7684\u8868\u73b0\uff0c\u8bc4\u4f30\u5404\u7b56\u7565\u4f18\u52a3\uff0c\u5982\u6700\u9ad8\u590f\u666e\u6bd4\u7387\u3001\u6700\u4f4e\u6ce2\u52a8\u5e45\u5ea6\u548c\u6700\u9ad8\u6536\u76ca\u7387\u7b49\u6307\u6807\u60c5\u51b5\u3002<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p><em>\u611f\u8c22\u60a8\u9605\u8bfb\u5230\u6700\u540e\uff0c\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u4e3a\u60a8\u5e26\u6765\u4e86\u65b0\u7684\u542f\u53d1\u548c\u5b9e\u7528\u7684\u77e5\u8bc6\uff01\u5982\u679c\u89c9\u5f97\u6709\u5e2e\u52a9\uff0c\u8bf7\u4e0d\u541d\u70b9\u8d5e\u548c\u5206\u4eab\uff0c\u60a8\u7684\u652f\u6301\u662f\u6211\u6301\u7eed\u521b\u4f5c\u7684\u52a8\u529b\u3002\u795d\u60a8\u6295\u8d44\u987a\u5229\uff0c\u6536\u76ca\u957f\u8679\uff01\u5982\u679c\u5bf9\u6587\u4e2d\u5185\u5bb9\u6709\u4efb\u4f55\u7591\u95ee\uff0c\u6b22\u8fce\u7559\u8a00\uff0c\u6211\u4f1a\u5c3d\u5feb\u56de\u590d\uff01<\/em><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"has-text-align-center\">\u672c\u6587\u5185\u5bb9\u4ec5\u9650\u6280\u672f\u63a2\u8ba8\u548c\u5b66\u4e60\uff0c\u4e0d\u6784\u6210\u4efb\u4f55\u6295\u8d44\u5efa\u8bae\u3002<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f5c\u8005\uff1a\u8001\u4f59\u635e\u9c7c \u539f\u521b\u4e0d\u6613\uff0c\u8f6c\u8f7d\u8bf7\u6807\u660e\u51fa\u5904\u53ca\u539f\u4f5c\u8005\u3002&#8230;<\/p>\n<div class=\"more-link-wrapper\"><a class=\"more-link\" href=\"https:\/\/laoyulaoyu.com\/index.php\/2025\/04\/10\/python-%e9%87%91%e8%9e%8d%e9%87%8f%e5%8c%96%e5%ae%9e%e6%88%98%e5%b9%b2%e8%b4%a7%ef%bc%9a%e8%bf%99%e5%87%a0%e7%a7%8d%e5%9b%9e%e5%bd%92%e6%96%b9%e6%b3%95%e5%92%8c%e6%8a%80%e6%9c%af%e4%b8%80%e5%ae%9a\/\">Continue reading<span class=\"screen-reader-text\">Python \u91d1\u878d\u91cf\u5316\u5b9e\u6218\u5e72\u8d27\uff1a\u8fd9\u51e0\u79cd\u56de\u5f52\u65b9\u6cd5\u548c\u6280\u672f\u4e00\u5b9a\u8981\u638c\u63e1\uff08\u9644\u6e90\u7801\uff09<\/span><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[4,5,6],"class_list":{"0":"post-2089","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"hentry","6":"category-aiinvest","7":"tag-ai","9":"tag-6","10":"entry"},"_links":{"self":[{"href":"https:\/\/laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/posts\/2089","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/comments?post=2089"}],"version-history":[{"count":2,"href":"https:\/\/laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/posts\/2089\/revisions"}],"predecessor-version":[{"id":2094,"href":"https:\/\/laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/posts\/2089\/revisions\/2094"}],"wp:attachment":[{"href":"https:\/\/laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/media?parent=2089"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/categories?post=2089"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/tags?post=2089"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}