{"id":521,"date":"2019-07-01T23:28:00","date_gmt":"2019-07-01T15:28:00","guid":{"rendered":"http:\/\/note.systw.net\/note\/?p=521"},"modified":"2023-11-03T23:30:44","modified_gmt":"2023-11-03T15:30:44","slug":"python-numpy","status":"publish","type":"post","link":"https:\/\/systw.net\/note\/archives\/521","title":{"rendered":"Python Numpy"},"content":{"rendered":"\n<p>NumPy Ndarray<br>NumPy(Numerical Python)<br>\u5e38\u7528\u65bc\u6578\u64da\u5206\u6790\u7684\u5957\u4ef6<\/p>\n\n\n\n<p>ndarray<br>\u5132\u5b58\u6578\u64da\u7684\u4e00\u7a2e\u985e\u578b,\u6548\u80fd\u8f03\u597d<\/p>\n\n\n\n<p><br>\u5b89\u88dd:<br>#sudo apt-get install python-numpy<\/p>\n\n\n\n<p>\u8f09\u5165<br># python<br>&gt;&gt;&gt; import numpy as np<\/p>\n\n\n\n<p><br>ps:<br>\u4e0d\u8981\u4ee5 scientific notation for small numbers\u986f\u793andarray<br>np.set_printoptions(suppress=True)<br>ex:<br>print ndarraydata1<br>[ 1.500e-10 1.500e+00 1.500e+03]<br>np.set_printoptions(suppress=True)<br>print ndarraydata1<br>[ 0. 1.5 1500. ]<\/p>\n\n\n\n<p>&#8230;..<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u5efa\u7acbndarray\u548c\u7522\u751f\u8cc7\u6599&nbsp;<\/strong><\/h2>\n\n\n\n<p><strong>\u5efa\u7acb\u7a7andarray<\/strong><br>ndarraydata1 = np.array([])<br>print ndarraydata1<\/p>\n\n\n\n<p><br><strong>\u7522\u751fndarray\u8cc7\u6599<\/strong><br>import numpy as np<br>listdata=[1,2,3,4,5]<br>ndarraydata1 = np.array(listdata)<br>print ndarraydata1<\/p>\n\n\n\n<p>&#8230;<\/p>\n\n\n\n<p>\u7522\u751f\u4e00\u7d44\u6578\u5b57<br><strong>.arange<\/strong><br>ex:<br>&gt;&gt;&gt; np.arange(5)<br>0 1 2 3 4<br><br>\u7522\u751f\u4e00\u7d44\u6578\u5b57<br><strong>.linspace<\/strong><br>ex:<br>&gt;&gt;&gt; np.linspace(2.0, 3.0, 5)<br>array([ 2. , 2.25, 2.5 , 2.75, 3. ])<\/p>\n\n\n\n<p>0~1\u7684\u4e82\u6578<br><strong>.random.rand<\/strong><br>ex:<br>&gt;&gt;&gt;np.random.rand(3)<br>array([ 0.6590211, 0.97780188, 0.7183219 ])<\/p>\n\n\n\n<p>-1~1\u7684\u4e82\u6578<br><strong>.random.randn<\/strong><br>ex:<br>&gt;&gt;&gt;np.random.randn(3)<br>array([ 0.6590211, -0.7780188, 0.7183219 ])<br>&gt;&gt;&gt;np.random.randn(2, 3)<br>array([[ 0.3590211, -0.6780188, 0.2183219 ],<br>[ 0.4590211, -0.8780188, 0.1183219 ] ])<\/p>\n\n\n\n<p>\u4e82\u6578<br><strong>.random.randint<\/strong><br>ex:<br>random.randint(3, size=10)<br>array([1, 0, 2, 0, 1, 1, 2, 0, 1, 0])<\/p>\n\n\n\n<p><br>&#8230;&#8230;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>numpy \u8cc7\u6599\u64cd\u4f5c<\/strong><\/h2>\n\n\n\n<p><strong>\u6392\u5e8f<\/strong><br>ex:<br>\u7528\u7b2c2\u500b\u6b04\u4f4d\u6392\u5e8f<br>data = [ [5, 7, 3],<br>[4, 2, 2],<br>[0, 3, 5],<br>&nbsp;]<br>data = sorted(data, key=lambda x: x[1])<br>[[4, 2, 2],<br>[0, 3, 5],<br>[5, 7, 3]]<\/p>\n\n\n\n<p><strong>\u79fb\u9664<\/strong><br>ex:<br>a = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])<br>index = [2, 3, 6]<br>new_a = np.delete(a, index)<\/p>\n\n\n\n<p><br><strong>\u53d6\u4ee3<\/strong><br>ex:<br>a = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])<br>a[a&gt;7]=10<br>a[a&lt;3]=0<br>print a<br>[0, 0, 3, 4, 5, 6, 7, 10, 10]<\/p>\n\n\n\n<p><br><strong>\u5c07\u8cc7\u6599\u6253\u4e82<\/strong><br>ex:<br>indices = np.random.permutation(data_train.shape[0])<br>data_train = data_train[indices]<br>target = target[indices]<\/p>\n\n\n\n<p><br>&#8230;&#8230;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u5408\u4f75ndarray<\/strong><\/h2>\n\n\n\n<p>\u7d2f\u7a4d<br><strong>np.append<\/strong><br>ex:<br>a = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])<br>np.append(a,10)<br>array([1, 2, 3, 4, 5, 6, 7, 8, 9,10])<\/p>\n\n\n\n<p><strong>np.column_stack()<\/strong><br>ex:<br>listdata=[1,2,3,4,5]<br>ndarraydata1 = np.array(listdata)<br>listdata=[1,0,0,1,1]<br>ndarraydata2 = np.array(listdata)<br>ndarraydata=np.column_stack((ndarraydata1,ndarraydata2))<br>print ndarraydata<br>array([[1, 1],<br>[2, 0],<br>[3, 0],<br>[4, 1],<br>[5, 1]])<\/p>\n\n\n\n<p><strong>np.row_stack()<\/strong><br>ex:<br>ndarraydata=np.row_stack((ndarraydata1,ndarraydata2))<br>array([[1],<br>[2],<br>[3],<br>[4],<br>[5],<br>[1],<br>[0],<br>[0],<br>[0],<br>[1]])<\/p>\n\n\n\n<p>&nbsp;&#8230;<\/p>\n\n\n\n<p>&gt;&gt;&gt; a = np.array([1, 2, 3])<br>&gt;&gt;&gt; b = np.array([4, 5, 6])<\/p>\n\n\n\n<p><strong>np.vstack<\/strong><br>ex:&nbsp;<br>&gt;&gt;&gt; np.vstack((a,b))<br>array([[1, 2, 3],<br>[4, 5, 6]])<\/p>\n\n\n\n<p><strong>np.hstack<\/strong><br>ex:<br>&gt;&gt;&gt;np.hstack((a,b))<br>array([1, 2, 3, 4, 5, 6])<br><br><strong>np.dstack<\/strong><br>ex:<br>&gt;&gt;&gt; np.dstack((a,b))<br>array([[[1, 4],<br>[2, 5],<br>[3, 6]]])<\/p>\n\n\n\n<p><br>\u5408\u4f75\u591a\u500barray<br>ex:<br>&gt;&gt;&gt; x = np.array([[1, 2, 3], [4, 5, 6]])<br>&gt;&gt;&gt; print(np.ravel(x))<br>[1 2 3 4 5 6]<\/p>\n\n\n\n<p>&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;..<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>ndarray\u7dad\u5ea6\u8f49\u63db<\/strong><\/h2>\n\n\n\n<p>\u4e00\u7dad\u8b8a2\u7dad<br>&gt;&gt;&gt; a=[1 2 3 4 5 6]<br>&gt;&gt;&gt; print a.reshape(6, 1)<br>[[1]<br>[2]<br>[3]<br>[4]<br>[5]<br>[6]]<\/p>\n\n\n\n<p>ps:\u900f\u904e-1\u53ef\u8b93Numpy\u81ea\u52d5\u8a08\u7b97\u6578\u91cf<br>&gt;&gt;&gt; print a.reshape((-1,1)) #\u9019\u4e5f\u7b49\u540c(6,1)<br>[[1]<br>[2]<br>[3]<br>[4]<br>[5]<br>[6]]<br>&gt;&gt;&gt; print a.reshape((1,-1)) #\u9019\u4e5f\u7b49\u540c(1,6)<br>[[1 2 3 4 5 6]]<\/p>\n\n\n\n<p><br>2\u7dad\u8b8a1\u7dad<br>z = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])<br>z.shape (3, 4)<br>z.reshape(-1)<br>array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])<\/p>\n\n\n\n<p><br>\u6bd4\u8f03\u5169\u500bnumpy array\u5143\u7d20<br>for item in ndarraydata :<br>\u3000if item[0]==item[1]:<br>\u3000\u3000print &#8216;match&#8217;<\/p>\n\n\n\n<p><br>&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u8b80\u6a94\u548c\u5b58\u6a94<\/strong><\/h2>\n\n\n\n<p>refer<br>http:\/\/docs.scipy.org\/doc\/numpy\/reference\/generated\/numpy.loadtxt.html<\/p>\n\n\n\n<p><br><strong>numpy loadtxt<\/strong><br>\u8b80\u53d6\u6587\u5b57\u6a94<br>ex:<br>import numpy as np<br>data=np.loadtxt(&#8216;file.txt&#8217;)<\/p>\n\n\n\n<p><strong>delimiter<\/strong><br>\u6307\u5b9a\u4f86\u6e90\u6a94\u5206\u9694\u7b26\u865f<br>default is any whitespace.<br>ex:<br>np.loadtxt(&#8216;file.txt&#8217;, delimiter=&#8217;,&#8217;)<\/p>\n\n\n\n<p><strong>skiprows \u7565\u904e\u884c\u6578<\/strong><br>ex:<br>\u7565\u904e\u7b2c1\u884c<br>np.loadtxt(&#8216;file.txt&#8217;, delimiter=&#8217;,&#8217;,skiprows=1)<\/p>\n\n\n\n<p><strong>dtype \u6307\u5b9a\u8cc7\u6599\u985e\u578b<\/strong><br>ex:<br>data=np.loadtxt(&#8216;file.txt&#8217;,delimiter=&#8217;,&#8217;,dtype=[(&#8216;f0&#8217;,str),(&#8216;f1&#8217;,float),(&#8216;f2&#8217;,float),(&#8216;f3&#8217;,float)])<br>data=np.loadtxt(&#8216;file.txt&#8217;,dtype=[(&#8216;f0&#8217;,str),(&#8216;f1&#8217;,float),(&#8216;f2&#8217;,float),(&#8216;f3&#8217;,float)])<\/p>\n\n\n\n<p><br>\u8cc7\u6599\u96c6\u900f\u904enploadtxt\u53d6\u5f97\u5f8c\uff0c\u8cc7\u6599\u96c6\u53ef\u900f\u904e[\u5217,\u6b04]\u65b9\u6cd5\u53d6\u5f97\u8cc7\u6599<br>ex:<br>\u53d6\u5217<br>dataset[0]\u3000 \u8868\u793a\u50c5\u53d6\u51fa\u7b2c0\u5217\u7684\u8cc7\u6599<br>dataset[1:] \u8868\u793a\u50c5\u51fa\u7b2c1\u5217\u5f8c\u7684\u8cc7\u6599<br>dataset[-1:]\u3000 \u8868\u793a\u50c5\u53d6\u51fa\u6700\u5f8c\u4e00\u5217\u7684\u8cc7\u6599<br>dataset[0:2]\u3000 \u8868\u793a\u50c5\u53d6\u51fa\u7b2c1\u5217~\u7b2c2\u5217\u7684\u8cc7\u6599<br>dataset[10:12]\u3000 \u8868\u793a\u50c5\u53d6\u51fa\u7b2c11\u5217~\u7b2c12\u5217\u7684\u8cc7\u6599<br>dataset[0:-1]\u3000 \u8868\u793a\u50c5\u53d6\u51fa\u7b2c1\u5217~\u5012\u6578\u7b2c\u4e8c\u5217\u7684\u8cc7\u6599<br>\u53d6\u6b04<br>dataset[:,0] \u8868\u793a\u50c5\u53d6\u51fa\u7b2c1\u6b04\u7684\u8cc7\u6599<br>dataset[:,1:] \u8868\u793a\u53d6\u51fa\u7b2c2\u6b04(\u5305\u542b\u7b2c2\u6b04)\u5f8c\u7684\u8cc7\u6599<br>dataset[:,2:4] \u8868\u793a\u53d6\u51fa\u7b2c2\u6b04\u5230\u7b2c3\u6b04\u7684\u8cc7\u6599<\/p>\n\n\n\n<p><br>ps:<br>\u8b80\u53d6\u5916\u90e8\u6578\u64da<br>#vi dataset.txt<br>0 1 1 1<br>0 2 2 2<br>0 1 2 3<br>0 2 1 4<br>1 9 9 7<br>1 8 8 8<br>1 9 8 9<br>#python<br>&gt;&gt;&gt;import numpy as np<br>&gt;&gt;&gt;f = open(&#8220;dataset.txt&#8221;)<br>&gt;&gt;&gt;dataset =a np.loadtxt(f)<br>&gt;&gt;&gt;print(dataset)<br>[[ 0. 1. 1. 1.]<br>[ 0. 2. 2. 2.]<br>[ 0. 1. 2. 3.]<br>[ 0. 2. 1. 4.]<br>[ 1. 9. 9. 7.]<br>[ 1. 8. 8. 8.]<br>[ 1. 9. 8. 9.]]<br>&gt;&gt;&gt;data = dataset[:, 1:]<br>&gt;&gt;&gt;target = dataset[:, 0] # select column 0, the stock price<br>&gt;&gt;&gt; print(data)<br>[[ 1. 1. 1.]<br>[ 2. 2. 2.]<br>[ 1. 2. 1.]<br>[ 2. 1. 2.]<br>[ 9. 9. 9.]<br>[ 8. 8. 8.]<br>[ 9. 8. 9.]]<br>&gt;&gt;&gt; print(target)<br>[ 0. 0. 0. 0. 1. 1. 1.]<\/p>\n\n\n\n<p><br>\u6aa2\u8996\u8cc7\u6599\u6b04\u5217<br>dataset.shape<\/p>\n\n\n\n<p>\u5132\u5b58\u6587\u5b57\u6a94<br>np.savetxt(&#8216;test.out&#8217;, data_array, delimiter=&#8217;,&#8217;,fmt=&#8217;%10.5f&#8217;)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>NumPy NdarrayNumPy(Numerical P &#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-521","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\/521","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=521"}],"version-history":[{"count":0,"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/posts\/521\/revisions"}],"wp:attachment":[{"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/media?parent=521"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/categories?post=521"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/tags?post=521"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}