0% found this document useful (0 votes)
61 views50 pages

Final ML

machine learning

Uploaded by

utsavnayak93
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views50 pages

Final ML

machine learning

Uploaded by

utsavnayak93
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

EXPERIMENT NO.

01

AIM:- Heuristics and search strategy for Travelling sales person problem.
SOFTWARE REQUIRED:- Python Compiler, Computers.
THEORY:-
 Heuristics Search:- Heuristic search is class of method which is used in
order to search a solution space for an optimal solution for a problem. The
heuristic here uses some method to search the solution space while assessing
where in the space the solution is most likely to be and focusing the search
on that area.
 Travelling Salesman Problem (TSP) : Given a set of cities and distances
between every pair of cities, the problem is to find the shortest possible route
that visits every city exactly once and returns to the starting point.

EXAMPLE :- consider the graph shown in the figure on the. A TSP tour in the
graph is 1-2-4-3-1. The cost of the tour is 10+25+30+15 which is 80.

Output of Given Graph:


minimum weight Hamiltonian Cycle : 10 + 25 + 30 + 15 := 80

1
ALGORITHM :-
1. Consider city 1 as the starting and ending point. Since the route is cyclic, we
can consider any point as a starting point.
2. Generate all (n-1)! permutations of cities.
3. Calculate the cost of every permutation and keep track of the minimum cost
permutation.
4. Return the permutation with minimum cost.

PROGRAM CODE:-

# Python3 program to implement traveling salesman


# problem using naive approach.
from sys import maxsize
from itertools import permutations
V=4
# implementation of traveling Salesman Problem
def travellingSalesmanProblem(graph, s):

# store all vertex apart from source vertex


vertex = []
for i in range(V):
if i != s:
vertex.append(i)
# store minimum weight Hamiltonian Cycle
min_path = maxsize
next_permutation=permutations(vertex)
for i in next_permutation:

# store current Path weight(cost)


current_pathweight = 0

2
# compute current path weight
k=s
for j in i:
current_pathweight += graph[k][j]
k=j
current_pathweight += graph[k][s]

# update minimum
min_path = min(min_path, current_pathweight)
return min_path

# Driver Code
if __name__ == "__main__":
# matrix representation of graph
graph = [[0, 10, 15, 20], [10, 0, 35, 25],
[15, 35, 0, 30], [20, 25, 30, 0]]
s=0
print(travellingSalesmanProblem(graph, s))

OUTPUT :- 80

 Time complexity: O(n!) where n is the number of vertices in the graph. This
is because the algorithm uses the next_permutation function which generates
all the possible permutations of the vertex set.
 Auxiliary Space: O(n) as we are using a vector to store all the vertices.

3
EXPERIMENT NO. 02

AIM:- Implement n-queens problem using Hill-climbing, simulated annealing, etc.

SOFTWARE REQUIRED:- python compiler

THEORY:-
N-Queens Problem: The N Queen Is The Problem Of Placing N Chess
Queens On An N*N Chessboard So That No Two Queens Attack Each Other
Hill-climbing: Hill climbing is a simple optimization algorithm used in
Artificial Intelligence (AI) to find the best possible solution for a given
problem. It belongs to the family of local search algorithms and is often used
in optimization problems where the goal is to find the best solution from a set
of possible solutions.

4
PROGRAM:-

# Python3 implementation of the


# above approach
from random import randint
N=8
def configureRandomly(board, state):

for i in range(N):

state[i] = randint(0, 100000) % N;


.
board[state[i]][i] = 1;

def printBoard(board):

for i in range(N):
print(*board[i]

def printState( state):


print(*state)
.
def compareStates(state1, state2):
for i in range(N):
if (state1[i] != state2[i]):
return False;
return True;

def fill(board, value):

5
for i in range(N):
for j in range(N):
board[i][j] = value;

def calculateObjective( board, state):

# Number of queens attacking each other,


attacking = 0;

for i in range(N):

row = state[i]
col = i - 1;
while (col >= 0 and board[row][col] != 1) :
col -= 1

if (col >= 0 and board[row][col] == 1) :


attacking += 1;

row = state[i]
col = i + 1;
while (col < N and board[row][col] != 1):
col += 1;

if (col < N and board[row][col] == 1) :


attacking += 1;

row = state[i] - 1

6
col = i - 1;
while (col >= 0 and row >= 0 and board[row][col] != 1) :
col-= 1;
row-= 1;

if (col >= 0 and row >= 0 and board[row][col] == 1) :


attacking+= 1;

row = state[i] + 1
col = i + 1;
while (col < N and row < N and board[row][col] != 1) :
col+= 1;
row+= 1;

if (col < N and row < N and board[row][col] == 1) :


attacking += 1;

row = state[i] + 1
col = i - 1;
while (col >= 0 and row < N and board[row][col] != 1) :
col -= 1;
row += 1;

if (col >= 0 and row < N and board[row][col] == 1) :


attacking += 1;
row = state[i] - 1
col = i + 1;
while (col < N and row >= 0 and board[row][col] != 1) :
col += 1;
row -= 1;

7
if (col < N and row >= 0 and board[row][col] == 1) :
attacking += 1;

return int(attacking / 2);

# generates a board configuration


def generateBoard( board, state):
fill(board, 0);
for i in range(N):
board[state[i]][i] = 1;

def copyState( state1, state2):

for i in range(N):
state1[i] = state2[i];

# This function gets the neighbour


def getNeighbour(board, state):
opBoard = [[0 for _ in range(N)] for _ in range(N)]
opState = [0 for _ in range(N)]

copyState(opState, state);
generateBoard(opBoard, opState);

opObjective = calculateObjective(opBoard, opState);

NeighbourBoard = [[0 for _ in range(N)] for _ in range(N)]

8
NeighbourState = [0 for _ in range(N)]
copyState(NeighbourState, state);
generateBoard(NeighbourBoard, NeighbourState);

for i in range(N):
for j in range(N):

if (j != state[i]) :

NeighbourState[i] = j;
NeighbourBoard[NeighbourState[i]][i] = 1;
NeighbourBoard[state[i]][i] = 0;

temp = calculateObjective( NeighbourBoard, NeighbourState);

if (temp <= opObjective) :


opObjective = temp;
copyState(opState, NeighbourState);
generateBoard(opBoard, opState);

NeighbourBoard[NeighbourState[i]][i] = 0;
NeighbourState[i] = state[i];
NeighbourBoard[state[i]][i] = 1;
copyState(state, opState);
fill(board, 0);
generateBoard(board, state);

def hillClimbing(board, state):


.

9
neighbourBoard = [[0 for _ in range(N)] for _ in range(N)]
neighbourState = [0 for _ in range(N)]

copyState(neighbourState, state);
generateBoard(neighbourBoard, neighbourState);

while True:

copyState(state, neighbourState);
generateBoard(board, state);

# Getting the optimal neighbour

getNeighbour(neighbourBoard, neighbourState);

if (compareStates(state, neighbourState)) :

printBoard(board);
break;

elif (calculateObjective(board, state) == calculateObjective(


neighbourBoard,neighbourState)):

neighbourState[randint(0, 100000) % N] = randint(0, 100000) % N;


generateBoard(neighbourBoard, neighbourState);
state = [0] * N

10
board = [[0 for _ in range(N)] for _ in range(N)]

configureRandomly(board, state);

hillClimbing(board, state);

OUTPUT:-

11
EXPERIMENT NO . 03

AIM : Tic-tac-toe game simulation using search and heuristics.

SOFTWARE REQUIRED : Online Compiler

THEORY:

Tic-tac-toe game: is a fun game played by two players. Before starting the
game, a 3x3 square grid is formed using two vertical and two horizontal lines.
The player who succeeds in placing three of their marks in a horizontal,
vertical, or diagonal row is the winner. In this study, we develop a computer
program where a player can challenge artificial intelligent heuristic approach
(opponent) to win the game. We want to prove that artificial intelligent
heuristic approach enables in giving a great performance for development of
tic-tac-toe game.

12
PROGRAM :
def sum(a, b, c ):
return a + b + c
def printBoard(xState, zState):
zero = 'X' if xState[0] else ('O' if zState[0] else 0)
one = 'X' if xState[1] else ('O' if zState[1] else 1)
two = 'X' if xState[2] else ('O' if zState[2] else 2)
three = 'X' if xState[3] else ('O' if zState[3] else 3)
four = 'X' if xState[4] else ('O' if zState[4] else 4)
five = 'X' if xState[5] else ('O' if zState[5] else 5)
six = 'X' if xState[6] else ('O' if zState[6] else 6)
seven = 'X' if xState[7] else ('O' if zState[7] else 7)
eight = 'X' if xState[8] else ('O' if zState[8] else 8)
print(f"{zero} | {one} | {two} ")
print(f"--|---|---")
print(f"{three} | {four} | {five} ")
print(f"--|---|---")
print(f"{six} | {seven} | {eight} ")
def checkWin(xState, zState):

wins = [[0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6], [1, 4, 7], [2, 5, 8], [0, 4, 8], [2, 4,
6]]
for win in wins:
if(sum(xState[win[0]], xState[win[1]], xState[win[2]]) == 3):
print("X Won the match")
return 1
if(sum(zState[win[0]], zState[win[1]], zState[win[2]]) == 3):
print("O Won the match")
return 0
return -1
if __name__ == "__main__":

13
xState = [0, 0, 0, 0, 0, 0, 0, 0, 0]
zState = [0, 0, 0, 0, 0, 0, 0, 0, 0]
turn = 1 # 1 for X and 0 for O
print("Welcome to Tic Tac Toe")
while(True):
printBoard(xState, zState)
if(turn == 1):
print("X's Chance")
value = int(input("Please enter a value: "))
xState[value] = 1
else:
print("O's Chance")
value = int(input("Please enter a value: "))
zState[value] = 1
cwin = checkWin(xState, zState)
if(cwin != -1):
print("Match over")
break turn = 1 – turn

OUTPUT:-

14
EXPERIMENT NO. 04

AIM: Describe the Sudoku game and represent the actions using Backtracking.

SOFTWARE REQUIRED: Python Compiler

THEORY:

Sudoku is a logic-based puzzle. It is a type of constraint satisfaction


problem, where the solver is given a finite number of objects (the numerals
1-9) and a set of conditions stating how the objects must be placed in
relation to one another. The puzzle consists of a 9×99×9 grid further divided
into nine 3×33×3 sub-grids (also called boxes, blocks, regions, or sub-
squares).

The objective is to place the digits within the grid so that each column, row,
and sub-grid contains all of the digits from 11 to 99. Each puzzle is
published with some of the boxes already filled in, and those constraints help
define the problem's difficulty level.

15
PROGRAM:
# A Backtracking program
# in Python to solve Sudoku problem

# A Utility Function to print the Grid


def print_grid(arr):
for i in range(9):
for j in range(9):
print (arr[i][j], end = " "),
print ()

# Function to Find the entry in


# the Grid that is still not used
# Searches the grid to find an
# entry that is still unassigned. If
# found, the reference parameters
# row, col will be set the location
# that is unassigned, and true is
# returned. If no unassigned entries
# remains, false is returned.
# 'l' is a list variable that has
# been passed from the solve_sudoku function
# to keep track of incrementation
# of Rows and Columns
def find_empty_location(arr, l):
for row in range(9):
for col in range(9):
if(arr[row][col]== 0):
l[0]= row
l[1]= col
return True
return False

# Returns a boolean which indicates


# whether any assigned entry
# in the specified row matches
# the given number.
def used_in_row(arr, row, num):
for i in range(9):
if(arr[row][i] == num):
return True
return False

# Returns a boolean which indicates


# whether any assigned entry

16
# in the specified column matches
# the given number.
def used_in_col(arr, col, num):
for i in range(9):
if(arr[i][col] == num):
return True
return False

# Returns a boolean which indicates


# whether any assigned entry
# within the specified 3x3 box
# matches the given number
def used_in_box(arr, row, col, num):
for i in range(3):
for j in range(3):
if(arr[i + row][j + col] == num):
return True
return False

# Checks whether it will be legal


# to assign num to the given row, col
# Returns a boolean which indicates
# whether it will be legal to assign
# num to the given row, col location.
def check_location_is_safe(arr, row, col, num):

# Check if 'num' is not already


# placed in current row,
# current column and current 3x3 box
return (not used_in_row(arr, row, num) and
(not used_in_col(arr, col, num) and
(not used_in_box(arr, row - row % 3,
col - col % 3, num))))

# Takes a partially filled-in grid


# and attempts to assign values to
# all unassigned locations in such a
# way to meet the requirements
# for Sudoku solution (non-duplication
# across rows, columns, and boxes)
def solve_sudoku(arr):

# 'l' is a list variable that keeps the


# record of row and col in
# find_empty_location Function
l =[0, 0]

17
# If there is no unassigned
# location, we are done
if(not find_empty_location(arr, l)):
return True

# Assigning list values to row and col


# that we got from the above Function
row = l[0]
col = l[1]

# consider digits 1 to 9
for num in range(1, 10):

# if looks promising
if(check_location_is_safe(arr,
row, col, num)):

# make tentative assignment


arr[row][col]= num

# return, if success,
# ya !
if(solve_sudoku(arr)):
return True
# failure, unmake & try again
arr[row][col] = 0

# this triggers backtracking


return False

# Driver main function to test above functions


if __name__=="__main__":

# creating a 2D array for the grid


grid =[[0 for x in range(9)]for y in range(9)]

# assigning values to the grid


grid =[[3, 0, 6, 5, 0, 8, 4, 0, 0],
[5, 2, 0, 0, 0, 0, 0, 0, 0],
[0, 8, 7, 0, 0, 0, 0, 3, 1],
[0, 0, 3, 0, 1, 0, 0, 8, 0],
[9, 0, 0, 8, 6, 3, 0, 0, 5],
[0, 5, 0, 0, 9, 0, 6, 0, 0],
[1, 3, 0, 0, 0, 0, 2, 5, 0],
[0, 0, 0, 0, 0, 0, 0, 7, 4],

18
[0, 0, 5, 2, 0, 6, 3, 0, 0]]

# success print the grid


if(solve_sudoku(grid)):
print_grid(grid)
else:
print ("No solution exists")

OUTPUT:

19
EXPERIMENT NO. 05
AIM: Sorting algorithms employing forward chaining.

SOFTWARE REQUIRED: Python Compiler

THEORY:

Forward chaining: is also known as a forward deduction or forward


reasoning method when using an inference engine. Forward chaining is a form
of reasoning which start with atomic sentences in the knowledge base and
applies inference rules (Modus Ponens) in the forward direction to extract
more data until a goal is reached.

The Forward-chaining algorithm starts from known facts, triggers all rules
whose premises are satisfied, and add their conclusion to the known facts.
This process repeats until the problem is solved

20
PROGRAM:
global facts
global is_changed
is_changed = True
facts = [["plant","mango"],["eating","mango"],["seed","sprouts"]]

def assert_fact(fact):
global facts
global is_changed
if not fact in facts:
facts += [fact]
is_changed = True
while is_changed:
is_changed = False
for A1 in facts:
if A1[0] == "seed":
assert_fact(["plant",A1[1]])
if A1[0] == "plant":
assert_fact(["fruit",A1[1]])
if A1[0] == "plant" and ["eating",A1[1]] in facts:
assert_fact(["human",A1[1]])
print(facts)

OUTPUT:

21
EXPERIMENT NO. 06

AIM: The study of machine learning tools.

SOFTWARE REQUIRED: Online python compiler.

Machine learning tools:

1.SCIKIT LEARN

Scikit-learn is for machine learning development in python. It provides a library


for the Python programming language.

Installation & configuration :

The installation of Scikit learn includes the following installation steps:

Step 1: Installing Python

22
 We can easily install Python by visiting the following link:

https://www.python.org/downloads/

Step 2: Installing NumPy

We can install NumPy by running the following command in our terminal:

pip install numpy

Step 3: Installing SciPy

 Download the SciPy installer using the following link and then run it:

http://sourceforge.net/projects/scipy/files/scipy/0.16.1/

Step 4: Installing Scikit-learn

 Use pip to install Scikit-learn using the following command:

pip install Scikit-learn

2.PYTORCH
PyTorch is a Torch based, Python machine learning library. The torch is a Lua
based computing framework, scripting language, and machine learning library.
Installation and Configuration :

STEP-1: Download and install Anaconda here. Select Anaconda 64-bit installer
for Windows Python 3.8.

23
STEP-2: Open Anaconda manager via Start - Anaconda3 - Anaconda PowerShell
Prompt and test your versions:

You can check your Python version by running the following command: python –-
version

You can check your Anaconda version by running the following command: conda
–-version

STEP-3: .Now, you can install PyTorch package from binaries via Conda.

3.TENSORFLOW

TensorFlow provides a JavaScript library that helps in machine learning. APIs will
help you to build and train the models.

Installation and configuration

if you want to use TensorFlow with GPU support, TensorFlow recommends using
Miniconda (installer for conda package manager) to kick things off.

Step-1 : Install Miniconda

curl https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -o
Miniconda3-latest-Linux-x86_64.sh
bash Miniconda3-latest-Linux-x86_64.sh

Step-2 : Create a conda environment

Create a new conda environment named tf with the following command.

conda create --name tf python=3.9

You can deactivate and activate it with the following commands.

24
conda deactivate
conda activate tf

Make sure it is activated for the rest of the installation.

Step-3 : GPU setup

You can skip this section if you only run TensorFlow on the CPU.

First install the NVIDIA GPU driver if you have not. You can use the following
command to verify it is installed.

nvidia-smi

Then install CUDA and cuDNN with conda and pip.

conda install -c conda-forge cudatoolkit=11.8.0


pip install nvidia-cudnn-cu11==8.6.0.163

Configure the system paths. You can do it with the following command every time
you start a new terminal after activating your conda environment.

CUDNN_PATH=$(dirname $(python -c "import


nvidia.cudnn;print(nvidia.cudnn.__file__)"))
export
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$CONDA_PREFIX/lib/:$CUDN
N_PATH/lib

For your convenience it is recommended that you automate it with the following
commands. The system paths will be automatically configured when you activate
this conda environment.

mkdir -p $CONDA_PREFIX/etc/conda/activate.d
echo 'CUDNN_PATH=$(dirname $(python -c "import
nvidia.cudnn;print(nvidia.cudnn.__file__)"))' >>
$CONDA_PREFIX/etc/conda/activate.d/env_vars.sh
echo 'export
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$CONDA_PREFIX/lib/:$CUDN
N_PATH/lib' >> $CONDA_PREFIX/etc/conda/activate.d/env_vars.sh

Step-4: Install TensorFlow

25
TensorFlow requires a recent version of pip, so upgrade your pip installation to be
sure you're running the latest version.

pip install --upgrade pip

Then, install TensorFlow with pip.

Note: Do not install TensorFlow with conda. It may not have the latest stable
version. pip is recommended since TensorFlow is only officially released to PyPI.
pip install tensorflow==2.12.*

Step-5 : Verify install

Verify the CPU setup:

python3 -c "import tensorflow as tf; print(tf.reduce_sum(tf.random.normal([1000,


1000])))"

If a tensor is returned, you've installed TensorFlow successfully.

Verify the GPU setup:

python3 -c "import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))"

If a list of GPU devices is returned, you've installed TensorFlow successfully.

4.WEKA
These machine learning algorithms help in data mining.

Installation and configuration

Step 1: Visit this website of weka using any web browser. Click on Free
Download

Step 2: It will redirect to a new webpage, click on Start Download. Downloading


of the executable file will start shortly. It is a big 118 MB file that will take some
minutes.

Step 3: Now check for the executable file in downloads in your system and run it.

26
Step 4: It will prompt confirmation to make changes to your system. Click on
Yes.

Step 5: Setup screen will appear, click on Next.

Step 6: The next screen will be of License Agreement, click on I Agree.

Step 7: Next screen is of choosing components, all components are already


marked so don’t change anything just click on the Install button.

Step 8: The next screen will be of installing location so choose the drive which
will have sufficient memory space for installation. It needed a memory space of
301 MB.

Step 9: Next screen will be of choosing the Start menu folder so don’t do
anything just click on Install Button.
Step 10: After this installation process will start and will hardly take a minute to
complete the installation.

Step 11: Click on the Next button after the installation process is complete.

Step 12: Click on Finish to finish the installation process.

Step 13: Weka is successfully installed on the system and an icon is created on
the desktop.

5.KNIME

KNIME is a tool for data analytics, reporting and integration platform. Using the
data pipelining concept, it combines different components for machine learning
and data mining.

27
Installation and configuration

1. Go to the download page on the KNIME.com website to start installing KNIME


Analytics Platform.
2. The download page shows three tabs which can be opened individually:
o Register for Help and Updates
o Download KNIME
o Getting Started
3.Now open the Download KNIME tab and click the installation option that fits
your operating system. KNIME Analytics Platform can be installed on Windows,
Linux, or macOS.

6.COLAB

Google Colab is a cloud service which supports Python. It will help you in building
the machine learning applications using the libraries of PyTorch, Keras,
TensorFlow, and OpenCV

The Earth Engine API is included by default in Google Colaboratory so requires


only importing and authenticating.

Installation and configuration

Step 1: Install Anaconda

Step 2: Install environment

After you have installed Anaconda go to your terminal and let us create an
environment called colab, but you can put the name that you like.

conda create -n colab python==3.10


28
conda activate colab
then we need to install Jupyter server extension for using a WebSocket to proxy
HTTP traffic

conda install -c conda-forge jupyter_http_over_ws


jupyter serverextension enable --py jupyter_http_over_ws
If you want to control you environment on Anaconda you can install the kernel
(optionally)

conda install ipykernel


python -m ipykernel install --user --name colab --display-name "Python (Colab)"

Step 3: Start a local Jupyter server

then you can start a local Jupyter server

jupyter notebook --NotebookApp.allow_origin='https://colab.research.google.com'


\ --port=9090 --no-browser
Once the server has started, You’ll need a copy initial backend URL used for
authentication.

in my case I copy

http://localhost:9090/?token=107b5b6de7b8fe5ada010e2ed066c9f6dafc1e5674392
63c

Step 4: Open Google Colab

You open a new web browser and go to the following link

https://colab.research.google.com/

29
Step 5: Connect to your local runtime

In Colab, click the “Connect” button and select “Connect to local runtime”.

Uninstall

If you dont need the enviroment you can delete by typing

conda remove -n colab --all


cd Desktop
del colab_local.bat

7.APACHE MAHAOUT

Apache Mahout helps mathematicians, statisticians, and data scientists for


executing their algorithms.

Installation and Configuration

We can install Mahout using different methods. Each method is independent


from the others. You can choose any one of these:

 Building Mahout code using Maven.


 Setting up the development environment using Eclipse
 Setting up Mahout for Windows users

Before performing any of the steps, the prerequisites are:

 Having Java installed on your system


 Having Hadoop installed on your system

 Enter y and your Mahout will start installing. Once done, you can test it by
typing the command – mahout, and this will show you the same screen as
shown in preceding figure.

30
Setting up Mahout for Windows users

Windows users can use cygwin to setup their environment. There is one more
easy-to-use way.

Download Hortonworks Sandbox for VirtualBox on your system


(http://hortonworks.com/products/hortonworks-sandbox/#install). On your system,
this will be a pseudo-distributed mode of Hadoop. Log in to the console, and enter
the following command:

yum install mahout

Now, you will see the following screen

Enter y and your Mahout will start installing. Once done, you can test it by typing
the command – mahout, and this will show you the same screen as shown in
preceding figure.

8.ACCORDNET: Accord.Net provides machine learning libraries for image and


audio processing.

31
Installation and Configuration
Must create a new C# project in Visual Studio, then right-click your project, select
"Manage NuGet packages", then look for "Accord.NET" in the NuGet packages
browser. Install any libraries that you think you would need in the application you
are trying to create (i.e. Accord. MachineLearning).

9.KERAS
Keras is an open-source software library that provides a Python interface for
artificial neural networks. Keras acts as an interface for the TensorFlow library

Installation and Configuration

Step 1: Install Python

Step 2: Now, Open the Command Prompt

Step 3: Now, type ‘pip’ in Command Prompt

Step 4: Write ‘pip install tensorflow==1.8’ in Command Prompt

Step 5: Write ‘pip install keras’ on Command Prompt

10.RAPID MINER

Rapid Miner provides a platform for machine learning, deep learning, data
preparation, text mining, and predictive analytics. It can be used for research,
education and application development.

Installation and Configuration


Follow these steps to download the full RapidMiner Server package:
1. Log into your RapidMiner account and click the Downloads icon or tab:
2. From the Downloads page, select the appropriate type of installation for
your environment.
32
3. Extract the contents of the download file to an installation directory.
The following steps describe Server installation. The installation script requires
Java 8; if it is not available (not on the path or JAVA_HOME is not set), the
installer will not run correctly. If it does not run, check your Java installation.

Change to the bin directory (within the installation directory) to locate the start
script (or batch file) for the installation.
1. For Windows, use the file rapidminer-server-installer.bat
2. For Linux, Unix, or Mac OS, use the file rapidminer-server-installer
3. As Admin, run the appropriate installer script. The RapidMiner Server
Installer window opens. This is not the RapidMiner service; it is a tool to
help you preconfigure the service.
4. In the RapidMiner Server Installer window, read the Before You
Start text and click Next when you are ready.
4. In the next window, select your installation type. You can install either locally or
you can create a configuration file used for a headless installation. If you select a
headless installation, browse to a file location for storing the resulting XML file.
Click Next.
1. Scroll to the bottom of the EULA screen, check the box to accept, and
click Next.
2. Enter or browse to the location where RapidMiner Server should be
installed. Please ensure that the folder path does not
contain + or % characters. Click Next. The RapidMiner Server
Installer License window opens.
3. From your account portal Licenses page, copy the license key to your
clipboard. (See this additional information if you need help copying your
key.)
4. Paste the key into the License window. The license registration information
updates based on the details of the entered license. Click Next.
5. Paste the key into the License window. The license registration information
updates based on the details of the entered license. Click Next.

33
EXPERIMENT NO. 07

AIM: K-means clustering implementation using tool

SOFTWARE REQUIRED: Python Software.

THEORY:

K-means clustering algorithm computes the centroids and iterates until we it


finds optimal centroid. It assumes that the number of clusters are already
known. It is also called flat clustering algorithm. The number of clusters
identified from data by algorithm is represented by ‘K’ in K-means.

In this algorithm, the data points are assigned to a cluster in such a manner
that the sum of the squared distance between the data points and centroid
would be minimum. It is to be understood that less variation within the clusters
will lead to more similar data points within same cluster.

34
PROGRAM:

%matplotlib inline
import matplotlib.pyplot as plt
import seaborn as sns; sns.set()
import numpy as np
from sklearn.cluster import KMeans

# The following code will generate the 2D, containing four blobs

from sklearn.datasets.samples_generator import make_blobs


X, y_true = make_blobs(n_samples=400, centers=4, cluster_std=0.60,
random_state=0)

# Next, the following code will help us to visualize the dataset −

plt.scatter(X[:, 0], X[:, 1], s=20);


plt.show()

# Next, make an object of KMeans along with providing number of clusters, train
the model and do the prediction as follows −

kmeans = KMeans(n_clusters=4)
kmeans.fit(X)
y_kmeans = kmeans.predict(X)
Now, with the help of following code we can plot and visualize the cluster’s
centers picked by k-means Python estimator −

plt.scatter(X[:, 0], X[:, 1], c=y_kmeans, s=20, cmap='summer')


centers = kmeans.cluster_centers_
plt.scatter(centers[:, 0], centers[:, 1], c='blue', s=100, alpha=0.9);
plt.show()

35
OUTPUT:

visualize the dataset

ploting and visualize the cluster’s centers picked by k-means

36
EXPERIMENT NO. 08

AIM: Agglomerative, divisive, fuzzy clustering using tool

SOFTWARE REQUIRED: python

THEROY:

Agglomerative clustering is a bottom-up approach. It starts


clustering by treating the individual data points as a single cluster then it
is merged continuously based on similarity until it forms one big cluster
containing all objects. It is good at identifying small clusters.

Divisive clustering works just the opposite of agglomerative clustering.


It starts by considering all the data points into a big single cluster and
later on splitting them into smaller heterogeneous clusters continuously
until all data points are in their own cluster. Thus, they are good at
identifying large clusters. It follows a top-down approach and is more
efficient than agglomerative clustering. But, due to its complexity in
implementation, it doesn’t have any predefined implementation in any of
the major machine learning frameworks.

Fuzzy clustering (also referred to as soft clustering or soft k-means) is a


form of clustering in which each data point can belong to more than
onecluster.

37
PROGRAM :- Agglomerative Clustering

import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import AgglomerativeClustering

x = [4, 5, 10, 4, 3, 11, 14 , 6, 10, 12]


y = [21, 19, 24, 17, 16, 25, 24, 22, 21, 21]

data = list(zip(x, y))

hierarchical_cluster = AgglomerativeClustering(n_clusters=2,
affinity='euclidean', linkage='ward')
labels = hierarchical_cluster.fit_predict(data)

plt.scatter(x, y, c=labels)
plt.show()

OUTPUT:

PROGRAM:- divisive Clustering

import numpy as np
from scipy.cluster.hierarchy import dendrogram, linkage
import matplotlib.pyplot as plt

38
# randomly chosen dataset
X = np.array([[1, 2], [1, 4], [1, 0],
[4, 2], [4, 4], [4, 0]])

# Perform hierarchical clustering


Z = linkage(X, 'ward')

# Plot dendrogram
dendrogram(Z)

plt.title('Hierarchical Clustering Dendrogram')


plt.xlabel('Data point')
plt.ylabel('Distance')
plt.show()

OUTPUT:

PROGRAM –Fuzzy Clustering


from _future_ import division, print_function
import numpy as np
import matplotlib.pyplot as plt

39
import skfuzzy as fuzz

colors = ['b', 'orange', 'g', 'r', 'c', 'm', 'y', 'k', 'Brown', 'ForestGreen']

# Define three cluster centers


centers = [[4, 2],
[1, 7],
[5, 6]]

# Define three cluster sigmas in x and y, respectively


sigmas = [[0.8, 0.3],
[0.3, 0.5],
[1.1, 0.7]]

# Generate test data


np.random.seed(42) # Set seed for reproducibility
xpts = np.zeros(1)
ypts = np.zeros(1)
labels = np.zeros(1)
for i, ((xmu, ymu), (xsigma, ysigma)) in enumerate(zip(centers, sigmas)):
xpts = np.hstack((xpts, np.random.standard_normal(200) * xsigma + xmu))
ypts = np.hstack((ypts, np.random.standard_normal(200) * ysigma + ymu))
labels = np.hstack((labels, np.ones(200) * i))

# Visualize the test data


fig0, ax0 = plt.subplots()
for label in range(3):
ax0.plot(xpts[labels == label], ypts[labels == label], '.',
color=colors[label])
ax0.set_title('Test data: 200 points x3 clusters.')

OUTPUT:

40
EXPERIMENT NO. 09

AIM: Use an appropriate data set for building Write a program to demonstrate the
working of the decision tree based ID3 algorithm

SOFTWARE REQUIRED: Python

THOERY:
 A decision tree is a structure that contains
• Nodes (Rectangular / Circular shape)
• Edges(arrows)
 It is built from a dataset
 Each node is either used to
• Make a decision
• Or represent an outcome

• ID3 stands for Iterative Dichotomiser 3.


• It is named such because the algorithm iteratively (repeatedly)
Dichotomizes (divides) features into two or more groups at each
step.
• ID3 uses a top-down greedy approach to build a decision tree.

41
PROGRAM:
# Import the necessary libraries
from sklearn.datasets import load_iris
from sklearn.tree import DecisionTreeClassifier
from sklearn.tree import export_graphviz
from graphviz import Source
# Load the dataset
iris = load_iris()
X = iris.data[:, 2:] # petal length and width
y = iris.target
# DecisionTreeClassifier
tree_clf = DecisionTreeClassifier(criterion='entropy',
max_depth=2)
tree_clf.fit(X, y)
# Plot the decision tree graph
export_graphviz(
tree_clf,
out_file="iris_tree.dot",
feature_names=iris.feature_names[2:],
class_names=iris.target_names,
rounded=True,
filled=True
)
with open("iris_tree.dot") as f:
dot_graph = f.read()

Source(dot_graph)

42
OUTPUT:

43
EXPERIMENT NO. 10

AIM: Build an Artificial Neural Network by implementing the Back propagation


algorithm and test the same using appropriate data sets.

EQUIPMENTS REQUIRED: Python Compiler, Computer.

THEORY:
Artificial Neural Networks:
Artificial Neural Networks contain artificial neurons which are called units.
These units are arranged in a series of layers that together constitute the whole
Artificial Neural Network in a system.
Back propagation:
Backpropagation is the essence of neural network training. It is the method of
fine-tuning the weights of a neural network based on the error rate obtained
in the previous epoch (i.e., iteration). Proper tuning of the weights allows you
to reduce error rates and make the model reliable by increasing its
generalization.

44
PROGRAM:

import random
from math import exp
from random import seed

# Ini alize a network

def ini alize_network(n_inputs, n_hidden, n_outputs):


network = list()
hidden_layer = [{'weights':[random.uniform(-0.5,0.5) for i in range(n_inputs +
1)]} for i in range(n_hidden)]
network.append(hidden_layer)
output_layer = [{'weights':[random.uniform(-0.5,0.5) for i in range(n_hidden +
1)]} for i in range(n_outputs)]
network.append(output_layer)
i= 1
print("\n The ini alised Neural Network:\n")
for layer in network:
j=1
for sub in layer:
print("\n Layer[%d] Node[%d]:\n" %(i,j),sub)
j=j+1
i=i+1
return network

# Calculate neuron ac va on (net) for an input

45
def ac vate(weights, inputs):
ac va on = weights[-1]
for i in range(len(weights)-1):
ac va on += weights[i] * inputs[i]
return ac va on

# Transfer neuron ac va on to sigmoid func on


def transfer(ac va on):
return 1.0 / (1.0 + exp(-ac va on))

# Forward propagate input to a network output


def forward_propagate(network, row):
inputs = row
for layer in network:
new_inputs = []
for neuron in layer:
ac va on = ac vate(neuron['weights'], inputs)
neuron['output'] = transfer(ac va on)
new_inputs.append(neuron['output'])
inputs = new_inputs
return inputs

# Calculate the deriva ve of an neuron output


def transfer_deriva ve(output):
return output * (1.0 - output)

# Backpropagate error and store in neurons

46
def backward_propagate_error(network, expected):
for i in reversed(range(len(network))):
layer = network[i]
errors = list()

if i != len(network)-1:
for j in range(len(layer)):
error = 0.0
for neuron in network[i + 1]:
error += (neuron['weights'][j] * neuron['delta'])
errors.append(error)
else:
for j in range(len(layer)):
neuron = layer[j]
errors.append(expected[j] - neuron['output'])

for j in range(len(layer)):
neuron = layer[j]
neuron['delta'] = errors[j] * transfer_deriva ve(neuron['output'])

# Update network weights with error


def update_weights(network, row, l_rate):
for i in range(len(network)):
inputs = row[:-1]
if i != 0:
inputs = [neuron['output'] for neuron in network[i - 1]]

47
for neuron in network[i]:
for j in range(len(inputs)):
neuron['weights'][j] += l_rate * neuron['delta'] * inputs[j]
neuron['weights'][-1] += l_rate * neuron['delta']

# Train a network for a fixed number of epochs


def train_network(network, train, l_rate, n_epoch, n_outputs):

print("\n Network Training Begins:\n")

for epoch in range(n_epoch):


sum_error = 0
for row in train:
outputs = forward_propagate(network, row)
expected = [0 for i in range(n_outputs)]
expected[row[-1]] = 1
sum_error += sum([(expected[i]-outputs[i])**2 for i in
range(len(expected))])
backward_propagate_error(network, expected)
update_weights(network, row, l_rate)
print('>epoch=%d, lrate=%.3f, error=%.3f' % (epoch, l_rate, sum_error))

print("\n Network Training Ends:\n")

#Test training backprop algorithm


seed(2)

48
dataset = [[2.7810836,2.550537003,0],
[1.465489372,2.362125076,0],
[3.396561688,4.400293529,0],
[1.38807019,1.850220317,0],
[3.06407232,3.005305973,0],
[7.627531214,2.759262235,1],
[5.332441248,2.088626775,1],
[6.922596716,1.77106367,1],
[8.675418651,-0.242068655,1],
[7.673756466,3.508563011,1]]

print("\n The input Data Set :\n",dataset)


n_inputs = len(dataset[0]) - 1
print("\n Number of Inputs :\n",n_inputs)
n_outputs = len(set([row[-1] for row in dataset]))
print("\n Number of Outputs :\n",n_outputs)

#Network Ini aliza on


network = ini alize_network(n_inputs, 2, n_outputs)

# Training the Network


train_network(network, dataset, 0.5, 20, n_outputs)

print("\n Final Neural Network :")


i= 1
for layer in network:
j=1

49
for sub in layer:
print("\n Layer[%d] Node[%d]:\n" %(i,j),sub)
j=j+1
i=i+1

OUTPUT:

50

You might also like