numpy is a science for multidimensional array (ndarray) calculation package. Array data is the same type of element combinations arranged in a certain order.
Use array () function to create - numpy required data structure is an array
- Generating
array of the incoming object (list, tuple, etc.). arr = np.array ([2,4,6,8]) arr = np.array ((1,4,6))
An array of fixed range np.arrang (start, stop, step)
prescribed shape array 0 np.zeros (3)
specify the shape of an array np.ones (3)
square matrix np.eye (3)
a random array np.random.rand (x, y) x row random number between 0-1 y columns
normally distributed random number np.random.randn ()
random number np.random.randint specified range (1,5,10) 10 1 a random number between 5 to
select np.random.choice () array from known
scrambled np.random.shuffle ()
-
The basic properties
The shape of the array arr.shape
how many elements arr.size
array type arr.dtype
array dimension arr.ndim -
data collection
- An interval: left and right open and closed [m, n] from m to n in which n
- A condition: ARR [ARR> 3]
[-1] - end value
[3: -2] - 3 penultimate bit 2, bit 2 is not included
- Deformation
reshape (m, n) - m rows and n columns into
.T transpose
.ravel () arr.ravel () - merge
- concatenate ([arr1, arr2], axis = 1) axis = 1 in the row direction are combined; axis = 0 in the column direction are combined
- hstack, vstack horizontal / vertical input np.hstack combined in the form of tuples ((arr1, arr2))
- colum_stack, row_stack ibid.
-
Dividing
hsplit, vsplit horizontal / vertical division np.hsplit (a, (3,4)) # Split a after the third and the fourth column -
Common analytical function
element level (acting on each element): np.abs (arr), sqrt , square, exp, log, isnan
descriptive statistics function: .sum (), mean, std , var ( variance), min, max, argmin, (smallest index / large value) the argmax -
Conditional function
np.where (condition, x, y) - similar in Excel if (condition, true, false)
arr=np.array([56,60,78])
np.where(arr>60,"及格","不及格")
-
Set relationship
comprising, crossing, and union, difference
comprising: an array of values in the array which contains arr1 arr2 in np.inld (arr1, arr2)
intersection: a common portion np.intersectld (arr1, arr2)
and set: a collection of all elements np .unionld (arr1, arr2)
difference set: in the presence arr1, arr2 np.setdiffld in the absence (arr1, arr2) -
Data preprocessing
- Type Conversion
- Missing values
- Value processing is repeated
1.astype () arr.astype (np.float64) arr.astype (np.string_)
2. missing values a. Does containing missing values, to find out b. Filling missing values
np.isnan (arr) arr [ np.isnan (ARR)] = 0
3. repeat value processing np.unique (arr)