numpy基础代码操练
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了numpy基础代码操练相关的知识,希望对你有一定的参考价值。
In [20]: b[0,:,1] Out[20]: array([1, 5, 9]) In [21]: b[0,:,1] Out[21]: array([1, 5, 9]) In [22]: b[0,:,-1] Out[22]: array([ 3, 7, 11]) In [23]: b[0,::-1, -1] Out[23]: array([11, 7, 3]) In [24]: b[0,::-2, -1] Out[24]: array([11, 3]) In [25]: b[::-1] Out[25]: array([[[12, 13, 14, 15], [16, 17, 18, 19], [20, 21, 22, 23]], [[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]]) In [26]: b Out[26]: array([[[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]], [[12, 13, 14, 15], [16, 17, 18, 19], [20, 21, 22, 23]]]) In [27]: b.ravel() Out[27]: array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]) In [28]: b.flatten() Out[28]: array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]) In [29]: b.shape = (6,4) In [30]: b Out[30]: array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11], [12, 13, 14, 15], [16, 17, 18, 19], [20, 21, 22, 23]]) In [31]: b.transpose() Out[31]: array([[ 0, 4, 8, 12, 16, 20], [ 1, 5, 9, 13, 17, 21], [ 2, 6, 10, 14, 18, 22], [ 3, 7, 11, 15, 19, 23]]) In [32]: b.resize(2,12)) File "<ipython-input-32-91b83b9b6cad>", line 1 b.resize(2,12)) ^ SyntaxError: invalid syntax In [33]: b.resize((2,12)) In [34]: b Out[34]: array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], [12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]]) In [35]: a = arange(3).reshape(3,3) --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-35-0863ec9f918e> in <module>() ----> 1 a = arange(3).reshape(3,3) ValueError: cannot reshape array of size 3 into shape (3,3) In [36]: a = arange(9).reshape(3,3) In [37]: a Out[37]: array([[0, 1, 2], [3, 4, 5], [6, 7, 8]]) In [38]: b = 2 * a In [39]: b Out[39]: array([[ 0, 2, 4], [ 6, 8, 10], [12, 14, 16]]) In [40]: hstack((a,b)) Out[40]: array([[ 0, 1, 2, 0, 2, 4], [ 3, 4, 5, 6, 8, 10], [ 6, 7, 8, 12, 14, 16]]) In [41]: concatenate((a,b), axis=1) Out[41]: array([[ 0, 1, 2, 0, 2, 4], [ 3, 4, 5, 6, 8, 10], [ 6, 7, 8, 12, 14, 16]]) In [42]: vstack((a,b)) Out[42]: array([[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8], [ 0, 2, 4], [ 6, 8, 10], [12, 14, 16]]) In [43]: concatenate((a,b), axis=0) Out[43]: array([[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8], [ 0, 2, 4], [ 6, 8, 10], [12, 14, 16]]) In [44]: dstack((a,b)) Out[44]: array([[[ 0, 0], [ 1, 2], [ 2, 4]], [[ 3, 6], [ 4, 8], [ 5, 10]], [[ 6, 12], [ 7, 14], [ 8, 16]]]) In [45]: a Out[45]: array([[0, 1, 2], [3, 4, 5], [6, 7, 8]]) In [46]: hsplit(a, 3) Out[46]: [array([[0], [3], [6]]), array([[1], [4], [7]]), array([[2], [5], [8]])] In [47]: hsplit(a, 3) Out[47]: [array([[0], [3], [6]]), array([[1], [4], [7]]), array([[2], [5], [8]])] In [48]: split(a, 3, axis=1) Out[48]: [array([[0], [3], [6]]), array([[1], [4], [7]]), array([[2], [5], [8]])] In [49]: vsplit(a, 3) Out[49]: [array([[0, 1, 2]]), array([[3, 4, 5]]), array([[6, 7, 8]])] In [50]: split(a, 3, axis=0) Out[50]: [array([[0, 1, 2]]), array([[3, 4, 5]]), array([[6, 7, 8]])] In [51]: c = arange(27).reshape(3, 3, 3) In [52]: c Out[52]: array([[[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8]], [[ 9, 10, 11], [12, 13, 14], [15, 16, 17]], [[18, 19, 20], [21, 22, 23], [24, 25, 26]]]) In [53]: dsplit(c,3) Out[53]: [array([[[ 0], [ 3], [ 6]], [[ 9], [12], [15]], [[18], [21], [24]]]), array([[[ 1], [ 4], [ 7]], [[10], [13], [16]], [[19], [22], [25]]]), array([[[ 2], [ 5], [ 8]], [[11], [14], [17]], [[20], [23], [26]]])] In [54]: b Out[54]: array([[ 0, 2, 4], [ 6, 8, 10], [12, 14, 16]]) In [55]: b Out[55]: array([[ 0, 2, 4], [ 6, 8, 10], [12, 14, 16]]) In [56]: b.ndim Out[56]: 2 In [57]: b.size Out[57]: 9 In [58]: b.itemsize Out[58]: 4 In [59]: b.nbytes Out[59]: 36 In [60]: b.size * b.itemsize Out[60]: 36 In [61]: b.resize(6,4) --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-61-a30f6357ab78> in <module>() ----> 1 b.resize(6,4) ValueError: cannot resize an array that references or is referenced by another array in this way. Use the resize function In [62]: b.resize(6,6) --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-62-5d4e603729e0> in <module>() ----> 1 b.resize(6,6) ValueError: cannot resize an array that references or is referenced by another array in this way. Use the resize function In [63]: b.resize() In [64]: b Out[64]: array([[ 0, 2, 4], [ 6, 8, 10], [12, 14, 16]]) In [65]: b Out[65]: array([[ 0, 2, 4], [ 6, 8, 10], [12, 14, 16]]) In [66]: b.tolist() Out[66]: [[0, 2, 4], [6, 8, 10], [12, 14, 16]] In [67]:
以上是关于numpy基础代码操练的主要内容,如果未能解决你的问题,请参考以下文章
[vscode]--HTML代码片段(基础版,reactvuejquery)