Numpy Array Modify Shape/Join/Split/Element Add and Delete

Modify Array Shape

Is to create an array, the place of the new array, after changing the shape of an existing array as required There are elements from the original array, and the number of elements remains the same, changing only the shape.

  • reshape. Modify shape without changing data
  • flat array element iterator
  • flatten. Returns a copy of the array. Changes made to the copy will not affect the original array
  • ravel. Returns the expanded array

reshape(...) Returns a copy of an array of a given shape, without sharing the same view

np. reshape(a, newshape, order='C ')
  • a is the original array,
  • Newshapes are the shapes that receive new arrays expressed in tuples
a = np.arange(10)
print(a)
b = a.reshape(2, 5)
print(b)
c = np.reshape(a, (2,5))
print(c)
//Output:
[0 1 2 3 4 5 6 7 8 9]
[[0 1 2 3 4]
 [5 6 7 8 9]]
[[0 1 2 3 4]
 [5 6 7 8 9]]

numpy.ndarray.flat()

numpy.ndarray.flat is an array element iterator


import numpy as np
 
a = np.arange(9).reshape(3,3) 
print ('Original array:')
for row in a:
    print (row)
 
#Each element in an array is processed using the flat attribute, which is an array element iterator:
print ('Iterated array:')
for element in a.flat:
    print (element)

Output:

Original array:
[0 1 2]
[3 4 5]
[6 7 8]
Iterated array:
0
1
2
3
4
5
6
7
8

flatten()/ravel() converts a multidimensional array into a one-dimensional array

  • flatten() turns a multidimensional array into a one-dimensional array, not in the same view as the original array
  • ravel() converts a multidimensional array into a one-dimensional array, sharing a view with the original array
ndarray.flatten(order='C')
numpy.ravel(a, order='C')
  • Order:'C'-- by row,'F' -- by column,'A'-- in the original order,'K' -- the order in which elements appear in memory.
a = np.arange(10).reshape(2, 5)
print(a)
b = a.flatten()
print(b)
c = a.ravel()
print(c)
//Output:
[[0 1 2 3 4]
 [5 6 7 8 9]]
[0 1 2 3 4 5 6 7 8 9]
[0 1 2 3 4 5 6 7 8 9]

Combination and Split

Horizontal/Vertical/Depth Combination

  • numpy.hstack(tup) level (in column order) stacks arrays
  • numpy.vstack(tup) stacks arrays vertically (in row order)
  • numpy.stack(arrays, axis=0) stacks a stack of array data in a specified dimension
  • numpy.dstack((a, b)) stack arrays are grouped sequentially deep (along the third dimension) into depths
  • Numpy.concatenate ((a1, a2,...), axis=0) Connects a tuple, a list in a list, or a ndarray based on a specified dimension. The first few functions can actually be equivalent using the concatenate() function

numpy.hstack(tup)

The horizontal composition function is numpy.hstack (where the tup parameter is a tuple and contains several arrays that will be grouped together).The following example assumes a two-dimensional array with the same shape in the 0-axis direction and different shapes in the 1-axis direction.

Equivalent to np.concatenate (tup,axis=1)

a= np.arange(9).reshape(3,3)
b = np.arange(12).reshape(3, 4)
c = np.arange(15).reshape(3, 5)
print(a)
print(b)
print(c)
print('**************')
d = np.hstack((a, b))
print(d)
print('******************')
e = np.hstack((a, b, c))
print(e)
//Output:
[[0 1 2]
 [3 4 5]
 [6 7 8]]
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
[[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]]
**************
[[ 0  1  2  0  1  2  3]
 [ 3  4  5  4  5  6  7]
 [ 6  7  8  8  9 10 11]]
******************
[[ 0  1  2  0  1  2  3  0  1  2  3  4]
 [ 3  4  5  4  5  6  7  5  6  7  8  9]
 [ 6  7  8  8  9 10 11 10 11 12 13 14]]

numpy.vstack(tup)

The horizontal combination ahead is along the 1-axis (as is the case with two-dimensional arrays and multidimensional arrays), whereas the vertical combination here is along the direction perpendicular to the 1-axis).

** Equivalent to: np.concatenate(tup,axis=0)**

a= np.arange(9).reshape(3,3)
b = np.arange(12).reshape(4, 3)
c = np.arange(15).reshape(5, 3)
print(a)
print(b)
print(c)
print('**************')
d = np.vstack((a, b))
print(d)
print('******************')
e = np.vstack((a, b, c))
print(e)
//Output:
[[0 1 2]
 [3 4 5]
 [6 7 8]]
[[ 0  1  2]
 [ 3  4  5]
 [ 6  7  8]
 [ 9 10 11]]
[[ 0  1  2]
 [ 3  4  5]
 [ 6  7  8]
 [ 9 10 11]
 [12 13 14]]
**************
[[ 0  1  2]
 [ 3  4  5]
 [ 6  7  8]
 [ 0  1  2]
 [ 3  4  5]
 [ 6  7  8]
 [ 9 10 11]]
******************
[[ 0  1  2]
 [ 3  4  5]
 [ 6  7  8]
 [ 0  1  2]
 [ 3  4  5]
 [ 6  7  8]
 [ 9 10 11]
 [ 0  1  2]
 [ 3  4  5]
 [ 6  7  8]
 [ 9 10 11]
 [12 13 14]]

numpy.stack(arrays, axis=0) arrays combined require the same shape

import numpy as np

a = np.arange(9).reshape(3, 3)
b = np.arange(12).reshape(3, 4)
c = np.arange(15) . reshape(3, 5)
m = a * 3

print(np.stack((a, m), axis=1))
//Output:
[[[ 0  1  2]
  [ 0  3  6]]

 [[ 3  4  5]
  [ 9 12 15]]

 [[ 6  7  8]
  [18 21 24]]]

numpy.dstack((a, b)) stack arrays are grouped sequentially deep (along the third dimension) into depths

Equivalent to: np.concatenate(tup,axis=2)

a = np.arange(9).reshape(3,3)
b = 3 * a
print(a)
print(b)

#Depth combination
c = np.dstack((a, b))
print(c)
//Output:
[[0 1 2]
 [3 4 5]
 [6 7 8]]
[[ 0  3  6]
 [ 9 12 15]
 [18 21 24]]
[[[ 0  0]
  [ 1  3]
  [ 2  6]]

 [[ 3  9]
  [ 4 12]
  [ 5 15]]

 [[ 6 18]
  [ 7 21]
  [ 8 24]]]

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

The most versatile concatenate() function, the first few functions can actually be used concatenate() function for equivalent operations

a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6]])
print(np.concatenate((a, b), axis=0))
print(np.concatenate((a, b.T), axis=1))
//Output:
array([[1, 2],
       [3, 4],
       [5, 6]])

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

Segmentation of Arrays

np.split() is a more general method of segmentation

Its function form is:

np.split(ary, indices_or_ sections, axis=0)
a = np.arange(24).reshape(4 , 6)
print(a)

c = np.split(a, 2, axis=1)
print(c)
//Output:
[[ 0  1  2  3  4  5]
 [ 6  7  8  9 10 11]
 [12 13 14 15 16 17]
 [18 19 20 21 22 23]]
[array([[ 0,  1,  2],
       [ 6,  7,  8],
       [12, 13, 14],
       [18, 19, 20]]), array([[ 3,  4,  5],
       [ 9, 10, 11],
       [15, 16, 17],
       [21, 22, 23]])]

Adding and deleting elements

  • numpy.append(arr,values,axis=None) adds an element at the end
  • numpy.insert(arr,obj,value,axis=None) adds an element at a specified location
  • numpy.delete(arr,obj,axis=None) Removes the specified element

np.append() function usage

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

Insert values at the end of the target arr. Notice here that values and arr should be vectors of the same dimension

Axis is an optional parameter.If axis is not set, the result returned by np.append() flattens arr and values and stitches them.

a = np.array([[1, 2, 3], [4, 5, 6]])
b = np.array([[7, 8, 9]])
print(a)
r = np.append(a, b)
print(r)
r = np.append(a, b, axis=0)
print(r)
//Output:
[[1 2 3]
 [4 5 6]]
[1 2 3 4 5 6 7 8 9]
[[1 2 3]
 [4 5 6]
 [7 8 9]]

np.insert() function usage

numpy.insert(arr,obj,value,axis=None) 
  • arr:Target vector
  • obj:Target location
  • Value:For the value you want to insert
  • axis: is the inserted dimension
a = np.array(np.arange(12).reshape(3, 4))
    print(a) 
    print(np.insert(a, 1, [1, 1, 1, 1], 0))  #First row adds array [1, 1, 1, 1]

	
//Output:
[[ 0  1  2  3]
  [ 4  5  6  7]
  [ 8  9 10 11]]

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

np.delete()

numpy.delete(arr,obj,axis=None) 

Ability to delete elements from arr according to axis and obj.If axis is not set, flatten arr before deleting it

  • arr: Input vector
  • obj: Indicates which subvector should be removed.A vector that can be an integer or an int
  • Axis: Indicates which axis subvector to delete, and returns a flattened vector by default
a = np.array(np.arange(12).reshape(3, 4))
print(a) 
print(np.delete(a,1,0))  #Delete the first row of data 
print(np.delete(a,1,1))  #Delete the first column of data
print(np.delete(a,[0,1],1)) #Delete column 0 and 1 
    
//Output:
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]

[[ 0  1  2  3]
 [ 8  9 10 11]]

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

[[ 2  3]
 [ 6  7]
 [10 11]]

Keywords: Attribute

Added by XeroXer on Sat, 21 Mar 2020 04:00:14 +0200