Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Numpy Array Operations

Numpy Array Operations

Numpy contains some functions for processing arrays, which can be roughly divided into the following categories:


Modify array shape

function Description
reshape Modify shape without changing data
flat Array element iterator
flatten Return a copy of the array; modifications made to the copy do not affect the original array
ravel Return a flattened array

numpy.reshape

The numpy.reshape function can modify the shape without changing the data. The format is as follows:

numpy.reshape(arr, newshape, order='C')

Example

import

numpy

as

np

a

=

np

.

arange

(

8

)

print

(

'

Original array:

'

)

print

(

a

)

print

(

'

\n

'

)

b

=

a

.

reshape

(

4

,

2

)

print

(

'

Modified array:

'

)

print

(

b

)

The output is as follows:

原始数组:

[0 1 2 3 4 5 6 7]



Modified array:

[[0 1]

 [2 3]

 [4 5]

 [6 7]]

numpy.ndarray.flat

numpy.ndarray.flat is an array element iterator, as shown in the following example:

Example

import

numpy

as

np

a

=

np

.

arange

(

9

)

.

reshape

(

3

,

3

)

print

(

'

Original array:

'

)

for

row

in

a

:

print

(

row

)

# To process each element in the array, you can use the flat attribute, which is an array element iterator:

print

(

'

Array after iteration:

'

)

for

element

in

a

.

flat

:

print

(

element

)

The output is as follows:

原始数组:

[0 1 2]

[3 4 5]

[6 7 8]

Array after iteration:

0

1

2

3

4

5

6

7

8

numpy.ndarray.flatten

numpy.ndarray.flatten returns a copy of the array; modifications made to the copy do not affect the original array. The format is as follows:

ndarray.flatten(order='C')

Parameter Description:

Example

import

numpy

as

np

a

=

np

.

arange

(

8

)

.

reshape

(

2

,

4

)

print

(

'

Original array:

'

)

print

(

a

)

print

(

'

\n

'

)

By row by default

print

(

'

Flattened array:

'

)

print

(

a

.

flatten

(

)

)

print

(

'

\n

'

)

print

(

'

Array flattened in F-style order:

'

)

print

(

a

.

flatten

(

order

=

'

F

'

)

)

The output is as follows:

原数组:

[[0 1 2 3]

 [4 5 6 7]]





Expanded array:

[0 1 2 3 4 5 6 7]





Array expanded in F-style order:

[0 4 1 5 2 6 3 7]

numpy.ravel

numpy.ravel() flattens the array elements, usually in 'C style', and returns a view of the array (view, somewhat similar to a C/C++ reference), so modifications will affect the original array.

This function accepts two parameters:

numpy.ravel(a, order='C')

Parameter Description:

Example

import

numpy

as

np

a

=

np

.

arange

(

8

)

.

reshape

(

2

,

4

)

print

(

'

Original array:

'

)

print

(

a

)

print

(

'

\n

'

)

print

(

'

After calling the ravel function:

'

)

print

(

a

.

ravel

(

)

)

print

(

'

\n

'

)

print

(

'

After calling the ravel function in F-style order:

'

)

print

(

a

.

ravel

(

order

=

'

F

'

)

)

The output is as follows:

原数组:

[[0 1 2 3]

 [4 5 6 7]]





After calling the ravel function:

[0 1 2 3 4 5 6 7]





After calling the ravel function in F-style order:

[0 4 1 5 2 6 3 7]


Transpose arrays

function Description
transpose Transpose the dimensions of an array
ndarray.T and self.transpose() are the same
rollaxis Roll a specified axis backward
swapaxes Swap two axes of an array

numpy.transpose

The numpy.transpose function is used to transpose the dimensions of an array. The format is as follows:

numpy.transpose(arr, axes)

Parameter description:

Example

import

numpy

as

np

a

=

np

.

arange

(

12

)

.

reshape

(

3

,

4

)

print

(

'

Original array:

'

)

print

(

a

)

print

(

'

\n

'

)

print

(

'

Transposed array:

'

)

print

(

np

.

transpose

(

a

)

)

The output is as follows:



Original array:

[[ 0  1  2  3]

 [ 4  5  6  7]

 [ 8  9 10 11]]





Swap array:

[[ 0  4  8]

 [ 1  5  9]

 [ 2  6 10]

 [ 3  7 11]]

numpy.ndarray.T is similar to numpy.transpose:

Example

import

numpy

as

np

a

=

np

.

arange

(

12

)

.

reshape

(

3

,

4

)

print

(

'

Original array:

'

)

print

(

a

)

print

(

'

\n

'

)

print

(

'

Transposed array:

'

)

print

(

a

.

T

)

The output is as follows:

原数组:

[[ 0  1  2  3]

 [ 4  5  6  7]

 [ 8  9 10 11]]





Transpose array:

[[ 0  4  8]

 [ 1  5  9]

 [ 2  6 10]

 [ 3  7 11]]

numpy.rollaxis

The numpy.rollaxis function rolls a specific axis backward to a specific position. The format is as follows:

numpy.rollaxis(arr, axis, start)

Parameter Description:

Example

import

numpy

as

np

Created a three-dimensional ndarray

a

=

np

.

arange

(

8

)

.

reshape

(

2

,

2

,

2

)

print

(

'

Original array:

'

)

print

(

a

)

print

(

'

Get a value from the array:

'

)

print

(

np

.

where

(

a

==

6

)

)

print

(

a

[

1

,

1

,

0

]

)

is 6

print

(

'

\n

'

)

Roll axis 2 to axis 0 (width to depth)

print

(

'

Call the rollaxis function:

'

)

b

=

np

.

rollaxis

(

a

,

2

,

0

)

print

(

b

)

Check element a[1,1,0], i.e. the coordinate of 6, becomes [0, 1, 1]

The last 0 moves to the very front

print

(

np

.

where

(

b

==

6

)

)

print

(

'

\n

'

)

Roll axis 2 to axis 1: (width to height)

print

(

'

Call the rollaxis function:

'

)

c

=

np

.

rollaxis

(

a

,

2

,

1

)

print

(

c

)

Check element a[1,1,0], i.e. the coordinate of 6, becomes [1, 0, 1]

The last 0 and the 1 before it swap positions

print

(

np

.

where

(

c

==

6

)

)

print

(

'

\n

'

)

The output is as follows:



Original array:

[[[0 1]

  [2 3]]



 [[4 5]

  [6 7]]]

# Get a value from the array:

(array([1]), array([1]), array([0]))

6





Call the rollaxis function:

[[[0 2]

  [4 6]]



 [[1 3]

  [5 7]]]

(array([0]), array([1]), array([1]))





Call the rollaxis function:

[[[0 2]

  [1 3]]



 [[4 6]

  [5 7]]]

(array([1]), array([0]), array([1]))

numpy.swapaxes

The numpy.swapaxes function is used to swap two axes of an array. The format is as follows:

numpy.swapaxes(arr, axis1, axis2)

Example

import

numpy

as

np

Created a three-dimensional ndarray

a

=

np

.

arange

(

8

)

.

reshape

(

2

,

2

,

2

)

print

(

'

Original array:

'

)

print

(

a

)

print

(

'

\n

'

)

Now swap axis 0 (depth direction) to axis 2 (width direction)

print

(

'

Array after calling the swapaxes function:

'

)

print

(

np

.

swapaxes

(

a

,

2

,

0

)

)

The output is as follows:

原数组:

[[[0 1]

  [2 3]]



 [[4 5]

  [6 7]]]





Array after calling the swapaxes function:

[[[0 4]

  [2 6]]



 [[1 5]

  [3 7]]]


Modify array dimensions

Dimension Description
broadcast Create an object that mimics broadcasting
broadcast_to Broadcast an array to a new shape
expand_dims Expand the shape of an array
squeeze Remove one-dimensional entries from the shape of an array

numpy.broadcast

numpy.broadcast is used to mimic a broadcasting object; it returns an object that encapsulates the result of broadcasting one array to another.

This function uses two arrays as input parameters, as in the following example:

Example

import

numpy

as

np

x

=

np

.

array

(

[

[

1

]

,

[

2

]

,

[

3

]

]

)

y

=

np

.

array

(

[

4

,

5

,

6

]

)

Broadcast x over y

b

=

np

.

broadcast

(

x

,

y

)

It has an iterator attribute, a tuple of iterators based on its own components

print

(

'

Broadcasting x over y:

'

)

r

,

c

=

b

.

iters

Python3.x uses next(context), Python2.x uses context.next()

print

(

next

(

r

)

,

next

(

c

)

)

print

(

next

(

r

)

,

next

(

c

)

)

print

(

'

\n

'

)

The shape attribute returns the shape of the broadcast object

print

(

'

Shape of the broadcast object:

'

)

print

(

b

.

shape

)

print

(

'

\n

'

)

Manually add x and y using broadcast

b

=

np

.

broadcast

(

x

,

y

)

c

=

np

.

empty

(

b

.

shape

)

print

(

'

Manually adding x and y using broadcast:

'

)

print

(

c

.

shape

)

print

(

'

\n

'

)

c

.

flat

=

[

u

v

for

(

u

,

v

)

in

b

]

print

(

'

Call the flat function:

'

)

print

(

c

)

print

(

'

\n

'

)

Get the same result as NumPy's built-in broadcasting support

print

(

'

Sum of x and y:

'

)

print

(

x

y

)

The output is:

对 y 广播 x:

1 4

1 5





Shape of the broadcast object:

(3, 3)





# Manually use broadcast to add x and y:

(3, 3)





Call the flat function:

[[5. 6. 7.]

 [6. 7. 8.]

 [7. 8. 9.]]





The sum of x and y:

[[5 6 7]

 [6 7 8]

 [7 8 9]]

numpy.broadcast_to

The numpy.broadcast_to function broadcasts an array to a new shape. It returns a read-only view on the original array. It is usually non-contiguous. If the new shape does not conform to NumPy's broadcasting rules, the function may raise a ValueError.

numpy.broadcast_to(array, shape, subok)

Example

import

numpy

as

np

a

=

np

.

arange

(

4

)

.

reshape

(

1

,

4

)

print

(

'

Original array:

'

)

print

(

a

)

print

(

'

\n

'

)

print

(

'

After calling the broadcast_to function:

'

)

print

(

np

.

broadcast_to

(

a

,

(

4

,

4

)

)

)

The output is:

原数组:

[[0 1 2 3]]





After calling the broadcast_to function:

[[0 1 2 3]

 [0 1 2 3]

 [0 1 2 3]

 [0 1 2 3]]

numpy.expand_dims

The numpy.expand_dims function expands the shape of an array by inserting a new axis at a specified position. The function format is as follows:

 numpy.expand_dims(arr, axis)

Parameter Description:

Example

import

numpy

as

np

x

=

np

.

array

(

(

[

1

,

2

]

,

[

3

,

4

]

)

)

print

(

'

Array x:

'

)

print

(

x

)

print

(

'

\n

'

)

y

=

np

.

expand_dims

(

x

,

axis

=

0

)

print

(

'

Array y:

'

)

print

(

y

)

print

(

'

\n

'

)

print

(

'

Shapes of arrays x and y:

'

)

print

(

x

.

shape

,

y

.

shape

)

print

(

'

\n

'

)

Insert an axis at position 1

y

=

np

.

expand_dims

(

x

,

axis

=

1

)

print

(

'

Array y after inserting an axis at position 1:

'

)

print

(

y

)

print

(

'

\n

'

)

print

(

'

x.ndim and y.ndim:

'

)

print

(

x

.

ndim

,

y

.

ndim

)

print

(

'

\n

'

)

print

(

'

x.shape and y.shape:

'

)

print

(

x

.

shape

,

y

.

shape

)

The output is:

数组 x:

[[1 2]

 [3 4]]





数组 y:

[[[1 2]

  [3 4]]]





Shape of arrays x and y:

(2, 2) (1, 2, 2)





在位置 1 插入轴之后的数组 y:

[[[1 2]]



 [[3 4]]]





x.ndim 和 y.ndim:

2 3





x.shape 和 y.shape:

(2, 2) (2, 1, 2)

numpy.squeeze

The numpy.squeeze function removes one-dimensional entries from the shape of a given array. The function format is as follows:

numpy.squeeze(arr, axis)

Parameter Description:

Example

import

numpy

as

np

x

=

np

.

arange

(

9

)

.

reshape

(

1

,

3

,

3

)

print

(

'

Array x:

'

)

print

(

x

)

print

(

'

\n

'

)

y

=

np

.

squeeze

(

x

)

print

(

'

Array y:

'

)

print

(

y

)

print

(

'

\n

'

)

print

(

'

Shapes of arrays x and y:

'

)

print

(

x

.

shape

,

y

.

shape

)

The output is:

数组 x:

[[[0 1 2]

  [3 4 5]

  [6 7 8]]]





数组 y:

[[0 1 2]

 [3 4 5]

 [6 7 8]]





Shape of arrays x and y:

(1, 3, 3) (3, 3)


Join arrays

function Description
concatenate Join a sequence of arrays along an existing axis
stack Join a sequence of arrays along a new axis.
hstack Stack arrays in a sequence horizontally (column-wise)
vstack Stack arrays in a sequence vertically (row-wise)

numpy.concatenate

The numpy.concatenate function is used to join two or more arrays of the same shape along a specified axis. The format is as follows:

numpy.concatenate((a1, a2, ...), axis)

Parameter Description:

Example

import

numpy

as

np

a

=

np

.

array

(

[

[

1

,

2

]

,

[

3

,

4

]

]

)

print

(

'

The first array:

'

)

print

(

a

)

print

(

'

\n

'

)

b

=

np

.

array

(

[

[

5

,

6

]

,

[

7

,

8

]

]

)

print

(

'

The second array:

'

)

print

(

b

)

print

(

'

\n

'

)

The two arrays have the same dimensions

print

(

'

Join two arrays along axis 0:

'

)

print

(

np

.

concatenate

(

(

a

,

b

)

)

)

print

(

'

\n

'

)

print

(

'

Join two arrays along axis 1:

'

)

print

(

np

.

concatenate

(

(

a

,

b

)

,

axis

=

1

)

)

The output is:

第一个数组:

[[1 2]

 [3 4]]





Second array:

[[5 6]

 [7 8]]





Concatenate two arrays along axis 0:

[[1 2]

 [3 4]

 [5 6]

 [7 8]]





Connect two arrays along axis 1:

[[1 2 5 6]

 [3 4 7 8]]

numpy.stack

The numpy.stack function is used to join a sequence of arrays along a new axis. The format is as follows:

numpy.stack(arrays, axis)

Parameter Description:

Example

import

numpy

as

np

a

=

np

.

array

(

[

[

1

,

2

]

,

[

3

,

4

]

]

)

print

(

'

The first array:

'

)

print

(

a

)

print

(

'

\n

'

)

b

=

np

.

array

(

[

[

5

,

6

]

,

[

7

,

8

]

]

)

print

(

'

The second array:

'

)

print

(

b

)

print

(

'

\n

'

)

print

(

'

Stack two arrays along axis 0:

'

)

print

(

np

.

stack

(

(

a

,

b

)

,

0

)

)

print

(

'

\n

'

)

print

(

'

Stack two arrays along axis 1:

'

)

print

(

np

.

stack

(

(

a

,

b

)

,

1

)

)

The output is as follows:

第一个数组:

[[1 2]

 [3 4]]





Second array:

[[5 6]

 [7 8]]





Stack two arrays along axis 0:

[[[1 2]

  [3 4]]



 [[5 6]

  [7 8]]]





Stack two arrays along axis 1:

[[[1 2]

  [5 6]]



 [[3 4]

  [7 8]]]

numpy.hstack

numpy.hstack is a convenient wrapper for numpy.concatenate, used to stack arrays horizontally along the second axis (axis=1).

numpy.hstack treats one-dimensional arrays as column vectors for concatenation, equivalent to numpy.concatenate([numpy.atleast_1d(arr) for arr in arrays], axis=1) (when all arrays have dimension >=2, concatenation is performed directly along axis=1).

Example

import

numpy

as

np

a

=

np

.

array

(

[

[

1

,

2

]

,

[

3

,

4

]

]

)

print

(

'

The first array:

'

)

print

(

a

)

print

(

'

\n

'

)

b

=

np

.

array

(

[

[

5

,

6

]

,

[

7

,

8

]

]

)

print

(

'

The second array:

'

)

print

(

b

)

print

(

'

\n

'

)

print

(

'

Horizontal stacking:

'

)

c

=

np

.

hstack

(

(

a

,

b

)

)

print

(

c

)

print

(

'

\n

'

)

The output is as follows:

第一个数组:

[[1 2]

 [3 4]]





Second array:

[[5 6]

 [7 8]]





Horizontal stacking:

[[1 2 5 6]

 [3 4 7 8]]

numpy.vstack

numpy.vstack is a convenient wrapper for numpy.concatenate, used to stack arrays vertically along the first axis (axis=0).

numpy.vstack automatically promotes one-dimensional arrays to two-dimensional row vectors before concatenating them vertically, equivalent to numpy.concatenate([numpy.atleast_2d(arr) for arr in arrays], axis=0).

Example

import

numpy

as

np

a

=

np

.

array

(

[

[

1

,

2

]

,

[

3

,

4

]

]

)

print

(

'

The first array:

'

)

print

(

a

)

print

(

'

\n

'

)

b

=

np

.

array

(

[

[

5

,

6

]

,

[

7

,

8

]

]

)

print

(

'

The second array:

'

)

print

(

b

)

print

(

'

\n

'

)

print

(

'

Vertical stacking:

'

)

c

=

np

.

vstack

(

(

a

,

b

)

)

print

(

c

)

The output is:

第一个数组:

[[1 2]

 [3 4]]





Second array:

[[5 6]

 [7 8]]





Vertical stacking:

[[1 2]

 [3 4]

 [5 6]

 [7 8]]


Split arrays

function Arrays and operations
split Split an array into multiple sub-arrays
hsplit Split an array horizontally into multiple sub-arrays (by column)
vsplit Split an array vertically into multiple sub-arrays (by row)

numpy.split

The numpy.split function splits an array into sub-arrays along a specific axis. The format is as follows:

numpy.split(ary, indices_or_sections, axis)

Parameter Description:

Example

import

numpy

as

np

a

=

np

.

arange

(

9

)

print

(

'

The first array:

'

)

print

(

a

)

print

(

'

\n

'

)

print

(

'

Split the array into three sub-arrays of equal size:

'

)

b

=

np

.

split

(

a

,

3

)

print

(

b

)

print

(

'

\n

'

)

print

(

'

Split the array at the positions indicated in the one-dimensional array:

'

)

b

=

np

.

split

(

a

,

[

4

,

7

]

)

print

(

b

)

The output is:

第一个数组:

[0 1 2 3 4 5 6 7 8]





Divide the array into three equally sized subarrays:

[array([0, 1, 2]), array([3, 4, 5]), array([6, 7, 8])]





Split the positions indicated by the array in a one-dimensional array:

[array([0, 1, 2, 3]), array([4, 5, 6]), array([7, 8])]

When axis is 0, split horizontally; when axis is 1, split vertically:

Example

import

numpy

as

np

a

=

np.

arange

(

16

)

.

reshape

(

4

,

4

)

print

(

'First array:'

)

print

(

a

)

print

(

'\n'

)

print

(

'Default split (axis 0):'

)

b

=

np.

split

(

a

,

2

)

print

(

b

)

print

(

'\n'

)

print

(

'Split horizontally:'

)

c

=

np.

split

(

a

,

2

,

1

)

print

(

c

)

print

(

'\n'

)

print

(

'Split horizontally:'

)

d

=

np.

hsplit

(

a

,

2

)

print

(

d

)

The output is:

第一个数组:

[[ 0  1  2  3]

 [ 4  5  6  7]

 [ 8  9 10 11]

 [12 13 14 15]]





默认分割(0轴):

[array([[0, 1, 2, 3],

       [4, 5, 6, 7]]), array([[ 8,  9, 10, 11],

       [12, 13, 14, 15]])]





Split along the horizontal direction:

[array([[ 0,  1],

       [ 4,  5],

       [ 8,  9],

       [12, 13]]), array([[ 2,  3],

       [ 6,  7],

       [10, 11],

       [14, 15]])]





Split along the horizontal direction:

[array([[ 0,  1],

       [ 4,  5],

       [ 8,  9],

       [12, 13]]), array([[ 2,  3],

       [ 6,  7],

       [10, 11],

       [14, 15]])]

numpy.hsplit

The numpy.hsplit function is used to split an array horizontally, splitting the original array by specifying the number of arrays of the same shape to return.

Example

import

numpy

as

np

harr

=

np

.

floor

(

10

np

.

random

.

random

(

(

2

,

6

)

)

)

print

(

'

Original array:

'

)

print

(

harr

)

print

(

'

After splitting:

'

)

print

(

np

.

hsplit

(

harr

,

3

)

)

The output is:

原array:

[[4. 7. 6. 3. 2. 6.]

 [6. 3. 6. 7. 9. 7.]]

After splitting:

[array([[4., 7.],

       [6., 3.]]), array([[6., 3.],

       [6., 7.]]), array([[2., 6.],

       [9., 7.]])]

numpy.vsplit

numpy.vsplit splits along the vertical axis, in the same way as hsplit.

Example

import

numpy

as

np

a

=

np

.

arange

(

16

)

.

reshape

(

4

,

4

)

print

(

'

The first array:

'

)

print

(

a

)

print

(

'

\n

'

)

print

(

'

Vertical split:

'

)

b

=

np

.

vsplit

(

a

,

2

)

print

(

b

)

The output is:

第一个数组:

[[ 0  1  2  3]

 [ 4  5  6  7]

 [ 8  9 10 11]

 [12 13 14 15]]





Vertical Split:

[array([[0, 1, 2, 3],

       [4, 5, 6, 7]]), array([[ 8,  9, 10, 11],

       [12, 13, 14, 15]])]


Add and delete array elements

function Element and description
resize Return a new array of the specified shape
append Append values to the end of an array
insert Insert values before the specified index along the specified axis
delete Delete a sub-array along an axis and return a new array with the deletion applied
unique Find the unique elements within an array

numpy.resize

The numpy.resize function returns a new array of the specified size.

If the new array size is larger than the original, it contains copies of the elements from the original array.

numpy.resize(arr, shape)

Parameter Description:

Example

import

numpy

as

np

a

=

np

.

array

(

[

[

1

,

2

,

3

]

,

[

4

,

5

,

6

]

]

)

print

(

'

The first array:

'

)

print

(

a

)

print

(

'

\n

'

)

print

(

'

Shape of the first array:

'

)

print

(

a

.

shape

)

print

(

'

\n

'

)

b

=

np

.

resize

(

a

,

(

3

,

2

)

)

print

(

'

The second array:

'

)

print

(

b

)

print

(

'

\n

'

)

print

(

'

Shape of the second array:

'

)

print

(

b

.

shape

)

print

(

'

\n

'

)

Note that the first row of a appears repeatedly in b because the size became larger

print

(

'

Resize the second array:

'

)

b

=

np

.

resize

(

a

,

(

3

,

3

)

)

print

(

b

)

The output is:

第一个数组:

[[1 2 3]

 [4 5 6]]





Shape of the first array:

(2, 3)





Second array:

[[1 2]

 [3 4]

 [5 6]]





Shape of the second array:

(3, 2)





Modify the size of the second array:

[[1 2 3]

 [4 5 6]

 [1 2 3]]

numpy.append

The numpy.append function adds values at the end of an array. The append operation allocates the entire array and copies the original array into the new array. In addition, the dimensions of the input arrays must match or a ValueError will be generated.

The append function always returns a one-dimensional array.

numpy.append(arr, values, axis=None)

Parameter Description:

Example

import

numpy

as

np

a

=

np

.

array

(

[

[

1

,

2

,

3

]

,

[

4

,

5

,

6

]

]

)

print

(

'

The first array:

'

)

print

(

a

)

print

(

'

\n

'

)

print

(

'

Add elements to an array:

'

)

print

(

np

.

append

(

a

,

[

7

,

8

,

9

]

)

)

print

(

'

\n

'

)

print

(

'

Add elements along axis 0:

'

)

print

(

np

.

append

(

a

,

[

[

7

,

8

,

9

]

]

,

axis

=

0

)

)

print

(

'

\n

'

)

print

(

'

Add elements along axis 1:

'

)

print

(

np

.

append

(

a

,

[

[

5

,

5

,

5

]

,

[

7

,

8

,

9

]

]

,

axis

=

1

)

)

The output is:

第一个数组:

[[1 2 3]

 [4 5 6]]





Add elements to the array:

[1 2 3 4 5 6 7 8 9]





Add element along axis 0:

[[1 2 3]

 [4 5 6]

 [7 8 9]]





# Add element to axis 1:

[[1 2 3 5 5 5]

 [4 5 6 7 8 9]]

numpy.insert

The numpy.insert function inserts values into an input array along a given axis, before the given index.

The function inserts the given value or array at the specified position (or array of positions) and returns a new array. The inserted elements can be scalar values or arrays. Note that the insert operation returns a new array and does not change the original array.

numpy.insert(arr, obj, values, axis)

Parameter Description:

Example

import

numpy

as

np

a

=

np

.

array

(

[

[

1

,

2

]

,

[

3

,

4

]

,

[

5

,

6

]

]

)

print

(

'

The first array:

'

)

print

(

a

)

print

(

'

\n

'

)

print

(

'

The Axis parameter is not passed. The input array is flattened before deletion.

'

)

print

(

np

.

insert

(

a

,

3

,

[

11

,

12

]

)

)

print

(

'

\n

'

)

print

(

'

The Axis parameter is passed. The value array is broadcast to match the input array.

'

)

print

(

'

Broadcast along axis 0:

'

)

print

(

np

.

insert

(

a

,

1

,

[

11

]

,

axis

=

0

)

)

print

(

'

\n

'

)

print

(

'

Broadcast along axis 1:

'

)

print

(

np

.

insert

(

a

,

1

,

11

,

axis

=

1

)

)

The output is as follows:

第一个数组:

[[1 2]

 [3 4]

 [5 6]]





未传递 Axis 参数。 在删除之前输入数组会被展开。

[ 1  2  3 11 12  4  5  6]





传递了 Axis 参数。 会广播值数组来配输入数组。

Broadcast along axis 0:

[[ 1  2]

 [11 11]

 [ 3  4]

 [ 5  6]]





Broadcast along axis 1:

[[ 1 11  2]

 [ 3 11  4]

 [ 5 11  6]]

numpy.delete

The numpy.delete function returns a new array with the specified sub-arrays deleted from the input array. As with the insert() function, if the axis parameter is not provided, the input array is flattened.

Numpy.delete(arr, obj, axis)

Parameter Description:

Example

import

numpy

as

np

a

=

np

.

arange

(

12

)

.

reshape

(

3

,

4

)

print

(

'

The first array:

'

)

print

(

a

)

print

(

'

\n

'

)

print

(

'

The Axis parameter is not passed. The input array is flattened before insertion.

'

)

print

(

np

.

delete

(

a

,

5

)

)

print

(

'

\n

'

)

print

(

'

Delete the second column:

'

)

print

(

np

.

delete

(

a

,

1

,

axis

=

1

)

)

print

(

'

\n

'

)

print

(

'

A slice containing alternative values to delete from the array:

'

)

a

=

np

.

array

(

[

1

,

2

,

3

,

4

,

5

,

6

,

7

,

8

,

9

,

10

]

)

print

(

np

.

delete

(

a

,

np

.

s_

[

::

2

]

)

)

The output is:

第一个数组:

[[ 0  1  2  3]

 [ 4  5  6  7]

 [ 8  9 10 11]]





未传递 Axis 参数。 在插入之前输入数组会被展开。

[ 0  1  2  3  4  6  7  8  9 10 11]





Delete the second column:

[[ 0  2  3]

 [ 4  6  7]

 [ 8 10 11]]





Slice containing replacement values deleted from the array:

[ 2  4  6  8 10]

numpy.unique

The numpy.unique function is used to remove duplicate elements from an array.

numpy.unique(arr, return_index, return_inverse, return_counts)

Example

import

numpy

as

np

a

=

np

.

array

(

[

5

,

2

,

6

,

2

,

7

,

5

,

6

,

8

,

2

,

9

]

)

print

(

'

The first array:

'

)

print

(

a

)

print

(

'

\n

'

)

print

(

'

Deduplicated values of the first array:

'

)

u

=

np

.

unique

(

a

)

print

(

u

)

print

(

'

\n

'

)

print

(

'

Index array of the deduplicated array:

'

)

u

,

indices

=

np

.

unique

(

a

,

return_index

=

True

)

print

(

indices

)

print

(

'

\n

'

)

print

(

'

We can see each value corresponding to an index of the original array:

'

)

print

(

a

)

print

(

'

\n

'

)

print

(

'

Indices of the deduplicated array:

'

)

u

,

indices

=

np

.

unique

(

a

,

return_inverse

=

True

)

print

(

u

)

print

(

'

\n

'

)

print

(

'

The indices are:

'

)

print

(

indices

)

print

(

'

\n

'

)

print

(

'

Reconstruct the original array using the indices:

'

)

print

(

u

[

indices

]

)

print

(

'

\n

'

)

print

(

'

Return the repetition count of the deduplicated elements:

'

)

u

,

indices

=

np

.

unique

(

a

,

return_counts

=

True

)

print

(

u

)

print

(

indices

)

The output is:

第一个数组:

[5 2 6 2 7 5 6 8 2 9]





Unique values of the first array:

[2 5 6 7 8 9]





Index array of the deduplicated array:

[1 0 2 4 7 9]





We can see the value corresponding to each index of the original array:

[5 2 6 2 7 5 6 8 2 9]





Index of the deduplicated array:

[2 5 6 7 8 9]





Index is:

[1 0 2 0 3 1 2 4 0 5]





Reconstruct the original array using indices:

[5 2 6 2 7 5 6 8 2 9]





Return the count of duplicates of the unique elements:

[2 5 6 7 8 9]

[3 2 2 1 1 1]