Python
Python
Python has many reasons for being popular and in demand. A few of the reasons are mentioned
below.
Emphasis on code readability, shorter codes, ease of writing.
Programmers can express logical concepts in fewer lines of code in comparison
to languages such as C++ or Java.
Python supports multiple programming paradigms, like object-oriented,
imperative and functional programming or procedural.
It provides extensive support libraries (Django for web development, Pandas
for data analytics etc)
Dynamically typed language (Data type is based on value assigned)
Variables
Variables in Python are not “statically typed”. We do not need to declare variables before using
them or declare their type. A variable is created the moment we first assign a value to it.
#!/usr/bin/python
# An integer assignment
age = 45
# A floating point
salary = 1456.8
# A string
name = "John"
print(age)
print(salary)
print(name)
Operators
Operators are the main building block of any programming language. Operators allow the
programmer to perform different kinds of operations on operands. These operators can be
categorized based upon their different functionality:
Arithmetic operators: Arithmetic operators are used to perform mathematical
operations like addition, subtraction, multiplication and division.
# Examples of Arithmetic Operator
a =9
b =4
# Addition of numbers
add = a + b
# Subtraction of numbers
sub = a - b
# Multiplication of number
mul = a * b
# Division(float) of number
div1 = a / b
# Division(floor) of number
div2 = a // b
# Modulo of both number
mod = a % b
# print results
print(add)
print(sub)
print(mul)
print(div1)
print(div2)
print(mod)
# a > b is False
print(a > b)
# a < b is True
print(a < b)
# a == b is False
print(a == b)
# a != b is True
print(a != b)
# a >= b is False
print(a >= b)
# a <= b is True
print(a <= b)
Logical Operators: Logical operators perform Logical AND, Logical OR and Logical
NOT operations.
# Examples of Logical Operator
a = True
b = False
# Print a or b is True
print(a or b)
a1 = 'GeeksforGeeks'
b1 = 'GeeksforGeeks'
# Identity operator
print(a1 is not b1)
print(a1 is b1)
# Membership operator
print("G" in a1)
print("N" not in b1)
Basics of Input/Output
Python provides us with two inbuilt functions to read the input from the keyboard.
raw_input(): This function works in older version (like Python 2.x). This function
takes exactly what is typed from the keyboard, convert it to string and then return it to
the variable in which we want to store. For example:
# Python program showing
# a use of raw_input()
g = raw_input("Enter your name : ")
print g
input(): This function first takes the input from the user and then evaluates the
expression, which means Python automatically identifies whether the user entered a
string or a number or list. For example:
# Python program showing
# a use of input()
print(val)
The simplest way to produce output is using the print() function where you can pass zero or
more expressions separated by commas. This function converts the expressions you pass into a
string before writing to the screen.
# Python 3.x program showing
# a screen
print("GeeksForGeeks")
x =5
print("x =", x)
print("GeeksforGeeks")
In Python, the data type is set when you assign a value to a variable:
x = 20 int
x = 20.5 float
x = 1j complex
x = range(6) range
x = True bool
x = b"Hello" bytes
x = bytearray(5) bytearray
x = memoryview(bytes(5)) memoryview
If you want to specify the data type, you can use the following constructor
functions:
x = int(20) int
x = float(20.5) float
x = complex(1j) complex
x = range(6) range
x = bool(5) bool
x = bytes(5) bytes
x = bytearray(5) bytearray
x = memoryview(bytes(5)) memoryview
Python Numbers
int
float
complex
Variables of numeric types are created when you assign a value to them:
Example
x = 1 # int
y = 2.8 # float
z = 1j # complex
To verify the type of any object in Python, use the type() function:
Example
print(type(x))
print(type(y))
print(type(z))
Int
Example
Integers:
x = 1
y = 35656222554887711
z = -3255522
print(type(x))
print(type(y))
print(type(z))
Float
Example
Floats:
x = 1.10
y = 1.0
z = -35.59
print(type(x))
print(type(y))
print(type(z))
Float can also be scientific numbers with an "e" to indicate the power of 10.
Example
Floats:
x = 35e3
y = 12E4
z = -87.7e100
print(type(x))
print(type(y))
print(type(z))
Complex
Example
Complex:
x = 3+5j
y = 5j
z = -5j
print(type(x))
print(type(y))
print(type(z))
Type Conversion
You can convert from one type to another with the int(), float(),
and complex() methods:
Example
Convert from one type to another:
x = 1 # int
y = 2.8 # float
z = 1j # complex
print(a)
print(b)
print(c)
print(type(a))
print(type(b))
print(type(c))
Random Number
Python does not have a random() function to make a random number, but
Python has a built-in module called random that can be used to make random
numbers:
Example
Import the random module, and display a random number between 1 and 9:
import random
print(random.randrange(1, 10))
Python Casting
Specify a Variable Type
Example
Integers:
x = int(1) # x will be 1
y = int(2.8) # y will be 2
z = int("3") # z will be 3
Example
Floats:
Example
Strings:
Python Strings
Strings
Strings in python are surrounded by either single quotation marks, or double
quotation marks.
'hello' is the same as "hello".
Example
print("Hello")
print('Hello')
Example
a = "Hello"
print(a)
Multiline Strings
You can assign a multiline string to a variable by using three quotes:
Example
You can use three double quotes:
Example
a = '''Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua.'''
print(a)
Example
Get the character at position 1 (remember that the first character has the
position 0):
a = "Hello, World!"
print(a[1])
Example
Loop through the letters in the word "banana":
for x in "banana":
print(x)
String Length
To get the length of a string, use the len() function. Example
The len() function returns the length of a string:
a = "Hello, World!"
print(len(a))
Check String
To check if a certain phrase or character is present in a string, we
can use the keyword in.
Example
Check if "free" is present in the following text:
Use it in an if statement:
Example
Print only if "free" is present:
Check if NOT
To check if a certain phrase or character is NOT present in a string,
we can use the keyword not in.
Example
Check if "expensive" is NOT present in the following text:
Use it in an if statement:
Example
print only if "expensive" is NOT present:
Specify the start index and the end index, separated by a colon, to return a
part of the string.
Example
Get the characters from position 2 to position 5 (not included):
b = "Hello, World!"
print(b[2:5])
Python - Modify Strings
Python has a set of built-in methods that you can use on strings.
Upper Case
Example
The upper() method returns the string in upper case:
a = "Hello, World!"
print(a.upper())
Lower Case
Example
The lower() method returns the string in lower case:
a = "Hello, World!"
print(a.lower())
Remove Whitespace
Whitespace is the space before and/or after the actual text, and very often
you want to remove this space.
Example
The strip() method removes any whitespace from the beginning or the end:
Replace String
Example
The replace() method replaces a string with another string:
a = "Hello, World!"
print(a.replace("H", "J"))
Split String
The split() method returns a list where the text between the specified
separator becomes the list items.
Example
The split() method splits the string into substrings if it finds instances of the
separator:
a = "Hello, World!"
print(a.split(",")) # returns ['Hello', ' World!']
Example
Merge variable a with variable b into variable c:
a = "Hello"
b = "World"
c = a + b
print(c)
Example
To add a space between them, add a " ":
a = "Hello"
b = "World"
c = a + " " + b
print(c)
Example
age = 36
txt = "My name is John, I am " + age
print(txt)
But we can combine strings and numbers by using the format() method!
The format() method takes the passed arguments, formats them, and places
them in the string where the placeholders {} are:
Example
Use the format() method to insert numbers into strings:
age = 36
txt = "My name is John, and I am {}"
print(txt.format(age))
Example
quantity = 3
itemno = 567
price = 49.95
myorder = "I want {} pieces of item {} for {} dollars."
print(myorder.format(quantity, itemno, price))
You can use index numbers {0} to be sure the arguments are placed in the
correct placeholders:
Example
quantity = 3
itemno = 567
price = 49.95
myorder = "I want to pay {2} dollars for {0} pieces of item {1}."
print(myorder.format(quantity, itemno, price))
Example
The escape character allows you to use double quotes when you normally
would not be allowed:
Escape Characters
Other escape characters used in Python:
Code Result
\\ Backslash
\n New Line
\r Carriage Return
\t Tab
\b Backspace
\f Form Feed
Note: All string methods returns new values. They do not change the original
string.
Method Description
find() Searches the string for a specified value and returns the
position of where it was found
index() Searches the string for a specified value and returns the
position of where it was found
isalpha() Returns True if all characters in the string are in the alphabet
isupper() Returns True if all characters in the string are upper case
partition() Returns a tuple where the string is parted into three parts
replace() Returns a string where a specified value is replaced with a
specified value
rfind() Searches the string for a specified value and returns the last
position of where it was found
rindex() Searches the string for a specified value and returns the last
position of where it was found
rpartition() Returns a tuple where the string is parted into three parts
rsplit() Splits the string at the specified separator, and returns a list
split() Splits the string at the specified separator, and returns a list
startswith() Returns true if the string starts with the specified value
Python Booleans
Booleans represent one of two values: True or False.
Boolean Values
In programming you often need to know if an expression is True or False.
You can evaluate any expression in Python, and get one of two
answers, True or False.
When you compare two values, the expression is evaluated and Python
returns the Boolean answer:
Example
print(10 > 9)
print(10 == 9)
print(10 < 9)
Example
Print a message based on whether the condition is True or False:
a = 200
b = 33
if b > a:
print("b is greater than a")
else:
print("b is not greater than a")
Example
Evaluate a string and a number:
print(bool("Hello"))
print(bool(15))
Example
Evaluate two variables:
x = "Hello"
y = 15
print(bool(x))
print(bool(y))
Any list, tuple, set, and dictionary are True, except empty ones.
Example
The following will return True:
bool("abc")
bool(123)
bool(["apple", "cherry", "banana"])
Some Values are False
In fact, there are not many values that evaluate to False, except empty
values, such as (), [], {}, "", the number 0, and the value None. And of course
the value False evaluates to False.
Example
The following will return False:
bool(False)
bool(None)
bool(0)
bool("")
bool(())
bool([])
bool({})
One more value, or object in this case, evaluates to False, and that is if you
have an object that is made from a class with a __len__ function that
returns 0 or False:
Example
class myclass():
def __len__(self):
return 0
myobj = myclass()
print(bool(myobj))
Example
Print the answer of a function:
def myFunction() :
return True
print(myFunction())
Example
Print "YES!" if the function returns True, otherwise print "NO!":
def myFunction() :
return True
if myFunction():
print("YES!")
else:
print("NO!")
Python also has many built-in functions that return a boolean value, like
the isinstance() function, which can be used to determine if an object is of
a certain data type:
Example
Check if an object is an integer or not:
x = 200
print(isinstance(x, int))
Python Operators
Python Operators
Operators are used to perform operations on variables and values.
In the example below, we use the + operator to add together two values:
Example
print(10 + 5)
Arithmetic operators
Assignment operators
Comparison operators
Logical operators
Identity operators
Membership operators
Bitwise operators
+ Addition x+y
- Subtraction x-y
* Multiplication x*y
/ Division x/y
% Modulus x%y
** Exponentiation x ** y
// Floor division x // y
= x=5 x=5
+= x += 3 x=x+3
-= x -= 3 x=x-3
*= x *= 3 x=x*3
/= x /= 3 x=x/3
%= x %= 3 x=x%3
//= x //= 3 x = x // 3
**= x **= 3 x = x ** 3
|= x |= 3 x=x|3
^= x ^= 3 x=x^3
== Equal x == y
!= Not equal x != y
not Reverse the result, returns False if the result not(x <
is true 5 and x
< 10)
<< Zero fill Shift left by pushing zeros in from the right and let the
left shift leftmost bits fall off
>> Signed Shift right by pushing copies of the leftmost bit in from
right the left, and let the rightmost bits fall off
shift
Python Lists
mylist = ["apple", "banana", "cherry"]
List
Lists are used to store multiple items in a single variable.
Lists are one of 4 built-in data types in Python used to store collections of
data, the other 3 are Tuple, Set, and Dictionary, all with different qualities
and usage.
Example
Create a List:
List Items
List items are ordered, changeable, and allow duplicate values.
List items are indexed, the first item has index [0], the second item has
index [1] etc.
Ordered
When we say that lists are ordered, it means that the items have a defined
order, and that order will not change.
If you add new items to a list, the new items will be placed at the end of the
list.
Note: There are some list methods that will change the order, but in
general: the order of the items will not change.
Changeable
The list is changeable, meaning that we can change, add, and remove items
in a list after it has been created.
Allow Duplicates
Since lists are indexed, lists can have items with the same value:
Example
Lists allow duplicate values:
List Length
To determine how many items a list has, use the len() function:
Example
Print the number of items in the list:
Example
String, int and boolean data types:
Example
A list with strings, integers and boolean values:
<class 'list'>
Example
What is the data type of a list?
Example
Using the list() constructor to make a List:
Example
Print the second item of the list:
Negative Indexing
Negative indexing means start from the end
-1 refers to the last item, -2 refers to the second last item etc.
Example
Print the last item of the list:
Range of Indexes
You can specify a range of indexes by specifying where to start and where to
end the range.
When specifying a range, the return value will be a new list with the specified
items.
Example
Return the third, fourth, and fifth item:
thislist =
["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(thislist[2:5])
By leaving out the start value, the range will start at the first item:
Example
This example returns the items from the beginning to, but NOT including,
"kiwi":
thislist =
["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(thislist[:4])
By leaving out the end value, the range will go on to the end of the list:
Example
This example returns the items from "cherry" to the end:
thislist =
["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(thislist[2:])
Example
This example returns the items from "orange" (-4) to, but NOT including
"mango" (-1):
thislist =
["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(thislist[-4:-1])
Example
Check if "apple" is present in the list:
Example
Change the second item:
Example
Change the values "banana" and "cherry" with the values "blackcurrant" and
"watermelon":
If you insert more items than you replace, the new items will be inserted
where you specified, and the remaining items will move accordingly:
Example
Change the second value by replacing it with two new values:
Note: The length of the list will change when the number of items inserted
does not match the number of items replaced.
If you insert less items than you replace, the new items will be inserted
where you specified, and the remaining items will move accordingly:
Example
Change the second and third value by replacing it with one value:
Insert Items
To insert a new list item, without replacing any of the existing values, we can
use the insert() method.
Example
Insert "watermelon" as the third item:
Note: As a result of the example above, the list will now contain 4 items.
Example
Using the append() method to append an item:
Insert Items
To insert a list item at a specified index, use the insert() method.
Example
Insert an item as the second position:
thislist = ["apple", "banana", "cherry"]
thislist.insert(1, "orange")
print(thislist)
Note: As a result of the examples above, the lists will now contain 4 items.
Extend List
To append elements from another list to the current list, use
the extend() method.
Example
Add the elements of tropical to thislist:
Example
Add elements of a tuple to a list:
Example
Remove "banana":
thislist = ["apple", "banana", "cherry"]
thislist.remove("banana")
print(thislist)
Example
Remove the second item:
If you do not specify the index, the pop() method removes the last item.
Example
Remove the last item:
Example
Remove the first item:
Example
Delete the entire list:
Example
Clear the list content:
Example
Print all items in the list, one by one:
Learn more about for loops in our Python For Loops Chapter.
Example
Print all items by referring to their index number:
Example
Print all items, using a while loop to go through all the index numbers
Example
A short hand for loop that will print all items in a list:
Example:
Based on a list of fruits, you want a new list, containing only the fruits with
the letter "a" in the name.
Without list comprehension you will have to write a for statement with a
conditional test inside:
Example
fruits = ["apple", "banana", "cherry", "kiwi", "mango"]
newlist = []
for x in fruits:
if "a" in x:
newlist.append(x)
print(newlist)
With list comprehension you can do all that with only one line of code:
Example
fruits = ["apple", "banana", "cherry", "kiwi", "mango"]
print(newlist)
The Syntax
newlist = [expression for item in iterable if condition == True]
Condition
The condition is like a filter that only accepts the items that valuate to True.
Example
Only accept items that are not "apple":
The condition if x != "apple" will return True for all elements other than
"apple", making the new list contain all fruits except "apple".
Example
With no if statement:
Iterable
The iterable can be any iterable object, like a list, tuple, set etc.
Example
You can use the range() function to create an iterable:
newlist = [x for x in range(10)]
Example
Accept only numbers lower than 5:
Expression
The expression is the current item in the iteration, but it is also the outcome,
which you can manipulate before it ends up like a list item in the new list:
Example
Set the values in the new list to upper case:
Example
Set all values in the new list to 'hello':
The expression can also contain conditions, not like a filter, but as a way to
manipulate the outcome:
Example
Return "orange" instead of "banana":
Example
Sort the list numerically:
Sort Descending
To sort descending, use the keyword argument reverse = True:
Example
Sort the list descending:
Example
Sort the list descending:
The function will return a number that will be used to sort the list (the lowest
number first):
Example
Sort the list based on how close the number is to 50:
def myfunc(n):
return abs(n - 50)
Example
Case sensitive sorting can give an unexpected result:
Luckily we can use built-in functions as key functions when sorting a list.
Example
Perform a case-insensitive sort of the list:
Reverse Order
What if you want to reverse the order of a list, regardless of the alphabet?
The reverse() method reverses the current sorting order of the elements.
Example
Reverse the order of the list items:
There are ways to make a copy, one way is to use the built-in List
method copy().
Example
Make a copy of a list with the copy() method:
Example
Make a copy of a list with the list() method:
Example
Join two list:
list1 = ["a", "b", "c"]
list2 = [1, 2, 3]
Another way to join two lists is by appending all the items from list2 into
list1, one by one:
Example
Append list2 into list1:
for x in list2:
list1.append(x)
print(list1)
Or you can use the extend() method, which purpose is to add elements from
one list to another list:
Example
Use the extend() method to add list2 at the end of list1:
list1.extend(list2)
print(list1)
Method Description
extend() Add the elements of a list (or any iterable), to the end of the
current list
index() Returns the index of the first element with the specified value
Python Tuples
mytuple = ("apple", "banana", "cherry")
Tuple
Tuples are used to store multiple items in a single variable.
Example
Create a Tuple:
Tuple Items
Tuple items are ordered, unchangeable, and allow duplicate values.
Tuple items are indexed, the first item has index [0], the second item has
index [1] etc.
Ordered
When we say that tuples are ordered, it means that the items have a defined
order, and that order will not change.
Unchangeable
Tuples are unchangeable, meaning that we cannot change, add or remove
items after the tuple has been created.
Allow Duplicates
Since tuples are indexed, they can have items with the same value:
Example
Tuples allow duplicate values:
Example
Print the number of items in the tuple:
Example
One item tuple, remember the comma:
thistuple = ("apple",)
print(type(thistuple))
#NOT a tuple
thistuple = ("apple")
print(type(thistuple))
Example
String, int and boolean data types:
Example
A tuple with strings, integers and boolean values:
tuple1 = ("abc", 34, True, 40, "male")
type()
From Python's perspective, tuples are defined as objects with the data type
'tuple':
<class 'tuple'>
Example
What is the data type of a tuple?
Example
Using the tuple() method to make a tuple:
Example
Print the second item in the tuple:
Negative Indexing
Negative indexing means start from the end.
-1 refers to the last item, -2 refers to the second last item etc.
Example
Print the last item of the tuple:
Range of Indexes
You can specify a range of indexes by specifying where to start and where to
end the range.
When specifying a range, the return value will be a new tuple with the
specified items.
Example
Return the third, fourth, and fifth item:
thistuple =
("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")
But there is a workaround. You can convert the tuple into a list, change the
list, and convert the list back into a tuple.
Example
Convert the tuple into a list to be able to change it:
Add Items
Once a tuple is created, you cannot add items to it.
Example
You cannot add items to a tuple:
Just like the workaround for changing a tuple, you can convert it into
a list, add your item(s), and convert it back into a tuple.
Example
Convert the tuple into a list, add "orange", and convert it back into a tuple:
Remove Items
Tuples are unchangeable, so you cannot remove items from it, but
you can use the same workaround as we used for changing and
adding tuple items:
Example
Convert the tuple into a list, remove "apple", and convert it back into a
tuple:
Example
The del keyword can delete the tuple completely:
Tuple Matching in Python is a method of grouping the tuples by matching the second
element in the tuples. It is achieved by using a dictionary by checking the second element in
each tuple in python programming. However, we can make new tuples by taking portions of
existing tuples.
Tuple Syntax
Tup = ('Jan','feb','march')
we have assigned tuple 1 with the persons information like name, surname,
birth year, etc. and another tuple 2 with the values in it like number (1,2,3,….,7).
For Example,
(name, surname, birth year, favorite movie and year, profession, birthplace) =
Robert
Comparing tuples
A comparison operator in Python can work with tuples.
The comparison starts with a first element of each tuple. If they do not compare
to =,< or > then it proceed to the second element and so on.
It starts with comparing the first element from each of the tuples
#case 1
a=(5,6)
b=(1,4)
if (a>b):print("a is bigger")
else: print("b is bigger")
#case 2
a=(5,6)
b=(5,4)
if (a>b):print("a is bigger")
else: print ("b is bigger")
#case 3
a=(5,6)
b=(6,4)
if (a>b):print("a is bigger")
else: print("b is bigger")
Case1: Comparison starts with a first element of each tuple. In this case 5>1, so
the output a is bigger
Case 2: Comparison starts with a first element of each tuple. In this case 5>5
which is inconclusive. So it proceeds to the next element. 6>4, so the output a is
bigger
Case 3: Comparison starts with a first element of each tuple. In this case 5>6
which is false. So it goes into the else block and prints "b is bigger."
directory[last,first] = number
Inside the brackets, the expression is a tuple. We could use tuple assignment in
a for loop to navigate this dictionary.
for last, first in directory:
print first, last, directory[last, first]
This loop navigates the keys in the directory, which are tuples. It assigns the
elements of each tuple to last and first and then prints the name and
corresponding telephone number.
Dictionary can return the list of tuples by calling items, where each tuple is a key
value pair.
a = {'x':100, 'y':200}
b = list(a.items())
print(b)
Deleting Tuples
Tuples are immutable and cannot be deleted. You cannot delete or remove
items from a tuple. But deleting tuple entirely is possible by using the keyword
del
Slicing of Tuple
To fetch specific sets of sub-elements from tuple or list, we use this unique
function called slicing. Slicing is not only applicable to tuple but also for array
and list.
#Comparing tuples
#case 1
a=(5,6)
b=(1,4)
if (a>b):print "a is bigger"
else: print "b is bigger"
#case 2
a=(5,6)
b=(5,4)
if (a>b):print "a is bigger"
else: print "b is bigger"
#case 3
a=(5,6)
b=(6,4)
if (a>b):print "a is bigger"
else: print "b is bigger"
Machine Learning
Machine Learning Algorithms helps computer system learn without being explicitly
programmed. These algorithms are categorized into supervised or unsupervised.
Let us now see a few algorithms −
This is the most commonly used machine learning algorithm. It is called supervised
because the process of algorithm learning from the training dataset can be thought
of as a teacher supervising the learning process. In this kind of ML algorithm, the
possible outcomes are already known and training data is also labeled with correct
answers. It can be understood as follows −
Suppose we have input variables x and an output variable y and we applied an
algorithm to learn the mapping function from the input to output such as −
Y = f(x)
Now, the main goal is to approximate the mapping function so well that when we
have new input data (x), we can predict the output variable (Y) for that data.
Mainly supervised leaning problems can be divided into the following two kinds of
problems −
Classification − A problem is called classification problem when we have the
categorized output such as “black”, “teaching”, “non-teaching”, etc.
Regression − A problem is called regression problem when we have the real
value output such as “distance”, “kilogram”, etc.
Decision tree, random forest, knn, logistic regression are the examples of
supervised machine learning algorithms.
As the name suggests, these kinds of machine learning algorithms do not have any
supervisor to provide any sort of guidance. That is why unsupervised machine
learning algorithms are closely aligned with what some call true artificial intelligence.
It can be understood as follows −
Suppose we have input variable x, then there will be no corresponding output
variables as there is in supervised learning algorithms.
In simple words, we can say that in unsupervised learning there will be no correct
answer and no teacher for the guidance. Algorithms help to discover interesting
patterns in data.
Unsupervised learning problems can be divided into the following two kinds of
problem −
Clustering − In clustering problems, we need to discover the inherent
groupings in the data. For example, grouping customers by their purchasing
behavior.
Association − A problem is called association problem because such kinds
of problem require discovering the rules that describe large portions of our
data. For example, finding the customers who buy both x and y.
K-means for clustering, Apriori algorithm for association are the examples of
unsupervised machine learning algorithms.
Linear Regression
Logistic Regression
Decision Tree
Decision tree is a supervised learning algorithm that is mostly used for classification
problems.
Basically it is a classifier expressed as recursive partition based on the independent
variables. Decision tree has nodes which form the rooted tree. Rooted tree is a
directed tree with a node called “root”. Root does not have any incoming edges and
all the other nodes have one incoming edge. These nodes are called leaves or
decision nodes. For example, consider the following decision tree to see whether a
person is fit or not.
It is used for both classification and regression problems. But mainly it is used for
classification problems. The main concept of SVM is to plot each data item as a
point in n-dimensional space with the value of each feature being the value of a
particular coordinate. Here n would be the features we would have. Following is a
simple graphical representation to understand the concept of SVM −
In the above diagram, we have two features hence we first need to plot these two
variables in two dimensional space where each point has two co-ordinates, called
support vectors. The line splits the data into two different classified groups. This line
would be the classifier.
Naïve Bayes
It is used for both classification and regression of the problems. It is widely used to
solve classification problems. The main concept of this algorithm is that it used to
store all the available cases and classifies new cases by majority votes of its k
neighbors. The case being then assigned to the class which is the most common
amongst its K-nearest neighbors, measured by a distance function. The distance
function can be Euclidean, Minkowski and Hamming distance. Consider the
following to use KNN −
Computationally KNN are expensive than other algorithms used for
classification problems.
The normalization of variables needed otherwise higher range variables can
bias it.
In KNN, we need to work on pre-processing stage like noise removal.
K-Means Clustering
Random Forest
Import numpy as np
Import sklearn.preprocessing
NumPy − Basically NumPy is a general purpose array-processing package
designed to efficiently manipulate large multi-dimensional arrays of arbitrary
records without sacrificing too much speed for small multi-dimensional
arrays.
Sklearn.preprocessing − This package provides many common utility
functions and transformer classes to change raw feature vectors into a
representation that is more suitable for machine learning algorithms.
Step 2 − Defining sample data − After importing the packages, we need to define some
sample data so that we can apply pre-processing techniques on that data. We will now
define the following sample data –
Binarization
This is the preprocessing technique which is used when we need to convert our
numerical values into Boolean values. We can use an inbuilt method to binarize the
input data say by using 0.5 as the threshold value in the following way –
data_binarized = preprocessing.Binarizer(threshold =
0.5).transform(input_data)
print("\nBinarized data:\n", data_binarized)
output: all the values above 0.5(threshold value) would be converted to 1 and all the values
below 0.5 would be converted to 0.
Binarized data
[[ 1. 0. 1.]
[ 0. 1. 1.]
[ 0. 0. 1.]
[ 1. 1. 0.]]
Mean Removal
output:
Mean = [ 1.75 -1.275 2.2]
Std deviation = [ 2.71431391 4.20022321 4.69414529]
The code below will remove the Mean and Standard deviation of the input data –
data_scaled = preprocessing.scale(input_data)
print("Mean =", data_scaled.mean(axis=0))
print("Std deviation =", data_scaled.std(axis = 0))
output :
Mean = [ 1.11022302e-16 0.00000000e+00 0.00000000e+00]
Std deviation = [ 1. 1. 1.]
Scaling
It is another data preprocessing technique that is used to scale the feature vectors. Scaling of feature
vectors is needed because the values of every feature can vary between many random values. In
other words we can say that scaling is important because we do not want any feature to be
synthetically large or small. With the help of the following Python code, we can do the scaling of our
input data, i.e., feature vector −
# Min max scaling
data_scaler_minmax =
preprocessing.MinMaxScaler(feature_range=(0,1))
data_scaled_minmax = data_scaler_minmax.fit_transform(input_data)
print ("\nMin max scaled data:\n", data_scaled_minmax)
output:
Normalization
It is another data preprocessing technique that is used to modify the feature vectors. Such kind of
modification is necessary to measure the feature vectors on a common scale. Followings are two
types of normalization which can be used in machine learning −
L1 Normalization
It is also referred to as Least Absolute Deviations. This kind of normalization modifies the values
so that the sum of the absolute values is always up to 1 in each row. It can be implemented on the
input data with the help of the following Python code –
# Normalize data
data_normalized_l1 = preprocessing.normalize(input_data, norm =
'l1')
print("\nL1 normalized data:\n", data_normalized_l1)
output:
L1 normalized data:
[[ 0.22105263 -0.2 0.57894737]
[ -0.2027027 0.32432432 0.47297297]
[ 0.03571429 -0.56428571 0.4 ]
[ 0.42142857 0.16428571 -0.41428571]]
L2 Normalization
It is also referred to as least squares. This kind of normalization modifies the values so that the sum
of the squares is always up to 1 in each row. It can be implemented on the input data with the help of
the following Python code −
# Normalize data
data_normalized_l2 = preprocessing.normalize(input_data, norm =
'l2')
print("\nL2 normalized data:\n", data_normalized_l2)
Output:
L2 normalized data:
[[ 0.33946114 -0.30713151 0.88906489]
[ -0.33325106 0.53320169 0.7775858 ]
[ 0.05156558 -0.81473612 0.57753446]
[ 0.68706914 0.26784051 -0.6754239 ]]