{"id":253846,"date":"2022-10-05T09:06:30","date_gmt":"2022-10-05T01:06:30","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=253846"},"modified":"2022-09-28T11:06:50","modified_gmt":"2022-09-28T03:06:50","slug":"python-numpy-cleck","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/python-numpy-cleck.html","title":{"rendered":"python numpy\u67e5\u8be2\u5b9a\u4f4d\u8d4b\u503c\u6570\u503c\u6240\u5728\u884c\u5217\u8be6\u89e3"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4ecb\u7ecd\u4e86python numpy\u67e5\u8be2\u5b9a\u4f4d\u8d4b\u503c\u6570\u503c\u6240\u5728\u884c\u5217,\u6587\u7ae0\u56f4\u7ed5\u4e3b\u9898\u5c55\u5f00\u8be6\u7ec6\u7684\u5185\u5bb9\u4ecb\u7ecd\uff0c\u5177\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u4ef7\u503c\uff0c\u9700\u8981\u7684\u5c0f\u4f19\u4f34\u53ef\u4ee5\u53c2\u8003\u4e00\u4e0b<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u6839\u636e\u6761\u4ef6\u7b5b\u9009\u884c\uff08\u7b5b\u9009\uff09<\/strong><\/div>\n

\u7b5b\u9009\u77e9\u9635\u4e2d\u7b2c7\u5217\u503c\u4e3a5\u7684\u884c<\/p>\n

B = A[ A[:,6] == 5]<\/pre>\n

\u7b5b\u9009\u77e9\u9635\u4e2d\u7b2c7\u5217\u5927\u4e8e5\u7684\u884c<\/p>\n

B = A[ A[:,6] > 5]<\/pre>\n

Numpy\u57fa\u7840\u64cd\u4f5c<\/p>\n

\u6839\u636e\u884c\u5217\u53f7\u53d6\u503c\uff08\u67e5\u8be2\uff09<\/strong><\/div>\n

\u53d6\u7b2c2\u884c\u7b2c2\u5217\u7684\u6570\u5b57<\/p>\n

import numpy as np\r\n#\u4ea7\u751f3\u884c4\u5217\u7684\u77e9\u9635\r\nx=np.arange(0,12)\r\nx=x.reshape((3,4))\r\nprint(x)\r\n \r\ny=x[1,1]\r\nprint(y)<\/pre>\n

\u622a\u53d6\u524d\u51e0\u884c\u524d\u51e0\u5217<\/p>\n

import numpy as np\r\n#\u4ea7\u751f3\u884c4\u5217\u7684\u77e9\u9635\r\nx=np.arange(0,12)\r\nx=x.reshape((3,4))\r\nprint(x)\r\n \r\n#[a:b,c:d] \u8868\u793a\u53d6a-b\u884c\uff0cc-d\u5217\u3002a\u3001c\u53ef\u4ee5\u7701\u7565\uff0c\u8868\u793a\u4ece0\u5f00\u59cb\r\n#\u53d6x\u7684\u524d\u4e24\u884c(\u6240\u6709\u5217)\r\ny=x[:2] # \u7b49\u4ef7\u4e8e y=x[:2,:]\uff0c\u7b49\u4ef7\u4e8e y=x[0:2,...]\r\nprint(y)\r\n \r\n#\u53d6x\u7684\u524d\u4e24\u5217\uff08\u7684\u6240\u6709\u884c\uff09\r\nz=x[:,:2] # \u7b49\u4ef7\u4e8e z=x[:,0:2]\uff0c\u7b49\u4ef7\u4e8e z=x[...,0:2]\r\nprint(z)<\/pre>\n

\u622a\u53d6\u67d0\u51e0\u884c\u67d0\u51e0\u5217<\/p>\n

import numpy as np\r\n#\u4ea7\u751f3\u884c4\u5217\u7684\u77e9\u9635\r\nx=np.arange(0,12)\r\nx=x.reshape((3,4))\r\nprint(x)\r\n \r\n#[a:b,c:d] \u8868\u793a\u53d6a-b\u884c\uff0cc-d\u5217\u3002a\u3001c\u53ef\u4ee5\u7701\u7565\uff0c\u8868\u793a\u4ece0\u5f00\u59cb\r\n#\u53d6x\u7684\u7b2c2\u884c\u7b2c2-3\u5217\r\ny=x[1,1:3]<\/pre>\n
\u6839\u636e\u503c\u6c42\u884c\u5217\u53f7\uff08\u5b9a\u4f4d\uff09<\/strong><\/div>\n

\u8f93\u51fa\u67d0\u884c\u6700\u5927\u503c\u6240\u5728\u7684\u5217\u7d22\u5f15<\/p>\n

import numpy as np\r\n#\u4ea7\u751f3\u884c4\u5217\u7684\u77e9\u9635\r\nx=np.arange(0,12)\r\nx=x.reshape((3,4))\r\nprint(x)\r\n \r\n#  \u53d6\u51fax\u4e2d\u5143\u7d20\u6700\u5927\u503c\u6240\u5bf9\u5e94\u7684\u7d22\u5f15\uff0c\u6309\u7167a[0][1]\u4e2d\u7684a[1]\u65b9\u5411\uff0c\u5373\u884c\u65b9\u5411\u641c\u7d22\u6700\u5927\u503c\u3002\r\ny = np.argmax(x, 1)\r\nprint(y)<\/pre>\n

\u8f93\u51fa\u67d0\u5217\u6700\u5927\u503c\u6240\u5728\u7684\u884c\u7d22\u5f15<\/p>\n

#  \u53d6\u51fax\u4e2d\u5143\u7d20\u6700\u5927\u503c\u6240\u5bf9\u5e94\u7684\u884c\u53f7\r\ny = np.argmax(x, 0)\r\nprint(y)<\/pre>\n

\u6700\u5927\u503c\u6240\u5728\u884c\u5217\u53f7<\/p>\n

x=np.arange(0,12)\r\nx=x.reshape((3,4))\r\nprint(x)\r\n \r\n# where\u8fd4\u56de\u4e00\u4e2a\u957f\u5ea6\u4e3a2\u7684\u5143\u7ec4\uff0c\u7b2c\u4e00\u4e2a\u5143\u7d20\u4fdd\u5b58\u7684\u662f\u884c\u53f7\uff0c\u7b2c\u4e8c\u4e2a\u5143\u7d20\u4fdd\u5b58\u7684\u662f\u5217\u53f7\r\ny = np.where(x == np.max(x))\r\nprint(y)\r\nprint(\"\u6700\u5927\u503c\u6240\u5728\u884c\uff1a\",y[0],\"\u6700\u5927\u503c\u6240\u5728\u5217\uff1a\",y[1])<\/pre>\n
\u6309\u884c\/\u5217\u6c42\u548c\uff08\u6c42\u548c\uff09<\/strong><\/div>\n

\u6309\u884c\u6c42\u548c\uff0c\u5c06\u548c\u6dfb\u52a0\u5230\u77e9\u9635\u6700\u540e\u4e00\u5217<\/p>\n

x=np.arange(0,12)\r\nx=x.reshape((3,4))\r\nprint(x)\r\n \r\ny = np.sum(x, axis=1)\r\nprint(y)\r\n \r\nxy = np.hstack((x, y.reshape(x.shape[0], -1)))\r\nprint(xy)<\/pre>\n

\u6309\u5217\u6c42\u548c\uff0c\u5c06\u548c\u6dfb\u52a0\u5230\u77e9\u9635\u6700\u540e\u4e00\u884c<\/p>\n

x=np.arange(0,12)\r\nx=x.reshape((3,4))\r\nprint(x)\r\n \r\nz = np.sum(x, axis=0)\r\nprint(z)\r\n \r\nxz = np.vstack((x, z))\r\nprint(xz)<\/pre>\n
\u8d4b\u503c<\/strong><\/div>\n

\u6309\u6761\u4ef6\u8d4b\u503c<\/p>\n

x=np.arange(0,12)\r\nx=x.reshape((3,4))\r\nprint(x)\r\n \r\nx[x<=5]=0 # \u5c06\u5c0f\u4e8e5\u7684\u6570\u8d4b\u503c\u4e3a0\r\nprint(x)<\/pre>\n

where()<\/p>\n

x=np.arange(0,12)\r\nx=x.reshape((3,4))\r\nprint(x)\r\n \r\n# results = np.where(condition, x, y)\r\n# \u5f53\u6761\u4ef6\u4e3a\u771f\u65f6\uff0c\u5bf9\u5e94\u4f4d\u7f6e\u8fd4\u56dex\u4e2d\u7684\u503c\uff0c\u6761\u4ef6\u4e0d\u6210\u7acb\u5219\u8fd4\u56dey\u4e2d\u7684\u503c\r\ny = np.where(x>5,x,0)  #\u6ee1\u8db3\u5927\u4e8e5\u7684\u503c\u8bbe\u7f6e\u4e3ax\uff0c\u4e0d\u6ee1\u8db3\u7684\u8bbe\u4e3a0\r\nprint(y)<\/pre>\n

\u903b\u8f91\u8fd0\u7b97<\/p>\n

x=np.arange(0,12)\r\nx=x.reshape((3,4))\r\nprint(x)\r\n \r\nprint(x>5) # x>5\u7684\u4e3aTure\uff0c\u5426\u5219\u4e3aFalse<\/pre>\n

\u7ed9\u6307\u5b9a \u884c \/ \u5217 \/ [\u884c\uff0c\u5217] \u8d4b\u503c<\/p>\n

x=np.arange(0,12)\r\nx=x.reshape((3,4))\r\nprint(x)\r\n \r\nx[1,1] = 999 # \u6307\u5b9a\u884c\u5217\u53f7\u8d4b\u503c\r\nprint(x)\r\n \r\nx[1] = 123 # \u6307\u5b9a\u884c\u8d4b\u503c\r\nprint(x)\r\n \r\nx[:,1] = 321 # \u6307\u5b9a\u5217\u8d4b\u503c\r\nprint(x)<\/pre>\n

\u5230\u6b64\u8fd9\u7bc7\u5173\u4e8epython numpy\u67e5\u8be2\u5b9a\u4f4d\u8d4b\u503c\u6570\u503c\u6240\u5728\u884c\u5217\u7684\u6587\u7ae0\u5c31\u4ecb\u7ecd\u5230\u8fd9\u4e86<\/p>\n","protected":false},"excerpt":{"rendered":"

\u7b5b\u9009\u77e9\u9635\u4e2d\u7b2c7\u5217\u503c\u4e3a5\u7684\u884c B = A[ A[:,6] == 5] \u7b5b\u9009\u77e9\u9635\u4e2d\u7b2c7\u5217\u5927\u4e8e5\u7684\u884c B = A[ […]<\/p>\n","protected":false},"author":668,"featured_media":231815,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-253846","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/253846","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/668"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=253846"}],"version-history":[{"count":4,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/253846\/revisions"}],"predecessor-version":[{"id":253850,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/253846\/revisions\/253850"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/231815"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=253846"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=253846"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=253846"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}