Saturday, 18 February 2023

ARTIFICIAL-INTELLIGENCE WITH PYTHON- SMALL -NOTES

 

Artificial Intelligence with Python:-

Agent- Entity that perceives its environment and acts upon that environment.

Initial State- The state in which the agent begins.

Actions- Actions(s) returns the set of actions that can be executed in state s

Transition Model- A description of what state results from performing any applicable action in any state.

RESULT(s, a) returns the state resulting from performing action a in state s


State Space- The set of all states reachable from the initial state by any sequence of actions.

Goal test- A way to determine whether a given state is a goal state.

Path Cost- Numerical cost associated with a given path.

Search Problems-

  • initial state
  • actions
  • transition model
  • goal test
  • path cost function

Solution -

A sequence of actions that leads from the initial state to a goal state.

Optimal-Solution- A solution that has the lowest path cost among all solutions.

Node- A data structure that keeps track of

  • a state
  • a parent (node that generated this node)
  • an action(action applied to parent to get code)
  • a path cost(from initial state to node)

Approach-

  • Start with a frontier that contains the initial state.

  • Repeat:

         * if the frontier is empty, then no solution.
    
  • Remove a node from the frontier.

  • If node contains goal state, return the solution.

  • Expand node, add resulting nodes to the frontier.

Find a node from A to E.

Revised Approach:-

  • Start with a frontier that contains the initial state.

  • Start with an empty explored set.

  • Repeat:

              if the frontier is empty, then no solution.
    
  • Remove a node from the frontier

  • If node contains goal state, return the solution.

  • Add the node to the explored set.

  • Expand node, add resulting nodes to the frontier if they aren't already in the frontier or the explored set.

  • One of the simplest data structures for adding and removing elements is called Stack- last-in-first-out data type.

  • So when we treat the frontier like a stack, a last in, first out data structure, that's the result we get.

Depth-First-Search-

Is the search algorithm that always expands the deepest node in the frontier.

Breath-First-Search-

search algorithm that always expands the shallowest node in the frontier.

It means that instead of using a stack, which depth-first-search, or DFS, used where the most recent item added to the frontier is the one we'll explore next, in breath-first-search, or BFS, will instead use a queue— First-in-first-out data type.

Uninformed Search -

Search strategy that uses no problem- specific knowledge.

Informed Search-

Search strategy that uses problem-specific knowledge to find solutions more efficiently.

Greedy best-first search-

Search algorithm that expands the node that is closes to the goal, as estimated by a heuristic function h(n).


A* Search -

search algorithm that expands node with lowest value of g(n) + h(n)

g(n) = cost to reach node

h(n) = estimated cost to goal

optimal if

— h(n) is admissible (never overestimates the true cost), and

— h(n) is consistent (for every node n and successor n’ with step cost c, h(n)_<_h(n’)+c)

Minimax -

  • Max(X) aims to maximize score.
  • Min (O) aims to minimize score.

Minimax-

  • Max(X) aims to maximize score.
  • Min (O) aims to minimize score.

Game-

  • So: initial state
  • PLAYER(s): returns which player to move in state s
  • Actions(s): returns legal moves in state s
  • RESULT(s, a): returns state after action a taken in state s
  • TERMINAL(s): check if state s is a terminal state
  • UTILITY(s): Final numerical value for terminal state s

Minimax-

  • Give a state s:
  • Max PICKS action a in Actions(s) that produces highest value of Min-Value(RESULT(s, a))
  • MIN picks action a in ACTIONS(s) that produces smallest value of MAX-VALUE(RESULT(s, a))

DEPTH-LIMITED MINIMAX-

ALPHA-BETA PRUNING-

EVALUATION-FUNCTION—

function that estimates the expected utility of the game from a given state.

PYTHON-NOTES:-

 

PYTHON- NOTES

Python is a high-level, general-purpose programming language. Its design philosophy emphasizes code readability with the use of significant indentation. Python is dynamically-typed and garbage-collected. It supports multiple programming paradigms, including structured, object-oriented and functional programming

Python is a Super popular in IT industry, making it one of the most common programming language used today. Python isn’t new. It’s first version was released by Guido van Rossum back in 1991.

1
**SYNTAX AND CODE BLOCK**

When writing code, using correct syntax is super important. Even a small typo, like a missing parentheses or extra comma, can cause a syntax error and the won’t execute at all. Likes if you code result in an error or an exception, pay close attention to syntax and watch out for minor mistakes.

If your syntax is correct, but the script has unexpected behavior or output, this may due to a semantic problem. Remember that syntax is the rule of how code is constructed, while semantic are the overall effect the code has. It is possible to have syntactically correct code that runs successfully, but doesn’t do what we want it to do.

When working with the code blocks in exercises for this course, be mindful of syntax errors, along with the overall result of your code. Just because you fixed a syntax error doesn’t mean that the code will have the desired effect when it runs! Once you’re fixed an error in your code, don’t forget to submit it to have your work checked.

  • Python are platform specific scripting language like power shall which is used on Windows, and Bash which is used on Linux.
  • Both are widely used by System Administrators on those platforms.
  • There are also general purpose scripting language similar to Python, like Perl or Ruby, which are also widely used for Scripting and automation.
  • JavaScript, which was originally developed as a Client-side scripting language for the web, is increasingly used server-side for a border set of tasks.
  • There’s a vast array of traditional language to explore like C, C++, Java, and Go. **KEYWORDS:-**
  • Syntax – The rule for how a sentence is constructed.
  • Semantics – The actual meaning of statements.
  • Script – A program that’s short, simple, and can be written very quickly.
  • Automation – The process of replacing a manual step with one that happens automatically.
  • Functions – Pieces of code that’s perform a unit of work.
  • Keywords – Reserved words that are used to construct instructions.
  • Code Style – Self documenting code written in a way that’s readable and doesn’t Concealed its intent.
  • None – A special data type in Python used to indicate that things are Empty or that they returned nothing.
  • Comparing Things – To evaluate as true, the (AND) operator would need both expressions to be true at the same time.

— If we use the (OR) operator, instead, the expressions will be true if either of the expressions are true and false only when both expressions are False. The (NOT) operator inverts the value of the Expression that’s in front of it.

  • Branching – The ability of a program to ALTER its Execution sequences.
  • Else – Statement – When a return statement is Executed the function exists, so that the code that follows doesn’t get Executed.
  • High-level-language:- A programming language like python that is designed to be easy for humans to read and write.
  • Low-level-language:- A programming language that is designed to be easy for computer to execute; also called “Machine language” or “Assembly language”.
  • Portability – A property of a program that can run on more than one kind of computer.
  • Interpret – To execute a program in a high-level-language by translating it one line at a time.
  • Compile – To translate a program written in a high-level-language into a low-level-language all at once, in preparation for letter execution.
  • Expression – A combination of Numbers, symbols, or other variables that produce a result when evaluated.
  • Value – One of the basic unit of data, like a number or String, that performs manipulates.
  • String – A type that represents sequence of character.
  • Operator – A symbol that’s represents a simple computation like addition, multiplication, subtraction, cation, or string concatenation.
  • Floating-Point – A type that represents numbers with fractional parts.
  • Variables – Names that we give to certain value in our programs.
  • Assignment – The process of storing a value inside a variable.
  • Implicit Conversion – The interpreter automatically converts one data type into another.
  • Source code – A program in a high language before being compiled.
  • Object code – The output of the compiler after it translates the program.
  • Executable – Another name for object code that is ready to be executed.
  • Prompt – Characters displayed by the interpreter to indicate that is ready to take input from the user.
  • Interactive mode – A way by using the python interpreter by typing commands and expressions at the prompt.
  • script: A program stored in a file (usually one that will be interpreted).
  • script mode: It is a method of using Python interpreter and executing statements in a script.
  • program: A set of instructions that specifies a computation.
  • Debugging – It is a process of finding and removing any of the three kinds of programming errors.
  • Parse – To execute a program and analyze the syntactic structure.
  • Print-Statement – An instruction that cause the Python interpreter to display a value on the screen.
  • Python-Interpreter– The programs that reads what’s in the recipe and translates it into instructions to follow your computer.
  • Initializing – To give an initial value to a value.
  • While Loops – Instruct your computer to continuously execute our code based on the value of a condition.
  • stack diagram: A graphical representation of a stack of functions, their variables, and the values they refer to.
  • frame- A box in a stack diagram that represents a function call. It contains the local variables and parameters of the function.
  • traceback- A list of the functions that are executing, printed when an exception occurs.
  • Infinite loop- A loop that keep executing and never stops.

Implicit vs Explicit Conversion—

As we saw earlier in the video, some data types can be mixed and matched due to implicit conversion. Implicit conversion is where the interpreter helps us out and automatically converts one data type into another, without having to explicitly tell it do so.

  • By contrast, explicit conversion is where we manually convert from one data into another by calling the relevant function for the data type we want to convert to. We used this in our video example.
  • When we wanted to print a number alongside some text. Before we could do that, we needed to call the str() function to convert the number into a string. Once the number was explicit converted to a string, we could join it with the rest of our textual string and print the result.

While Loops- Instruct your computer to continuously execute your code based on the value of a given condition.

Anatomy of a While Loop-

A while loop will continuously execute code depending on the value of a condition. It begins with the keyword while, followed by the comparison to be executed, that a colon. On the next line is the code block to be executed, intended to the right. Similar to an if- statement, the code in the body will only be executed if the comparison is evaluated to be true. What sets a while loop apart, however, is that this code block will keep executing as long as the evaluation statement is true. Once the statement is no longer true, the code exits and the next line of code will be executed.

Conditional cheat sheet

  • a==b: a is equal to b
  • a ! =b: a is different than b
  • a<b: a is smaller than b
  • a<=b: a is smaller or equal to b
  • a>b: a is bigger than b
  • a>=b: a is bigger or equal to b

LOGICAL OPERATORS-

  • a and b : True if both a and b are True, False otherwise.
  • a or b: True if either a or b or both are True. False If both are false.
  • not a: True if a is false, False if a is true.

Branching-Blocks-

In Python, we branch our code using if, else and elif. This is the branching Syntax:

if condition1:

1
if -block

else condition2:

else:

1
else-block

Remember: True if-block will be executed if condition1 is True. The elif-block will be executed if condition1 is False and condition2 is True. The else block will be executed when all the specified condition are false.

COMPARISON OPERATORS-

In Python, We can use comparison operators to compare values. When a comparison is made, Python returns a Boolean result, or simply a True or False.

  • To check if two values are same, we can use the equality operator: ==
  • To check if two values are not the same, we can use the not equal operator: !=

We can also check if values are greater than or less than each other using > and <. If ****you try to compare data types that are not compatible, like checking if a string is greater than an Integer, Python will thrown a Type Error.

We can also make very complex comparison by joining statements together using logical operators with our comparison operators. These logical operators are and, or & not when using the and operators, both side of the statements being evaluated must be True for the whole statement to be true.

When using the or operator, if either side of the comparison is true, when the whole statement is true. Lastly, the not operator simply invents the value of the statement immediately following it. So if a statement evaluates the True, we put the NOT operator front of it, it would be false.

Infinite loops and code blocks-

Another easy mistake people can happen easily whin using while loops is introducing an infinite loop. A infinite loop means the code block in the loop will continue to execute and never stops. This can happen when the condition being evaluated in a while loop doesn’t change. Pay close attention to your variables and what possible value they can take. Think about unexpected values, like zero.

Common Pitfalls with Variable Initialization

In general you’ll want to watch out for a common mistakes: forgetting to Initialize variables.

If you try to use a variable without first initialize it, you’ll run into a Name Error. This is the python interpreter catching the mistake and that you’re using an undefined variable. The fix is pretty simple: initialize the variable by assigning the variable a value before use it.

Another common mistake to watch out for that can be a little trickier to spot is forgetting to initialize variable with the correct value. If you use a variable earlier in your code and then reuse it later in a loop without first setting the value to something you want, your code may wind up doing something you didn’t expect. Don’t forget to initialize your variables before using them.

FOR LOOP:

literates over a sequence of values.

First, in Python and a lot of other programming languages, a range of numbers with start the value 0 by default. Second, the list of numbers generated will be 1 less than the given value. In the simple example here, X will take the values 0,1,2,3, and 4. Let’s check out.

So there, we have a basic of loop. It iterates over a sequence of numbers generated by the range of function. When we are using for loop, we point the variable defined between in, in this case, X at each element of the sequence. This means on the first iteration X at point 1.

On the second iteration, it points at 2 and so on. Whatever you write a code in the body of the loop we will executed on each of the values, one value at a time. The body of the loop can do a lot of things with the values it iterates.

Expressions and Statement-

An expression is a combination of values, variables, and operators. A value all by itself is considered an expression, and so is a variable, so the following are all legal expressions (assuming that the variable x has been assigned a value):I 17 x x + 17

Python Keywords

The python interpreter uses keywords to recognize the structure of the python, and they can’t be used as variable names.

and del from not while

as elif global or with

assert else if pass yield

break expect import print

class exec in raise

continue finally is return

def for lamda try


What Is a Method?

Calling methods on objects executes functions that operate on attributes of a specific instance of the class. This means that calling a method on a list, for example, only modifies that instance of a list, and not all lists globally. We can define methods within a class by creating functions inside the class definition. These instance methods can take a parameter called self which represents the instance the method is being executed on. This will allow you to access attributes of the instance using dot notation, like self.name, which will access the name attribute of that specific instance of the class object. When you have variables that contain different values for different instances, these are called instance variables.

BLUE RAY CD’S IS BACK?

Introduction Remember the days when CDs were the go-to medium for storing data and music? Well, it seems like those days might be back, alth...