1 Introduction to Machine Learning
Module-1
Introduction to Machine Learning
What is Machine learning?
Machine learning (ML) is a branch of artificial intelligence (AI) and computer science that focuses
on the using data and algorithms to enable AI to imitate the way that humans learn, gradually
improving its accuracy.
How does machine learning work?
1. A Decision Process: In general, machine learning algorithms are used to make a prediction
or classification. Based on some input data, which can be labeled or unlabeled, your
algorithm will produce an estimate about a pattern in the data.
2. An Error Function: An error function evaluates the prediction of the model. If there are
known examples, an error function can make a comparison to assess the accuracy of the
model.
3. set A Model Optimization Process: If the model can fit better to the data points in the
training, then weights are adjusted to reduce the discrepancy between the known example
and the model estimate. The algorithm will repeat this iterative “evaluate and optimize”
process, updating weights autonomously until a threshold of accuracy has been met.
Creativity and motivation
The Journey of a Programmer
When someone begins programming, they typically serve as both the programmer and the end-
user. This dual role is because they are learning to solve problems by themselves. Initially, their
focus is on understanding the basics of programming and solving simple problems. As they gain
experience, they become proficient programmers capable of tackling more complex challenges.
Key Skills for Programmers
1. Logical Thinking:
Definition: The ability to reason systematically and methodically.
Importance: Essential for breaking down problems into smaller, manageable parts and devising
algorithms to solve them.
Example: Creating a flowchart to map out the steps needed to solve a problem.
2. Creativity:
Definition: The ability to think outside the box and come up with innovative solutions.
Importance: Crucial for analyzing problems from different angles, finding multiple solutions, and
optimizing resources.
Example: Developing a unique algorithm to improve the efficiency of a software application.
Yashaswini D M, Asst. Prof, Dept of CSE, SVIT
2 Introduction to Machine Learning
Motivation in Programming
1. Intrinsic Motivation:
Definition: Driven by internal satisfaction and personal interest.
Example: Enjoyment of problem-solving and the desire to create something new.
2. Extrinsic Motivation:
Definition: Driven by external factors such as job requirements or career prospects.
Example: The need to complete a project for a job or to gain recognition in the field.
Ethics in Programming
A programmer's motivation should be complemented by a strong ethical foundation. Key ethical
considerations include:
Quality of Output: Striving to deliver the best possible results to clients.
User-Friendliness: Ensuring the software is easy to use and meets the needs of the end-user.
Resource Optimization: Efficiently using available resources to achieve the desired outcome.
Responsibility: Taking ownership of the software's functionality and user experience.
Responsibilities of a Programmer
1. Developing Feasible Software:
Definition: Creating software that is practical and viable given the constraints.
Example: Ensuring the software can run on the intended hardware and meets performance
requirements.
2. Ensuring User-Friendliness:
Definition: Making software intuitive and easy to navigate.
Example: Implementing clear and consistent user interfaces.
3. Minimizing Hassles:
Definition: Reducing or eliminating bugs and ensuring smooth operation.
Example: Thoroughly testing the software before release.
4. Abstracting Complexity:
Definition: Shielding the user from the intricate details of the software's implementation.
Example: Providing simple and clear documentation and help guides.
5. Striving for Effectiveness:
Definition: Ensuring the software effectively solves the user's problems.
Example: Regularly updating and improving the software based on user feedback.
Conclusion
For a programmer, both creativity and motivation are crucial. Logical thinking and creativity help
in devising efficient solutions, while motivation, whether intrinsic or extrinsic, drives the
programmer to achieve their goals. By adhering to ethical standards and fulfilling their
responsibilities, programmers can deliver high-quality, user-friendly software that meets the needs
of their clients and end-users.
Yashaswini D M, Asst. Prof, Dept of CSE, SVIT
3 Introduction to Machine Learning
Computer hardware architecture
These parts are as explained below:
1. Central Processing Unit (CPU): It performs basic arithmetic, logical, control and I/O
operations specified by the program instructions. CPU will perform the given tasks with a
tremendous speed. Hence, the good programmer has to keep the CPU busy by providing
enough tasks to it.
2. Main Memory: It is the storage area to which the CPU has a direct access. Usually, the
programs stored in the secondary storage are brought into main memory before the
execution. The processor (CPU) will pick a job from the main memory and performs the
tasks. Usually, information stored in the main memory will be vanished when the computer
is turned-off.
3. Secondary Memory: The secondary memory is the permanent storage of computer.
Usually, the size of secondary memory will be considerably larger than that of main
memory. Hard disk, USB drive etc. can be considered as secondary memory storage.
4. I/O Devices: These are the medium of communication between the user and the computer.
Keyboard, mouse, monitor, printer etc. are the examples of I/O devices.
5. Network Connection: Nowadays, most of the computers are connected to network and
hence they can communicate with other computers in a network. Retrieving the information
from other computers via network will be slower compared to accessing the secondary
memory. Moreover, network is not reliable always due to problem in connection.
The programmer has to use above resources sensibly to solve the problem. Usually, a programmer
will be communicating with CPU by telling it ‘What to do next’. The usage of main memory,
secondary memory, I/O devices also can be controlled by the programmer.
To communicate with the CPU for solving a specific problem, one has to write a set of instructions.
Such a set of instructions is called as a program.
Yashaswini D M, Asst. Prof, Dept of CSE, SVIT
4 Introduction to Machine Learning
Understanding programming
A programmer must have skills to look at the data/information available about a problem, analyze
it and then to build a program to solve the problem. The skills to be possessed by a good
programmer includes
1. Thorough knowledge of programming language: One needs to know the vocabulary and
grammar (technically known as syntax) of the programming language. This will help in
constructing proper instructions in the program.
2. Skill of implementing an idea: A programmer should be like a ‘story teller’. That is, he
must be capable of conveying something effectively. He/she must be able to solve the
problem by designing suitable algorithm and implementing it. And, the program must
provide appropriate output as expected.
Thus, the art of programming requires the knowledge about the problem’s requirement and the
strength/weakness of the programming language chosen for the implementation.
It is always advisable to choose appropriate programming language that can cater the complexity
of the problem to be solved.
word and sentence
Unlike human languages, the Python vocabulary is actually pretty small. We call this “vocabulary”
the “reserved words”. These are words that have very special meaning to Python. When Python
sees these words in a Python program, they have one and only one meaning to Python. Later as
you write programs you will make up your own words that have meaning to you called variables.
You will have great latitude in choosing your names for your variables, but you cannot use any of
Python’s reserved words as a name for a variable.
When we train a dog, we use special words like “sit”, “stay”, and “fetch”. When you talk to a dog
and don’t use any of the reserved words, they just look at you with a quizzical look on their face
until you say a reserved word. For example, if you say, “I wish more people would walk to improve
their overall health”,
The reserved words in the language where humans talk to Python include the following:
and del global not with
as elif if or yield
assert else import pass
break except in raise
class finally is return
continue for lambda try
def from nonlocal while
Python Editors and Installing Python
Before getting into details of the programming language Python, it is better to learn how to install the
software. Python is freely downloadable from the internet. There are multiple IDEs (Integrated
Yashaswini D M, Asst. Prof, Dept of CSE, SVIT
5 Introduction to Machine Learning
Development Environment) available for working with Python. Some of them are PyCharm, LiClipse,
IDLE etc. When you install Python, the IDLE editor will be available automatically.
Apart from all these editors, Python program can be run on command prompt also. One has to install suitable
IDE depending on their need and the Operating System they are using. Because, there are separate set of
editors (IDE) available for different OS like Window, UNIX, Ubuntu, Soloaris, Mac, etc. The basic Python
can be downloaded from the link: https://www.python.org/downloads/
Python has rich set of libraries for various purposes like large-scale data processing, predictive analytics,
scientific computing etc. Based on one’s need, the required packages can be downloaded. But, there is a
free open source distribution Anaconda, which simplifies package management and deployment. Hence, it
is suggested for the readers to install Anaconda from the below given link, rather than just installing a
simple Python. https://anaconda.org/anaconda/python
Successful installation of anaconda provides you Python in a command prompt, the default editor IDLE
and also a browser-based interactive computing environment known as jupyter notebook
The jupyter notebook allows the programmer to create notebook documents including live code,
interactive widgets, plots, equations, images etc. To code in Python using jupyter notebook, search
for jupyter notebook in windows search (at Start menu). Now, a browser window will be opened
similar to the one shown in Figure
Homepage of Jupyter Notebook
Yashaswini D M, Asst. Prof, Dept of CSE, SVIT
6 Introduction to Machine Learning
IDE of Jupyter Notebook
You can choose the working directory of your choice for storing your work. To open a notebook
for Python programming, click on New button at the right-side of the screen. Now select Python
3 from the drop-down list. A new notebook (or workbook will be created as shown in above Figure.
Type a command of your choice and press Ctrl+Enter to run that command. One can give
headings/subheadings etc for the commands being typed, store the entire workbook for future
reference etc.
Conversing with Python
Once Python is installed, one can go ahead with working with Python. Use the IDE of your choice for doing
programs in Python. After installing Python (or Anaconda distribution), if you just type ‘python’ in the
command prompt, you will get the message as shown in Figure. The prompt >>> (usually called as chevron)
indicates the system is ready to take Python instructions. If you would like to use the default IDE of Python,
that is, the IDLE, then you can just run IDLE and you will get the editor as shown in Figure.
Yashaswini D M, Asst. Prof, Dept of CSE, SVIT
7 Introduction to Machine Learning
Python initialization in command prompt
Python IDLE editor
After understanding the basics of few editors of Python, let us start our communication with
Python, by saying Hello World. The Python uses print() function for displaying the contents.
Consider the following code
- >>> print(“Hello World”) #type this and press enter key
Hello World #output displayed
>>> #prompt returns again
Here, after typing the first line of code and pressing the enter key, we could able to get the output
of that line immediately. Then the prompt (>>>) is returned on the screen. This indicates, Python
is ready to take next instruction as input for processing.
Yashaswini D M, Asst. Prof, Dept of CSE, SVIT
8 Introduction to Machine Learning
Python program to find area of triangle
a=float(input("enter the first side:"))
b=float(input("enter the second side:"))
c=float(input("enter the third side:"))
s=(a+b+c)/2
area=(s*(s-a)*(s-b)*(s-c))**0.5
print("the area of the triangle is %0.2f"% c)
output:
enter the first side: 2
enter the second side: 2
enter the third side: 22
the area of the triangle is 22.00
sum of two numbers
num1=int(input("enter the number:"))
num2=int(input("enter the number:"))
sum= num1+num2
print(sum)
output:
enter the number: 1
enter the number: 2
3
Swapping two numbers
x= input("enter value of x:")
y= input("enter value of y:")
temp=x
x=y
y=temp
print("the value of x after swapping:{}".format(x))
print("the value of y after swapping:{}".format(y))
output:
enter value of x: 4
enter value of y: 5
the value of x after swapping:5
the value of y after swapping:4
Yashaswini D M, Asst. Prof, Dept of CSE, SVIT
9 Introduction to Machine Learning
python program to print Fibonacci series
# Number of terms to generate in the Fibonacci series
num = 10
# First two terms of the Fibonacci series
n1 = 0
n2 = 1
# Print the first two terms
print("fibonacci series:", n1, n2, end=" ")
# Loop to generate the remaining terms
for i in range(2, num):
n3 = n1 + n2 # Calculate the next term
n1 = n2 # Update n1 to the previous n2
n2 = n3 # Update n2 to the new term
print(n3,end="") # print the next terms
Once we are done with the program, we can close or terminate Python by giving quit() command
as shown - >>> quit() #Python terminates
Terminology: Interpreter and Compiler
All digital computers can understand only the machine language written in terms of zeros and
ones. But, for the programmer, it is difficult to code in machine language. Hence, we generally use
high level programming languages like Java, C++, PHP, Perl, JavaScript etc. Python is also one of
the high-level programming languages. The programs written in high level languages are then
translated to machine level instruction so as to be executed by CPU. How this translation behaves
depending on the type of translators viz. compilers and interpreters.
A compiler translates the source code of high-level programming language into machine level
language. For this purpose, the source code must be a complete program stored in a file (with
extension, say, .java, .c, .cpp etc). The compiler generates executable files (usually with extensions
.exe, .dll etc) that are in machine language. Later, these executable files are executed to give the
output of the program.
On the other hand, interpreter performs the instructions directly, without requiring them to be pre-
compiled. Interpreter parses (syntactic analysis) the source code ant interprets it immediately.
Hence, every line of code can generate the output immediately, and the source code as a complete
set, need not be stored in a file. That is why, in the previous section, the usage of single line
print(“Hello World”) could able to generate the output immediately.
Yashaswini D M, Asst. Prof, Dept of CSE, SVIT
10 Introduction to Machine Learning
Consider an example of adding two numbers
- >>> x=10
>>> y=20
>>> z= x+y
>>> print(z)
30
Here, x, y and z are variables storing respective values. As each line of code above is processed
immediately after the line, the variables are storing the given values. Observe that, though each
line is treated independently, the knowledge (or information) gained in the previous line will be
retained by Python and hence, the further lines can make use of previously used variables. Thus,
each line that we write at the Python prompt are logically related, though they look independent.
Writing a Program
As Python is interpreted language, one can keep typing every line of code one after the other (and
immediately getting the output of each line) as shown in previous section. But, in real-time
scenario, typing a big program is not a good idea. It is not easy to logically debug such lines.
Hence, Python programs can be stored in a file with extension .py and then can be run. Programs
written within a file are obviously reusable and can be run whenever we want. Also, they are
transferrable from one machine to other machine via pen-drive, CD etc.
What is a Program?
A program is a sequence of instructions intended to do some task. For example, if we need to
count the number of occurrences of each word in a text document, we can write a program to do
so. Writing a program will make the task easier compared to manually counting the words in a
document. Moreover, most of the times, the program is a generic solution. Hence, the same
program may be used to count the frequency of words in another file. The person who does not
know anything about the programming also can run this program to count the words.
Programming languages like Python will act as an intermediary between the computer and the
programmer. The end-user can request the programmer to write a program to solve one’s problem.
The Building Blocks of Programs
There are certain low-level conceptual structures to construct a program in any programming
language. They are called as building-blocks of a program and listed below –
Input: Every program may take some inputs from outside. The input may be through keyboard,
mouse, disk-file etc. or even through some sensors like microphone, GPS etc.
Yashaswini D M, Asst. Prof, Dept of CSE, SVIT
11 Introduction to Machine Learning
Output: Purpose of a program itself is to find the solution to a problem. Hence, every program
must generate at least one output. Output may be displayed on a monitor or can be stored in a file.
Output of a program may even be a music/voice message.
Sequential Execution: In general, the instructions in the program are sequentially executed from
the top.
Conditional Execution: In some situations, a set of instructions have to be executed based on the
truth-value of a variable or expression. Then conditional constructs (like if) have to be used. If the
condition is true, one set of instructions will be executed and if the condition is false, the true-
block is skipped.
Repeated Execution: Some of the problems require a set of instructions to be repeated multiple
times. Such statements can be written with the help of looping structures like for, while etc.
Reuse: When we write the programs for general-purpose utility tasks, it is better to write them
with a separate name, so that they can be used multiple times whenever/wherever required. This
is possible with the help of functions.
What Could Possibly Go Wrong?
It is obvious that one can do mistakes while writing a program. The possible mistakes are
categorized as below –
Syntax Errors: The statements which are not following the grammar (or syntax) of the
programming language are tend to result in syntax errors. Python is a casesensitive language.
Hence, there is a chance that a beginner may do some syntactical mistakes while writing a program.
The lines involving such mistakes are encountered by the Python when you run the program and
the errors are thrown by specifying possible reasons for the error. The programmer has to correct
them and then proceed further.
Logical Errors: Logical error occurs due to poor understanding of the problem. Syntactically, the
program will be correct. But it may not give the expected output. For example, you are intended
to find a%b, but by mistake you have typed a/b. Then it is a logical error.
Semantic Errors: A semantic error may happen due to wrong use of variables, wrong operations
or in wrong order. For example, trying to modify un-initialized variable etc.
Debugging
When you Python spits out an error or even when it gives you a result that is different from what
you had intended, then begins the hunt for the cause of the error. Debugging is the process of
finding the cause of the error in your code. When you are debugging a program, and especially if
you are working on a hard bug, there are four things to try:
Yashaswini D M, Asst. Prof, Dept of CSE, SVIT
12 Introduction to Machine Learning
• Reading
Examine your code, read it back to yourself, and check that it says what you meant to say.
• Running
Experiment by making changes and running different versions. Often if you display the
right thing at the right place in the program, the problem becomes obvious, but sometimes
you have to spend some time to build scaffolding.
• Ruminating
Take some time to think! What kind of error is it: syntax, runtime, semantic? What
information can you get from the error messages, or from the output of the program? What
kind of error could cause the problem you’re seeing? What did you change last, before the
problem appeared?
• Retreating
At some point, the best thing to do is back off, undoing recent changes, until you get back
to a program that works and that you understand. Then you can start rebuilding.
Beginning programmers sometimes get stuck on one of these activities and forget the others.
Finding a hard bug requires reading, running, ruminating, and sometimes retreating. If you get
stuck on one of these activities, try the others. Each activity comes with its own failure mode.
Yashaswini D M, Asst. Prof, Dept of CSE, SVIT