“This is the fifth day of my participation in the First Challenge 2022. For details: First Challenge 2022”

preface

I just demonstrated matplotlib, one of the most common drawing libraries for machine learning. So let’s move on to another common numpy. The library is used to clean data in pandas. The library is used to clean data in pandas.

We often use matrices for machine learning or math operations, and Numpy provides a lot of similar functionality. In addition, Numpy is relatively easy to use and relatively fast. Compared with List in Python, Numpy directly avoids the restriction of GIL due to its underlying C language and supports parallel computing. At the same time, compared with list storage mode, Numpy is more like an array.

Please note that the environment I am using here is Jupyter speed contrast

import random 
import time import numpy as np 
a = [] 
for i in range(100000000): 
    a.append(random.random()) 
# Use %time magic to see how long it takes the current line of code to run once
%time sum1=sum(a) 
b=np.array(a) 
%time sum2=np.sum(b)
Copy the code

Numpy basic operations

First, numpy is used similarly to our list. The difference is only slightly in the use of the API.

In addition, the supported data types are

>>> a = np.array([[1, 2, 3],[4, 5, 6]], dtype=np.float32)

>>> a.dtype

dtype(‘float32’)

Generate the 0,1 array

This is also commonly used, for example when we draw a picture. Assuming an RGB image, then it is typical (NXNX3) NXN stands for pixels.

ones = np.ones([4.8]) 
ones
Copy the code

Array is different from ASarray

Say first conclusion

An Arrary is a deep copy. An ASarrary is just a type conversion. It’s a shallow copy

Create an arithmetic array

  • Specify the step size argument
    • Step: indicates the step. The default value is 1

    np.arange(10, 50, 2)

Create a normal distribution

Np. Random. Normal (loc = 0.0, scale = 1.0, size = None)

Scale: float The standard deviation of the probability distribution (corresponding to the width of the distribution, the larger the scale is, the fatter the scale is, the thinner the scale is) size: Int or tuple of ints output shape, default to None, output only one value np.random. Standard_normal (size=None) returns a standard normally distributed array of the specified shape.

The shape change

np.reshape(shape)

Elements of the Curd

The TEMPLATE for this API is fixed, but the API name is different. We’re going to show you adding

import numpy as np
Arr = np.array([[1,2,3,4],[5,6,7,8]])
print('first array arr:',arr)print('Add elements to arR array:') 
print(np.append(arr,[[9.10], [11.12]])) 
print('Original array:',arr)print('Add elements along axis 0:') 
print(np.append(arr,[[9.10.11.12], [11.11.11.11]], axis=0)) 
print('Add elements along axis 1:') 
print(np.append(arr,[[9.10], [11.12]], axis=1))
Copy the code

Note the acceptance parameters

operation

Logical operations

Generate data for 10 students and 5 courses
>>> score = np.random.randint(40.100, (10.5))
Select the last 4 students' scores and use them for logical judgment
>>> test_score = score[6:, 0:5]
If the grade is greater than 60 mark True otherwise False
>>> test_score > 60
array([[ True.True.True.False.True],
[ True.True.True.False.True],
[ True.True.False.False.True],
[False.True.True.True.True]])
# BOOL assigns the value that meets the condition to the specified value - Boolean index
>>> test_score[test_score > 60] = 1
>>> test_score
array([[ 1.1.1.52.1],
[ 1.1.1.59.1],
[ 1.1.44.44.1],
[59.1.1.1.1]])
Copy the code

The ternary operation

np.where(temp > 60, 1, 0)

Matrix operations

Broadcast mechanism

arr1 = np.array([[0], [1], [2], [3]])
arr1.shape
# (4, 1)
arr2 = np.array([1.2.3])
arr2.shape
# (3),
arr1+arr2
# Result:
array([[1.2.3],
[2.3.4],
[3.4.5],
[4.5.6]])
Copy the code

Basic operation of matrix

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

To transpose

Matrix multiplication

np.dot(A,B)

Inverse moment

np.linalg.inv(a)

Please accompany

So the det (a)

For the np. Linalg. Det (a)

det_a=np.linalg.det(a)

ni=det_a*np.linalg.inv(a)