“This is the 16th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

Numpy cleanup tool

Numpy common data structures


  • A common data structure used in Numpy is the NDARray format
  • Created using the array function with the syntax array(list or tuple)
  • It can be created using other functions such as arange, Linspace, Zeros, and so on
import numpy as np
Copy the code
arr1 = np.array([-9.7.4.3])
Copy the code
arr1
Copy the code
array([-9,  7,  4,  3])
Copy the code
type(arr1)  # n-dimensional array
Copy the code
numpy.ndarray
Copy the code
arr1 = np.array([-9.7.4.3], dtype='str')
Copy the code
arr1
Copy the code
array(['-9', '7', '4', '3'], dtype='<U2')
Copy the code
arr1 = np.array([-9.7.4.3], dtype=float)
Copy the code
arr1
Copy the code
array([-9.,  7.,  4.,  3.])
Copy the code
arr1 = np.array([-9.7.4.3], dtype=int)
Copy the code
arr1
Copy the code
array([-9,  7,  4,  3])
Copy the code
Copy the code
arr2 = np.array([[1.2.3.4], [5.6.7.8], [9.10.11.12]])
Copy the code
arr2
Copy the code
array([[ 1,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 9, 10, 11, 12]])
Copy the code
for i in range(1.10) :print(i)
Copy the code
1, 2, 3, 4, 5, 6, 7, 8, 9Copy the code
np.arange(1.10.0.5)
Copy the code
Array ([1., 1.5, 2., 2.5, 3., 3.5, 4, 4.5, 5, 5.5, 6, 6.5, 7, 7.5 to 8, 8.5, 9, 9.5])Copy the code
# arithmetic array
The first parameter: the starting value
# Second argument: termination value
Number of elements
# endpoint: Whether to include a final value
np.linspace(1.10.10, endpoint=True)
Copy the code
array([ 1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.])
Copy the code
np.linspace(1.10.20, endpoint=True)
Copy the code
Array ([1., 1.47368421, 1.94736842, 2.42105263, 2.89473684, 3.36842105, 3.84210526, 4.31578947, 4.78947368, 5.26315789, 5.73684211, 6.21052632, 6.68421053, 7.15789474, 7.63157895, 8.10526316, 8.57894737, 9.05263158, 9.52631579, 10.)Copy the code
9/19  # step
Copy the code
0.47368421052631576
Copy the code
1 + 3 * (9/19)
Copy the code
2.4210526315789473
Copy the code
Create an array of 4 rows and 5 columns with a value of 0
np.zeros([4.5])
Copy the code
array([[0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.]])
Copy the code
np.zeros(4)
Copy the code
array([0., 0., 0., 0.])
Copy the code
Create an array of 2 rows and 3 columns with the value 1
np.ones([2.3])
Copy the code
array([[1., 1., 1.],
       [1., 1., 1.]])
Copy the code
Add 1 to each array element
arr2 + 1
Copy the code
array([[ 2,  3,  4,  5],
       [ 6,  7,  8,  9],
       [10, 11, 12, 13]])
Copy the code
Determine the dimension of the array
arr1.ndim
Copy the code
1
Copy the code
arr2.ndim
Copy the code
2
Copy the code
Determine the shape of the array
arr1.shape
Copy the code
(4)Copy the code
arr2.shape
Copy the code
(3, 4)
Copy the code
Return the number of elements in the array
arr2.size
Copy the code
12
Copy the code
Return the array element type
arr2.dtype
Copy the code
dtype('int32')
Copy the code
Copy the code
data2 = ((8.5.6.4.1.2.0.7), (1.5.3.5.4.7.3.9), (3.2.4.5.6.3.9), (11.2.13.4.15.6.17.8.19))
Copy the code
arr3 = np.array(data2)
Copy the code
arr3
Copy the code
Array ([[8.5, 6, 4, 1.2, 0.7], [1.5, 3., 5.4, 7.3, 9.], [3.2, 4.5, 6., 3., 9.], [11.2, 13.4, 15.6, 17.8, 19.]])Copy the code
arr3[0]
Copy the code
Array ([8.5, 6., 4., 1.2, 0.7])Copy the code
arr3[3]
Copy the code
Array ([11.2, 13.4, 15.6, 17.8, 19.])Copy the code
Take the second row, the third column
arr3[1.2]
Copy the code
5.4
Copy the code
arr3[1] [2]
Copy the code
5.4
Copy the code
arr3[:, 3]
Copy the code
Array ([1.2, 7.3, 3., 17.8])Copy the code
Take the second through the third columns
arr3[:, 1:3]
Copy the code
Array ([[6, 4], [3., 5.4], [4.5, 6.], [13.4, 15.6]])Copy the code
arr3[3] [1]
Copy the code
13.4
Copy the code


Array subscript from 0 Start and close left and open right \color{red} Array subscripts start at 0 and close left and open right

Numpy is a common data cleansing function

import numpy as np
Copy the code
s = np.array([1.2.3.4.3.2.1.2.2.4.6.7.2.4.8.4.5])
Copy the code
s = np.sort(s)
Copy the code
s
Copy the code
array([1, 1, 2, 2, 2, 2, 2, 3, 3, 4, 4, 4, 4, 5, 6, 7, 8])
Copy the code
np.array(sorted(s, reverse=True))
Copy the code
array([8, 7, 6, 5, 4, 4, 4, 4, 3, 3, 2, 2, 2, 2, 2, 1, 1])
Copy the code
Return the sorted index
np.argsort(s)
Copy the code
array([ 0,  6, 12,  7,  5,  8,  1,  2,  4,  3, 15,  9, 13, 16, 10, 11, 14],
      dtype=int64)
Copy the code
arr1 = np.array([[0.1.3], [4.2.9], [4.5.9], [1, -3.4]])
Copy the code
# axis=0: indicates column sorting
# axis=1: indicates row sorting
np.sort(arr1, axis=0)
Copy the code
array([[ 0, -3,  3],
       [ 1,  1,  4],
       [ 4,  2,  9],
       [ 4,  5,  9]])
Copy the code
np.sort(arr1, axis=1)
Copy the code
array([[ 0,  1,  3],
       [ 2,  4,  9],
       [ 4,  5,  9],
       [-3,  1,  4]])
Copy the code
s
Copy the code
array([1, 2, 3, 4, 3, 2, 1, 2, 2, 4, 6, 7, 2, 4, 8, 4, 5])
Copy the code
# The first parameter: condition
# second argument: the return value of the condition
# third argument: the return value if the condition is not met
# greater than 3 returns the element itself, and less than 3 returns -1
np.where(s>3, s, -1)
Copy the code
array([-1, -1, -1,  4, -1, -1, -1, -1, -1,  4,  6,  7, -1,  4,  8,  4,  5])
Copy the code
# The first parameter: condition
# second argument: the value returned
Filter elements in the array with a value greater than 3
np.extract(s > 3, s)
Copy the code
array([4, 4, 6, 7, 4, 8, 4, 5])
Copy the code

Finally, welcome to pay attention to my personal wechat public account “Little Ape Ruochen”, get more IT technology, dry goods knowledge, hot news