{"id":953532,"date":"2024-12-27T01:44:38","date_gmt":"2024-12-26T17:44:38","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/953532.html"},"modified":"2024-12-27T01:44:42","modified_gmt":"2024-12-26T17:44:42","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e8%ae%be%e8%ae%a1%e7%9f%a9%e9%98%b5","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/953532.html","title":{"rendered":"\u5982\u4f55\u7528python\u8bbe\u8ba1\u77e9\u9635"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25091010\/e7143ce3-28e3-48ef-8bc2-96d51fedeb90.webp\" alt=\"\u5982\u4f55\u7528python\u8bbe\u8ba1\u77e9\u9635\" \/><\/p>\n<p><p> <strong>\u7528Python\u8bbe\u8ba1\u77e9\u9635\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528\u5217\u8868\u5d4c\u5957\u3001NumPy\u5e93\u548cPandas\u5e93\u7b49\u3002\u4f7f\u7528\u5217\u8868\u5d4c\u5957\u6784\u5efa\u7b80\u5355\u77e9\u9635\u3001\u5229\u7528NumPy\u5e93\u5904\u7406\u5927\u89c4\u6a21\u77e9\u9635\u8fd0\u7b97\u3001\u501f\u52a9Pandas\u5e93\u8fdb\u884c\u6570\u636e\u5206\u6790\u662f\u5e38\u7528\u7684\u4e09\u79cd\u65b9\u6cd5\u3002<\/strong>\u4e0b\u9762\uff0c\u6211\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5176\u4e2d\u7684\u4e00\u79cd\u65b9\u6cd5\uff0c\u5373\u4f7f\u7528NumPy\u5e93\u6765\u8bbe\u8ba1\u548c\u64cd\u4f5c\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528NumPy\u5e93\u6765\u5904\u7406\u77e9\u9635\u662f\u56e0\u4e3a\u5176\u5f3a\u5927\u7684\u529f\u80fd\u548c\u9ad8\u6548\u7684\u5904\u7406\u80fd\u529b\u3002NumPy\u662fPython\u4e2d\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u7684\u57fa\u7840\u5305\uff0c\u652f\u6301\u591a\u7ef4\u6570\u7ec4\u548c\u77e9\u9635\u8fd0\u7b97\uff0c\u5e76\u63d0\u4f9b\u5927\u91cf\u7684\u6570\u5b66\u51fd\u6570\u3002\u5229\u7528NumPy\u5e93\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u521b\u5efa\u3001\u64cd\u4f5c\u548c\u5904\u7406\u77e9\u9635\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528NumPy\u5e93\u6765\u8bbe\u8ba1\u548c\u64cd\u4f5c\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5b89\u88c5\u548c\u5bfc\u5165NumPy\u5e93<\/p>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u4f7f\u7528NumPy\u5e93\u4e4b\u524d\uff0c\u9700\u8981\u786e\u4fdd\u5728\u4f60\u7684Python\u73af\u5883\u4e2d\u5df2\u7ecf\u5b89\u88c5\u4e86NumPy\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u5728\u4f60\u7684Python\u811a\u672c\u4e2d\u5bfc\u5165NumPy\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u521b\u5efa\u77e9\u9635<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528\u6570\u7ec4\u521b\u5efa\u77e9\u9635<\/strong><\/li>\n<\/ol>\n<p><p>NumPy\u4e2d\u7684\u6570\u7ec4\u5bf9\u8c61\u662f\u521b\u5efa\u77e9\u9635\u7684\u57fa\u7840\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u5217\u8868\u5d4c\u5957\u6765\u521b\u5efa\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\uff0c\u5373\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])<\/p>\n<p>print(matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u521b\u5efa\u4e86\u4e00\u4e2a3&#215;3\u7684\u77e9\u9635\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u4f7f\u7528NumPy\u51fd\u6570\u521b\u5efa\u7279\u6b8a\u77e9\u9635<\/strong><\/li>\n<\/ol>\n<p><p>NumPy\u63d0\u4f9b\u4e86\u4e00\u4e9b\u51fd\u6570\u6765\u521b\u5efa\u7279\u6b8a\u7684\u77e9\u9635\uff0c\u6bd4\u5982\u96f6\u77e9\u9635\u3001\u5355\u4f4d\u77e9\u9635\u3001\u968f\u673a\u77e9\u9635\u7b49\u3002<\/p>\n<\/p>\n<ul>\n<li>\u96f6\u77e9\u9635<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">zero_matrix = np.zeros((3, 3))<\/p>\n<p>print(zero_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li>\u5355\u4f4d\u77e9\u9635<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">identity_matrix = np.eye(3)<\/p>\n<p>print(identity_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li>\u968f\u673a\u77e9\u9635<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">random_matrix = np.random.rand(3, 3)<\/p>\n<p>print(random_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u77e9\u9635\u8fd0\u7b97<\/p>\n<\/p>\n<ol>\n<li><strong>\u77e9\u9635\u52a0\u6cd5\u548c\u51cf\u6cd5<\/strong><\/li>\n<\/ol>\n<p><p>\u77e9\u9635\u7684\u52a0\u51cf\u6cd5\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u52a0\u53f7\u548c\u51cf\u53f7\u8fdb\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">matrix_a = np.array([[1, 2], [3, 4]])<\/p>\n<p>matrix_b = np.array([[5, 6], [7, 8]])<\/p>\n<p>matrix_sum = matrix_a + matrix_b<\/p>\n<p>matrix_diff = matrix_a - matrix_b<\/p>\n<p>print(matrix_sum)<\/p>\n<p>print(matrix_diff)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u77e9\u9635\u4e58\u6cd5<\/strong><\/li>\n<\/ol>\n<p><p>\u77e9\u9635\u4e58\u6cd5\u53ef\u4ee5\u4f7f\u7528<code>np.dot()<\/code>\u51fd\u6570\u6216<code>@<\/code>\u8fd0\u7b97\u7b26\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">matrix_prod = np.dot(matrix_a, matrix_b)<\/p>\n<p>print(matrix_prod)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u77e9\u9635\u8f6c\u7f6e<\/strong><\/li>\n<\/ol>\n<p><p>\u77e9\u9635\u8f6c\u7f6e\u53ef\u4ee5\u4f7f\u7528<code>.T<\/code>\u5c5e\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">matrix_transpose = matrix_a.T<\/p>\n<p>print(matrix_transpose)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u77e9\u9635\u7684\u5176\u4ed6\u64cd\u4f5c<\/p>\n<\/p>\n<ol>\n<li><strong>\u77e9\u9635\u7684\u884c\u5217\u64cd\u4f5c<\/strong><\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u6765\u8bbf\u95ee\u548c\u64cd\u4f5c\u77e9\u9635\u7684\u884c\u548c\u5217\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bbf\u95ee\u7b2c\u4e00\u884c<\/p>\n<p>first_row = matrix[0, :]<\/p>\n<p>print(first_row)<\/p>\n<h2><strong>\u4fee\u6539\u7b2c\u4e8c\u5217<\/strong><\/h2>\n<p>matrix[:, 1] = [10, 20, 30]<\/p>\n<p>print(matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u77e9\u9635\u7684\u5f62\u72b6\u548c\u91cd\u5851<\/strong><\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>.shape<\/code>\u5c5e\u6027\u67e5\u770b\u77e9\u9635\u7684\u5f62\u72b6\uff0c\u5e76\u4f7f\u7528<code>reshape()<\/code>\u51fd\u6570\u91cd\u5851\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(matrix.shape)<\/p>\n<p>reshaped_matrix = matrix.reshape(1, 9)<\/p>\n<p>print(reshaped_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u77e9\u9635\u7684\u6c42\u9006\u548c\u884c\u5217\u5f0f<\/strong><\/li>\n<\/ol>\n<ul>\n<li>\u77e9\u9635\u6c42\u9006\u53ef\u4ee5\u4f7f\u7528<code>np.linalg.inv()<\/code>\u51fd\u6570\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">inverse_matrix = np.linalg.inv(matrix_a)<\/p>\n<p>print(inverse_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li>\u884c\u5217\u5f0f\u53ef\u4ee5\u4f7f\u7528<code>np.linalg.det()<\/code>\u51fd\u6570\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">determinant = np.linalg.det(matrix_a)<\/p>\n<p>print(determinant)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u5229\u7528Pandas\u5e93\u8fdb\u884c\u6570\u636e\u5206\u6790<\/p>\n<\/p>\n<p><p>\u9664\u4e86NumPy\u5e93\u5916\uff0cPandas\u5e93\u4e5f\u662f\u5904\u7406\u77e9\u9635\u6570\u636e\u7684\u4e00\u4e2a\u5f3a\u5927\u5de5\u5177\uff0c\u7279\u522b\u662f\u5728\u6570\u636e\u5206\u6790\u65b9\u9762\u3002Pandas\u63d0\u4f9b\u4e86DataFrame\u7ed3\u6784\uff0c\u4fbf\u4e8e\u5904\u7406\u8868\u683c\u6570\u636e\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u5b89\u88c5\u548c\u5bfc\u5165Pandas\u5e93<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-bash\">pip install pandas<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u4f7f\u7528DataFrame\u521b\u5efa\u77e9\u9635<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">data = {&#39;A&#39;: [1, 2, 3], &#39;B&#39;: [4, 5, 6], &#39;C&#39;: [7, 8, 9]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>DataFrame\u7684\u57fa\u672c\u64cd\u4f5c<\/strong><\/li>\n<\/ol>\n<ul>\n<li>\u8bbf\u95ee\u884c\u548c\u5217<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">print(df[&#39;A&#39;])<\/p>\n<p>print(df.loc[0])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li>\u6dfb\u52a0\u548c\u5220\u9664\u5217<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">df[&#39;D&#39;] = [10, 11, 12]<\/p>\n<p>print(df)<\/p>\n<p>df = df.drop(&#39;D&#39;, axis=1)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li>\u6570\u636e\u7edf\u8ba1\u548c\u5206\u6790<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">print(df.describe())<\/p>\n<p>print(df.mean())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0cPython\u53ef\u4ee5\u5728\u77e9\u9635\u7684\u521b\u5efa\u3001\u64cd\u4f5c\u548c\u5206\u6790\u65b9\u9762\u63d0\u4f9b\u5f3a\u5927\u7684\u652f\u6301\u3002\u65e0\u8bba\u662f\u4f7f\u7528NumPy\u8fdb\u884c\u79d1\u5b66\u8ba1\u7b97\uff0c\u8fd8\u662f\u5229\u7528Pandas\u8fdb\u884c\u6570\u636e\u5206\u6790\uff0c\u90fd\u80fd\u6709\u6548\u63d0\u9ad8\u5904\u7406\u77e9\u9635\u6570\u636e\u7684\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u548c\u64cd\u4f5c\u77e9\u9635\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u521b\u5efa\u77e9\u9635\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u5f0f\uff0c\u6700\u5e38\u7528\u7684\u5305\u62ec\u4f7f\u7528\u5217\u8868\u7684\u5d4c\u5957\u7ed3\u6784\u6216\u5229\u7528NumPy\u5e93\u3002\u4f7f\u7528\u5217\u8868\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u5217\u8868\u7684\u5217\u8868\u6765\u8868\u793a\u77e9\u9635\uff0c\u4f8b\u5982\uff1a<code>matrix = [[1, 2], [3, 4]]<\/code>\u3002\u800c\u4f7f\u7528NumPy\u5e93\uff0c\u521b\u5efa\u77e9\u9635\u66f4\u4e3a\u7b80\u4fbf\uff0c\u53ef\u4ee5\u901a\u8fc7<code>numpy.array()<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\uff0c\u4f8b\u5982\uff1a<code>import numpy as np; matrix = np.array([[1, 2], [3, 4]])<\/code>\u3002NumPy\u8fd8\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u77e9\u9635\u64cd\u4f5c\u529f\u80fd\uff0c\u5982\u8f6c\u7f6e\u3001\u76f8\u52a0\u3001\u76f8\u4e58\u7b49\u3002<\/p>\n<p><strong>\u5982\u4f55\u5bf9Python\u4e2d\u7684\u77e9\u9635\u6267\u884c\u6570\u5b66\u8fd0\u7b97\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u4f7f\u7528NumPy\u5e93\u53ef\u4ee5\u65b9\u4fbf\u5730\u5bf9\u77e9\u9635\u8fdb\u884c\u6570\u5b66\u8fd0\u7b97\u3002\u5e38\u89c1\u7684\u8fd0\u7b97\u5305\u62ec\u77e9\u9635\u52a0\u6cd5\u3001\u4e58\u6cd5\u548c\u8f6c\u7f6e\u7b49\u3002\u52a0\u6cd5\u53ef\u4ee5\u901a\u8fc7<code>numpy.add()<\/code>\u6216\u76f4\u63a5\u4f7f\u7528<code>+<\/code>\u8fd0\u7b97\u7b26\u6765\u5b9e\u73b0\uff1b\u4e58\u6cd5\u5219\u53ef\u4ee5\u4f7f\u7528<code>numpy.dot()<\/code>\u6216<code>@<\/code>\u8fd0\u7b97\u7b26\u3002\u8f6c\u7f6e\u53ef\u4ee5\u901a\u8fc7<code>numpy.transpose()<\/code>\u6216<code>.T<\/code>\u5c5e\u6027\u6765\u5b8c\u6210\u3002\u8fd9\u4e9b\u64cd\u4f5c\u4e0d\u4ec5\u9ad8\u6548\uff0c\u8fd8\u80fd\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u53ef\u89c6\u5316\u77e9\u9635\uff1f<\/strong><br \/>\u53ef\u89c6\u5316\u77e9\u9635\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u5de5\u5177\uff0cMatplotlib\u662f\u5176\u4e2d\u6700\u5e38\u7528\u7684\u5e93\u4e4b\u4e00\u3002\u53ef\u4ee5\u901a\u8fc7<code>matplotlib.pyplot.imshow()<\/code>\u51fd\u6570\u5c06\u77e9\u9635\u4ee5\u70ed\u56fe\u7684\u5f62\u5f0f\u5c55\u793a\u51fa\u6765\uff0c\u4f7f\u7528<code>plt.colorbar()<\/code>\u53ef\u4ee5\u6dfb\u52a0\u989c\u8272\u6761\u4ee5\u4fbf\u4e8e\u7406\u89e3\u6570\u636e\u7684\u5927\u5c0f\u3002\u6b64\u5916\uff0cSeaborn\u5e93\u4e5f\u63d0\u4f9b\u4e86\u65b9\u4fbf\u7684\u63a5\u53e3\u6765\u7ed8\u5236\u77e9\u9635\u70ed\u56fe\uff0c<code>sns.heatmap()<\/code>\u51fd\u6570\u80fd\u591f\u8ba9\u56fe\u5f62\u66f4\u52a0\u7f8e\u89c2\u548c\u6613\u4e8e\u89e3\u8bfb\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u7528Python\u8bbe\u8ba1\u77e9\u9635\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528\u5217\u8868\u5d4c\u5957\u3001NumPy\u5e93\u548cPandas\u5e93\u7b49\u3002\u4f7f\u7528\u5217\u8868\u5d4c\u5957 [&hellip;]","protected":false},"author":3,"featured_media":953539,"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\/953532"}],"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=953532"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/953532\/revisions"}],"predecessor-version":[{"id":953541,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/953532\/revisions\/953541"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/953539"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=953532"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=953532"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=953532"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}