Docsity

Artificial Intelligence Lab Manual, Schemes and Mind Maps of Artificial Intelligence

Partial preview of the text

Download Artificial Intelligence Lab Manual and more Schemes and Mind Maps Artificial Intelligence in PDF only on Docsity!

CSC462 Artificial Intelligence

Lab Manual

Name

Registration

Number

Class

Instructor’s Name

Introduction This lab is aimed to provide students a complete insight of the implementation of different Artificial Intelligence algorithms. In order to achieve the set goal, lab will be divided into three main modules;

  1. Introduction to Python (Pycharm), aimed to give students the basic concepts of python language in the context of AI.
  2. Implementation of searching Algorithms in Python 3) Implementation of Machine Learning algorithms using MATLAB and Python. This covers fundamentals to advance topics including different AI agents, searching algorithms, knowledge representation, supervised learning (linear classifier, KNN, Neural Networks) and unsupervised learning (K-means clustering). Lab is also intended to get students familiarized with the modern tool Pycharm (Python) in the context of Artificial Intelligence.

List of Experiments of AI Lab # Description

Lab 1 Introduction to Python – Data Structures and Loops

Lab 2 Introduction to Python – Functions ,Tuples, Sets, and Dictionaries

Lab 3 Object Oriented Design Practices in Python

Lab 4 Searching – Breadth First Search

Lab 5 Uniform Cost Search and Depth First Search

Lab 6 Heuristic and Local Search

Lab 7 Genetic Algorithm

Lab 8 Prolog Introduction

Lab 9 Introduction to Matlab

Lab 10 Linear Regression

Lab 11 Linear Classification

Lab 12 Regression and Classification using K Nearest Neighbors

Statement Purpose: This lab is an introductory session on Python. It is a powerful object-oriented programming language, comparable to Perl, Ruby, Scheme and Java. Some of Python’s notable features are: Easy to use that makes it simple to get your first programs working Easy to learn that makes it an excellent choice for beginners Runs everywhere, including Mac OS X, Windows, Linux and Unix

Activity Outcomes:

The lab will teach students to: Basic Operations on strings and numbers Basic use of conditionals Basic use of loops

Instructor Note:

As a pre-lab activity, read Chapters 3-5 from the book ( Introduction to Programming Using Python - Y. Liang (Pearson, 2013) ) to gain an insight about python programming and its fundamentals. LAB # 01

  1. Type print(45.90) and press Enter. The output will show 45.90 on screen.

Activity 2:

Display strings on screen.

Solution:

  1. Python recognized strings through quotes (single, double). Anything inside quotes is a string for Python interpreter.
  2. Type hello , press Enter. An error message will be displayed as Python interpreter does not understand this as a string.
  3. Type ‘hello’ and press Enter. Hello will be displayed.
  4. Type ‘Quote me on this!’ and press Enter. Same string will be displayed.
  5. Type “What’s your name?” and press Enter. What’s your name? will be printed on the screen.
  6. You can specify multi-line strings using triple quotes – “ “ “ or ‘ ‘ ‘. Type following text and press Enter

Activity 3:

Use Python as a calculator.

Solution:

  1. The interpreter acts as a simple calculator: you can type an expression at it and it will write the value. Expression syntax is straightforward: the operators + (addition), - (subtraction), * (multiplication) and / (division) work just like in most other languages.Parentheses (()) can be used for grouping.
  2. Type in 2 + 2 and press Enter. Python will output its result i.e. 4.
  3. Try following expressions in the shell. a. 50 – 4 b. 23.5 – 2. c. 23 – 18. d. 5 * 6 e. 2.5 * 10 f. 2.5 * 2. '''This is a multi-line string. This is the first line. This is the second line. "What's your name?," I asked. He said "Bond, James Bond."

g. 28 / 4 (note: division returns a floating point number) h. 26 / 4 i. 23.4 / 3.

Activity 4:

Get an integer answer from division operation. Also get remainder of a division operation in the output.

Solution:

  1. Division (/) always gives a float answer. There are different ways to get a whole as an answer.
  2. Use // operator: a. Type 28 // 4 and press Enter. The answer is a whole number. b. Type 26 // 4 and press Enter.
  3. Use (int) cast operator: this operator changes the interchangeable types. Type int (26/4) float (26/4) a. Type (int)26 / 4 and press Enter. The answer is a wholenumber. b. Type (int) 28/4 ; press Enter.
  4. The modulus (or mod) % operator is used to get the remainder as an output (division / operator returns the quotient). a. Type 28 % 4. Press Enter. 0 will be the result. b. Type 26 % 4. Press Enter. 2 will be the result.

Activity 5:

Calculate 4^3 , 4^10 , 4^29 , 4^150 , 4^1000

Solution:

  1. The multiplication (*) operator can be used for calculating powers of a number. However, if the power is big, the task will be tedious. For calculating powers of a number, Python uses ** operator.
  2. Type following and obtain the results of above expressions. a. 4 ** 3 b. 4 ** 10 c. 4 ** 29 d. 4 ** 150 e. 4 ** 1000

#!/usr/bin/env python name = input('What is your name? ') print('Hello ' + name) job = input('What is your job? ') print('Your job is ' + job)

Activity 8:

Take input from the keyboard and use it in your program.

Solution:

In Python and many other programming languages you can get user input. In Python the input() function will ask keyboard input from the user.The input function prompts text if a parameter is given. The function reads input from the keyboard, converts it to a string and removes the newline (Enter).Type and experiment with the script below. Activity 9: Let us take an integer from user as input and check whether the given value is even or not. Solution: A. Create a new Python file from Python Shell and type the following code.

Text

x = "Nancy" print(x)

Combine numbers and text

s = "My lucky number is %d, what is yours?" % 7 print(s)

alternative method of combining numbers and text

s = "My lucky number is " + str(7) + ", what is yours?" print(s)

B. Run the code by pressing F5. You will get the following output.

Activity 10:

Let us modify the code to take an integer from user as input and check whether the given value is even or odd. If the given value is not even then it means that it will be odd. So here we need to use if-else statement an demonstrated below. Solution: A. Create a new Python file from Python Shell and type the following code. B. Run the code by pressing F5. You will get the following output. Activity 11: Calculate the sum of all the values between 0-10 using while loop. Solution:  Create a new Python file from Python Shell and type the following code.  Run the code by pressing F5.

Activity 13: Write a Python code to keep accepting integer values from user until 0 is entered. Display sum of the given values. Solution:  Create a new Python file from Python Shell and type the following code.  Run the code by pressing F5. You will get the following output. Activity 14: Write a Python code to accept an integer value from user and check that whether the given value is prime number or not. Solution:  Create a new Python file from Python Shell and type the following code.  Run the code by pressing F5.

You will get the following output(s). 3) Stage v (verify)

Home Activities:

Activity 1: Write a Python code to accept marks of a student from 1-100 and display the grade according to the following formula. Grade F if marks are less than 50 Grade E if marks are between 50 to 60 Grade D if marks are between 61 to 70 Grade C if marks are between 71 to 80 Grade B if marks are between 81 to 90 Grade A if marks are between 91 to 100 Activity 2: Write a program that takes a number from user and calculate the factorial of that number.

Statement Purpose:

This lab will give you practical implementation of different types of sequences including lists, tuples, sets and dictionaries. We will use lists alongside loops in order to know about indexing individual items of these containers. This lab will also allow students to write their own functions.

Activity Outcomes:

This lab teaches you the following topics: How to use lists, tuples, sets and dictionaries How to use loops with lists How to write customized functions

Instructor Note:

As a pre-lab activity, read Chapters 6, 10 and 14 from the book ( Introduction to Programming Using Python - Y. Liang (Pearson, 2013) ) to gain an insight about python programming and its fundamentals. LAB # 2

1) Stage J (Journey) Introduction: Python provides different types of data structures as sequences. In a sequence, there are more than one values and each value has its own index. The first value will have an index 0 in python, the second value will have index 1 and so on. These indices are used to access a particular value in the sequence. Python offers different types of sequences. Lists are the most important type of sequence being used in Python. It is a collection of same or different type of objects. These objects are separated by commas to distinguish from each other enclosed in square brackets. The following activities show that how lists are used in Python. A tuple is a sequence of immutable Python objects. Tuples are sequences, just like lists. The differences between tuples and lists are, the tuples cannot be changed unlike lists and tuples use parentheses, whereas lists use square brackets. The data type "set", which is a collection type, has been part of Python since version 2.4. A set contains an unordered collection of unique and immutable objects. The set data type is, as the name implies, a Python implementation of the sets as they are known from mathematics. This explains, why sets unlike lists or tuples can't have multiple occurrences of the same element. A dictionary is a collection which is unordered, changeable and indexed. In Python dictionaries are written with curly brackets, and they have keys and values. 2) Stage a1 (apply)

Lab Activities:

Activity 1: Use loops to accept 5 values from user and store them in a list. Display all the values (objects) of the list.

Solution: E. Create a new Python file from Python Shell and type the following code. F. Run the code by pressing F5. You will get the following output. Activity 4: Accept two lists from user and display their join. Solution:

You will get the following output. Activity 5: Write a Python code to accept a list from user and find a required element in it. Solution: You will get the following output. If we run the program again and enter 55 to find in the list then the output will be as below.