{"id":1071420,"date":"2025-01-08T11:07:09","date_gmt":"2025-01-08T03:07:09","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1071420.html"},"modified":"2025-01-08T11:07:11","modified_gmt":"2025-01-08T03:07:11","slug":"%e5%a6%82%e4%bd%95%e8%bf%9b%e5%85%a5python%e5%ae%98%e7%bd%91%e6%a8%a1%e5%9d%97%e5%ba%93-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1071420.html","title":{"rendered":"\u5982\u4f55\u8fdb\u5165python\u5b98\u7f51\u6a21\u5757\u5e93"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25101915\/adde624f-d3c3-464e-bc2c-c14418e29b5d.webp\" alt=\"\u5982\u4f55\u8fdb\u5165python\u5b98\u7f51\u6a21\u5757\u5e93\" \/><\/p>\n<p><p> <strong>\u8981\u8fdb\u5165Python\u5b98\u7f51\u6a21\u5757\u5e93\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u8bbf\u95eePython\u5b98\u65b9\u7f51\u7ad9\u5e76\u5bfc\u822a\u81f3\u5176\u6587\u6863\u90e8\u5206\u3001\u76f4\u63a5\u4f7f\u7528Python\u81ea\u5e26\u7684pip\u5de5\u5177\u6765\u641c\u7d22\u548c\u5b89\u88c5\u6a21\u5757\u3001\u6216\u8005\u901a\u8fc7Python\u7684\u5b98\u65b9\u4ed3\u5e93PyPI\u6765\u6d4f\u89c8\u548c\u4e0b\u8f7d\u6a21\u5757\u3002<\/strong> \u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u8ba8\u8bba\u8fd9\u4e09\u79cd\u65b9\u6cd5\u4e2d\u7684\u4e00\u79cd\uff1a\u5982\u4f55\u901a\u8fc7Python\u5b98\u65b9\u7f51\u7ad9\u8bbf\u95ee\u6a21\u5757\u5e93\u5e76\u67e5\u627e\u76f8\u5173\u8d44\u6e90\u3002<\/p>\n<\/p>\n<p><p>Python\u8bed\u8a00\u4ee5\u5176\u4e30\u5bcc\u7684\u6a21\u5757\u5e93\u548c\u5f3a\u5927\u7684\u793e\u533a\u652f\u6301\u8457\u79f0\u3002\u65e0\u8bba\u4f60\u662f\u521d\u5b66\u8005\u8fd8\u662f\u7ecf\u9a8c\u4e30\u5bcc\u7684\u5f00\u53d1\u8005\uff0c\u719f\u6089\u5982\u4f55\u8bbf\u95ee\u548c\u5229\u7528Python\u6a21\u5757\u5e93\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002Python\u5b98\u7f51\u6a21\u5757\u5e93\u63d0\u4f9b\u4e86\u8be6\u7ec6\u7684\u6587\u6863\u3001\u793a\u4f8b\u4ee3\u7801\u548c\u5b89\u88c5\u8bf4\u660e\uff0c\u5e2e\u52a9\u5f00\u53d1\u8005\u66f4\u9ad8\u6548\u5730\u5b8c\u6210\u5de5\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u901a\u8fc7Python\u5b98\u65b9\u7f51\u7ad9\u8bbf\u95ee\u6a21\u5757\u5e93<\/h3>\n<\/p>\n<p><h4>1. \u8bbf\u95eePython\u5b98\u65b9\u7f51\u7ad9<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6253\u5f00\u4f60\u7684\u6d4f\u89c8\u5668\u5e76\u8bbf\u95eePython\u5b98\u65b9\u7f51\u7ad9 <a href=\"https:\/\/www.python.org\/\">https:\/\/www.python.org\/<\/a>\u3002\u8fd9\u662fPython\u7f16\u7a0b\u8bed\u8a00\u7684\u5b98\u65b9\u95e8\u6237\uff0c\u63d0\u4f9b\u4e86\u8bb8\u591a\u8d44\u6e90\uff0c\u5305\u62ec\u4e0b\u8f7d\u94fe\u63a5\u3001\u6587\u6863\u3001\u65b0\u95fb\u548c\u793e\u533a\u652f\u6301\u3002<\/p>\n<\/p>\n<p><h4>2. \u5bfc\u822a\u5230\u6587\u6863\u90e8\u5206<\/h4>\n<\/p>\n<p><p>\u5728Python\u5b98\u65b9\u7f51\u7ad9\u7684\u9996\u9875\uff0c\u4f60\u53ef\u4ee5\u770b\u5230\u9876\u90e8\u5bfc\u822a\u680f\u4e2d\u7684\u201cDocs\u201d\u9009\u9879\u3002\u70b9\u51fb\u8fd9\u4e2a\u9009\u9879\uff0c\u8fdb\u5165Python\u7684\u6587\u6863\u90e8\u5206\u3002\u5728\u8fd9\u91cc\uff0c\u4f60\u5c06\u627e\u5230Python\u4e0d\u540c\u7248\u672c\u7684\u5b98\u65b9\u6587\u6863\uff0c\u5305\u62ec\u6807\u51c6\u5e93\u53c2\u8003\u548c\u8bed\u8a00\u53c2\u8003\u3002<\/p>\n<\/p>\n<p><h4>3. \u9009\u62e9\u5408\u9002\u7684Python\u7248\u672c<\/h4>\n<\/p>\n<p><p>\u5728\u6587\u6863\u9875\u9762\uff0c\u4f60\u4f1a\u770b\u5230\u591a\u4e2aPython\u7248\u672c\u7684\u6587\u6863\u94fe\u63a5\u3002\u9009\u62e9\u4e0e\u4f60\u4f7f\u7528\u7684Python\u7248\u672c\u76f8\u5339\u914d\u7684\u6587\u6863\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662fPython 3.10\uff0c\u90a3\u4e48\u5c31\u70b9\u51fb\u201cPython 3.10.0 documentation\u201d\u94fe\u63a5\u3002<\/p>\n<\/p>\n<p><h4>4. \u6d4f\u89c8\u6807\u51c6\u5e93\u6587\u6863<\/h4>\n<\/p>\n<p><p>\u8fdb\u5165\u7279\u5b9a\u7248\u672c\u7684\u6587\u6863\u9875\u9762\u540e\uff0c\u4f60\u4f1a\u770b\u5230\u4e00\u4e2a\u4fa7\u8fb9\u680f\uff0c\u5217\u51fa\u4e86\u8be5\u7248\u672c\u7684\u6240\u6709\u6587\u6863\u5185\u5bb9\u3002\u5728\u4fa7\u8fb9\u680f\u4e2d\uff0c\u627e\u5230\u5e76\u70b9\u51fb\u201cLibrary Reference\u201d\u6216\u201c\u6807\u51c6\u5e93\u201d\u94fe\u63a5\u3002\u8fd9\u4e2a\u90e8\u5206\u8be6\u7ec6\u5217\u51fa\u4e86Python\u6807\u51c6\u5e93\u4e2d\u7684\u6240\u6709\u6a21\u5757\u3001\u5305\u548c\u5185\u7f6e\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h4>5. \u67e5\u627e\u5177\u4f53\u6a21\u5757<\/h4>\n<\/p>\n<p><p>\u5728\u6807\u51c6\u5e93\u6587\u6863\u4e2d\uff0c\u4f60\u53ef\u4ee5\u6309\u7167\u5b57\u6bcd\u987a\u5e8f\u6d4f\u89c8\u6240\u6709\u6a21\u5757\u3002\u6bcf\u4e2a\u6a21\u5757\u9875\u9762\u90fd\u4f1a\u63d0\u4f9b\u6a21\u5757\u7684\u8be6\u7ec6\u4fe1\u606f\uff0c\u5305\u62ec\u529f\u80fd\u63cf\u8ff0\u3001\u4f7f\u7528\u793a\u4f8b\u548cAPI\u53c2\u8003\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u60f3\u4e86\u89e3\u5982\u4f55\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\uff0c\u4f60\u53ef\u4ee5\u5728\u5217\u8868\u4e2d\u627e\u5230<code>datetime<\/code>\u5e76\u70b9\u51fb\u5b83\uff0c\u8fdb\u5165\u8be5\u6a21\u5757\u7684\u8be6\u7ec6\u6587\u6863\u9875\u9762\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u901a\u8fc7pip\u5de5\u5177\u641c\u7d22\u548c\u5b89\u88c5\u6a21\u5757<\/h3>\n<\/p>\n<p><h4>1. \u4ec0\u4e48\u662fpip<\/h4>\n<\/p>\n<p><p><code>pip<\/code>\u662fPython\u7684\u5305\u7ba1\u7406\u5de5\u5177\uff0c\u5141\u8bb8\u4f60\u4ecePython\u5b98\u65b9\u5305\u4ed3\u5e93PyPI\uff08Python Package Index\uff09\u4e0b\u8f7d\u5e76\u5b89\u88c5\u6a21\u5757\u3002\u5b83\u662f\u4e00\u4e2a\u547d\u4ee4\u884c\u5de5\u5177\uff0c\u975e\u5e38\u6613\u4e8e\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h4>2. \u5b89\u88c5\u548c\u5347\u7ea7pip<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86pip\u3002\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662fPython 3.4\u53ca\u4ee5\u4e0a\u7248\u672c\uff0c\u90a3\u4e48pip\u662f\u9ed8\u8ba4\u5b89\u88c5\u7684\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u6765\u68c0\u67e5pip\u662f\u5426\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pip --version<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u6ca1\u6709\u5b89\u88c5pip\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">python -m ensurepip --default-pip<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e3a\u4e86\u786e\u4fddpip\u662f\u6700\u65b0\u7248\u672c\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5347\u7ea7\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pip install --upgrade pip<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u641c\u7d22\u6a21\u5757<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528pip\u7684<code>search<\/code>\u547d\u4ee4\u53ef\u4ee5\u641c\u7d22PyPI\u4e0a\u7684\u6a21\u5757\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u60f3\u67e5\u627e\u4e0e\u201crequests\u201d\u76f8\u5173\u7684\u6a21\u5757\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pip search requests<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4. \u5b89\u88c5\u6a21\u5757<\/h4>\n<\/p>\n<p><p>\u4e00\u65e6\u627e\u5230\u4f60\u9700\u8981\u7684\u6a21\u5757\uff0c\u53ef\u4ee5\u4f7f\u7528<code>install<\/code>\u547d\u4ee4\u6765\u5b89\u88c5\u5b83\u3002\u4f8b\u5982\uff0c\u8981\u5b89\u88c5<code>requests<\/code>\u6a21\u5757\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pip install requests<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u4f60\u5c31\u53ef\u4ee5\u5728\u4f60\u7684Python\u4ee3\u7801\u4e2d\u5bfc\u5165\u5e76\u4f7f\u7528\u8be5\u6a21\u5757\u4e86\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u901a\u8fc7PyPI\u5b98\u7f51\u6d4f\u89c8\u548c\u4e0b\u8f7d\u6a21\u5757<\/h3>\n<\/p>\n<p><h4>1. \u8bbf\u95eePyPI\u5b98\u65b9\u7f51\u7ad9<\/h4>\n<\/p>\n<p><p>\u9664\u4e86\u901a\u8fc7pip\u5de5\u5177\u5916\uff0c\u4f60\u8fd8\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95eePyPI\u5b98\u65b9\u7f51\u7ad9 <a href=\"https:\/\/pypi.org\/\">https:\/\/pypi.org\/<\/a> \u6765\u6d4f\u89c8\u548c\u4e0b\u8f7dPython\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><h4>2. \u641c\u7d22\u6a21\u5757<\/h4>\n<\/p>\n<p><p>\u5728PyPI\u9996\u9875\uff0c\u4f60\u53ef\u4ee5\u770b\u5230\u4e00\u4e2a\u641c\u7d22\u680f\u3002\u5728\u641c\u7d22\u680f\u4e2d\u8f93\u5165\u4f60\u611f\u5174\u8da3\u7684\u6a21\u5757\u540d\u79f0\uff0c\u7136\u540e\u70b9\u51fb\u641c\u7d22\u6309\u94ae\u3002\u4f8b\u5982\uff0c\u8f93\u5165\u201crequests\u201d\u5e76\u641c\u7d22\u3002<\/p>\n<\/p>\n<p><h4>3. \u6d4f\u89c8\u6a21\u5757\u8be6\u60c5<\/h4>\n<\/p>\n<p><p>\u641c\u7d22\u7ed3\u679c\u9875\u9762\u4f1a\u5217\u51fa\u6240\u6709\u4e0e\u641c\u7d22\u8bcd\u76f8\u5173\u7684\u6a21\u5757\u3002\u70b9\u51fb\u4f60\u611f\u5174\u8da3\u7684\u6a21\u5757\u540d\u79f0\uff0c\u8fdb\u5165\u8be5\u6a21\u5757\u7684\u8be6\u7ec6\u9875\u9762\u3002\u5728\u6a21\u5757\u8be6\u60c5\u9875\u9762\uff0c\u4f60\u53ef\u4ee5\u770b\u5230\u8be5\u6a21\u5757\u7684\u7b80\u4ecb\u3001\u5b89\u88c5\u8bf4\u660e\u3001\u4f7f\u7528\u793a\u4f8b\u3001\u9879\u76ee\u94fe\u63a5\u7b49\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h4>4. \u4e0b\u8f7d\u548c\u5b89\u88c5\u6a21\u5757<\/h4>\n<\/p>\n<p><p>\u6a21\u5757\u8be6\u60c5\u9875\u9762\u901a\u5e38\u4f1a\u63d0\u4f9b\u5b89\u88c5\u547d\u4ee4\u3002\u4f8b\u5982\uff0c\u4f60\u4f1a\u770b\u5230\u7c7b\u4f3c\u4ee5\u4e0b\u7684\u5b89\u88c5\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pip install requests<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u590d\u5236\u5e76\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884c\u8be5\u547d\u4ee4\u6765\u5b89\u88c5\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u6a21\u5757\u5e93\u7684\u6700\u4f73\u5b9e\u8df5<\/h3>\n<\/p>\n<p><h4>1. \u9605\u8bfb\u5b98\u65b9\u6587\u6863<\/h4>\n<\/p>\n<p><p>\u65e0\u8bba\u4f60\u901a\u8fc7\u54ea\u79cd\u65b9\u5f0f\u8bbf\u95eePython\u6a21\u5757\u5e93\uff0c\u9605\u8bfb\u5b98\u65b9\u6587\u6863\u59cb\u7ec8\u662f\u6700\u4f73\u5b9e\u8df5\u3002\u5b98\u65b9\u6587\u6863\u63d0\u4f9b\u4e86\u6a21\u5757\u7684\u8be6\u7ec6\u8bf4\u660e\u548c\u4f7f\u7528\u793a\u4f8b\uff0c\u6709\u52a9\u4e8e\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528\u8be5\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u865a\u62df\u73af\u5883<\/h4>\n<\/p>\n<p><p>\u5728\u5b89\u88c5\u548c\u4f7f\u7528Python\u6a21\u5757\u65f6\uff0c\u5efa\u8bae\u4f7f\u7528\u865a\u62df\u73af\u5883\u3002\u865a\u62df\u73af\u5883\u5141\u8bb8\u4f60\u5728\u9694\u79bb\u7684\u73af\u5883\u4e2d\u5b89\u88c5\u6a21\u5757\uff0c\u907f\u514d\u4e0e\u7cfb\u7edfPython\u73af\u5883\u4e2d\u7684\u6a21\u5757\u53d1\u751f\u51b2\u7a81\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u521b\u5efa\u548c\u6fc0\u6d3b\u865a\u62df\u73af\u5883\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">python -m venv myenv<\/p>\n<p>source myenv\/bin\/activate  # Linux\u548cmacOS<\/p>\n<p>myenv\\Scripts\\activate  # Windows<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u5b9a\u671f\u66f4\u65b0\u6a21\u5757<\/h4>\n<\/p>\n<p><p>\u5b9a\u671f\u66f4\u65b0\u4f60\u5b89\u88c5\u7684\u6a21\u5757\u53ef\u4ee5\u786e\u4fdd\u4f60\u4f7f\u7528\u7684\u662f\u6700\u65b0\u7248\u672c\uff0c\u4eab\u53d7\u6700\u65b0\u7684\u529f\u80fd\u548c\u4fee\u590d\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u66f4\u65b0\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pip install --upgrade &lt;module_name&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u66f4\u65b0<code>requests<\/code>\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pip install --upgrade requests<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5e38\u7528Python\u6a21\u5757\u4ecb\u7ecd<\/h3>\n<\/p>\n<p><h4>1. requests<\/h4>\n<\/p>\n<p><p><code>requests<\/code>\u662f\u4e00\u4e2a\u975e\u5e38\u6d41\u884c\u7684HTTP\u5e93\uff0c\u7b80\u5316\u4e86\u5728Python\u4e2d\u53d1\u8d77HTTP\u8bf7\u6c42\u7684\u8fc7\u7a0b\u3002\u5b83\u652f\u6301GET\u3001POST\u3001PUT\u3001DELETE\u7b49HTTP\u65b9\u6cd5\uff0c\u63d0\u4f9b\u4e86\u7b80\u5355\u6613\u7528\u7684API\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>response = requests.get(&#39;https:\/\/api.github.com&#39;)<\/p>\n<p>print(response.status_code)<\/p>\n<p>print(response.json())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. numpy<\/h4>\n<\/p>\n<p><p><code>numpy<\/code>\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u503c\u8ba1\u7b97\u5e93\uff0c\u63d0\u4f9b\u4e86\u591a\u7ef4\u6570\u7ec4\u5bf9\u8c61\u548c\u8bb8\u591a\u6709\u7528\u7684\u51fd\u6570\uff0c\u7528\u4e8e\u6267\u884c\u5404\u79cd\u6570\u5b66\u8fd0\u7b97\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>array = np.array([1, 2, 3, 4])<\/p>\n<p>print(array)<\/p>\n<p>print(np.mean(array))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. pandas<\/h4>\n<\/p>\n<p><p><code>pandas<\/code>\u662f\u4e00\u4e2a\u6570\u636e\u5206\u6790\u5e93\uff0c\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u636e\u7ed3\u6784\u548c\u6570\u636e\u5206\u6790\u5de5\u5177\u3002\u5b83\u7684\u4e3b\u8981\u6570\u636e\u7ed3\u6784\u662fDataFrame\uff0c\u7c7b\u4f3c\u4e8e\u7535\u5b50\u8868\u683c\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>data = {<\/p>\n<p>    &#39;Name&#39;: [&#39;Alice&#39;, &#39;Bob&#39;, &#39;Charlie&#39;],<\/p>\n<p>    &#39;Age&#39;: [25, 30, 35]<\/p>\n<p>}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>print(df)<\/p>\n<p>print(df.describe())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4. matplotlib<\/h4>\n<\/p>\n<p><p><code>matplotlib<\/code>\u662f\u4e00\u4e2a\u7ed8\u56fe\u5e93\uff0c\u7528\u4e8e\u521b\u5efa\u5404\u79cd\u56fe\u8868\u548c\u53ef\u89c6\u5316\u3002\u5b83\u4e0e<code>numpy<\/code>\u548c<code>pandas<\/code>\u7d27\u5bc6\u96c6\u6210\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u7ed8\u5236\u6570\u636e\u56fe\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>import numpy as np<\/p>\n<p>x = np.linspace(0, 10, 100)<\/p>\n<p>y = np.sin(x)<\/p>\n<p>plt.plot(x, y)<\/p>\n<p>plt.xlabel(&#39;x&#39;)<\/p>\n<p>plt.ylabel(&#39;sin(x)&#39;)<\/p>\n<p>plt.title(&#39;Sine Wave&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>5. scikit-learn<\/h4>\n<\/p>\n<p><p><code>scikit-learn<\/code>\u662f\u4e00\u4e2a<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u5e93\uff0c\u63d0\u4f9b\u4e86\u8bb8\u591a\u5e38\u7528\u7684\u673a\u5668\u5b66\u4e60\u7b97\u6cd5\u548c\u5de5\u5177\uff0c\u7528\u4e8e\u6570\u636e\u6316\u6398\u548c\u6570\u636e\u5206\u6790\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.datasets import load_iris<\/p>\n<p>from sklearn.model_selection import tr<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n_test_split<\/p>\n<p>from sklearn.ensemble import RandomForestClassifier<\/p>\n<p>from sklearn.metrics import accuracy_score<\/p>\n<p>data = load_iris()<\/p>\n<p>X_train, X_test, y_train, y_test = train_test_split(data.data, data.target, test_size=0.2)<\/p>\n<p>model = RandomForestClassifier()<\/p>\n<p>model.fit(X_train, y_train)<\/p>\n<p>predictions = model.predict(X_test)<\/p>\n<p>print(accuracy_score(y_test, predictions))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u8bbf\u95eePython\u5b98\u7f51\u6a21\u5757\u5e93\uff0c\u5b66\u4e60\u548c\u4f7f\u7528\u5404\u79cdPython\u6a21\u5757\u3002\u65e0\u8bba\u662f\u901a\u8fc7\u5b98\u7f51\u6587\u6863\u3001pip\u5de5\u5177\u8fd8\u662fPyPI\u7f51\u7ad9\uff0c\u4e30\u5bcc\u7684\u6a21\u5757\u8d44\u6e90\u5c06\u5e2e\u52a9\u4f60\u66f4\u9ad8\u6548\u5730\u8fdb\u884cPython\u7f16\u7a0b\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u627e\u5230Python\u5b98\u7f51\u6a21\u5757\u5e93\u7684\u5165\u53e3\uff1f<\/strong><br \/>\u8981\u8bbf\u95eePython\u5b98\u7f51\u6a21\u5757\u5e93\uff0c\u60a8\u53ef\u4ee5\u76f4\u63a5\u524d\u5f80Python\u7684\u5b98\u65b9\u7f51\u7ad9\uff08python.org\uff09\u3002\u5728\u4e3b\u9875\u4e0a\uff0c\u5bfc\u822a\u5230\u201c\u6587\u6863\u201d\u90e8\u5206\uff0c\u60a8\u4f1a\u627e\u5230\u201c\u6807\u51c6\u5e93\u201d\u94fe\u63a5\uff0c\u70b9\u51fb\u540e\u5373\u53ef\u8fdb\u5165\u8be6\u7ec6\u7684\u6a21\u5757\u5e93\u4ecb\u7ecd\uff0c\u5305\u62ec\u5404\u79cd\u6a21\u5757\u7684\u529f\u80fd\u548c\u4f7f\u7528\u793a\u4f8b\u3002<\/p>\n<p><strong>Python\u5b98\u7f51\u6a21\u5757\u5e93\u4e2d\u6709\u54ea\u4e9b\u91cd\u8981\u7684\u6a21\u5757\u53ef\u4ee5\u4f7f\u7528\uff1f<\/strong><br \/>Python\u5b98\u7f51\u6a21\u5757\u5e93\u5305\u542b\u4e86\u8bb8\u591a\u91cd\u8981\u7684\u6a21\u5757\uff0c\u5982<code>os<\/code>\u6a21\u5757\u7528\u4e8e\u64cd\u4f5c\u7cfb\u7edf\u4ea4\u4e92\uff0c<code>sys<\/code>\u6a21\u5757\u7528\u4e8e\u8bbf\u95ee\u4e0ePython\u89e3\u91ca\u5668\u76f8\u5173\u7684\u53d8\u91cf\u548c\u51fd\u6570\uff0c<code>datetime<\/code>\u6a21\u5757\u7528\u4e8e\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u7b49\u3002\u6bcf\u4e2a\u6a21\u5757\u90fd\u6709\u8be6\u7ec6\u7684\u6587\u6863\uff0c\u63d0\u4f9b\u4e86\u51fd\u6570\u3001\u7c7b\u548c\u5e38\u7528\u65b9\u6cd5\u7684\u8bf4\u660e\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u9879\u76ee\u4e2d\u5b89\u88c5\u548c\u4f7f\u7528\u5b98\u7f51\u6a21\u5757\u5e93\u7684\u6a21\u5757\uff1f<\/strong><br \/>\u5927\u591a\u6570Python\u5b98\u7f51\u6a21\u5757\u5e93\u4e2d\u7684\u6a21\u5757\u90fd\u662f\u81ea\u5e26\u7684\uff0c\u65e0\u9700\u5355\u72ec\u5b89\u88c5\u3002\u60a8\u53ea\u9700\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528<code>import<\/code>\u8bed\u53e5\u5bfc\u5165\u6240\u9700\u7684\u6a21\u5757\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>import os<\/code>\u6765\u5f15\u5165<code>os<\/code>\u6a21\u5757\u3002\u5728\u7f16\u5199\u4ee3\u7801\u65f6\uff0c\u5efa\u8bae\u67e5\u9605\u5bf9\u5e94\u6a21\u5757\u7684\u5b98\u65b9\u6587\u6863\uff0c\u4ee5\u83b7\u53d6\u6700\u65b0\u7684\u529f\u80fd\u548c\u4f7f\u7528\u6280\u5de7\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u8fdb\u5165Python\u5b98\u7f51\u6a21\u5757\u5e93\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u8bbf\u95eePython\u5b98\u65b9\u7f51\u7ad9\u5e76\u5bfc\u822a\u81f3\u5176\u6587\u6863\u90e8\u5206\u3001\u76f4\u63a5\u4f7f\u7528Python\u81ea\u5e26\u7684 [&hellip;]","protected":false},"author":3,"featured_media":1071429,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1071420"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1071420"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1071420\/revisions"}],"predecessor-version":[{"id":1071430,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1071420\/revisions\/1071430"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1071429"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1071420"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1071420"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1071420"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}