{"id":519,"date":"2019-07-01T23:26:00","date_gmt":"2019-07-01T15:26:00","guid":{"rendered":"http:\/\/note.systw.net\/note\/?p=519"},"modified":"2023-11-04T00:20:37","modified_gmt":"2023-11-03T16:20:37","slug":"python-numpy-compute","status":"publish","type":"post","link":"https:\/\/systw.net\/note\/archives\/519","title":{"rendered":"Python Numpy compute"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\"><strong>\u7d71\u8a08\u5206\u6790<\/strong><\/h2>\n\n\n\n<p><strong>\u7b97\u7e3d\u6578<\/strong><br>.sum(ndarray)<br>ex:<br>&gt;&gt;&gt; np.sum([0.5, 1.5])<br>2.0<\/p>\n\n\n\n<p><br><strong>\u53d6\u6700\u5927\u503c<\/strong><br>.amax(ndarray)<br>ex:<br>&gt;&gt;&gt;a = np.array([1,1,2,2,2,2,3,3,3])<br>&gt;&gt;&gt;print numpy.amax(a)<br>3<\/p>\n\n\n\n<p><strong>\u53d6\u6700\u5c0f\u503c<\/strong><br>.amin(ndarray)<\/p>\n\n\n\n<p><strong>\u7b97\u51fa\u6a19\u6e96\u5dee<\/strong><br>.std(ndarray)<br>ex:<br>&gt;&gt;&gt;a = np.array([1,1,2,2,2,2,3,3,3])<br>&gt;&gt;&gt;print numpy.std(a)<\/p>\n\n\n\n<p><br><strong>\u5e73\u5747\u6578<\/strong><br>.average(ndarray)<br>ex:<br>&gt;&gt;&gt;a=[1, 2, 3, 4]<br>&gt;&gt;&gt; np.average(a)<br>2.5<\/p>\n\n\n\n<p><br><strong>\u5c07\u6240\u6709\u5143\u7d20\u53d6\u5c0f\u6578\u5f8cn\u4f4d<\/strong><br>.round(ndarray)<br>ex:<br>.round(ndarray)<br>&gt;&gt;&gt;a=[0.11,0.22,0.33]<br>&gt;&gt;&gt;np.round(a,1)<br>[0.1,0.2,0.3]&nbsp;<\/p>\n\n\n\n<p>&#8230;<\/p>\n\n\n\n<p><strong>\u5c07\u8cc7\u6599\u5207\u6210\u591a\u4efd<\/strong><br>.histogram( ndarray, bins= &lt; num&gt;)<br>ex:<br>\u5c07\u6578\u5217\u5206\u62103\u7b49\u4efd<br>hist, bins = np.histogram([1,1,2,2,2,2,3,3,3], bins=3)<br>print bins<br>&gt;&gt;&gt; [1 1.66666667 2.33333333 3]<br>print hist<br>&gt;&gt;&gt; [2 4 3]<\/p>\n\n\n\n<p>ex: \u5c07\u6578\u5217\u5206\u70ba50\u7b49\u4efd\u4e26\u756b\u5716<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>hist, bins = np.histogram(x, bins=50)\nwidth = 0.7 * (bins&#91;1] - bins&#91;0])\ncenter = (bins&#91;:-1] + bins&#91;1:]) \/ 2\nplt.bar(center, hist, align='center', width=width)\nplt.show()<\/code><\/pre>\n\n\n\n<p>refer:<br>http:\/\/stackoverflow.com\/questions\/5328556\/histogram-matplotlib<br>http:\/\/docs.scipy.org\/doc\/numpy\/reference\/generated\/numpy.histogram.html<\/p>\n\n\n\n<p><br><strong>\u8a08\u7b97\u5143\u7d20\u91cd\u8907\u6b21\u6578<\/strong><br>.bincount(ndarray)<\/p>\n\n\n\n<p>ex:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>target=&#91;1, 1, 2, 3, 4]\n\ndcount = np.bincount(target)\ndindex = np.nonzero(dcount)&#91;0]\nprint dcount\nprint np.nonzero(dcount)&#91;0]\nprint zip(dindex,dcount&#91;dindex])<\/code><\/pre>\n\n\n\n<p>&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u77e9\u9663\u904b\u7b97<\/strong><\/h2>\n\n\n\n<p>\u8a2da,b\u5982\u4e0b<br>&gt;&gt;&gt; a = np.array([1, 2, 3])<br>&gt;&gt;&gt; b = np.array([2, 4, 6])<\/p>\n\n\n\n<p><br><strong>\u6bcf\u500b\u5143\u7d20*2<\/strong><br>&gt;&gt;&gt; a * 2<br>array([2, 4, 6])<\/p>\n\n\n\n<p><strong>\u5e73\u65b9<\/strong><br>&gt;&gt;&gt; a ** 2<br>array([1, 4, 9])<\/p>\n\n\n\n<p><strong>\u5411\u91cf\u76f8\u52a0<\/strong><br>&gt;&gt;&gt; a+b<br>array([3, 6, 9])<\/p>\n\n\n\n<p><strong>\u5411\u91cf\u76f8\u4e58<\/strong><br>&gt;&gt;&gt;a*b<br>array([ 2, 8, 18])<\/p>\n\n\n\n<p><strong>\u5411\u91cf\u76f8\u6e1b<\/strong><br>&gt;&gt;&gt; a-b<br>array([-1, -2, -3])<\/p>\n\n\n\n<p><strong>\u5411\u91cf\u76f8\u9664<\/strong><br>&gt;&gt;&gt; a\/b<br>array([ 0.5, 0.5, 0.5])<\/p>\n\n\n\n<p>refer<br>http:\/\/ccckmit.wikidot.com\/la:vector<\/p>\n\n\n\n<p><strong>dot<br>\u77e9\u9663\u7684\u4e58\u7a4d<\/strong><\/p>\n\n\n\n<p>\u5169\u6578\u503c\u76f8\u4e58<br>ex:<br>&gt;np.dot(3, 4)<br>12<\/p>\n\n\n\n<p>\u5c0d\u65bc\u4e00\u7dad\u6578\u7d44\uff0c\u5b83\u8a08\u7b97\u7684\u662f\u5176\u9ede\u7a4d\/\u5167\u7a4d<br>ex:<br>X=[1,3,-5], Y=[4,-2,-1]<br>(X^T)*(Y)=1*4+3*-2+-5*-1=3<br>\u505a\u6cd5\u5982\u4e0b<br>&gt;&gt;&gt; X=[1,3,-5]<br>&gt;&gt;&gt; Y=[4,-2,-1]<br>&gt;&gt;&gt; print np.dot(X,Y)<br>3<\/p>\n\n\n\n<p>\u5c0d\u65bc\u4e8c\u7dad\u6578\u7d44\uff0c\u5b83\u8a08\u7b97\u7684\u662f\u77e9\u9663\u4e58\u7a4d\uff0c<br>ex:<br>&gt;&gt;&gt; a = [[1, 0], [0, 1]]<br>&gt;&gt;&gt; b = [[4, 1], [2, 2]]<br>&gt;&gt;&gt; np.dot(a, b)<br>array([[4, 1], [2, 2]])<\/p>\n\n\n\n<p>refer<br>NumPy Basics: Arrays and Vectorized Computation<br>http:\/\/pda.readthedocs.org\/en\/latest\/chp4.html#ndarray<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7d71\u8a08\u5206\u6790 \u7b97\u7e3d\u6578.sum(ndarray)ex:&gt;&#038;g &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"","fifu_image_alt":"","_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[14],"tags":[],"class_list":["post-519","post","type-post","status-publish","format-standard","hentry","category-develop"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/posts\/519","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/comments?post=519"}],"version-history":[{"count":0,"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/posts\/519\/revisions"}],"wp:attachment":[{"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/media?parent=519"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/categories?post=519"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/tags?post=519"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}