Ministry of Higher Education Higher Technology Institute 10th of Ramadan City 6th of October Branch Electronics and Communications Department Company: National Telecommunication Institute Artificial Intelligence Under Supervision: Dr/ Shahira Muhammad Student Name: Abdallah Galaa Student ID: 32021137 May / August 2023 ACKNOWLEDGMENT Many thanks for the instructors and the supervisors for overall efforts. Thanks for Engineers in NTI for their shared experience and overall efforts. Special thanks to Eng. Muhammad Al-Zaqani for his effort, the time he shared to pass on the experience and information. Very special thanks for Shahira Muhammad for guidance, useful instruction, advices, knowledge, and efforts to gain knowledge as well as experience I ABSTRACT At first, thanks to Allah the most merciful the most gracious, for this moment has come and this work has been accomplished. Thanks to the Higher Technological Institute of 10th Ramadan for preparing me to be A successful Engineer and lifting me up to achieve this training in an environment that's full of encouragement and motivation. My training that year was in National Telecommunication Institute The National Telecommunication Institute (NTI), being a reputed scientific, educational and research institution founded in 1983 with the mandate of becoming the center of excellence for telecommunication education, training, research and development and consultancy services, is devoting all of its resources to the build up the pillars of development and is well prepared and well equipped more than ever to play a major role in the fast paced development of the ICT industry. This report was written in order to write some Notes on the summer training for National Telecommunication Institute II Table of Contents ACKNOWLEDGEMENT...........................................................................I ABSTRACT.................................................................................II TABLE OF CONTENTS.....................................................................III LIST OF FIGURES.....................................................................IV LIST OF ABBREVIATION.....................................................................V CHAPTER ONE: AI Overview 1.1 AI in the Eyes of the Society……………………………1 1.2. What is AI?.............................................................................1 1.3 Why Python is best for Artificial Intelligence? .......................2 1.4 Relation of AI, Machine Learning and Deep Learning..........3 1.5 Types of AI: .............................................................................4 CHAPTER TWO: PYTHON 2.1. 2.1 What is Python …………………………………………………………5 2.2 Python Literals……………………………………………….6 2.3 Arithmetic operators and the hierarchy of priorities ………………………7 2.4 Variables……………………………………….7 2.5 Comments …………………………………………….8 2.6 The input() function and strings operators …………………………………..8 2.7 Comparison operator and conditional execution……………………..….9 2.8 loops ………………………………………..11 2.9 logical operators ………………………………………………………..13 III 3.1.1. Machine Learning………………………………………….12 3.1.2. Deep Learning……………………………………………...13 2.10 lists...........................................................................13 2.11functions……………......................................................................15 CHAPTER three: Machine Learning......................................................20 3.1 Machine Learning Algorithms.............................................................20 3.2 Machine Learning Types…………….................................................22 3.3 Basic Machine Learning Concept — Dataset………………………23 3.4 Machine Learning Algorithm Overview...................................25 CHAPTER Four : Deep learning 4.1 Traditional Machine Learning and Deep Learning..................30 4.2Deep learning……………………………………………...........31 4.3 loss Function........................................................................32 4.4 Activation Function...............................................................32 4.5 loss Function........................................................................32 4.6 optimizer……………………………………………………………………………34 4.7 convolutional Nerural Network............................................34 CHAPTER Five: AI project 5.1 Dataset…………………………………………………………38 5.2 Importing the required libraries ……………………………………………….38 5.3 Importing data into TensorFlow dataset object ………………………39 5.4 Creating the CNN model ……………………………………….39 5.5 Compiling the model………………………………………………………40 4 5.6 Testing the prediction on sample of image from test dataset…………41 LIST OF FIGURES Fig. 1.1 dentification of concepts related to AI and machine learning AI Development Report 2020……………………1 Fig. 1.2 AI relations……………………………………………………………………………………………………….……………………………………..1 Fig. 1.3 Relation of AI, Machine Learning and Deep Learning………………………………………………………………………………3 Fig 3.1 Traditional Rule-Based Algorithms ............................................................................................................... 20 Fig 3.2 Machine Learning Algorithms ...................................................................................................................... 21 Fig 3.3 Application Scenarios of Machine Learning ................................................................................................. 21 Fig 3.4 Supervised learning ...................................................................................................................................... 22 Fig 3.6 Semi-supervised learning ............................................................................................................................. 23 Fig 3.7 Machine Learning Process: ........................................................................................................................... 23 Fig 3.8 Data Processing ............................................................................................................................................ 24 Fig 3.9 CrowdFlower Data Science Report 2016 ...................................................................................................... 24 Fig 3.10 Model Validity ............................................................................................................................................ 25 Fig. 3.11 Machine Learning Algorithms ................................................................................................................... 25 Fig. 3.12 Logistic Regression Graph .......................................................................................................................... 26 Fig. 3.13 Decision Tree Structure ............................................................................................................................. 26 Fig. 3.14 SVM Algorithm .......................................................................................................................................... 27 Fig 3.15 KNN Algorithm ............................................................................................................................................ 27 Fig. 3.16 Naive Bayes Graph ..................................................................................................................................... 27 Fig. 3.18 Multi-dimensional linear regression.......................................................................................................... 28 Fig. 3.17 Unary linear regression ............................................................................................................................. 28 Fig. 3.19 Random Forest Algorithm ......................................................................................................................... 28 Fig. 3.20 K-means Algorithm .................................................................................................................................... 29 Fig. 4.1 neural network ............................................................................................................................................ 31 Fig. 4.2 Feedforward Neural Network ...................................................................................................................... 31 Fig. 4.3 Sigmoid function.......................................................................................................................................... 32 Fig. 4.4 ReLU function .............................................................................................................................................. 33 Fig. 4.5 Architecture of Convolutional Neural Network ........................................................................................... 35 Fig. 4.6 Description of convolution calculation ........................................................................................................ 35 Fig. 4.7 Demonstration of convolution calculation .................................................................................................. 35 Fig. 4.8 Convolutional Layer ..................................................................................................................................... 36 Fig. 4.9 Pooling Layer ............................................................................................................................................... 36 Fig. 4.10 Fully connected layer ................................................................................................................................. 37 Fig. 5.1 connect drive ............................................................................................................................................... 38 Fig. 5.2 the required libraries ................................................................................................................................... 38 IV Fig. 5.3 Importing data into TensorFlow dataset object ......................................................................................... 39 Fig. 5.4 the CNN model ............................................................................................................................................ 39 Fig. 5.5 the model summary .................................................................................................................................... 40 Fig. 5.6 Plotting the accuracy and loss curves .......................................................................................................... 41 Fig. 5.8 Testing the prediction on sample of image from test dataset ..................................................................... 42 LIST OF ABBREVIATIONS AI ……………………………….Artificial Intelligence NumPy …………………………….. Numerical Python Pandas ………“Python Data Analysis” or “Panel Data” ML………………………………….Machine Learning DL…………………………………….. Deep Learning CNN………………… Convolutional Neural Network Int ……………………………………………....Integers str ………………………………………………..Strings NTI……………..National Telecommunication Institute V Chapter One AI Overview 1.1 AI in the Eyes of the Society • People get to know AI through news, movies, and actual applications in daily life. What is AI in the eyes of the public? 1.2 What Is AI? • Artificial Intelligence (AI) is a new technical science that studies and develops theories, methods, techniques, and application systems for simulating and extending human intelligence. In 1956, the concept of AI was first proposed by John McCarthy, who defined the subject as "science and engineering of making intelligent machines, especially intelligent computer programs". AI is concerned with making machines work in an intelligent way, similar to the way that the human mind works. At present, AI has become an interdisciplinary course that involves various fields. Fig. 1.1 dentification of concepts related to AI and Fig. 1.2 AI relations machine learning AI Development Report 2020 1 1.3 Why Python is best for Artificial Intelligence? Python is a popular programming language among data scientists and machine learning engineers because of its simplicity, flexibility, and ease of use. Here are some reasons why Python is considered the best programming language for artificial intelligence: 1. Large and Active Community: Python has a large and active community of developers who have created many powerful libraries and frameworks for data science and machine learning. Libraries such as NumPy, Pandas, Matplotlib, and Scikit-learn have been widely used in AI development. 2. Easy to Learn: Python is easy to learn, and its syntax is concise and readable. This makes it easy for beginners to get started with AI development. 3. Versatile and Flexible: Python is a versatile and flexible language that can be used for a variety of tasks, including data manipulation, data analysis, and machine learning. 4. Extensive Libraries: Python has a vast collection of libraries and frameworks that make it easy to perform complex machine learning tasks with just a few lines of code. This saves time and effort and makes the development process much more efficient. 5. Integration with Other Languages: Python can be easily integrated with other programming languages such as C++, Java, and R. This makes it a great choice for developing AI applications that require the use of multiple languages. Overall, Python's simplicity, flexibility, and extensive libraries make it an ideal language for AI development. 2 1.4 Relationship of AI, Machine Learning, and Deep Learning: Fig. 1.3 Relation of AI, Machine Learning and Deep Learning • AI: A new technical science that focuses on the research and development of theories, methods, techniques, and application systems for simulating and extending human intelligence. • Machine learning: A core research field of AI. It focuses on the study of how computers can obtain new knowledge or skills by simulating or performing learning behavior of human beings and reorganize existing knowledge architecture to improve its performance. It is one of the core research fields of AI. • Deep learning: A new field of machine learning. The concept of deep learning originates from the research on artificial neural networks. The multi-layer perceptron (MLP) is a type a deep learning architecture. Deep learning aims to simulate the human brain to interpret data such as images, sounds, and texts. 3 1.5 Types of AI: • Strong AI The strong AI view holds that it is possible to create intelligent machines that can really reason and solve problems. Such machines are conscious and self-aware, can independently think about problems and work out optimal solutions to problems, have their own system of values and world views, and have all the same instincts as living things, such as survival and security needs. It can be regarded as a new civilization in a certain sense. • Weak AI The weak AI view holds that intelligent machines cannot really reason and solve problems. These machines only look intelligent, but do not have real intelligence or self-awareness. 4 Chapter two Python 2.1 What is Python? Python is a popular programming language. It was created by Guido van Rossum and released in 1991. It is used for: • • • • web development (server-side), software development, mathematics, system scripting. What can Python do? • • • • • Python can be used Python can be used Python can connect Python can be used Python can be used development. on a server to create web applications. alongside software to create workflows. to database systems. It can also read and modify files. to handle big data and perform complex mathematics. for rapid prototyping, or for production-ready software Why Python? • • • • • Python works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc). Python has a simple syntax like the English language. Python has syntax that allows developers to write programs with fewer lines than some other programming languages. Python runs on an interpreter system, meaning that code can be executed as soon as it is written. This means that prototyping can be very quick. Python can be treated in a procedural way, an object-oriented way or a functional way. Good to know • • The most recent major version of Python is Python 3, which we shall be using in this tutorial. However, Python 2, although not being updated with anything other than security updates, is still quite popular. In this tutorial Python will be written in a text editor. It is possible to write Python in an Integrated Development Environment, such as Thonny, Pycharm, Netbeans or Eclipse which are particularly useful when managing larger collections of Python files. 5 Python Syntax compared to other programming languages. • • • Python was designed for readability and has some similarities to the English language with influence from mathematics. Python uses new lines to complete a command, as opposed to other programming languages which often use semicolons or parentheses. Python relies on indentation, using whitespace, to define scope, such as the scope of loops, functions and classes. Other programming languages often use curly brackets for this purpose 2.2 Python Literals 1. Literals are notations for representing some fixed values in code. Python has various types of literals - for example, a literal can be a number (numeric literals, e.g., 123), or a string (string literals, e.g., "I am a literal."). 2. The binary system is a system of numbers that employs 2 as the base. Therefore, a binary number is made up of 0s and 1s only, e.g., 1010 is 10 in decimal. Octal and hexadecimal numeration systems, similarly, employ 8 and 16 as their bases respectively. The hexadecimal system uses the decimal numbers and six extra letters. 3. Integers (or simply ints) are one of the numerical types supported by Python. They are numbers written without a fractional component, e.g., 256, or -1 (negative integers) 4. Floating-point numbers (or simply floats) are another one of the numerical types supported by Python. They are numbers that contain (or are able to contain) a fractional component, e.g., 1.27. 5. To encode an apostrophe or a quote inside a string you can either use the escape character, e.g., 'I\'m happy.', or open and close the string using an opposite set of symbols to the ones you wish to encode, e.g., "I'm happy." to encode an apostrophe, and 'He said "Python", not "typhoon"' to encode a (double) quote. 6. Boolean values are the two constant objects True and False used to represent truth values (in numeric contexts 1 is True, while 0 is False. EXTRA There is one more, special literal that is used in Python: the None literal. This literal is a socalled NoneType object, and it is used to represent the absence of a value. We'll tell you more about it soon. 6 2.3 Arithmetic operators and the hierarchy of priorities 1. An expression is a combination of values (or variables, operators, calls to functions ‒ you will learn about them soon) which evaluates to a certain value, e.g., 1 + 2 . 2. Operators are special symbols or keywords which are able to operate on the values and perform (mathematical) operations, e.g., the * operator multiplies two values: x * y . 3. Arithmetic operators in Python: + (addition), - (subtraction), * (multiplication), / (classic division ‒ always returns a float), % (modulus ‒ divides left operand by right operand and returns the remainder of the operation, e.g., 5 % 2 = 1 ), ** (exponentiation ‒ left operand raised to the power of right operand, e.g., 2 ** 3 = 2 * 2 * 2 = 8 ), // (floor/integer division ‒ returns a number resulting from division, but rounded down to the nearest whole number, e.g., 3 // 2.0 = 1.0 ) 4. A unary operator is an operator with only one operand, e.g., -1 , or +3 . 5. A binary operator is an operator with two operands, e.g., 4 + 5 , or 12 % 5 . 6. Some operators act before others – the hierarchy of priorities: • • • • the ** operator (exponentiation) has the highest priority; then the unary + and - (note: a unary operator to the right of the exponentiation operator binds more strongly, for example: 4 ** -1 equals 0.25 ) then * , / , // , and % ; and, finally, the lowest priority: the binary + and - . 7. Subexpressions in parentheses are always calculated first, e.g., 15 - 1 * (5 * (1 + 2)) = 0 . 8. The exponentiation operator uses right-sided binding, e.g., 2 ** 2 ** 3 = 256 . 2.4 Variables 1. A variable is a named location reserved to store values in the memory. A variable is created or initialized automatically when you assign a value to it for the first time. 2. Each variable must have a unique name - an identifier. A legal identifier name must be a non-empty sequence of characters, must begin with the underscore( _ ), or a letter, and it cannot be a Python keyword. The first character may be followed by underscores, letters, and digits. Identifiers in Python are case-sensitive. 7 3. Python is a dynamically-typed language, which means you don't need to declare variables in it. (2.1.4.3) To assign values to variables, you can use a simple assignment operator in the form of the equal ( = ) sign, i.e., var = 1 . 4. You can also use compound assignment operators (shortcut operators) to modify values assigned to variables, e.g., var += 1 , or var /= 5 * 2 . 5. You can assign new values to already existing variables using the assignment operator or one of the compound operators 2.5 Comments 1. Comments can be used to leave additional information in code. They are omitted at runtime. The information left in source code is addressed to human readers. In Python, a comment is a piece of text that begins with # . The comment extends to the end of line. 2. If you want to place a comment that spans several lines, you need to place # in front of them all. Moreover, you can use a comment to mark a piece of code that is not needed at the moment (see the last line of the snippet below), e.g.: # This program prints # an introduction to the screen. print("Hello!") # Invoking the print() function # print("I'm Python.") 3. It's important to use comments to make programs easier to understand, and to use readable and meaningful variable names in code. However, it's equally important not to use variable names that are confusing, or leave comments that contain wrong or incorrect information! 2.6 The input() function and strings operators 1. The print() function sends data to the console, while the input() function gets data from the console. 2. The input() function comes with an optional parameter: the prompt string. It allows you to write a message before the user input, e.g.: name = input("Enter your name: ") print("Hello, " + name + ". Nice to meet you!") 3. When the input() function is called, the program's flow is stopped, the prompt symbol keeps blinking (it prompts the user to take action when the console is switched to input mode) until the user has entered an input and/or pressed the Enter key. 8 NOTE You can test the functionality of the input() function in its full scope locally on your machine. For resource optimization reasons, we have limited the maximum program execution time in Edube to a few seconds. Go to the Sandbox, copy-paste the above snippet, run the program, and do nothing ‒ just wait a few seconds to see what happens. Your program should be stopped automatically after a short moment. Now open IDLE, and run the same program there ‒ can you see the difference? Tip: the above-mentioned feature of the input() function can be used to prompt the user to end a program. Look at the code below: name = input ("Enter your name: ") print ("Hello, " + name + ". Nice to meet you!") print ("\nPress Enter to end the program.") input () print("THE END.") 4. The result of the input () function is a string. You can add strings to each other using the concatenation (+) operator. Check out this code: num_1 = input ("Enter the first number: ") # Enter 12 num_2 = input ("Enter the second number: ") # Enter 21 print (num_1 + num_2) # the program returns 1221 5. You can also multiply (* ‒ replication) strings, e.g.: my_input = input ("Enter something: ") # Example input: hello print (my_input * 3) # Expected output: hellohellohello 2.7 Comparison operator and conditional execution 1.The comparison (otherwise known as relational) operators are used to compare values. The table below illustrates how the comparison operators work, assuming that x = 0, y = 1, and z = 0: Operator Description == returns True if operands' values are equal, and False otherwise != returns True if operands' values are not equal, and False otherwise x != y x != z True if the left operand's value is greater than the right x > y y > z # False # True x < y y < z # True # False > Example operand's value, and False otherwise < True if the left operand's value is less than the right operand's value, and False otherwise ≥ True if the left operand's value is greater than or equal to the right operand's value, and False otherwise 9 x >= y x >= z y >= z # True # False # False # True # True True if the left operand's value is less than or equal to the right ≤ operand's value, and False otherwise x <= y x <= z y <= z # True # True # False 2. When you want to execute some code only if a certain condition is met, you can use a conditional statement: a single if statement, e.g.: x = 10 • if x == 10: # condition print("x is equal to 10") • # Executed if the condition is True. a series of if statements, e.g.: x = 10 if x > 5: # condition one print("x is greater than 5") if x < 10: # condition two print("x is less than 10") if x == 10: # condition three print("x is equal to 10") # Executed if condition one is True. # Executed if condition two is True. # Executed if condition three is True. Each if statement is tested separately. an if-else statement, e.g.: x = 10 if x < 10: # Condition print("x is less than 10") # Executed if the condition is True. else: print("x is greater than or equal to 10") False. #output : x is greater than or equal to 10 • a series of if statements followed by an else , e.g.: x = 10 if x > 5: # True print("x > 5") if x > 8: # True print("x > 8") if x > 10: # False print("x > 10") else: print("else will be executed") #output : 10 # Executed if the condition is x > 5 x > 8 else will be executed Each if is tested separately. The body of else is executed if the last if is False . The if-elif-else statement, e.g.: x = 10 if x == 10: # True print("x == 10") if x > 15: # False print("x > 15") elif x > 10: # False print("x > 10") elif x > 5: # True print("x > 5") else: print("else will not be executed") #out put : x == 10 x > 5 If the condition for if is False , the program checks the conditions of the subsequent elif blocks – the first elif block that is True is executed. If all the conditions are False , the else block will be executed. Nested conditional statements, e.g.: x = 10 • if x > 5: # True if x == 6: # False print("nested: x == 6") elif x == 10: # True print("nested: x == 10") else: print("nested: else") else: print("else") #output : nested: x == 10 2.8 loops 1. There are two types of loops in Python: while and for : the while loop executes a statement or a set of statements as long as a specified boolean condition is true, e.g.: # Example 1 while True: • 11 print("Stuck in an infinite loop.") # infinite loop # Example 2 counter = 5 while counter > 2: print(counter) counter -= 1 #output 5 4 3 the for loop executes a set of statements many times; it's used to iterate over a sequence (e.g., a list, a dictionary, a tuple, or a set - you will learn about them soon) or other objects that are iterable (e.g., strings). You can use the for loop to iterate over a sequence of numbers using the built-in range function. Look at the examples below: # Example 1 word = "Python" for letter in word: print(letter, end="*") #output : P*y*t*h*o*n* • # Example 2 for i in range(1, 10): if i % 2 == 0: print(i) #output : 2 4 6 8 2. You can use the break and continue statements to change the flow of a loop: • You use break to exit a loop, e.g.: text = "OpenEDG Python Institute" for letter in text: if letter == "P": break print(letter, end="") #output : OpenEDG • You use continue to skip the current iteration, and continue with the next iteration, e.g.: text = "pyxpyxpyx" for letter in text: if letter == "x": continue print(letter, end="") #output: pypypy 3. The range() function generates a sequence of numbers. It accepts integers and returns range objects. The syntax of range() looks as follows: range(start, stop, step) , where: 12 • start is an optional parameter specifying the starting number of the sequence (0 by default) • • stop is an optional parameter specifying the end of the sequence generated (it is not included), and step is an optional parameter specifying the difference between the numbers in the sequence (1 by default.) Example code: for i in range(3): print(i, end=" ") # Outputs: 0 1 2 for i in range(6, 1, -2): print(i, end=" ") # Outputs: 6, 4, 2 2.9 logical operators Python supports the following logical operators: • • and → if both operands are true, the condition is true, e.g., (True and True) is True , or → if any of the operands are true, the condition is true, e.g., (True or False) is True , • not → returns false if the result is true, and returns true if the result is false, e.g., not True is False . x = 1 y = 0 z = ((x == y) and (x == y)) or not(x == y) print(not(z)) #output : False 2.10 lists 1. The list is a type of data in Python used to store multiple objects. It is an ordered and mutable collection of comma-separated items between square brackets, e.g.: my_list = [1, None, True, "I am a string", 256, 0] 2. Lists can be indexed and updated, e.g.: my_list = [1, None, True, 'I am a string', 256, 0] print(my_list[3]) # outputs: I am a string print(my_list[-1]) # outputs: 0 my_list[1] = '?' print(my_list) # outputs: [1, '?', True, 'I am a string', 256, 0] my_list.insert(0, "first") my_list.append("last") print(my_list) # outputs: ['first', 1, '?', True, 'I am a string', 256, 0, 'last'] 3. Lists can be nested, e.g.: my_list = [1, 'a', ["list", 64, [0, 1], False]] 4. List elements and lists can be deleted, e.g.: 13 my_list = [1, 2, 3, 4] del my_list[2] print(my_list) # outputs: [1, 2, 4] del my_list # deletes the whole list 5.Lists can be iterated through using the for loop, e.g.: my_list = ["white", "purple", "blue", "yellow", "green"] for color in my_list: print(color) 6.Lists can be iterated through using the for loop, e.g.: my_list = ["white", "purple", "blue", "yellow", "green"] print(len(my_list)) # outputs 5 del my_list[2] print(len(my_list)) # outputs 4 lists simple Sorting: 1. You can use the sort() method to sort elements of a list, e.g.: lst = [5, 3, 1, 2, 4] print(lst) lst.sort() print(lst) # outputs: [1, 2, 3, 4, 5] 2. There is also a list method called reverse(), which you can use to reverse the list, e.g.: lst = [5, 3, 1, 2, 4] print(lst) lst.reverse() print(lst) # outputs: [4, 2, 1, 3, 5] lists processing: 1. If you have a list l1, then the following assignment: l2 = l1 does not make a copy of the l1 list, but makes the variables l1 and l2 point to one and the same list in memory. For example: vehicles_one = ['car', 'bicycle', 'motor'] print(vehicles_one) # outputs: ['car', 'bicycle', 'motor'] vehicles_two = vehicles_one del vehicles_one[0] # deletes 'car' print(vehicles_two) # outputs: ['bicycle', 'motor'] 2. If you want to copy a list or part of the list, you can do it by performing slicing: colors = ['red', 'green', 'orange'] copy_whole_colors = colors[:] # copy the entire list 14 copy_part_colors = colors[0:2] # copy part of the list 3. You can use negative indices to perform slices, too. For example: sample_list = ["A", "B", "C", "D", "E"] new_list = sample_list[2:-1] print(new_list) # outputs: ['C', 'D'] 4. The start and end parameters are optional when performing a slice: list[start:end], e.g.: my_list = [1, 2, 3, 4, 5] slice_one = my_list[2: ] slice_two = my_list[ :2] slice_three = my_list[-2: ] print(slice_one) # outputs: [3, 4, 5] print(slice_two) # outputs: [1, 2] print(slice_three) # outputs: [4, 5] 5.You can delete slices using the del instruction: my_list = [1, 2, 3, 4, 5] del my_list[0:2] print(my_list) # outputs: [3, 4, 5] del my_list[:] print(my_list) # deletes the list content, outputs: [] 6.You can test if some items exist in a list or not using the keywords in and not in, e.g.: my_list = ["A", "B", 1, 2] print("A" in my_list) # outputs: True print("C" not in my_list) # outputs: True print(2 not in my_list) # outputs: False 2.11functions: 1. A function is a block of code that performs a specific task when the function is called (invoked). You can use functions to make your code reusable, better organized, and more readable. Functions can have parameters and return values. 2. You can define your own function using the def keyword and the following syntax: def your_function(optional parameters): # the body of the function You can define a function which doesn't take any arguments, e.g.: def message (): # defining a function print("Hello") # body of the function message () # calling the function 15 You can define a function which takes arguments, too, just like the one-parameter function below: def hello(name): # defining a function. print ("Hello,", name) # body of the function name = input ("Enter your name: ") hello(name) # calling the function Function parameters and argument passing: 1. You can pass information to functions by using parameters. Your functions can have as many parameters as you need. An example of a one-parameter function: def hi(name): print("Hi,", name) hi("Greg") An example of a two-parameter function: def hi_all(name_1, name_2): print("Hi,", name_2) print("Hi,", name_1) hi_all("Sebastian", "Konrad") An example of a three-parameter function: def address(street, city, postal_code): print("Your address is:", street, "St.,", city, postal_code) s = input("Street: ") p_c = input("Postal Code: ") c = input("City: ") address(s, c, p_c) 2. You can pass arguments to a function using the following techniques: • • • positional argument passing in which the order of arguments passed matters (Ex. 1), keyword (named) argument passing in which the order of arguments passed doesn't matter (Ex. 2), a mix of positional and keyword argument passing (Ex. 3) Ex. 1 16 def subtra(a, b): print(a - b) subtra(5, 2) subtra(2, 5) # outputs: 3 # outputs: -3 Ex. 2 def subtra(a, b): print(a - b) subtra(a=5, b=2) subtra(b=2, a=5) # outputs: 3 # outputs: 3 Ex. 3 def subtra(a, b): print(a - b) subtra(5, b=2) subtra(5, 2) # outputs: 3 # outputs: 3 It's important to remember that positional arguments mustn't follow keyword arguments. That's why if you try to run the following snippet: def subtra(a, b): print(a - b) subtra(5, b=2) # outputs: 3 subtra(a=5, 2) # Syntax Error Python will not let you do it by signalling a SyntaxError. 3. You can use the keyword argument passing technique to pre-define a value for a given argument: def name(first_name, last_name="Smith"): print(first_name, last_name) name("Andy") # outputs: Andy Smith name("Betty", "Johnson") # outputs: Betty Johnson (the keyword argument replaced by "Johnson") Returning results from functions: 1. You can use the return keyword to tell a function to return some value. The return statement exits the function, e.g.: def multiply(a, b): return a * b print(multiply(3, 4)) # outputs: 12 def multiply(a, b): return print(multiply(3, 4)) # outputs: None 2. The result of a function can be easily assigned to a variable, e.g.: 17 def wishes (): return "Happy Birthday!" w = wishes () print(w) # outputs: Happy Birthday! Look at the difference in output in the following two examples: # Example 1 def wishes(): print("My Wishes") return "Happy Birthday" wishes() # outputs: My Wishes # Example 2 def wishes(): print("My Wishes") return "Happy Birthday" print(wishes()) # outputs: My Wishes # Happy Birthday 3. You can use a list as a function's argument, e.g.: def hi_everybody(my_list): for name in my_list: print("Hi,", name) hi_everybody(["Adam", "John", "Lucy"]) 4. A list can be a function result, too, e.g.: def create_list(n): my_list = [] for i in range(n): my_list.append(i) return my_list print(create_list(5)) #output : [0, 1, 2, 3, 4] Functions and scopes: 1. A variable that exists outside a function has a scope inside the function body (Example 1) unless the function defines a variable of the same name (Example 2, and Example 3), e.g.: Example 1: var = 2 def mult_by_var(x): return x * var print(mult_by_var(7)) # outputs: 14 18 Example 2: def mult(x): var = 5 return x * var print(mult(7)) # outputs: 35 Example 3: def mult(x): var = 7 return x * var var = 3 print(mult(7)) # outputs: 49 2. A variable that exists inside a function has a scope inside the function body (Example 4), e.g.: Example 4: def adding(x): var = 7 return x + var print(adding(4)) # outputs: 11 print(var) # NameError 3. You can use the global keyword followed by a variable name to make the variable's scope global, e.g.: var = 2 print(var) # outputs: 2 def return_var(): global var var = 5 return var print(return_var()) # outputs: 5 print(var) # outputs: 5 19 Chapter three Machine Learning 3.1 Machine Learning Algorithms: • Machine learning (including deep learning) is a study of learning algorithms. A computer program is said to learn from experience 𝐸 with respect to some class of tasks 𝑇 and performance measure 𝑃 if its performance at tasks in 𝑇, as measured by 𝑃, improves with experience 𝐸. Differences Between Machine Learning Algorithms and Traditional RuleBased Algorithms: Fig 3.1 Traditional Rule-Based Algorithms • Explicit programming is used to solve problems. • Rules can be manually specified. 20 Fig 3.2 Machine Learning Algorithms • Samples are used for training. • The decision-making rules are complex or difficult to describe. • Rules are automatically learned by machines. Application Scenarios of Machine Learning • The solution to a problem is complex, or the problem may involve a large amount of data without a clear data distribution function. • Machine learning can be used in the following scenarios: Fig 3.3 Application Scenarios of Machine Learning 21 3.2 Machine Learning Types: Machine Learning Classification: • Supervised learning: Obtain an optimal model with required performance through training and learning based on the samples of known categories. Then, use the model to map all inputs to outputs and check the output for the purpose of classifying unknown data. Fig 3.4 Supervised learning • Unsupervised learning: For unlabeled samples, the learning algorithms directly model the input datasets. Clustering is a common form of unsupervised learning. We only need to put highly similar samples together, calculate the similarity between new samples and existing ones, and classify them by similarity. Fig 3.5 unsupervised learning 22 • Semi-supervised learning: In one task, a machine learning model that automatically uses a large amount of unlabeled data to assist learning directly of a small amount of labeled data. Fig 3.6 Semi-supervised learning • Reinforcement learning: It is an area of machine learning concerned with how agents ought to take actions in an environment to maximize some notion of cumulative reward. The difference between reinforcement learning and supervised learning is the teacher signal. The reinforcement signal provided by the environment in reinforcement learning is used to evaluate the action (scalar signal) rather than telling the learning system how to perform correct actions. Machine Learning Process: Fig 3.7 Machine Learning Process: 3.3 Basic Machine Learning Concept — Dataset: • Dataset: a collection of data used in machine learning tasks. Each data record is called a sample. Events or attributes that reflect the performance or nature of a sample in a particular aspect are called features. • Training set: a dataset used in the training process, where each sample is referred to as a training sample. The process of creating a model from data is called learning (training). • Test set: Testing refers to the process of using the model obtained after learning for prediction. The dataset used is called a test set, and each sample is called a test ample. 23 Importance of Data Processing: o Data is crucial to models. It is the ceiling of model capabilities. Without good data, there is no good model. Fig 3.8 Data Processing Workload of Data Cleansing o Statistics on data scientists' work in machine learning Fig 3.9 CrowdFlower Data Science Report 2016 Data Cleansing • • ▪ ▪ ▪ ▪ ▪ Most machine learning models process features, which are usually numeric representations of input variables that can be used in the model. In most cases, the collected data can be used by algorithms only after being preprocessed. The preprocessing operations include the following: Data filtering Processing of lost data Processing of possible exceptions, errors, or abnormal values Combination of data from multiple data sources Data consolidation Model Validity (1) 24 • Generalization capability: The goal of machine learning is that the model obtained after learning should perform well on new samples, not just on samples used for training. The capability of applying a model to new samples is called generalization or robustness. • Error: difference between the sample result predicted by the model obtained after learning and the actual sample result. ▪ Training error: error that you get when you run the model on the training data. ▪ Generalization error: error that you get when you run the model on new samples. Obviously, we prefer a model with a smaller generalization error. • Underfitting: occurs when the model or the algorithm does not fit the data well enough. • Overfitting: occurs when the training error of the model obtained after learning is small but the generalization error is large (poor generalization capability). Fig 3.10 Model Validity 3.4 Machine Learning Algorithm Overview: Fig. 3.11 Machine Learning Algorithms Logistic regression: is the appropriate regression analysis to conduct when the dependent variable is dichotomous (binary). Like all regression analyses, logistic regression is a predictive analysis. Logistic 25 regression is used to describe data and to explain the relationship between one dependent binary variable and one or more nominal, ordinal, interval or ratio-level independent variables. Fig. 3.12 Logistic Regression Graph Decision Tree: • A decision tree is a tree structure (a binary tree or a non-binary tree). Each non-leaf node represents a test on a feature attribute. Each branch represents the output of a feature attribute in a certain value range, and each leaf node stores a category. To use the decision tree, start from the root node, test the feature attributes of the items to be classified, select the output branches, and use the category stored on the leaf node as the final result Fig. 3.13 Decision Tree Structure SVM: • Support Vector Machine (SVM) is a powerful machine learning algorithm used for linear or nonlinear classification, regression, and even outlier detection tasks. SVMs can be used for a variety of tasks, such as text classification, image classification, spam detection, handwriting identification, gene expression analysis, face detection, and anomaly detection. SVMs are adaptable and efficient in a variety of applications because they can manage high-dimensional data and nonlinear relationships. • SVM algorithms are very effective as we try to find the maximum separating hyperplane between the different classes available in the target feature. 26 Fig. 3.14 SVM Algorithm KNN Algorithm: • The KNN classification algorithm is a theoretically mature method and one of the simplest machine learning algorithms. According to this method, if the majority of k samples most similar to one sample (nearest neighbors in the eigenspace) belong to a specific category, this sample also belongs to this category. Fig 3.15 KNN Algorithm Naive Bayes: • Naive Bayes is a popular machine learning algorithm used for classification tasks. It is based on Bayes' Theorem and the assumption of independence among predictors. Simply put, the algorithm predicts the likelihood of an input belonging to a certain class based on the presence or absence of certain features. Naive Bayes can be applied to various applications such as email spam classification and medical diagnosis. This algorithm is simple to implement, computationally efficient, and requires a small amount of training data. Fig. 3.16 Naive Bayes Graph 27 • Linear regression: a statistical analysis method to determine the quantitative relationships between two or more variables through regression analysis in mathematical statistics. • Linear regression is a type of supervised learning. Fig. 3.18 Multi-dimensional linear regression Fig. 3.17 Unary linear regression Random Forest: Random Forest is a supervised learning algorithm. The “forest” it builds is an ensemble of decision trees, usually trained with the bagging method. The general idea of the bagging method is that a combination of learning models increases the overall result. Fig. 3.19 Random Forest Algorithm Unsupervised Learning - K-means: • K-means clustering aims to partition n observations into k clusters in which each observation belongs to the cluster with the nearest mean, serving as a prototype of the cluster. • For the k-means algorithm, specify the final number of clusters (k). Then, divide n data objects into k clusters. The clusters obtained meet the following conditions: 1. Objects in the same cluster are highly similar. 28 2. The similarity of objects in different clusters is small. Fig. 3.20 K-means Algorithm 29 Chapter Four Deep Learning 4.1 Traditional Machine Learning and Deep Learning: • As a model based on unsupervised feature learning and feature hierarchy learning, deep learning has great advantages in fields such as computer vision, speech recognition, and natural language processing. Traditional Machine Learning: 30 4.2 Deep Learning: • generally, the deep learning architecture is a deep neural network. "Deep" in "deep learning" refers to the number of layers of the neural network. Fig. 4.1 neural network Neural Network: • Currently, the definition of the neural network has not been determined yet. Hecht Nielsen, a neural network researcher in the U.S., defines a neural network as a computer system composed of simple and highly interconnected processing elements, which process information by dynamic response to external inputs. • A neural network can be simply expressed as an information processing system designed to imitate the human brain structure and functions based on its source, features, and explanations. • Artificial neural network (neural network): Formed by artificial neurons connected to each other, the neural network extracts and simplifies the human brain's microstructure and functions. It is an important approach to simulate human intelligence and reflect several basic features of human brain functions, such as concurrent information processing, learning, association, model classification, and memory. Fig. 4.2 Feedforward Neural Network 31 4.3 loss function: • At its core, a loss function is incredibly simple: It’s a method of evaluating how well your algorithm models your dataset. If your predictions are totally off, your loss function will output a higher number. If they’re pretty good, it’ll output a lower number. As you change pieces of your algorithm to try and improve your model, your loss function will tell you if you’re getting anywhere. • Loss functions are related to model accuracy, a key component of AI/ML governance. • We can design our own (very) basic loss function to further explain how it works. For each prediction that we make, our loss function will simply measure the absolute difference between our prediction and the actual value. In mathematical notation. 4.4 Activation Function: • Activation functions are important for the neural network model to learn and understand complex non-linear functions. They allow introduction of non-linear features to the network. • Without activation functions, output signals are only simple linear functions. • The complexity of linear functions is limited, and the capability of learning complex function mappings from data is low. Types of Activation Function: • Sigmoid ▪ Logistic Activation Function ▪ This function takes any real value as input and outputs values in the range of 0 to 1. ▪ The larger the input (more positive), the closer the output value will be to 1.0, whereas the smaller the input (more negative), the closer the output will be to 0.0, as shown below. Fig. 4.3 Sigmoid function 32 • Rectified Linear Unit (ReLU): ▪ ReLU stands for Rectified Linear Unit. ▪ Although it gives an impression of a linear function, ReLU has a derivative function and allows for backpropagation while simultaneously making it computationally efficient. ▪ The main catch here is that the ReLU function does not activate all the neurons at the same time. ▪ The neurons will only be deactivated if the output of the linear transformation is less than 0 Fig. 4.4 ReLU function • Softmax: ▪ The softmax function is a function that turns a vector of K real values into a vector of K real values that sum to 1. The input values can be positive, negative, zero, or greater than one, but the softmax transforms them into values between 0 and 1, so that they can be interpreted as probabilities. If one of the inputs is small or negative, the softmax turns it into a small probability, and if an input is large, then it turns it into a large probability, but it will always remain between 0 and1. ▪ Many multi-layer neural networks end in a penultimate layer which outputs real-valued scores that are not conveniently scaled and which may be difficult to work with. Here the softmax is very useful because it converts the scores to a normalized probability distribution, which can be displayed to a user or used as input to other systems. For this reason it is usual to append a softmax function as the final layer of the neural network. 33 • Optimizer: ▪ Optimizer algorithms are optimization method that helps improve a deep learning model’s performance. These optimization algorithms or optimizers widely affect the accuracy and speed training of the deep learning model. But first of all, the question arises of what an optimizer is. ▪ While training the deep learning optimizers model, modify each epoch’s weights and minimize the loss function. An optimizer is a function or an algorithm that adjusts the attributes of the neural network, such as weights and learning rates. Thus, it helps in reducing the overall loss and improving accuracy. The problem of choosing the right weights for the model is a daunting task, as a deep learning model generally consists of millions of parameters. It raises the need to choose a suitable optimization algorithm for your application. Hence understanding these machine learning algorithms is necessary for data scientists before having a deep dive into the field. • 4.5 Convolutional Neural Network ▪ A convolutional neural network (CNN) is a type of artificial neural network used primarily for image recognition and processing, due to its ability to recognize patterns in images. A CNN is a powerful tool but requires millions of labelled data points for training. CNNs must be trained with high-power processors, such as a GPU or an NPU, if they are to produce results quickly enough to be useful. 34 • Architecture of Convolutional Neural Network: Fig. 4.5 Architecture of Convolutional Neural Network • Single-Filter Calculation Fig. 4.6 Description of convolution calculation Fig. 4.7 Demonstration of convolution calculation 35 • Convolutional Layer: ▪ The basic architecture of a CNN is multi-channel convolution consisting of multiple single convolutions. The output of the previous layer (or the original image of the first layer) is used as the input of the current layer. It is then convolved with the filter in the layer and serves as the output of this layer. The convolution kernel of each layer is the weight to be learned. Similar to FCN, after the convolution is complete, the result should be biased and activated through activation functions before being input to the next layer. Fig. 4.8 Convolutional Layer • Pooling Layer: ▪ Pooling combines nearby units to reduce the size of the input on the next layer, reducing dimensions. Common pooling includes max pooling and average pooling. When max pooling is used, the maximum value in a small square area is selected as the representative of this area, while the mean value is selected as the representative when average pooling is used. The side of this small area is the pool window size. The following figure shows the max pooling operation whose pooling window size is 2. Fig. 4.9 Pooling Layer 36 • Fully connected layer ▪ the fully connected layer is essentially a classifier. The features extracted on the convolutional layer and pooling layer are straightened and placed at the fully connected layer to output and classify results. ▪ Generally, the Softmax function is used as the activation function of the final fully connected output layer to combine all local features into global features and calculate the score of each type. Fig. 4.10 Fully connected layer 37 Chapter Five AI Project (Cars Classification) A software that can analyze images or videos of cars and automatically identify their make and model. This program uses machine learning algorithms to learn from a large dataset of images of different cars, and then uses this knowledge to classify new images of cars. This dataset consists of various types of cars. The dataset is organized into 2 folders (train, test) and contains subfolders for each car category. There are 4,165 images (JPG) and 7 classes of cars. Project Code: 1.Dataset: Uploading data on Google drive then inserting it on Google Collab to use it in our project Fig. 5.1 connect drive 2. Importing the required libraries: Fig. 5.2 the required libraries 38 3. Importing data into TensorFlow dataset object: Fig. 5.3 Importing data into TensorFlow dataset object 4.Creating the CNN model: Fig. 5.4 the CNN model 39 5. model summary : Fig. 5.5 the model summary 6. Compiling the model: Fig. 5.5 Compiling the model 40 7. Plotting the accuracy and loss curves: Fig. 5.6 Plotting the accuracy and loss curves 8. Testing the prediction on sample of image from test dataset: Fig. 5.7 Testing the prediction on sample of image from test dataset 41 Fig. 5.8 Testing the prediction on sample of image from test dataset 42 References • • • • • • • • • • • • • • • Wikipedia.com ScienceDirect.com Cicso.com AnalyticsVidhya.com Scikit-Learn.org Geeksforgeeks.org Monkeylearn.com DigitalOcean.com W3schools.com Tutorialspoint.com Theaisummer.com Tensorflow.org Huawei AI Overview PDF Huawei Machine Learning PDF Huawei Deep Learning PDF 43