{"id":2094,"date":"2023-04-01T22:05:02","date_gmt":"2023-04-01T14:05:02","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=2094"},"modified":"2023-04-06T13:57:57","modified_gmt":"2023-04-06T05:57:57","slug":"numpy-linalg-learning","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/04\/01\/numpy-linalg-learning\/","title":{"rendered":"numpy.linalg\u5b66\u4e60"},"content":{"rendered":"<h2>\u77e9\u9635\u548c\u5411\u91cf\u79ef<\/h2>\n<h3>\u4e24\u4e2a\u6570\u7ec4\u70b9\u79ef<\/h3>\n<pre><code class=\"language-python\">numpy.dot(a, b, out=None)<\/code><\/pre>\n<p><!-- more --><\/p>\n<h4>a\u3001b\u90fd\u662f\u5e38\u91cf\u6216\u4e00\u7ef4\u6570\u7ec4\uff0c\u5219\u8fd4\u56de\u6807\u91cf<\/h4>\n<p>\u5bf9\u4e8e\u4e00\u7ef4\u6570\u7ec4\uff0c\u5176\u7ed3\u679c\u7b49\u4e8e\u4e24\u5411\u91cf\u7684\u5185\u79ef\uff1a\u8bbe\u5411\u91cf $a=(x_1, y_1)$\uff0c\u5411\u91cf$b=(x_2, y_2)$\uff0c\u7ed3\u679c\u7b49\u4e8e$x_1<em>x_2 + y_1<\/em>y_2$<\/p>\n<pre><code class=\"language-python\">import numpy as np\n\nr1 = np.dot(3, 4)\nr2 = np.dot([1, 2, 3], [4, 5, 6])\nprint(r1)  # 12\nprint(r2)  # 32<\/code><\/pre>\n<h4>a\u3001b\u90fd\u662f\u4e8c\u7ef4\u6570\u7ec4\uff0c\u76f8\u5f53\u4e8e\u77e9\u9635\u7684\u4e58\u6cd5<\/h4>\n<p>\u77e9\u9635\u4e58\u6cd5\uff0c\u7b2c\u4e00\u4e2a\u77e9\u9635$A$\u7684\u884c\u6570\u5fc5\u987b\u7b49\u4e8e\u7b2c\u4e8c\u4e2a\u77e9\u9635$B$\u7684\u5217\u6570\uff1b\u77e9\u9635$A$\u4e58\u4ee5\u77e9\u9635$B$\u5f97\u5230\u7684\u7ed3\u679c\u77e9\u9635$C$\uff0c\u5176\u7b2cm\u884cn\u5217\u5143\u7d20\u7b49\u4e8e\u77e9\u9635$A$\u7684\u7b2cm\u884c\u5143\u7d20\u4e58\u4ee5\u77e9\u9635$B$\u7b2cn\u5217\u5bf9\u5e94\u5143\u7d20\u4e4b\u548c\u3002<\/p>\n<pre><code class=\"language-python\">import numpy as np\n\na = np.array([[1, 2], [3, 4], [2, 5]])\nb = np.array([[2, 3, 1], [4, 5, 2]])\nc = np.dot(a, b)\nprint(c)\n\n&#039;&#039;&#039;\n[[10 13  5]\n [22 29 11]\n [24 31 12]]\n&#039;&#039;&#039;<\/code><\/pre>\n<h4>a\u3001b\u90fd\u662fN\u7ef4\uff0c\u5176\u7ed3\u679c\u7b49\u4e8ea\u7684\u6700\u540e\u4e00\u8f74\u548cb\u7684\u5012\u6570\u7b2c\u4e8c\u8f74\u79ef\u4e4b\u548c<\/h4>\n<pre><code class=\"language-python\">import numpy as np\n\na = np.array(range(12)).reshape(2, 3, 1, 2)\nb = np.array(range(12)).reshape(3, 2, 2)\nnp.dot(a, b)\nprint(a)\nprint(b)\nprint(c)\nprint(c.shape)\n\n&#039;&#039;&#039;\n[[[[ 0  1]]\n  [[ 2  3]]\n  [[ 4  5]]]\n [[[ 6  7]]\n  [[ 8  9]]\n  [[10 11]]]]\n\n[[[ 0  1]\n  [ 2  3]]\n [[ 4  5]\n  [ 6  7]]\n [[ 8  9]\n  [10 11]]]\n\n[[[[[  2   3]\n    [  6   7]\n    [ 10  11]]]\n  [[[  6  11]\n    [ 26  31]\n    [ 46  51]]]\n  [[[ 10  19]\n    [ 46  55]\n    [ 82  91]]]]\n [[[[ 14  27]\n    [ 66  79]\n    [118 131]]]\n  [[[ 18  35]\n    [ 86 103]\n    [154 171]]]\n  [[[ 22  43]\n    [106 127]\n    [190 211]]]]]\n\n(2, 3, 1, 3, 2)\n&#039;&#039;&#039;<\/code><\/pre>\n<h3>\u4e24\u5411\u91cf\u70b9\u79ef<\/h3>\n<pre><code class=\"language-python\">numpy.vdot(a, b)<\/code><\/pre>\n<h4>\u53c2\u6570a\u3001b\u90fd\u662f\u9ad8\u7ef4\u6570\u7ec4<\/h4>\n<p>vdot\u5904\u7406\u591a\u7ef4\u6570\u7ec4\u548cdot\u5904\u7406\u65b9\u5f0f\u4e0d\u540c\uff0c\u4e0d\u662f\u6267\u884c\u77e9\u9635\u4e58\u79ef\uff0c\u53ea\u80fd\u6267\u884c\u5411\u91cf\u70b9\u79ef\uff0c\u5219\u9700\u5c06\u6570\u7ec4\u5148\u6241\u5e73\u5316\uff0c\u7136\u540e\u518d\u8ba1\u7b97<\/p>\n<pre><code class=\"language-python\">import numpy as np\n\na = np.array([[1, 4], [5, 6]])\nb = np.array([[4, 1], [2, 2]])\nc = np.vdot(a, b)\nprint(c)\n# a\u3001b\u6570\u7ec4\u6241\u5e73\u5316\u5373\u5c06\u591a\u7ef4\u6570\u7ec4\u8f6c\u6362\u6210\u4e00\u7ef4\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528ravel\u51fd\u6570\u5904\u7406\nc = np.vdot(a.ravel(), b.ravel())\nprint(c)\n\n&#039;&#039;&#039;\n30\n30\n&#039;&#039;&#039;<\/code><\/pre>\n<h4>\u53c2\u6570a\u3001b\u4e3a\u590d\u6570<\/h4>\n<pre><code class=\"language-python\">import numpy as np\n\na = np.array([1+2j, 3+4j])\nb = np.array([5+6j, 7+8j])\nc = np.vdot(a, b)\nprint(c)\nc = np.vdot(b, a)\nprint(c)\n\n&#039;&#039;&#039;\n(70-8j)\n(70+8j)\n&#039;&#039;&#039;<\/code><\/pre>\n<p>\u901a\u8fc7\u4e0a\u8ff0\u7ed3\u679c\u53ef\u77e5\uff1a<code>np.vdot(a,b)<\/code>\u548c<code>np.vdot(b,a)<\/code>\u8ba1\u7b97\u51fa\u6765\u7684\u7ed3\u679c\u521a\u597d\u4e92\u4e3a\u5171\u8f6d\u590d\u6570\u5173\u7cfb(\u5b9e\u90e8\u76f8\u540c\uff0c\u865a\u90e8\u4e92\u4e3a\u76f8\u53cd\u6570)\uff0c\u5176\u8ba1\u7b97\u7ed3\u679c\u4e3a\u53d6vdot\u51fd\u6570\u4e2d\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u7684\u5171\u8f6d\u590d\u6570\u4e0e\u53e6\u5916\u4e00\u4e2a\u53c2\u6570\u70b9\u79ef\u3002<\/p>\n<p>\u4ee5np.vdot(a,b)\u8ba1\u7b97\u4e3a\u4f8b\uff1a<\/p>\n<p>\u7b2c\u4e00\u6b65\uff1a\u8ba1\u7b97a\u7684\u5171\u8f6d\u590d\u6570<\/p>\n<pre><code class=\"language-python\">c = np.array([1-2j, 3-4j])<\/code><\/pre>\n<p>\u7b2c\u4e8c\u6b65\uff1a\u8ba1\u7b97c\u4e0eb\u7684\u70b9\u79ef<\/p>\n<pre><code class=\"language-python\">import numpy as np\n\na = np.array([1+2j, 3+4j])\nb = np.array([5+6j, 7+8j])\nc = np.array([1-2j, 3-4j])\nd = np.array([5-6j, 7-8j])\nnp.dot(c, b)\nnp.dot(d, a)\n\n&#039;&#039;&#039;\n(70-8j)\n(70+8j)\n&#039;&#039;&#039;<\/code><\/pre>\n<h2>\u6c42\u89e3\u65b9\u7a0b\u4e0e\u6c42\u9006\u77e9\u9635<\/h2>\n<h3>\u9006\u77e9\u9635<\/h3>\n<pre><code class=\"language-python\">numpy.linalg.inv(a)<\/code><\/pre>\n<pre><code class=\"language-python\">import numpy as np\nfrom numpy.linalg import inv\n\na = np.array([[1., 2.], [3., 4.]])\nb = inv(a)\nprint(b)\nc = np.dot(a, b)\nprint(c)\n# \u6bd4\u8f83\u4e24\u6570\u7ec4: np.allclose(a, b, rtol=1e-05, atol=1e-08, equal_nan=False)\ne = np.allclose(c, np.eye(2))\nprint(e)\n\n&#039;&#039;&#039;\n[[-2.   1. ]\n [ 1.5 -0.5]]\n\n[[1.0000000e+00 0.0000000e+00]\n [8.8817842e-16 1.0000000e+00]]\n\nTrue\n&#039;&#039;&#039;<\/code><\/pre>\n<p>\u77e5\u8bc6\u70b9\uff1a\u77e9\u9635\u4e0e\u5176\u9006\u77e9\u9635\u70b9\u79ef\u7b49\u4e8e\u540c\u9636\u5355\u4f4d\u77e9\u9635\uff0c\u6c42\u89e3\u9006\u77e9\u9635\u7684\u65b9\u6cd5\u6709\u5f85\u5b9a\u7cfb\u6570\u6cd5\u3001\u4f34\u968f\u77e9\u9635\u6cd5\u3001\u521d\u7b49\u53d8\u6362\u6cd5<\/p>\n<h3>\u6700\u5c0f\u4e8c\u4e58\u6cd5<\/h3>\n<pre><code class=\"language-python\">numpy.linalg.lstsq(a, b, rcond=-1)<\/code><\/pre>\n<h4>b\u4e3a\u4e00\u7ef4\u6570\u7ec4<\/h4>\n<pre><code class=\"language-python\">import numpy as np\nfrom numpy.linalg import lstsq\n\nX = np.array([[1, 6, 2], [1, 8, 1], [1, 10, 0], [1, 14, 2], [1, 18, 0]])\nY = np.array([[7], [9], [13], [17.5], [18]])\nz = np.linalg.lstsq(X, Y, rcond=None)\nprint(z)\n\n&#039;&#039;&#039;\n(array([[1.1875    ],\n       [1.01041667],\n       [0.39583333]]), array([8.22916667]), 3, array([26.97402951,  2.46027806,  0.59056212]))\n&#039;&#039;&#039;<\/code><\/pre>\n<p>\u4ece\u4e0a\u8ff0\u7ed3\u679c\u53ef\u77e5\uff1a\u8fd4\u56de\u5143\u7ec4\uff0c\u5143\u7ec4\u4e2d\u56db\u4e2a\u5143\u7d20\uff0c\u7b2c\u4e00\u5143\u7d20\u8868\u793a\u6240\u6c42\u7684\u6700\u5c0f\u4e8c\u4e58\u89e3\uff0c\u7b2c\u4e8c\u4e2a\u5143\u7d20\u8868\u793a\u6b8b\u5dee\u603b\u548c\uff0c\u7b2c\u4e09\u4e2a\u5143\u7d20\u8868\u793aX\u77e9\u9635\u79e9\uff0c\u7b2c\u56db\u4e2a\u5143\u7d20\u8868\u793aY\u7684\u5947\u5f02\u503c<\/p>\n<h4>b\u4e3a\u591a\u7ef4\u6570\u7ec4<\/h4>\n<pre><code class=\"language-python\">import numpy as np\nfrom numpy.linalg import lstsq\n\nX = np.array([[1, 6, 2], [1, 8, 1], [1, 10, 0], [1, 14, 2], [1, 18, 0]])\nY = np.array([[7, 8], [9, 7], [13, 10], [17.5, 16], [18, 17]])\nz = np.linalg.lstsq(X, Y, rcond=None)\nprint(z)\n\n&#039;&#039;&#039;\n(array([[ 1.1875    , -1.125     ],\n       [ 1.01041667,  1.02083333],\n       [ 0.39583333,  1.29166667]]), array([8.22916667, 2.91666667]), 3, array([26.97402951,  2.46027806,  0.59056212]))\n&#039;&#039;&#039;<\/code><\/pre>\n<p>\u901a\u8fc7\u4e0a\u9762\u4e24\u4e2a\u7ed3\u679c\u5bf9\u6bd4\u5206\u6790\uff1a\u53c2\u6570b\u7ef4\u5ea6\u589e\u52a0\uff0c\u7b2c\u4e00\u4e2a\u3001\u7b2c\u4e8c\u4e2a\u5143\u7d20\u6570\u7ec4\u7ef4\u5ea6\u4e5f\u53d8\u5316\uff0c\u5176\u5bf9\u5e94\u7684\u7b2ck\u5217\u5206\u522b\u8868\u793a\u5bf9b\u6570\u7ec4\u4e2d\u7b2ck\u5217\u7684\u6700\u5c0f\u4e8c\u4e58\u6cd5\u6c42\u89e3\u3001\u6b8b\u5dee\u603b\u548c<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u77e9\u9635\u548c\u5411\u91cf\u79ef \u4e24\u4e2a\u6570\u7ec4\u70b9\u79ef numpy.dot(a, b, out=None) a\u3001b\u90fd\u662f\u5e38\u91cf\u6216\u4e00\u7ef4\u6570\u7ec4\uff0c\u5219\u8fd4 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[527],"tags":[528],"class_list":["post-2094","post","type-post","status-publish","format-standard","hentry","category-ai","tag-numpy"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2094","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=2094"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2094\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2094"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2094"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2094"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}