{"id":233554,"date":"2020-12-29T14:38:31","date_gmt":"2020-12-29T06:38:31","guid":{"rendered":"http:\/\/4563.org\/?p=233554"},"modified":"2020-12-29T14:38:31","modified_gmt":"2020-12-29T06:38:31","slug":"%e5%a6%82%e4%bd%95%e5%88%a9%e7%94%a8-numpy-%e8%bf%9b%e8%a1%8c%e7%b4%a2%e5%bc%95%e5%92%8c%e5%88%87%e7%89%87","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=233554","title":{"rendered":"\u5982\u4f55\u5229\u7528 NumPy \u8fdb\u884c\u7d22\u5f15\u548c\u5207\u7247?"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u5982\u4f55\u5229\u7528 NumPy \u8fdb\u884c\u7d22\u5f15\u548c\u5207\u7247?               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : fanqieipnet <\/span>  <span><i><\/i> 8<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\">                        \u5904\u7406\u591a\u7ef4\u6570\u7ec4\uff0c\u5982\u4f55\u5229\u7528 NumPy \u8fdb\u884c\u7d22\u5f15\u548c\u5207\u7247?\u4eca\u5929\u756a\u8304\u52a0\u901f\u5c31\u6765\u8ddf\u5927\u5bb6\u8bb2\u89e3\u4e0b\u3002<\/p>\n<p>\u3000\u3000\u5e73\u65f6\u9047\u5230\u5173\u4e8e NumPy \u7684 bug\uff0c\u89e3\u51b3\u4e0d\u4e86\u7684\uff0c\u53ef\u4ee5\u5148\u53bb\u67e5\u770b API \u7684\u4f7f\u7528\u8bf4\u660e\u3002<\/p>\n<p>\u3000\u3000\u7406\u89e3 NumPy \u7684\u5411\u91cf\u5316\u80fd\u529b\uff0c\u8fd9\u6b63\u662f\u8d4b\u4e88\u5b83\u7b80\u6d01\u7684\u91cd\u8981\u539f\u56e0\u4e4b\u4e00\u3002\u4f7f\u7528 Python \u539f\u751f API \u4f1a\u7ecf\u5e38\u5199\u4e9b for\uff0c\u4f46\u662f NumPy \u8ba9\u5b83\u53d8\u5f97\u4e0d\u518d\u9700\u8981\uff0cNumPy \u4e00\u5207\u90fd\u662f\u6309\u7167\u5411\u91cf\u8ba1\u7b97\u3002\u5982\u4e0b\u8ba1\u7b97\u5c0f\u4e8e 3 \u7684\u5143\u7d20\u7f6e 0\uff0c\u5426\u5219\u7f6e\u4e3a 1\uff1a<\/p>\n<p>\u3000\u3000# \u4f7f\u7528 Python \u539f\u751f<\/p>\n<p>\u3000\u3000 a = [10,4,-6,3,5,1]<\/p>\n<p>\u3000\u3000# \u5c0f\u4e8e 3 \u7684\u5143\u7d20\u7f6e\u4e3a 0\uff0c\u4e0d\u5c0f\u4e8e 3 \u7684\u5143\u7d20\u7f6e\u4e3a 1<\/p>\n<p>\u3000\u3000 b = []<\/p>\n<p>\u3000\u3000 for i in a:<\/p>\n<p>\u3000\u3000 b.append(0 if i &lt; 3 else 1)<\/p>\n<p>\u3000\u3000 print(b)<\/p>\n<p>\u3000\u3000\u4f7f\u7528 NumPy \u7684 where \u65b9\u6cd5\uff0c\u8bed\u6cd5\u66f4\u52a0\u7b80\u6d01\uff0c\u770b\u4e0d\u5230 for \u8bed\u53e5\uff0c\u7b26\u5408 Python \u54f2\u5b66\uff1a<\/p>\n<p>\u3000\u3000# \u4f7f\u7528 NumPy<\/p>\n<p>\u3000\u3000 na = np.array(a)<\/p>\n<p>\u3000\u3000# \u4e00\u884c\u4ee3\u7801<\/p>\n<p>\u3000\u3000 b = np.where(na &lt; 3, 0, 1)<\/p>\n<p>\u3000\u3000 print(b)<\/p>\n<p>\u3000\u3000 3. \u5904\u7406\u591a\u7ef4\u6570\u7ec4\uff0cNumPy \u7684\u7d22\u5f15\u548c\u5207\u7247\u66f4\u5f3a\u5927\uff0c\u5982\u4e0b na &lt; 3 \u5f97\u5230\u5e03\u5c14\u7d22\u5f15\uff0c\u4e00\u5207\u90fd\u6309\u7167\u5411\u91cf\u5316\u64cd\u4f5c\uff1a<\/p>\n<p>\u3000\u3000 na = np.array([10,4,-6,3,5,1])<\/p>\n<p>\u3000\u3000 na[na &lt; 3]<\/p>\n<p>\u3000\u3000# \u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<p>\u3000\u3000# array([-6, 1])<\/p>\n<p>\u3000\u3000# \u521b\u5efa\u4e8c\u7ef4\u6570\u7ec4<\/p>\n<p>\u3000\u3000 np.random.randint(1,10,size=(3,4))<\/p>\n<p>\u3000\u3000 c2 = np.random.randint(1,10,size=(3,4))<\/p>\n<p>\u3000\u3000 c2<\/p>\n<p>\u3000\u3000 array([[3, 2, 9, 9],<\/p>\n<p>\u3000\u3000[9, 5, 2, 4],<\/p>\n<p>\u3000\u3000[8, 1, 2, 4]])<\/p>\n<p>\u3000\u3000# \u66f4\u5f3a\u5927\u7b80\u6d01\u7684\u5207\u7247\u529f\u80fd\uff1a<\/p>\n<p>\u3000\u3000 c2[:2,1:3]<\/p>\n<p>\u3000\u3000 array([[2, 9],<\/p>\n<p>\u3000\u3000[5, 2]])      <\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>0<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u5229\u7528 NumPy \u8fdb\u884c\u7d22\u5f15\u548c\u5207&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/233554"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=233554"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/233554\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=233554"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=233554"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=233554"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}