Huawei AI Certification Training Experiment Guide

Page 1 sur 22Lecteur de document UniversityLib

Huawei AI Certification Training Experiment Guide

Machine Learning · course

Voir tous les documents en intelligence artificielle et données

Huawei AI Certification Training

HCIA-AI

Machine Learning

Experiment Guide

ISSUE:3.0

![C:\Users\jwx341670\AppData\Local\Microsoft\Windows\INetCache\Content.Word\HW_POS_RBG_Vertical-150ppi.png](data:image/png;base64...)

HUAWEI TECHNOLOGIES CO., LTD.

| |

| --- |

| Copyright © Huawei Technologies Co., Ltd. 2020. All rights reserved. No part of this document may be reproduced or transmitted in any form or by any means without prior written consent of Huawei Technologies Co., Ltd. Trademarks and Permissions ![C:\Users\jwx341670\AppData\Local\Microsoft\Windows\INetCache\Content.Word\HW_POS_RBG_Vertical-150ppi.png](data:image/png;base64...) and other Huawei trademarks are trademarks of Huawei Technologies Co., Ltd. All other trademarks and trade names mentioned in this document are the property of their respective holders. Notice The purchased products, services and features are stipulated by the contract made between Huawei and the customer. All or part of the products, services and features described in this document may not be within the purchase scope or the usage scope. Unless otherwise specified in the contract, all statements, information, and recommendations in this document are provided "AS IS" without warranties, guarantees or representations of any kind, either express or implied. The information in this document is subject to change without notice. Every effort has been made in the preparation of this document to ensure accuracy of the contents, but all statements, information, and recommendations in this document do not constitute a warranty of any kind, express or implied. |

| | |

| --- | --- |

| Huawei Technologies Co., Ltd. | |

| Address: | Huawei Industrial Base Bantian, Longgang Shenzhen 518129 People's Republic of China |

| Website: | http://[e](http://e.huawei.com/).huawei.com |

Huawei Certificate System

Huawei Certification is an integral part of the company's "Platform + Ecosystem" strategy, it supports the ICT infrastructure featuring "Cloud-Pipe-Device". It evolves to reflect the latest trends of ICT development. Huawei Certification consists of two categories: ICT Infrastructure, and Cloud Service & Platform.

Huawei offers three levels of certification: Huawei Certified ICT Associate (HCIA), Huawei Certified ICT Professional (HCIP), and Huawei Certified ICT Expert (HCIE).

With its leading talent development system and certification standards, Huawei is committed to developing ICT professionals in the digital era, building a healthy ICT talent ecosystem.HCIA-AI V3.0 aims to train and certify engineers who are capable of designing and developing AI products and solutions using algorithms such as machine learning and deep learning.

HCIA-AI V3.0 certification demonstrates that: You know the development history of AI, Huawei Ascend AI system and full-stack all-scenario AI strategies, and master traditional machine learning and deep learning algorithms; you can use the TensorFlow and MindSpore development frameworks to build, train, and deploy neural networks; you are competent for sales, marketing, product manager, project management, and technical support positions in the AI field.

Huawei Certification Portfolio

![](data:image/png;base64...)

About This Document

Overview

This document is applicable to the candidates who are preparing for the HCIA-AI exam and the readers who want to understand the AI programming basics. After learning this guide, you will be able to perform basic machine learning programming.

Description

This guide contains one experiment, which is based on how to use sklearn-learn and python packages to predict house prices in Boston using different regression algorithms. It is hoped that trainees or readers can get started with machine learning and have the basic programming capability of machine learning building.

Background Knowledge Required

To fully understand this course, the readers should have basic Python programming capabilities, knowledge of data structures and mechine learning algorithms.

Experiment Environment Overview

Python Development Tool

This experiment environment is developed and compiled based on Python 3.6 and XGBoost will be used.

Contents

[About This Document 3](#_Toc59024320)

[Overview 3](#_Toc59024321)

[Description 3](#_Toc59024322)

[Background Knowledge Required 3](#_Toc59024323)

[Experiment Environment Overview 3](#_Toc59024324)

[1 Detail of linear regression 5](#_Toc59024325)

[1.1 Introduction 5](#_Toc59024326)

[1.1.1 About This Experiment 5](#_Toc59024327)

[1.1.2 Objectives 5](#_Toc59024328)

[1.2 Experiment Code 5](#_Toc59024329)

[1.2.1 Data preparation 5](#_Toc59024330)

[1.2.2 Define related functions 6](#_Toc59024331)

[1.2.3 Start the iteration 7](#_Toc59024332)

[1.3 Thinking and practice 12](#_Toc59024333)

[1.3.1 Question 1 12](#_Toc59024334)

[1.3.2 Question 2 12](#_Toc59024335)

[2 Decision tree details 13](#_Toc59024336)

[2.1 Introduction 13](#_Toc59024337)

[2.1.1 About This Experiment 13](#_Toc59024338)

[2.1.2 Objectives 13](#_Toc59024339)

[2.2 Experiment Code 13](#_Toc59024340)

[2.2.1 Import the modules you need 13](#_Toc59024341)

[2.2.2 Superparameter definition section 13](#_Toc59024342)

[2.2.3 Define the functions required to complete the algorithm 14](#_Toc59024343)

[2.2.4 Execute the code 19](#_Toc59024344)

Detail of linear regression

Introduction

About This Experiment

This experiment mainly uses basic Python code and the simplest data to reproduce how a linear regression algorithm iterates and fits the existing data distribution step by step.

The experiment mainly used Numpy module and Matplotlib module.Numpy for calculation, Matplotlib for drawing.

Objectives

The main purpose of this experiment is as follows.

  • Familiar with basic Python statements
  • Master the implementation steps of linear regression

Experiment Code

Data preparation

10 data were randomly set, and the data were in a linear relationship.

The data is converted to array format so that it can be computed directly when multiplication and addition are used.

Code:

#Import the required modules, numpy for calculation, and Matplotlib for drawing

import numpy as np

import matplotlib.pyplot as plt

#This code is for jupyter Notebook only

%matplotlib inline

define data, and change list to array

x = [3,21,22,34,54,34,55,67,89,99]

x = np.array(x)

y = [1,10,14,34,44,36,22,67,79,90]

y = np.array(y)

#Show the effect of a scatter plot

plt.scatter(x,y)

Output:

![](data:image/png;base64...)

Scatter Plot

Define related functions

Model function: Defines a linear regression model wx+b.

Loss function: loss function of Mean square error.

Optimization function: gradient descent method to find partial derivatives of w and b.

Code:

#The basic linear regression model is wx+ b, and since this is a two-dimensional space, the model is ax+ b

def model(a, b, x):

return a\*x + b

Publicité

#The most commonly used loss function of linear regression model is the loss function of mean variance difference

def loss\_function(a, b, x, y):

num = len(x)

prediction=model(a,b,x)

return (0.5/num) \* (np.square(prediction-y)).sum()

#The optimization function mainly USES partial derivatives to update two parameters a and b

def optimize(a,b,x,y):

num = len(x)

prediction = model(a,b,x)

#Update the values of A and B by finding the partial derivatives of the loss function on a and b

da = (1.0/num) \ ((prediction -y)\x).sum()

db = (1.0/num) \* ((prediction -y).sum())

a = a - Lr\*da

b = b - Lr\*db

return a, b

#iterated function, return a and b

def iterate(a,b,x,y,times):

for i in range(times):

a,b = optimize(a,b,x,y)

return a,b

Start the iteration

Initialization and Iterative optimization model

Code:

#Initialize parameters and display

a = np.random.rand(1)

print(a)

b = np.random.rand(1)

print(b)

Lr = 1e-4

#For the first iteration, the parameter values, losses, and visualization after the iteration are displayed

a,b = iterate(a,b,x,y,1)

prediction=model(a,b,x)

loss = loss\_function(a, b, x, y)

print(a,b,loss)

plt.scatter(x,y)

plt.plot(x,prediction)

Output:

![](data:image/png;base64...)

Iterate 1 time

In the second iteration, the parameter values, loss values and visualization effects after the iteration are displayed

Code:

a,b = iterate(a,b,x,y,2)

prediction=model(a,b,x)

loss = loss\_function(a, b, x, y)

print(a,b,loss)

plt.scatter(x,y)

plt.plot(x,prediction)

Output:

![](data:image/png;base64...)

Iterate 2 times

The third iteration shows the parameter values, loss values and visualization after iteration

Code:

a,b = iterate(a,b,x,y,3)

prediction=model(a,b,x)

loss = loss\_function(a, b, x, y)

print(a,b,loss)

plt.scatter(x,y)

plt.plot(x,prediction)

Output:

![](data:image/png;base64...)

Iterate 3 times

In the fourth iteration, parameter values, loss values and visualization effects are displayed

Code:

a,b = iterate(a,b,x,y,4)

prediction=model(a,b,x)

loss = loss\_function(a, b, x, y)

print(a,b,loss)

plt.scatter(x,y)

plt.plot(x,prediction)

Output:

![](data:image/png;base64...)

Iterate 4 times

The fifth iteration shows the parameter value, loss value and visualization effect after iteration

Code:

a,b = iterate(a,b,x,y,5)

prediction=model(a,b,x)

loss = loss\_function(a, b, x, y)

print(a,b,loss)

plt.scatter(x,y)

plt.plot(x,prediction)

Output:

![](data:image/png;base64...)

Iterate 5 times

The 10000th iteration, showing the parameter values, losses and visualization after iteration

Code:

a,b = iterate(a,b,x,y,10000)

prediction=model(a,b,x)

loss = loss\_function(a, b, x, y)

print(a,b,loss)

plt.scatter(x,y)

plt.plot(x,prediction)

Output:

![](data:image/png;base64...)

Iterate 10000 times

Publicité

Thinking and practice

Question 1

Try to modify the original data yourself, Think about it: Does the loss value have to go to zero?

Question 2

Modify the values of Lr, Think: What is the role of the Lr parameter?

Decision tree details

Introduction

About This Experiment

This experiment focuses on the decision tree algorithm through the basic Python code.

It mainly uses Numpy module, Pandas module and Math module. We will implement the CART tree(Classification and Regressiontree models) in this experiment.

You have to download the dataset before this experiment through this link:

https://data-certification.obs.cn-east-2.myhuaweicloud.com/ENG/HCIA-AI/V3.0/ML-Dataset.rar

Objectives

The purpose of this experiment is as follows:

  • Familiar with basic Python syntax
  • Master the principle of Classification tree and implement with Python code
  • Master the principle of Regression tree and implement with Python code

Experiment Code

Import the modules you need

Pandas is a tabular data processing module.

Math is mainly used for mathematical calculations.

Numpy is the basic computing module.

Code:

import pandas as pd

import math

import numpy as np

Superparameter definition section

Here you can choose to use Classification tree or Regression tree. Specifies the address of the dataset. Get feature name. Determine whether the algorithm matches the data set

Code:

algorithm = "Regression" # Algorithm: Classification, Regression

algorithm = "Classification" # Algorithm: Classification, Regression

Dataset1: Text features and text labels

#df = pd.read\_csv("D:/Code/Decision Treee/candidate/decision-trees-for-ml-master/decision-trees-for-ml-master/dataset/golf.txt")

Dataset2: Mix features and Numeric labels, here you have to change the path to yours.

df = pd.read\_csv("ML-Dataset/golf4.txt")

This dictionary is used to store feature types of continuous numeric features and discrete literal features for subsequent judgment

dataset\_features = dict()

num\_of\_columns = df.shape[1]-1

#The data type of each column of the data is saved for displaying the data name

for i in range(0, num\_of\_columns):

#Gets the column name and holds the characteristics of a column of data by column

column\_name = df.columns[i]

#Save the type of the data

dataset\_features[column\_name] = df[column\_name].dtypes

The size of the indent when display

root = 1

If the algorithm selects a regression tree but the label is not a continuous value, an error is reported

if algorithm == 'Regression':

if df['Decision'].dtypes == 'object':

raise ValueError('dataset wrong')

If the tag value is continuous, the regression tree must be used

if df['Decision'].dtypes != 'object':

algorithm = 'Regression'

global\_stdev = df['Decision'].std(ddof=0)

Define the functions required to complete the algorithm

ProcessContinuousFeatures: Used to convert a continuous digital feature into a category feature.

Code:

This function is used to handle numeric characteristics

def processContinuousFeatures(cdf, column\_name, entropy):

Numerical features are arranged in order

unique\_values = sorted(cdf[column\_name].unique())

subset\_ginis = [];

subset\_red\_stdevs = []

for i in range(0, len(unique\_values) - 1):

threshold = unique\_values[i]

Find the segmentation result if the first number is used as the threshold

subset1 = cdf[cdf[column\_name] <= threshold]

subset2 = cdf[cdf[column\_name] > threshold]

Calculate the proportion occupied by dividing the two parts

subset1\_rows = subset1.shape[0];

subset2\_rows = subset2.shape[0]

total\_instances = cdf.shape[0]

In the text feature part, entropy is calculated by using the cycle,

and in the numeric part, entropy is calculated by using the two groups after segmentation,

and the degree of entropy reduction is obtained

if algorithm == 'Classification':

decision\_for\_subset1 = subset1['Decision'].value\_counts().tolist()

decision\_for\_subset2 = subset2['Decision'].value\_counts().tolist()

gini\_subset1 = 1;

gini\_subset2 = 1

for j in range(0, len(decision\_for\_subset1)):

gini\_subset1 = gini\_subset1 - math.pow((decision\_for\_subset1[j] / subset1\_rows), 2)

for j in range(0, len(decision\_for\_subset2)):

gini\_subset2 = gini\_subset2 - math.pow((decision\_for\_subset2[j] / subset2\_rows), 2)

gini = (subset1\_rows / total\_instances) \ gini\_subset1 + (subset2\_rows / total\_instances) \ gini\_subset2

subset\_ginis.append(gini)

Take standard deviation as the judgment basis, calculate the decrease value of standard deviation at this time

elif algorithm == 'Regression':

superset\_stdev = cdf['Decision'].std(ddof=0)

subset1\_stdev = subset1['Decision'].std(ddof=0)

subset2\_stdev = subset2['Decision'].std(ddof=0)

threshold\_weighted\_stdev = (subset1\_rows / total\_instances) \* subset1\_stdev + (

subset2\_rows / total\_instances) \* subset2\_stdev

threshold\_reducted\_stdev = superset\_stdev - threshold\_weighted\_stdev

subset\_red\_stdevs.append(threshold\_reducted\_stdev)

#Find the index of the split value

Publicité

if algorithm == "Classification":

winner\_one = subset\_ginis.index(min(subset\_ginis))

elif algorithm == "Regression":

winner\_one = subset\_red\_stdevs.index(max(subset\_red\_stdevs))

Find the corresponding value according to the index

winner\_threshold = unique\_values[winner\_one]

Converts the original data column to an edited string column.

Characters smaller than the threshold are modified with the <= threshold value

cdf[column\_name] = np.where(cdf[column\_name] <= winner\_threshold, "<=" + str(winner\_threshold),">" + str(winner\_threshold))

return cdf

CalculateEntropy: Used to calculate Gini or variances, they are the criteria for classifying.

Code:

This function calculates the entropy of the column, and the input data must contain the Decision column

def calculateEntropy(df):

The regression tree entropy is 0

if algorithm == 'Regression':

return 0

rows = df.shape[0]

Use Value\_counts to get all values stored as dictionaries, keys: finds keys, and Tolist: change to lists.

This line of code finds the tag value.

decisions = df['Decision'].value\_counts().keys().tolist()

entropy = 0

Here the loop traverses all the tags

for i in range(0, len(decisions)):

Record the number of times the tag value appears

num\_of\_decisions = df['Decision'].value\_counts().tolist()[i]

probability of occurrence

class\_probability = num\_of\_decisions / rows

Calculate the entropy and sum it up

entropy = entropy - class\_probability \* math.log(class\_probability, 2)

return entropy

FindDecision: Find which feature in the current data to classify.

Code:

The main purpose of this function is to traverse the entire column of the table,

find which column is the best split column, and return the name of the column

def findDecision(ddf):

If it's a regression tree, then you take the standard deviation of the true value

if algorithm == 'Regression':

stdev = ddf['Decision'].std(ddof=0)

Get the entropy of the decision column

entropy = calculateEntropy(ddf)

columns = ddf.shape[1];

rows = ddf.shape[0]

Used to store Gini and standard deviation values

ginis = [];

reducted\_stdevs = []

Traverse all columns and calculate the relevant indexes of all columns according to algorithm selection

for i in range(0, columns - 1):

column\_name = ddf.columns[i]

column\_type = ddf[column\_name].dtypes

Determine if the column feature is a number, and if so, process the data using the following function,

which modifies the data to a string type category on return.

The idea is to directly use character characteristics, continuous digital characteristics into discrete character characteristics

if column\_type != 'object':

ddf = processContinuousFeatures(ddf, column\_name, entropy)

The statistical data in this column can be obtained, and the continuous data can be directly classified after processing,

and the categories are less than the threshold and greater than the threshold

classes = ddf[column\_name].value\_counts()

gini = 0;

weighted\_stdev = 0

Start the loop with the type of data in the column

for j in range(0, len(classes)):

current\_class = classes.keys().tolist()[j]

The final classification result corresponding to the data is selected

by deleting the value of the df column equal to the current data

subdataset = ddf[ddf[column\_name] == current\_class]

subset\_instances = subdataset.shape[0]

The entropy of information is calculated here

if algorithm == 'Classification': # GINI index

decision\_list = subdataset['Decision'].value\_counts().tolist()

subgini = 1

for k in range(0, len(decision\_list)):

subgini = subgini - math.pow((decision\_list[k] / subset\_instances), 2)

gini = gini + (subset\_instances / rows) \* subgini

The regression tree is judged by the standard deviation,

and the standard deviation of the subclasses in this column is calculated here

elif algorithm == 'Regression':

subset\_stdev = subdataset['Decision'].std(ddof=0)

weighted\_stdev = weighted\_stdev + (subset\_instances / rows) \* subset\_stdev

Used to store the final value of this column

if algorithm == "Classification":

ginis.append(gini)

Store the decrease in standard deviation for all columns

elif algorithm == 'Regression':

reducted\_stdev = stdev - weighted\_stdev

reducted\_stdevs.append(reducted\_stdev)

Determine which column is the first branch

by selecting the index of the largest value from the list of evaluation indicators

if algorithm == "Classification":

winner\_index = ginis.index(min(ginis))

elif algorithm == "Regression":

winner\_index = reducted\_stdevs.index(max(reducted\_stdevs))

winner\_name = ddf.columns[winner\_index]

return winner\_name

Publicité

FormatRule: Standardize the final output format.

Code:

ROOT is a number used to generate ' 'to adjust the display format of the decision making process

def formatRule(root):

resp = ''

for i in range(0, root):

resp = resp + ' '

return resp

BuildDecisionTree: Main function.

Code:

With this function, you build the decision tree model,

entering data in dataframe format, the root value, and the file address

If the value in the column is literal, it branches directly by literal category

def buildDecisionTree(df, root):

Identify the different charForResp

charForResp = "'"

if algorithm == 'Regression':

charForResp = ""

tmp\_root = root \* 1

df\_copy = df.copy()

Output the winning column of the decision tree, enter a list,

and output the column name of the decision column in the list

winner\_name = findDecision(df)

Determines whether the winning column is a number or a character

numericColumn = False

if dataset\_features[winner\_name] != 'object':

numericColumn = True

To ensure the integrity of the original data and prevent the data from changing,

mainly to ensure that the data of other columns besides the winning column does not change,

so as to continue the branch in the next step.

columns = df.shape[1]

for i in range(0, columns - 1):

column\_name = df.columns[i]

if df[column\_name].dtype != 'object' and column\_name != winner\_name:

df[column\_name] = df\_copy[column\_name]

Find the element in the branching column

classes = df[winner\_name].value\_counts().keys().tolist()

Traversing all classes in the branch column has two functions:

1. Display which class is currently traversed to; 2. Determine whether the current class is already leaf node

for i in range(0, len(classes)):

Find the Subdataset as in FindDecision, but discard this column of the current branch

current\_class = classes[i]

subdataset = df[df[winner\_name] == current\_class]

At the same time, the data of the first branch column is discarded and the remaining data is processed

subdataset = subdataset.drop(columns=[winner\_name])

Edit the display situation. If it is a numeric feature, the character conversion has been completed when searching for branches.

#If it is not a character feature, it is displayed with column names

if numericColumn == True:

compareTo = current\_class # current class might be <=x or >x in this case

else:

compareTo = " == '" + str(current\_class) + "'"

terminateBuilding = False

-----------------------------------------------

This determines whether it is already the last leaf node

if len(subdataset['Decision'].value\_counts().tolist()) == 1:

final\_decision = subdataset['Decision'].value\_counts().keys().tolist()[

0] # all items are equal in this case

terminateBuilding = True

At this time, only the Decision column is left, that is, all the segmentation features have been used

elif subdataset.shape[1] == 1:

get the most frequent one

final\_decision = subdataset['Decision'].value\_counts().idxmax()

terminateBuilding = True

The regression tree is judged as leaf node if the number of elements is less than 5

#elif algorithm == 'Regression' and subdataset.shape[0] < 5: # pruning condition

Another criterion is to take the standard deviation as the criterion and the sample mean in the node as the value of the node

elif algorithm == 'Regression' and subdataset['Decision'].std(ddof=0)/global\_stdev < 0.4:

get average

final\_decision = subdataset['Decision'].mean()

terminateBuilding = True

-----------------------------------------------

Here we begin to output the branching results of the decision tree.。

print(formatRule(root), "if ", winner\_name, compareTo, ":")

-----------------------------------------------

check decision is made

if terminateBuilding == True:

print(formatRule(root + 1), "return ", charForResp + str(final\_decision) + charForResp)

else: # decision is not made, continue to create branch and leafs

The size of the indent at display represented by root

root = root + 1

Call this function again for the next loop

buildDecisionTree(subdataset, root)

root = tmp\_root \* 1

Execute the code

Code:

call the function

buildDecisionTree(df, root)

Output:

![](data:image/png;base64...)

Regression tree result

![](data:image/png;base64...)

CART tree result