numpy Task03

array operation

change shape

  • numpy.ndarray.shape represents the dimension of the array and returns a tuple whose length is the number of dimensions, namely the ndim attribute (rank).
import numpy as np

x = np.array([1,2,9,4,5,6,7,8])
print(x.shape)
x.shape = [2,4]
print(x)

(8,)
[[1 2 9 4]
[5 6 7 8]]

  • numpy.ndarray.flat converts the array into a one-dimensional iterator, and can use for to access each element of the array.
import numpy as np
x = np.array([[11, 12, 13, 14, 15],
              [16, 17, 18, 19, 20],
              [21, 22, 23, 24, 25],
              [26, 27, 28, 29, 30],
              [31, 32, 33, 34, 35]])
y = x.flat

for i in y:
    print(i,end=',')

11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,

  • numpy.ndarray.flatten ([order='C']) Convert a copy of the array to a one-dimensional array, and return it.
    [order: 'C' - by row, 'F' - by column, 'A' - original order, 'k' - the order in which elements appear in memory.
import numpy as np
x = np.array([[11, 12, 13, 14, 15],
              [16, 17, 18, 19, 20],
              [21, 22, 23, 24, 25],
              [26, 27, 28, 29, 30],
              [31, 32, 33, 34, 35]])
y = x.flatten()
print(y)

[11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
35]

  • numpy.ravel(a, order='C') converts the array to a one-dimensional array. Unlike the above, this method returns a view of the array
  • When the reshape() function parameter newshape = -1, it means to reduce the array to one dimension

array transpose

  • numpy.transpose(a,axes=None)
  • numpy.ndarray.T
import numpy as np
x = np.random.rand(5,5) * 10
x = np.around(x ,2)
print(x)
print("++++++++++++++")
y = x.T
print(y)
print("++++++++++++++")
z = np.transpose(x)
print(z)

[[1.62 1.64 2.18 2.61 3.45]
[3.42 9.84 7.68 4.1 7.24]
[3.95 1.1 0.8 2.89 0.19]
[0.15 0.91 5.34 9.95 8.62]
[7.48 6.67 0.38 2.31 0.33]]
++++++++++++++
[[1.62 3.42 3.95 0.15 7.48]
[1.64 9.84 1.1 0.91 6.67]
[2.18 7.68 0.8 5.34 0.38]
[2.61 4.1 2.89 9.95 2.31]
[3.45 7.24 0.19 8.62 0.33]]
++++++++++++++
[[1.62 3.42 3.95 0.15 7.48]
[1.64 9.84 1.1 0.91 6.67]
[2.18 7.68 0.8 5.34 0.38]
[2.61 4.1 2.89 9.95 2.31]
[3.45 7.24 0.19 8.62 0.33]]

change dimensions

  • After creating an array, you can also add a dimension to it, which is often used in matrix calculations.
    [numpy. newaxis = None None]
import numpy as np
x = np.array([1,2,9,4,5,6,7,8])
print(x.shape)
print(x)
print("++++++++++++")
y = x[:,np.newaxis]
print(y.shape)
print(y)

(8,)
[1 2 9 4 5 6 7 8]
++++++++++++
(8, 1)
[[1]
[2]
[9]
[4]
[5]
[6]
[7]
[8]]

  • numpy.squeeze(a, axis = None) deletes single-dimensional entries from the shape of the array, that is, removes the dimension of 1 in the shape [
    a indicates that the input is an array; axis is used to specify the dimension to be deleted, but the specified dimension must be It is single-dimensional, otherwise an error will be reported]
import numpy as np 
x = np.arange(10)
print(x.shape)
x = x[np.newaxis,:]
print(x.shape)
y = np.squeeze(x)
print(y.shape)

(10,)
(1, 10)
(10,)

array combination

  • Concatenate array sequences along existing axes (originally x, y are all one-dimensional, and the spliced ​​result is also one-dimensional).
import numpy as np
x = np.array([1,2,3])
y = np.array([7,8,9])
z = np.concatenate([x,y])
print(z)

[1 2 3 7 8 9]

  • It turns out that x and y are both two-dimensional, and the result after splicing is also two-dimensional
import numpy as np
x = np.array([1,2,3]).reshape(1,3)
y = np.array([7,8,9]).reshape(1,3)
z = np.concatenate([x,y])
print(z)
print('+++++++++++++++')
z = np.concatenate([x, y], axis=1)
print(z)

[[1 2 3]
[7 8 9]]
+++++++++++++++
[[1 2 3 7 8 9]]

  • Join a series of arrays (stack) along the new axis
import numpy as np

x = np.array([[1, 2, 3], [4, 5, 6]])
y = np.array([[7, 8, 9], [10, 11, 12]])
z = np.stack([x, y])
print(z.shape)
print(z)

(2, 2, 3)
[[[ 1 2 3]
[ 4 5 6]]

[[ 7 8 9]
[10 11 12]]]

Array split

  • numpy.split(ary, indices_orsections,sxis=0) Split the array
import numpy as np
x = np.array([[11, 12, 13, 14],
              [16, 17, 18, 19],
              [21, 22, 23, 24]])
y = np.split(x,[1,3])
print(y)

[array([[11, 12, 13, 14]]), array([[16, 17, 18, 19],
[21, 22, 23, 24]]), array([], shape=(0, 4), dtype=int32)]

  • Vertical splitting is to split the array according to height
import numpy as np

x = np.array([[11, 12, 13, 14],
              [16, 17, 18, 19],
              [21, 22, 23, 24]])
y = np.vsplit(x, 3)
print(y)

[array([[11, 12, 13, 14]]), array([[16, 17, 18, 19]]), array([[21, 22, 23, 24]])]

  • Horizontal slicing is to slice the array according to its width.
import numpy as np

x = np.array([[11, 12, 13, 14],
              [16, 17, 18, 19],
              [21, 22, 23, 24]])
y = np.hsplit(x, 2)
print(y)

[array([[11, 12],
[16, 17],
[21, 22]]), array([[13, 14],
[18, 19],
[23, 24]])]

array tiling

  • numpy.tile(A, reps) copies the original matrix horizontally and vertically
import numpy as np

x = np.array([[1, 2], [3, 4]])
print(x)
print("++++++++++")
y = np.tile(x,(1,3))
print(y)

[[1 2]
[3 4]]
++++++++++
[[1 2 1 2 1 2]
[3 4 3 4 3 4]]

  • numpy.repeat(a, repeats, axis=None) [axis=0, copy along the y-axis, increase the number of rows; axis=1, copy along the x-axis, increase the number of columns; repeats, can be a number, It can also be a matrix; when axis=None, it will flatten the current matrix, which actually becomes a row vector]
import numpy as np

x = np.array([[1, 2], [3, 4]])
y = np.repeat(x, 2, axis=1)
print(y)

[[1 1 2 2]
[3 3 4 4]]

Add and remove elements

  • numpy.unique(ar, return_index=False, return_inverse=False,return_counts=False, axis=None)

Guess you like

Origin blog.csdn.net/BigCabbageFy/article/details/109279502