NumPy is an extension library of the Python language. Support a large number of advanced dimensional array and matrix operations, also provides a large number of mathematical functions for array operations. Numpy internally unlocks Python’s PIL(global interpreter lock), is extremely efficient, and is the base library for many machine learning frameworks!

Numpy simply creates arrays

import numpy as np
# Create simple lists
a = [1.2.3.4]
Convert a list to an array
b = np.array(b)
Copy the code

Numpy looks at array properties

Number of array elements

b.size
Copy the code

The array shape

b.shape
Copy the code

Dimension of the array

b.ndim
Copy the code

Array element type

b.dtype
Copy the code

Quickly create N – dimensional array API function

  • Create a matrix with 10 rows and 10 columns of floating point 1
array_one = np.ones([10.10])
Copy the code
  • Create a matrix with 10 rows and 10 columns of floating point 0
array_zero = np.zeros([10.10])
Copy the code
  • Create an array from existing data
    • Array (deep copy)
    • Asarray (shallow copy)

Numpy creates a random number groupnp.random

  • #### uniform distribution

    • np.random.rand(10, 10)Creates an array (range 0 to 1) of the specified shape (example: 10 rows and 10 columns)
    • np.random.uniform(0, 100)Creates a number in the specified range
    • np.random.randint(0, 100)Creates an integer in the specified range
  • #### Normal distribution Normal distribution given mean/standard deviation/dimension np.random. Normal (1.75, 0.1, (2, 3))

  • Array index, slice

Generate a two-dimensional array with 4 rows and 5 columns normally
arr = np.random.normal(1.75.0.1, (4.5))
print(arr)
Select columns 2 to 3 from rows 1 to 2 (counting from row 0)
after_arr = arr[1:3.2:4]
print(after_arr)
Copy the code

  • Change the shape of the array (to match the number of elements)

print("0 Use of the 0 0 function!")
one_20 = np.ones([20])
print("-->1 row 20 columns <--")
print (one_20)

one_4_5 = one_20.reshape([4.5])
print("-->4 rows and 5 columns <--")
print (one_4_5)
Copy the code

Numpy calculations (important)

Conditions of operation

import numpy as np
stus_score = np.array([[80.88], [82.81], [84.75], [86.83], [75.81]])
stus_score > 80
Copy the code

import numpy as np
stus_score = np.array([[80.88], [82.81], [84.75], [86.83], [75.81]])
np.where(stus_score < 80.0.90)
Copy the code

Statistical calculations

  • Specify axis maximum valueamax(Parameter 1: array; Parameter 2: axis = 0/1; 0 for column 1 for row)

stus_score = np.array([[80.88], [82.81], [84.75], [86.83], [75.81]])
# find the maximum value of each column (0 indicates column)
print("The maximum value of each column is :")
result = np.amax(stus_score, axis=0)
print(result)

print("The maximum value of each row is :")
result = np.amax(stus_score, axis=1)
print(result)
Copy the code
  • Specifies the axis minimumamin

stus_score = np.array([[80.88], [82.81], [84.75], [86.83], [75.81]])
Find the minimum value of each row (0 indicates column)
print("The minimum value of each column is :")
result = np.amin(stus_score, axis=0)
print(result)

Find the minimum value of each row (1 represents a row)
print("The minimum value of each row is :")
result = np.amin(stus_score, axis=1)
print(result)
Copy the code
  • Specified axis meanmean

stus_score = np.array([[80.88], [82.81], [84.75], [86.83], [75.81]])
Find the average value of each row (0 denotes column)
print("Average value of each column :")
result = np.mean(stus_score, axis=0)
print(result)

Find the average value of each row (1 means row)
print("Average of each row :")
result = np.mean(stus_score, axis=1)
print(result)
Copy the code
  • The variancestd

stus_score = np.array([[80.88], [82.81], [84.75], [86.83], [75.81]])
Find the variance of each row (0 denotes column)
print("Variance of each column :")
result = np.std(stus_score, axis=0)
print(result)

Find the variance of each row (1 denotes row)
print("Variance of each row :")
result = np.std(stus_score, axis=1)
print(result)
Copy the code

Array operation

  • Array and number operations

stus_score = np.array([[80.88], [82.81], [84.75], [86.83], [75.81]])
print("Before extra points :")
print(stus_score)

Add 5 points to all normal scores
stus_score[:, 0] = stus_score[:, 0] +5
print("After bonus points :")
print(stus_score)
Copy the code

stus_score = np.array([[80.88], [82.81], [84.75], [86.83], [75.81]])
print("Before halving :")
print(stus_score)

# Halve your usual score
stus_score[:, 0] = stus_score[:, 0] *0.5
print("Halved :")
print(stus_score)
Copy the code
  • Arrays can also be added, subtracted, multiplied, and divided, but are rarely used

a = np.array([1.2.3.4])
b = np.array([10.20.30.40])
c = a + b
d = a - b
e = a * b
f = a / b
print("A + b for", c)
print("A - b for", d)
print("A * b", e)
print("A/b for", f)
Copy the code

Matrix operationsnp.dot()(Very important)

  • Calculation rules

(M rows, N columns) times (N rows, Z columns) = (M rows, Z columns)

stus_score = np.array([[80.88], [82.81], [84.75], [86.83], [75.81]])
The final score is 60%
q = np.array([[0.4], [0.6]])
result = np.dot(stus_score, q)
print("The final result is :")
print(result)
Copy the code
  • Matrix joining together
    • Matrix vertical splicing

print("V1 for.")
v1 = [[0.1.2.3.4.5],
      [6.7.8.9.10.11]]
print(v1)
print("V2 is:")
v2 = [[12.13.14.15.16.17], 
      [18.19.20.21.22.23]]
print(v2)
# Vertical splicing
result = np.vstack((v1, v2))
print("The result of the vertical stitching of V1 and V2 is)
print(result)
Copy the code
  • Matrix horizontal splicing

print("V1 for.")
v1 = [[0.1.2.3.4.5],
      [6.7.8.9.10.11]]
print(v1)
print("V2 is:")
v2 = [[12.13.14.15.16.17], 
      [18.19.20.21.22.23]]
print(v2)
# Vertical splicing
result = np.hstack((v1, v2))
print("The result of the horizontal splicing of V1 and V2 is)
print(result)
Copy the code

Numpy reads the datanp.genfromtxt

If the numeric data has an unrecognized value, it is displayed as nan, which is equivalent to Np.nan and is of type float.