numpy learning1 (write, create, operate)

Table of Contents

external write

create array

np.array

np.zeros

np.ones

np.full

np.arange

np.linspace

np.eye

(np.random.) series

np.empty

Operate array

View array attributes: ndim, shape, size

index(point) & slice(piece)

Reverse order

deformation

Splicing

Splitting (the inverse of splicing)


External writing

 datas = np.genfromtxt('sale_datas.txt', # file name
                       delimiter=',', # delimiter
                       skip_header=1) # Skip the first line
 datas

Output:

 array([[2020., 56.],
        [2021., 48.],
        [2022., 78.]])

Create array

np.array

 # Generally use a list to create
 print(np.array([1,2,3]),type(np.array([1,2,3])))
 # Convert up
 print(np.array([1.0,2,3]))
 # Custom data type
 print(np.array([1,2,3],dtype='float32'))
 # List comprehension
 print(np.array([i for i in range(1,4)]))
Output:
 [1 2 3] <class 'numpy.ndarray'>
 [1. 2. 3.]
 [1. 2. 3.]
 [1 2 3]

np.zeros

 # All-zero array
 np.zeros([3,3],dtype='int32')

Output:

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

np.ones

 # All-one array
 np.ones([3,3],dtype='int32')

Output:

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

np.full

 # It’s all an array of specific values
 np.full([3,3],3)

Output:

array([[3, 3, 3],
       [3, 3, 3],
       [3, 3, 3]])

np.arange

# Linear fixed-step array
np.arange(1,10,2)

Output:

array([1, 3, 5, 7, 9])

np.linspace

# Linear fixed-length array
np.linspace(1,10,5)

Output:

array([ 1. , 3.25, 5.5 , 7.75, 10. ])

np.eye

# Identity matrix
np.eye(3)

Output:

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

(np.random.)Series

# Random 0-1 uniform distribution
np.random.random([3,3])

Output:

array([[0.59293684, 0.49771022, 0.48960677],
       [0.03606951, 0.49223748, 0.5821016],
       [0.64193147, 0.54725748, 0.93691189]])
# Random normal distribution array (mean 0, standard deviation 1,)
np.random.normal(0,1,[3,3])

Output:

array([[-0.00501102, 0.40129366, 0.99769825],
       [-0.67094819, -1.12539403, -0.69537214],
       [0.0060821, -1.38601887, 0.54184239]])
# Random integer array 0-10
np.random.randint(0,10,[3,3])

Output:

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

np.empty

# Any array that meets the conditions in the memory space
np.empty([3,3])

Output:

array([[0.00501102, 0.40129366, 0.99769825],
       [0.67094819, 1.12539403, 0.69537214],
       [0.0060821, 1.38601887, 0.54184239]])

Operation array

  • View array properties: ndim, shape, size

np.random.seed(1) # Set random seed
x1 = np.random.randint(10,size=6) # One-dimensional array
x2 = np.random.randint(10,size=[3,4]) # Two-dimensional array
x3 = np.random.randint(10,size=[3,4,5]) # Three-dimensional array

print('x3 ndim:',x3.ndim) # Check how many dimensions there are
print('x3 shape:',x3.shape) # Check the size of each dimension
print('x3 size:',x3.size) # Check the number of elements

print('x3 dtype:',x3.dtype) # Check the element type in the array
print('itemsize:',x3.itemsize,'bytes') # Check the byte size of the elements in the array
print('x3 nbytes:',x3.nbytes,'bytes') # Check the total byte size of the array
Output:
x3ndim: 3
x3 shape: (3, 4, 5)
x3 size: 60
x3 dtype: int32
itemsize: 4 bytes
x3 nbytes: 240 bytes
  • index (point) & slice (slice)

# python starts counting from 0
x1 = np.array([1,2,3,4])# One-dimensional array
x2 = np.array([[5,6,7,8],
               [9,1,3,2]]) # Two-dimensional array
x3 = np.array([[[1,3,4,5],
                [6,9,3,4]],
               
               [[2,5,7,9],
                [6,1,5,2]]]) # Three-dimensional array

print('x1 1st element:',x1[0])
print('1-3 elements of x1:',x1[0:3])

print('x2 row 1 and column 1 elements:',x2[0,0])
print('All elements in columns 2 and 3 of x2:\
',x2[:,1:3])

print('x3, 1st dimension, 1st row, 1st column elements:',x3[0,0,0])
print('All dimensions 2 and 3 elements of x3:\
',x3[:,:,1:3])

# Use index to modify value
print('Original x1:',x1)
x1[0] = 9
print('After modification:',x1)
Output:
x1 1st element: 1
1-3 elements of x1: [1 2 3]
x2 row 1 column 1 elements: 5
All elements in columns 2 and 3 of x2:
 [[6 7]
 [1 3]]
x3 1st dimension 1 row 1 column elements: 1
All dimensions 2 and 3 elements of x3:
 [[[3 4]
  [9 3]]

 [[5 7]
  [1 5]]]
Original x1: [1 2 3 4]
After modification: [9 2 3 4]

reverse order

x1 = x1[::-1]

Output:

array([4, 3, 2, 9])

Views are sliced out in numpy, and the original data will also be modified when the view is modified. This means that when processing large data sets, you can obtain the fragments to be modified and modify them to reduce overhead

# Creating a copy is a separate method
x1_duplicate = x1.copy()

Output:

array([4, 3, 2, 9])
  • Transformation

# 法一reshape
example1 = np.arange(1,10)
print('example1:',example1,example1.shape)

print()

example2 = example.reshape(3,3)
print('example2:\
',example2,example2.shape)

print()

example3 = example.reshape(1,9)
print('example3:',example3,example3.shape)

print()

example4 = example.reshape(9,1)
print('example4:',example4,example4.shape)
Output:
example1: [1 2 3 4 5 6 7 8 9] (9,)

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

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

example4: [[1]
 [2]
 [3]
 [4]
 [5]
 [6]
 [7]
 [8]
 [9]] (9, 1)
# Method 2 newaxis (add a new dimension based on the number of existing elements)
example1[:,np.newaxis]

Output:

array([[1],
       [2],
       [3],
       [4],
       [5],
       [6],
       [7],
       [8],
       [9]])
  • Splicing

x = np.array([[1,2,3],
              [1,2,3]])
y = np.array([[4,5,6],
              [4,5,6]])
z = np.array([[7,8,9],
              [7,8,9]])
#法一
np.concatenate([x,y,z],axis=0) # Vertical splicing

Output:

array([[1, 2, 3],
       [1, 2, 3],
       [4, 5, 6],
       [4, 5, 6],
       [7, 8, 9],
       [7, 8, 9]])
np.concatenate([x,y,z],axis=1) # Horizontal splicing

Output:

array([[1, 2, 3, 4, 5, 6, 7, 8, 9],
       [1, 2, 3, 4, 5, 6, 7, 8, 9]])
np.vstack([x,y,z]) # Vertical splicing

Output:

array([[1, 2, 3],
       [1, 2, 3],
       [4, 5, 6],
       [4, 5, 6],
       [7, 8, 9],
       [7, 8, 9]])
np.hstack([x,y,z]) # Horizontal splicing

Output:
array([[1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9]])

m = np.array([[[1,2,3], [1,2,3]], [[1,1,1], [2,2,2]]])

n = np.array([[[4,5,6], [4,5,6]], [[2,2,2], [3,3,3]]])

np.dstack([m,n]) # Deep stitching

Output:

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

       [[1, 1, 1, 2, 2, 2],
        [2, 2, 2, 3, 3, 3]]])
  • Split (reverse of splicing)

x = np.array([[1,2,3,4,5],
              [5,6,7,8,9],
              [1,2,3,4,5]])
x1,x2,x3 = np.split(x,[1,2]) #vertical split
Output:
(array([[1, 2, 3, 4, 5]]), array([[5, 6, 7, 8, 9]]), array([[1, 2, 3, 4, 5]]))
x1,x2,x3 = np.split(x,[2,4],axis=1) # Horizontal split

Output:

(array([[1, 2],
        [5, 6],
        [1, 2]]),
 array([[3, 4],
        [7, 8],
        [3, 4]]),
 array([[5],
        [9],
        [5]]))
x1,x2,x3 = np.vsplit(x,[1,2]) # Vertical split

Output:

(array([[1, 2, 3, 4, 5]]), array([[5, 6, 7, 8, 9]]), array([[1, 2, 3, 4, 5]]))
x1,x2,x3 = np.hsplit(x,[2,4]) # Horizontal split

Output:

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

np.dsplit(x,[1,2]) # Depth split

Output:

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

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. Python entry skill treeScientific computing basic software package NumPyNumPy overview 385,860 people are learning the system