Download as pdf or txt
Download as pdf or txt
You are on page 1of 62

211001506

211001506
211001506

I
NDEX

S.
NO EX.
NO DATE EXPERI
MENT SI
GN
211001506

I
NDEX

S.
NO EX.
NO DATE EXPERI
MENT SI
GN
211001506

Exptno:1 STUDYOFPROLOG

Ai
m:

Tost
udyt
hepr
ologt
ool
inst
all
ati
onandov
erv
iewandper
for
m knowl
edge-
basedpr
ogr
am

Pr
olog:

PROgr
ami
ngi
nLOGi
c

•PROLOGi
sdecl
arat
ive-speci
fyf
act
sandl
ogi
cal
rel
ati
onshi
ps

•Non-
procedur
al:
&
quot
;what
&
quot
;,
not&
quot
;how&
quot
;.
(
Wespeci
fyt
hepr
obl
em butnott
hesol
uti
on)

•Dat
abasel
anguagewi
thaut
omat
edsear
chandt
heabi
l
ityt
ofol
l
owgener
alr
ules.

Sy
mbol
i
c-sy
mbol
sar
eusedt
orepr
esentobj
ect
s

Hi
ghl
evel
-cont
ainsabui
l
t-i
npr
obl
em sol
vi
ngmechani
sm

• PROLOGPr
ogr
ammi
ng(
Whenwepr
ogr
am i
nPr
ologweneedt
opr
ovi
de
f
oll
owi
ngt
hreet
hingsdecl
ari
ngsomef
act
saboutobj
ect
sandt
hei
r
r
elat
ionshi
ps.

• Def
ini
ngsomer
ulesaboutobj
ect
sandt
hei
rrel
ati
onshi
ps.

• Aski
ngquest
ionsaboutobj
ect
sandt
hei
rrel
ati
onshi
ps.

Pr
ologSy
ntax

Ter
ms:

Thecent
ral
dat
ast
ruct
urei
nPROLOGi
sthatofa

t
erm.Ther
ear
eter
msoff
ourki
nds:

• at
oms,

• number
s,

• v
ari
abl
es,
and

• Compoundt
erms.
211001506

At
omsandnumber
sar
esomet
imesgr
oupedt
oget
herandcal
l
edat
omi
cter
ms.

At
omi
cTer
ms:

At
omi
cter
msar
eusual
l
yst
ri
ngsmadeupofl
ower
-andupper
casel
ett
ers,
digi
ts,
andt
heunder
scor
e,st
art
ingwi
thal
ower
casel
ett
er.

Thef
oll
owi
ngar
eal
lval
i
dPROLOGat
oms:

• el
ephant
,

• b,

• abcXYZ,

• x
_123,

• anot
her
_pr
int
_for
_me_
please

CompoundTer
ms:

• Compoundt
ermsar
emadeupofaPROLOGat
om andanumberof
ar
gument
s(PROLOGt
erms,
i.
e.,
atoms,
number
s,v
ari
abl
es,
orot
her
compoundt
erms)encl
osedi
npar
ent
hesesandsepar
atedbycommas.

• Thef
oll
owi
ngar
esomeexampl
esf
orcompound

t
erms:
ois_
bigger
(hor
se,
X),
f(g(
X,_
),7)
,

• Theset
sofcompoundt
ermsandat
omst
oget
herf
ormt
hesetofPROLOGpr
edi
cat
es.

• At
ermt
hatdoesn’
tcont
ainanyv
ari
abl
esi
scal
l
edagr
oundt
erm.

Var
iabl
es:

Var
iabl
esar
est
ri
ngsofl
ett
ers,
digi
ts,
andt
heunder
scor
e,st
art
ingwi
thacapi
tal
l
ett
eroranunder
scor
e.

Ex
ampl
es:

• X,
211001506

• El
ephant
,

• _
4711,

• X_
1_2,

• My
Var
iabl
e,

Fact
s:

Af
acti
sapr
edi
cat
efol
l
owedbyadot
.

Ex
ampl
es:

• bi
gger
_ani
mal
(whal
e).

• l
i
fe_
is_
beaut
if
ul.

• Thei
ntui
ti
vemeani
ngofaf
acti
sthatwedef
ineacer
tai
ninst
anceofa
r
elat
ionasbei
ngt
rue.

Rul
es:

• Ar
uleconsi
stsofahead(
apr
edi
cat
e)andabody(
asequenceofpr
edi
cat
es
separ
atedbycommas)
.

• Headandbodyar
esepar
atedbyt
hesi
gn:
-and,
li
keev
eryPROLOG
expr
essi
on,
arul
ehast
obet
ermi
nat
edbyadot
.

Ex
ampl
es:

i
s_smal
l
er(
X,Y):
-is_
bigger
(Y,
X).

aunt
(Aunt
,Chi
l
d):
-si
ster
(Aunt
,Par
ent
),
par
ent
(Par
ent
,Chi
l
d).

• Thei
ntui
ti
vemeani
ngofar
ulei
sthatt
hegoal
expr
essedbyi
tsheadi
str
ue,
if
we(
orr
athert
hePROLOGsy
stem)canshowt
hatal
loft
heexpr
essi
ons
(
subgoal
s)i
nther
ule’
sbodyar
etr
ue.

Sy
ntaxofr
ule:

<head>:
-<body
>
211001506

Read:
-asi
f.
-
>li
kes(
john,
X):
-li
kes(
X,cr
icket
).

~“
Johnli
kesXifXl
i
kes
cr
icket
”.

i
.e.
,“Johnl
i
kesany
onewhol
i
kescr
icket
“.

•Rul
esal
way
sendwi
th’
.

Pr
ogr
amsandQuer
ies:

Pr
ogr
ams-

APROLOGpr
ogr
am i
sasequenceofcl
auses.

Quer
ies–

• Af
tercompi
l
ati
onaPROLOGpr
ogr
am i
srunbysubmi
tt
ingquer
iest
othei
nter
pret
er.

• Aquer
yhast
hesamest
ruct
ureast
hebodyofar
ule,
i.
e.i
tisasequenceof
pr
edi
cat
essepar
atedbycommasandt
ermi
nat
edbyadot
.

• Theycanbeent
eredatt
hePROLOGpr
ompt
,whi
chi
nmost
i
mpl
ement
ati
onsl
ookssomet
hingl
i
ket
his:
?-.

• Whenwr
it
ingaboutquer
iesweof
teni
ncl
udet
he?
.

• Ex
ampl
es:

?
-is_
bigger
(el
ephant
,donkey
).

?
-smal
l
(X)
,gr
een(
X),
sli
my(
X).

•I
ntui
ti
vel
y,whensubmi
tt
ingaquer
yli
ket
hel
astexampl
e,weaskPROLOG
whet
heral
lit
spr
edi
cat
esar
epr
obabl
ytr
ue,
ori
not
herwor
dswhet
hert
her
e
i
sanXsucht
hatsmal
l
(X)
,gr
een(
X),
andsl
i
my(
X)ar
eal
ltr
ue.

Ther
ear
etwot
ypesofquer
ies:

1)Gr
oundQuer
y

• edge(
a,h)
211001506

• Thi
squer
yiscal
l
edagr
oundquer
ybecausei
tconsi
stsonl
yofv
alue
i
dent
if
ieraspar
amet
ert
othepr
edi
cat
e.

• Agr
oundquer
yisposedweexpectay
es/
noanswer
.

2)Non-
GroundQuer
y

Theyhav
evar
iabl
esaspar
amet
ers

• edge(
a,X)

Var
iabl
es:

• Al
way
sbegi
nwi
thacapi
tal
let
ter

• ?
-li
kes(
john,
X).

• ?
-li
kes(
john,
Somet
hing)
.

• Butnot

• ?
-li
kes(
john,
somet
hing)

I
nst
allSWI
-Pr
olog:

St
epst
oCompi
l
e:

1.
OpenNot
epad.

2.
Wri
tey
ourFact
sandRul
esandsav
ethef
il
ewi
th.
plex
tensi
on.

3.
Toopent
hedesi
redf
il
e

Fi
l
e->Edi
t->Got
othel
ocat
ion-
>sel
ectf
il
e->Open

4.
Tocheckt
hesy
ntax

I
nfi
l
eedi
tor

Compi
l
e->makeI
fnoer
ror

i
twi
l
lcompi
l
e

5.
Tof
ir
equer
y
211001506

I
nPROLOGedi
tor

Fi
l
e->consul
t->sel
ectt
hef
il
ename

Wr
it
ethequer
y,donotf
orgett
oputf
ull
stop(
.)I
fther
eismor
ethanoneanswert
o
getuse(
;)

Exer
cises:

KB1

woman(
mia)
.

woman(
jody
).

woman(
yol
anda)
.

pl
aysAi
rGui
tar
(j
ody
).

par
ty.

QUERY:

?
-woman(
mia)
.

?
-pl
aysAi
rGui
tar
(mi
a).

?
-par
ty.

?
-concer
t.

OUTPUT:
211001506

KB2

happy
(yol
anda)
.

l
i
stens2musi
c(mi
a).

l
i
stens2musi
c(y
olanda):
-

happy
(yol
anda)
.pl
aysAi
rGui
tar
(mi
a):
-

l
i
stens2musi
c(mi
a).

pl
aysAi
rGui
tar
(Yol
anda):
-

l
i
stens2musi
c(y
olanda)
.

QUERY:

?
-pl
aysAi
rGui
tar
(mi
a).

?
-pl
aysAi
rGui
tar
(yol
anda)
.

OUTPUT:

KB3

l
i
kes(
dan,
sal
l
y).

l
i
kes(
sal
l
y,dan)
.

l
i
kes(
john,
bri
tt
ney
).

mar
ri
ed(
X,Y):
-li
kes(
X,Y),
li
kes(
Y,X)
.

f
ri
ends(
X,Y):
-li
kes(
X,Y);
li
kes(
Y,X)
.
211001506

QUERY:

?
-l
ikes(
dan,
X).

?
-mar
ri
ed(
dan,
sal
l
y).

?
-mar
ri
ed(
john,
bri
tt
ney
).

OUTPUT:

KB4

bi
gger
(el
ephant
,

hor
se)
.bi
gger
(hor
se,

donkey
).

bi
gger
(donkey
,dog)
.

bi
gger
(donkey
,

monkey
).RULES:

i
s_bi
gger
(X,
Y):
-bi
gger
(X,
Y).

i
s_bi
gger
(X,
Y):
-bi
gger
(X,
Z),
is_
bigger
(Z,
Y).

QUERI
ES:

?
-bi
gger
(donkey
,dog)
.

?
-bi
gger
(monkey
,el
ephant
).

?
-bi
gger
(el
ephant
,monkey
).
211001506

OUTPUT:

KB5

mal
e(r
am)
.

mal
e(shy
am)
.

f
emal
e(gi
ta)
.

f
emal
e(si
ta)
.

par
ent
s(sy
am,
gi
ta,
ra

m)
.

par
ent
s(si
ta,
gi
ta,
ram)
.

si
ster
_of(
X,Y)
:-f
emal
e(X)
,par
ent
s(X,
M,F)
,par
ent
s(Y,
M,F)
.

QUERI
ES:

?
-si
ster
_of
(si
ta,
shy
am)
.

?
-si
ster
_of
(si
ta,
x)
211001506

OUTPUT:

KB6

Fact
s:

mal
e(j
ack)
.
mal
e(ol
i
ver
).
mal
e(al
i
).
mal
e(j
ames)
.
mal
e(si
mon)
.
mal
e(har
ry)
.
f
emal
e(hel
en)
.
f
emal
e(sophi
e).
f
emal
e(j
ess)
.
f
emal
e(l
i
ly)
.

par
ent
_of
(j
ack,
j
ess)
.
par
ent
_of
(j
ack,
l
il
y).
par
ent
_of
(hel
en,
jess)
.
par
ent
_of
(hel
en,
li
ly
).
par
ent
_of
(ol
i
ver
,j
ames)
.
par
ent
_of
(sophi
e,
j
ames)
.par
ent
_of
(j
ess,
si
mon)
.
211001506

par
ent
_of
(al
i
,si
mon)
.
par
ent
_of
(l
il
y,har
ry)
.
par
ent
_of
(j
ames,
har
ry)
.

RULES:
f
ather
_of
(X,
Y):
-mal
e(X)
,
par
ent
_of
(X,
Y).

mot
her
_of
(X,
Y):
-femal
e(X)
,
par
ent
_of
(X,
Y).

gr
andf
ather
_of
(X,
Y):
-mal
e(X)
,
par
ent
_of
(X,
Z),
par
ent
_of
(Z,
Y).

gr
andmot
her
_of
(X,
Y):
-femal
e(X)
,
par
ent
_of
(X,
Z),
par
ent
_of
(Z,
Y).

si
ster
_of
(X,
Y):
-%(
X,YorY,
X)%f
emal
e(X)
,
f
ather
_of
(F,
Y),
fat
her
_of
(F,
X),
X\=Y.

si
ster
_of
(X,
Y):
-femal
e(X)
,
mot
her
_of
(M,
Y),
mot
her
_of
(M,
X),
X\=Y.

aunt
_of
(X,
Y):
-femal
e(X)
,
par
ent
_of
(Z,
Y),
sist
er_
of(
Z,X)
,!
.

br
other
_of
(X,
Y):
-%(
X,YorY,
X)%mal
e(X)
,
f
ather
_of
(F,
Y),
fat
her
_of
(F,
X),
X\=Y.

br
other
_of
(X,
Y):
-mal
e(X)
,
211001506

mot
her
_of
(M,
Y),
mot
her
_of
(M,
X),
X\=Y.

uncl
e_of
(X,
Y):
-
par
ent
_of
(Z,
Y),
brot
her
_of
(Z,
X).

ancest
or_
of(
X,Y)
:-
par
ent
_of
(X,
Y).ancest
or_
of(
X,Y)
:
-par
ent
_of
(X,
Z),
ancest
or_
of(
Z,Y)
.
QUERI
ES:

?
-mot
her
_of
(j
ess,
hel
en)
.
?
-br
other
_of
(j
ames,
si
mon)
.
?
-ancest
or_
of(
jack,
si
mon)
.
?
-par
ent
_of
(l
il
y,har
ry)
.
?
-ancest
or_
of(
X,Y)
.
?
-aunt
_of
(X,
Y).
?
-fat
her
_of
(j
ack,
l
il
y).
?
-gr
andf
ather
_of
(j
ames,
j
ess)
.
?
-gr
andmot
her
_of
(sophi
e,
james)
.
?
-gr
andmot
her
_of
(X,
Y).
?
-par
ent
_of
(j
ess,
simon)
.
211001506

OUTPUT:

RESULT:

Thuspr
ologt
ool
inst
all
ati
onandov
erv
iewi
sst
udi
edandknowl
edgebased
pr
ogr
am i
sper
for
medsuccessf
ull
y
211001506

Exptno:2a FOODTABLE

Ai
m:

Towr
it
efact
s,r
ulesandquer
iesf
orf
oodt
abl
eandper
for
mthepr
ogr
am

Pr
ocedur
e:

St
ep1:Opennot
epad

St
ep2:Ent
ert
henecessar
yfact
sandr
ules,
sav
ethef
il
ewi
thext
ensi
on.
pl

St
ep3:Openpr
ologpr
omptandcompi
l
ethef
il
e

St
ep4:Runt
hef
il
eandent
ert
hequer
ies

St
ep5:Di
spl
ayt
her
esul
t

Fact
s:

f
ood(
bur
ger
).

f
ood(
sandwi
ch)
.

f
ood(
piz
za)
.

l
unch(
sandwi
ch)

.di
nner
(pi
zza)
.

Rul
es:

meal
(X):
-food(
X).

Quer
ies/Goal
s:

?
-food(
piz
za)
.

?
-meal
(X)
,lunch(
X).

?
-di
nner
(sandwi
ch)
.
211001506

Out
put
:

Resul
t:

Thus,
thef
act
sandquer
iesar
eent
eredandt
hepr
ogr
am i
sper
for
medsuccessf
ull
y
211001506

Exptno:
2b STUDENT–PROFESSORRELATI
ONTABLE

Dat
e:

Ai
m:Towr
it
efact
s,r
ulesandquer
iesf
orst
udent
-pr
ofessort
abl
eandper
for
mthepr
ogr
am

Pr
ocedur
e:

St
ep1:
OpenNot
epad

St
ep2:
Ent
ert
henecessar
yfact
sandr
ulesandsav
ethef
il
ewi
th

ext
ensi
on.
plSt
ep3:
OpenPr
ologandcompi
l
ethef
il
e

St
ep4:
Runt
hef
il
eandent
ert
hequer
iest
ogett
he

out
putSt
ep5:
Displ
ayt
her
esul
ts

Fact
s:

st
udi
es(
char
li
e,

csc135)
.st
udi
es(
oli
vi
a,

csc135)
.st
udi
es(
jack,

csc131)
.

st
udi
es(
art
hur
,

csc134)
.

t
eaches(
kir
ke,

csc135)
.

t
eaches(
col
l
ins,

csc131)
.

t
eaches(
col
l
ins,

csc171)
.

t
eaches(
juni
per
,

csc134)
.Rul
es:

pr
ofessor
(X,
Y):
-

t
eaches(
X,C)
,st
udi
es(
Y,

C)
.Quer
ies/Goal
s:

?
-st
udi
es(
char
li
e,What
).

?
-pr
ofessor
(ki
rke,
Student
s).
211001506

Out
put
:

Resul
t:

Thus,
thef
act
sandquer
iesar
eent
eredandt
hepr
ogr
am i
sper
for
medsuccessf
ull
y
211001506

Exptno:
2c FAMI
LYTREE

Dat
e:

Ai
m:Towr
it
efact
sandquer
iesf
orf
ami
l
ytr
eeandper
for
mthepr
ogr
am

Pr
ocedur
e:

St
ep1:
OpenNot
epad

St
ep2:
Ent
ert
henecessar
yfact
sandr
ulesandsav
ethef
il
ewi
th

ext
ensi
on.
plSt
ep3:
OpenPr
ologandcompi
l
ethef
il
e

St
ep4:
Runt
hef
il
eandent
ert
hequer
iest
ogett
he

out
putSt
ep5:
Displ
ayt
her
esul
ts

Fact
s:

f
ather_
of (
joe,
paul
).
f
ather_
of (
joe,
mary).
f
ather_
of (
joe,
hope).
mother_of(j
ane,
paul
).
mother_of(j
ane,
mary).
mother_of(j
ane,
hope).

mal
e(paul)
.
mal
e(j
oe).
mal
e(ral
ph).
mal
e(X):-fat
her
_of
(X,
Y).

f
emal
e(mary)
.
f
emal
e(j
ane).
f
emal
e(hope)
.

son_
of(
X,Y):
-fat
her_
of (
Y,X),
male(X).
son_
of(
X,Y):
-mother_of(
Y,X),
male(X).

daught
er_
of(
X,Y):
-fat
her_
of (
Y,X),
femal
e(X).
daught
er_
of(
X,Y):
-mother_of(
Y,X),
femal
e(X).

si
bli
ng_
of(
X,Y):
-!,
fat
her_
of (
Z,X),
fat
her_
of(Z,
Y),
X\=Y.
si
bli
ng_
of(
X,Y):
-!,
mother_of(
Z,X),
mother_
of(Z,
Y),
X\=Y.

Quer
ies:

?
-si
bli
ng_
of(
paul
,
mar
y).

?
-fat
her
_of(
X,Y)
.

?
-son_
of(
paul
,j
oe)
.

?
-son_
of(
joe,
hope)
.

?
-daught
erof(
X,Y)
.
211001506

Out
put
:

Resul
t:

Thust
hef
act
sandquer
iesar
eent
eredandt
hepr
ogr
am i
sper
for
medsuccessf
ull
y
211001506

Exptno:
2d ARI
THMETI
CEXPRESSI
ON

Dat
e:

Ai
m:Towr
it
equer
iesf
orar
it
hmet
icex
pressi
onandper
for
mthe

pr
ogr
am Pr
ocedur
e:

St
ep1:
OpenPr
ologQuer
y

pr
omptSt
ep2:
Ent
ert
he

necessar
yquer
ies

St
ep3:
Per
for
mthear
it
hmet
ic

oper
ati
onsSt
ep4:
Displ
ayt
her
esul
ts

Quer
ies:

?
-Xi
s3+2.

?
-3+2i
sX.

?
-X=3+2.

?
-3+2=X.

?
-Xi
s+(
3,2)
.

?
-5i
s3+2.

?
-3+2i
s5.

?
-Xi
s3*
2.

?
-Xi
s3-
2.

?
-Xi
s-(
2,3)
.

?
-Xi
s5-
3-1.

?
-Xi
s-(
5,3,
1).

?
-Xi
s-(
-(
5,3)
,1)
.

?
-Xi
s5-
3-1.

?
-Xi
s3/
5.

?
-Xi
s3mod5.

?
-Xi
s5mod3.

?
-Xi
s5^
3.

?
-Xi
s(5^
3)^
2.
211001506

?
-X=(
5^3)
^2.

?
-25i
s5^
2.

?
-Yi
s3+2*
4-1.

?
-Yi
s(3+2)
*(4)
-(
1).

?
-Yi
s-(
*(+(
3,2)
,4)
,1)
.

?
-Xi
s3*
2,Yi
sX*
2.

?
-3<
5.

?
-4<
2.

?
-6>5.

?
-12<=12.

?
-12=<12.

?
-3+4=<7.

?
-5=\
=5.

?
-5=\
=4.

?
-5=\
=(3+2)
.

?
-8=8.

?
-8=:
=8.

?
-8=:
=9.

?
-(2+1)
*10=30.

?
-(2+1)
*10=:
=30.

?
-X=2,
X<3.

?
-X=4,
X<3.

?
-*(
2,3)=2*
3.

?
-X=Z.

?
-(X>3)
=(4>3)
.

?
-X=3,
CisX*
X*X.

?
-X=3,
X*X*
XisC.

?
-is(
Y,^
(2,
2))
.
211001506

Out
put
:

Resul
t:

Thust
hequer
iesar
eent
eredandt
hepr
ogr
am i
sper
for
medsuccessf
ull
y
211001506

Exptno:
2e OWNPETANDLOVETABLE

Dat
e:

Ai
m:Towr
it
efact
s,r
ulesandquer
iesf
orownpetandl
ovet
abl
eandper
for
mthepr
ogr
am

Pr
ocedur
e:

St
ep1:
OpenNot
epad

St
ep2:
Ent
ert
henecessar
yfact
sandr
ulesandsav
ethef
il
ewi
th

ext
ensi
on.
plSt
ep3:
OpenPr
ologandcompi
l
ethef
il
e

St
ep4:
Runt
hef
il
eandent
ert
hequer
iest
ogett
he

out
putSt
ep5:
Displ
ayt
her
esul
t

Fact
s:

cat
(f
ubby
).

bl
ack
_spot
s(f
ubby
).

dog(
fi
gar
o).

whi
te_
spot
s(f
igar
o).

Rul
es:

owns(
mar
y,Pet
):
-cat
(Pet
),

bl
ack_
spot
s(Pet
).l
oves(
Who,
What
):
-

owns(
Who,
What
).

Quer
ies/Goal
s:

?
-

l
i
sti
ng(
cat
).

cat
(f
ubby
).

cat
(coby
).

?
-li
sti
ng(
owns)
.

owns(
mar
y,A):
-cat
(A)
,bl
ack_
spot
s(A)
.

?
-lov
es(
Who,
What
).

?
-owns(
mar
y,_
).
211001506

Out
put
:

Resul
t:

Thust
hef
act
s,r
ulesandquer
iesar
eent
eredandt
hepr
ogr
am i
sper
for
medsuccessf
ull
y
211001506

Exptno:
2f CARTABLE

Dat
e:

Ai
m:Towr
it
efact
sandquer
iesf
orcart
abl
eandper
for
mthepr
ogr
am

Pr
ocedur
e:

St
ep1:
OpenNot
epad

St
ep2:
Ent
ert
henecessar
yfact
sandr
ulesandsav
ethef
il
ewi
th

ext
ensi
on.
plSt
ep3:
OpenPr
ologandcompi
l
ethef
il
e

St
ep4:
Runt
hef
il
eandent
ert
hequer
iest
ogett
he

out
putSt
ep5:
Displ
ayt
her
esul
ts

Fact
s:

owns(
jack,
car
(bmw)
).

owns(
john,

car
(chev
y))
.

owns(
oli
vi
a,

car
(ci
vi
c))
.owns(
jane,

car
(chev
y))
.

sedan(
car
(bmw)
).

sedan(
car
(ci
vi
c))
.

t
ruck(
car
(chev
y))
.

Quer
ies:

?
-owns(
john,
X).

?
-owns(
john,
_).

?
-owns(
Who,
car
(chev
y))
.

?
-owns(
jane,
X),
sedan(
X).

?
-owns(
jane,
X),
truck(
X).
211001506

Out
put
:

Resul
t:

Thust
hef
act
sandquer
iesar
eent
eredandt
hepr
ogr
am i
sper
for
medsuccessf
ull
y
211001506

Exptno:
3a FAMI
LYRELATI
ONSHI
P

Dat
e:

Ai
m:Towr
it
efact
s,r
ulesandquer
iesf
orf
ami
l
yrel
ati
onshi
ptabl
eandper
for
mthepr
ogr
am

Pr
ocedur
e:

St
ep1:
OpenNot
epad

St
ep2:
Ent
ert
henecessar
yfact
sandr
ulesandsav
ethef
il
ewi
th

ext
ensi
on.
plSt
ep3:
OpenPr
ologandcompi
l
ethef
il
e

St
ep4:
Runt
hef
il
eandent
ert
hequer
iest
ogett
he

out
putSt
ep5:
Displ
ayt
her
esul
ts

Fact
s:

f
emal
e(pammi
).

f
emal
e(l
i
zza)
.

f
emal
e(pat
ty)
.

f
emal
e(anny
).

mal
e(j
i
mmy
).

mal
e(bobby
).

mal
e(t
omy
).

mal
e(pi
tt
er)
.

par
ent
(pammi
,
bobby
).

par
ent
(t
omy
,bobby
).

par
ent
(t
omy
,l
iz
za)
.

par
ent
(bobby
,anny
).

par
ent
(bobby
,pat
ty)
.

par
ent
(pat
ty,
j
immy
).

par
ent
(bobby
,pi
tt
er)
.

par
ent
(pi
tt
er,
j
immy
).

mot
her
(X,
Y):
-par
ent
(X,
Y),
femal
e(X)
.

f
ather
(X,
Y):
-par
ent
(X,
Y),
mal
e(X)
.
211001506

haschi
l
d(X)
:-par
ent
(X,
_)
.

si
ster
(X,
Y):
-par
ent
(Z,
X),
par
ent
(Z,
Y),
femal
e(X)
,X\
==Y.

br
other
(X,
Y):
-par
ent
(Z,
X),
par
ent
(Z,
Y),
mal
e(X)
,X\
==Y.
Quer
ies:

?
-par
ent
(X,
sam)
.

?
grandf
ather
(X,
Y).

Out
put
:

Resul
t:

Thust
hef
act
s,r
ulesandquer
iesar
eent
eredandt
hepr
ogr
am i
sper
for
medsuccessf
ull
y
211001506

Exptno:
3b SI
MPLEFACTSANDQUERI
ES

Dat
e:

Ai
m:Towr
it
efact
sandquer
iesandper
for
mthepr
ogr
am

Pr
ocedur
e:

St
ep1:
OpenNot
epad

St
ep2:
Ent
ert
henecessar
yfact
sandr
ulesandsav
ethef
il
ewi
th

ext
ensi
on.
plSt
ep3:
OpenPr
ologandcompi
l
ethef
il
e

St
ep4:
Runt
hef
il
eandent
ert
hequer
iest
ogett
he

out
putSt
ep5:
Displ
ayt
her
esul
ts

Fact
s:

Ram l
i
kesmango.

Seemai
sagi
rl
.

Bi
l
lli
kesCi
ndy
.

Rosei
sred.

Johnownsgol
d.

Cl
auses:

l
i
kes(
ram

,
mango)
.

gi
rl
(seema)
.

r
ed(
rose)
.

l
i
kes(
bil
l,
ci
ndy
).

owns(
john,
gol
d).

Quer
ies:

?
-l
ik
es(
ram,
What
).

?
-l
ik
es(
Who,
ci
ndy
).

?
-red(
What
).

?
-owns(
Who,
What
).
211001506

Out
put
:

Resul
t:

Thust
hef
act
s,r
ulesandquer
iesar
eent
eredandt
hepr
ogr
am i
sper
for
medsuccessf
ull
y
211001506

Exptno:
3c CONVERTCENTI
GRADETOFAHRENHEI
T

Dat
e:

Ai
m:Towr
it
efact
s,r
ulesandquer
iesandper
for
mthepr
ogr
am

Pr
ocedur
e:

St
ep1:
OpenNot
epad

St
ep2:
Ent
ert
henecessar
yfact
sandr
ulesandsav
ethef
il
ewi
th

ext
ensi
on.
plSt
ep3:
OpenPr
ologandcompi
l
ethef
il
e

St
ep4:
Runt
hef
il
eandent
ert
hequer
iest
ogett
he

out
putSt
ep5:
Displ
ayt
her
esul
ts

Fact
s:

fi
sc*9/5+32.

f<=32.

Rul
es:

c_
to_
f(C,
F):
-

Fi
sC*9/5+32.

f
reezi
ng(
F):
-

F=<32.

Quer
ies:

?
-c_
to_
f(100,
X).

?
-fr
eez
ing(
15)
.

?
-fr
eez
ing(
45)
.

Out
put
:

Resul
t:

Thust
hef
act
s,r
ulesandquer
iesar
eent
eredandt
hepr
ogr
am i
sper
for
medsuccessf
ull
y
211001506

Exptno:
4 MONKEYBANANAPROBLEM

Dat
e:

Ai
m:Towr
it
eapr
ologpr
ogr
am t
osol
vemonkeybananapr
obl
em

Pr
ocedur
e:

St
ep1:
OpenNot
epad

St
ep2:
Ent
ert
hecl
ausesandsav
ethef
il
ewi
th

ext
ensi
on.
plSt
ep3:
Openpr
ologandcompi
l
ethef
il
e

St
ep4:
Runt
hef
il
eandent
ert
hequer
iest
ogett
heout
put

St
ep5:
Displ
ayt
her
esul
t

Pr
ogr
am:

mov
e(st
ate(
middl
e,
onbox
,mi
ddl
e,
hasnot
)

,
grasp,

st
ate(
middl
e,
onbox
,mi
ddl
e,
ha

s)
).mov
e(st
ate(
P,onf
loor
,P,
H),

cl
i
mb,

st
ate(
P,onbox
,P,
H))
.

mov
e(st
ate(
P1,
onf
loor
,P1,
H)

,
drag(
P1,
P2)
,

st
ate(
P2,
onf
loor
,P2,
H))
.

mov
e(st
ate(
P1,
onf
loor
,B,
H),

wal
k(P1,
P2)
,

st
ate(
P2,
onf
loor
,B,
H))
.

canget
(st
ate(
_,
_,
_,
has)
).

canget
(St
ate1):
-
211001506

mov
e(St
ate1,
_,
Stat
e2)
,

canget
(St
ate2)
.

Out
put
:

Resul
t:

Thus,
thepr
ogr
am i
sent
eredandexecut
edsuccessf
ull
y
211001506

Exptno:5 MEDI
CALDI
AGNOSI
S

Dat
e:

Ai
m:

Towrit
eaprol
ogprogram formedi
cal
diagnosi
sandshowt
headv
ant
ageand
di
sadv
antageofgr
eenandredcuts.

Al
gor
it
hm:

St
ep1:
OpenNot
epad

St
ep2:
Ent
ert
hecl
ausesandsav
ethef
il
ewi
th

ext
ensi
on.
plSt
ep3:
Openpr
ologandcompi
l
ethef
il
e

St
ep4:
Runt
hef
il
eandent
ert
hequer
iest
ogett
heout
put

St
ep5:
Displ
ayt
her
esul
t

Pr
ogr
am:

go:-
hypothesis(
Disease)
,
writ
e('
˜Itissuggestedthatt
hepati
enthas'
),
writ
e(Disease),
nl
,
undo;
writ
e('
˜Sor r
y,thesyst
em isunabl
etoident
if
ythedi
seaes'
),
nl
,undo.

hypothesis( cold):-
symptom( headache) ,
symptom( runny _
nose)
,symptom( sneez i
ng),
symptom( sor e_ t
hroat
)
,nl
,
writ
e('
Adv icesand
Sugestions') ,
nl,
writ
e('
1: Tylenol '
),
nl,
writ
e('
2:
Panadol '
),nl,
writ
e('
˜3: Nasa
spray'
),nl,
writ
e('
Pleasewearwar m cl
oths
because'),nl ,
!
.
hypothesis( i
nfluenza):
-
symptom( sor e_ t
hroat
),
symptom( fev er),
symptom( headache) ,
symptom( chi ll
s),
211001506

symptom( body _
ache),
nl
,
wri
te('
˜Adv icesand
Sugesti
ons' ),nl,
wri
te('
1:
Tamifl
u'),nl,
wri
te('
2:
Panadol'
),nl,
wri
te('
3:Zanami v
ir'
),
nl
,
wri
te('
Pleaset akeawar m bat
handdosal
tgar
gli
ng
because')
, nl,
!.

hypothesis( typhoid):
-
symptom( headache) ,
symptom( abdomi nal
_pain)
,
symptom( poor _appeti
te),
symptom( fever),
nl
,
writ
e('
˜ Adv icesand
Sugestions' ),nl
,
writ
e('
1: Chl orampheni col'
),
nl
,
writ
e('
˜ 2:
Amoxi cill
i
n') ,
nl,
writ
e('
3:
Cipr
ofloxaci n'),nl
,
writ
e('
4:
Azit
hromy cin'),
nl,
writ
e('
Pl easedocompl etebedr
estandt
akesof
tdi
et
because' )
,nl ,
!.

hypothesis(chi cken_pox):
-
symptom( rash) ,
symptom( body _ache),
symptom( fev er),
nl,
writ
e('
Adv icesand
Sugestions:'
), nl,
writ
e('
1: Varicel l
av acci
ne'
),
nl,
writ
e('
2:
Immunogl obul in')
, nl
,
writ
e('
3:
Acetomenaphi n'
),nl,
writ
e('
4: Acycl ovir'
),
nl,
writ
e('
Pleasedohav eoatmeal
bat
handst
ayathome
because'),nl.
hypothesis(measl es):-
211001506

symptom( fever),
symptom( runny_nose),
symptom( rash),
symptom( conjunct i
vi
ti
s),
nl,
wri
te('
Adv i
cesand
Sugesti
ons: '
),nl,
wri
te('
1:Ty l
enol '
),
nl
,
wri
te('
2:Alev e'
),
nl
,
wri
te('
3:Adv il'
),
nl
,
wri
te('
4:Vitami nA' )
,
nl
,
wri
te('
Pleasegetr estandusemor
eli
qui
d
because')
,nl,!.

hypothesi s(mal ari


a)
:-sympt om( f ev er),
sympt om( sweat i
ng),
sympt om( headache)
,sympt om( nausea) ,
sympt om( vomi ting),
sympt om( diar rhea),
nl,
writ
e('˜Adv i
cesand
Sugest i
ons' ), nl,
writ
e('1:
Aralen')
, nl,
writ
e('˜2:
Qualaqui n'),nl ,
writ
e('˜3:
Plaquen' ),nl,
writ
e('˜4:
Mef l
oqui n'),nl ,
writ
e('˜Pleasedonotsl eepi
nopenai
randcov
ery
ourf
ull
ski
n
becaue'), nl,
!.
hypothesi s(mal ar i
a)
:
-sy mpt om( fev er),
sympt om( sweat i
ng)
,
sympt om( headach
e),
sympt om( nausea) ,
sympt om( vomi ting)
,
sympt om( diar rhea),
nl,
writ
e('Adv icesand
Sugest i
ons: '
), nl,
writ
e('1:
Aralen')
, nl,
211001506

write('
2:
Qual aquin'
), nl
,
write('
3:
Plaqueni l
')
, nl,
write('
4:
Mef loquine'),nl
,
write('
Pleasedonotsl eepi
nopenai
randcov
ery
ourf
ull
ski
n
because' ),
nl ,
!.
ask(Quest ion):-
write('
Doest hepati
enthasthesy
mptom')
,
writ
e( Quest i
on),
write('
?: '
),
read(Response) ,
nl,
((Response==y es;Response==y)
->
asser t
(yes(Quest i
on));
asser t
(no(Quest i
on)),
fail
).
:
-dy nami cy es/1,
no/1.

sy mptom(S):
-
(yes(S)
->
true;
(no(S)
->
fail;
ask(S)))
.

undo:-r
etr
act
(yes(
)),
fai
l
.undo:
-ret
ract
(no(
)),
fai
l
.
undo.
211001506

Out
put
:

Resul
t:

Thus,
thepr
ogr
am i
sent
eredandexecut
edsuccessf
ull
y
211001506

Exptno:6 N-
QUEENPROBLEM

Dat
e:

Ai
m:Towr
it
eapr
ologpr
ogr
am t
osol
ve4-
Queenpr
obl
em.

Al
gor
it
hm:

St
ep1:
OpenNot
epad

St
ep2:
Ent
ert
hecl
ausesandsav
ethef
il
ewi
th

ext
ensi
on.
plSt
ep3:
Openpr
ologandcompi
l
ethef
il
e

St
ep4:
Runt
hef
il
eandent
ert
hequer
iest
ogett
heout
put

St
ep5:
Displ
ayt
her
esul
t

Pr
ogr
am:

commands:-
wri
te(
"Li
stofcommands: "
),nl
,nl
,
wri
te(
"pr
int
_coor
dinates"
),nl
,
wri
te(
"pr
int
_boar
d"),nl
,
nl
.

pri
nt_coordi
nates:
-
solut
ion(
A),
displ
ay_coordi
nat
es(
A,
1).

pri
nt_board:-
sol
ution(A),
pri
nt_boundry,
pri
nt_columns(A,1),
pri
nt_col
umn_ l
ett
ers,
nl
.

pr
int
_square:-write("
|")
.
pr
int
_queen:-write("|
Q")
.
pr
int
_end_quare:-wr i
te(
"|"
),nl.
pr
int
_boundry:-wr i
te(
" ")
,nl
.
pr
int
_col
umn_ let
ter s:
-write("hgfedcba"
),nl
.

pri
nt_columns([]
,_).
pri
nt_columns([H|
T],C):-
wri
te(C),writ
e("")
,
pri
nt_row(H,1),
C1isC+1,
pri
nt_columns(T,C1).

pr
int
_row(_,9):-
pr
int
_end_quare,
pr
int
_boundry,!.
pr
int
_row(X,Col):-
211001506

XisCol ,
print
_queen,
Col1isCol +1, pr
int
_row(
X,Col
1).
pri
nt_row(X,Col):-
X=\ =Col,pri
nt_squar
e,
Col1isCol +1, pr
int
_row(
X,Col
1).

soluti
on(Queens):-
permutati
on([
1,2,
3,
4,5,
6,
7,
8],
Queens),saf
e(Queens)
.

per
mutation([
],
[
]).
per
mutation([
H|T],P):
-
permutation(
T,P1),
my del
ete(H,P,P1).

my
del
ete(
X,[
X|L]
,L)
.
my
del
ete(
X,[
Y|L]
,[
Y|L1]
):-
my
del
ete(
X,L,
L1).

safe(
[]
).
safe(
[Queen|Ot
hers]
):-
safe(
Others)
,
noatt
ack(Queen,
Other
s,1)
.

noat
tack(_,
[]
,
_ )
.
noat
tack(Y,[Y1|L]
,X1):
-
Y1-Y=\ =X1,
Y-Y1=\ =X1,
X2isX1+1,
noattack(Y,L,X2).

display_coordinates([
],9).
display_coordinates([
H|T],Y):
-nth1( H,[h,
g,
f,e,d,
c,b,
a],X),
wr i
te("
[")
,wr i
te(X),
wr i
te(Y),wri
te("]"
),nl
,
Y1i sY+1,
display_coordinates(
T, Y1)
.
211001506

Out
put
:

Resul
t:

Thus,
thepr
ogr
am i
sent
eredandexecut
edsuccessf
ull
y
211001506

Exptno:7 TRAVELLI
NGSALESMANPROBLEM

Dat
e:

Ai
m:Towr
it
eapr
ologpr
ogr
am t
osol
vet
rav
eli
ngsal
esmanpr
obl
em.

Al
gor
it
hm:

St
ep1:
OpenNot
epad

St
ep2:
Ent
ert
hecl
ausesandsav
ethef
il
ewi
th

ext
ensi
on.
plSt
ep3:
Openpr
ologandcompi
l
ethef
il
e

St
ep4:
Runt
hef
il
eandent
ert
hequer
iest
ogett
heout
put

St
ep5:
Displ
ayt
her
esul
t

Pr
ogr
am:

road(birmi ngham, br i
st ol,9) .
road(london, bir
mi ngham, 3).
road(london, bri
st ol, 6).
road(london, plymout h, 5).
road(plymout h,
london, 5).
road(portsmout h,london, 4).
road(portsmout h,pl ymout h,8) .
get_road( Star t,End, Visi t
ed, Resul t):-
get_ r
oad( Start,End, [Star t
],0, Visited,Result
).
get_road( Star t,End, Way poi nts, DistanceAcc, Vi
sit
ed,
Tot al
Di stance): -r oad( St art, End, Dist
ance),
reverse( [End| Way poi nt s],Vi sit
ed) ,
Tot al
Di stancei sDi st anceAcc+
Distance.
get_road( Star t,End, Way poi nts, DistanceAcc, Vi
sit
ed,
Tot al
Di stance): -r oad( St art, Way point,Dist
ance),
\+member (Way poi nt ,
Way points),
NewDi stanceAcci sDi st anceAcc+Di st
ance,
get_ r
oad( Way poi nt,End, [
Way point |
Way poi
nts]
,NewDi
stanceAcc,
Visi
ted,
TotalDistance) .
211001506

Out
put
:

Resul
t:

Thus,
thepr
ogr
am i
sent
eredandexecut
edsuccessf
ull
y
211001506

Exptno:8 WATERJUGPROBLEM

Dat
e:

Ai
m:Towr
it
eapr
ologpr
ogr
am t
osol
vewat
erj
ugpr
obl
em.

Al
gor
it
hm:

St
ep1:
OpenNot
epad

St
ep2:
Ent
ert
hecl
ausesandsav
ethef
il
ewi
th

ext
ensi
on.
plSt
ep3:
Openpr
ologandcompi
l
ethef
il
e

St
ep4:
Runt
hef
il
eandent
ert
hequer
iest
ogett
heout
put

St
ep5:
Displ
ayt
her
esul
t

Pr
ogr
am:

mi
n(X,
Y,X)
:-
X<Y,
!
.
mi
n(_,
Y,
Y).

r
ev(L,
R):
-r
ev acc(L,
[]
,R).
r
evacc([
],
R,R):-
!.
r
evacc([
H|T],A,
R):-
revacc(
T,[
H|A]
,
R).

%Solvewat erjugpr oblem usi ngDFS


%X,Yar ei
nitialcontent s,Nx ,
Nyar efinalcontentsofjug1ofcapacity_andjug2ofcapaci
ty
Myr espectiv
elyaf t
erpour ingf r
om j
ug1i ntojug2
chk(_,
X,My ,
Y,Nx ,
Ny):-X>0, Y<My ,Eyi
sMy -
Y,min(X,Ey,
P),Nx
i
sX- P,NyisY+P.
%Given3j ugsofcapaci t
iesMx ,
My ,
Mzandf i
lledwithX,Y,Zunit
sofal i
quidr
espect
ivel
y,
giv
e
stepssot hatfinall
yt heycont ainFx,Fy,Fzunitsoftheliqui
drespecti
vely
.
j
ug(Mx ,My,Mz ,
X,Y,
Z,Fx ,
Fy,Fz )
:-
jug(Mx,
X,My ,
Y,Mz ,
Z,Fx
,Fy,
Fz ,
[
],
['
I
nit
ial
l
y']
).

j
ug(
_,
Fx,_,Fy
,_,
Fz,Fx,
Fy,Fz,
T,R) :
-
!
,
rev
([[
Fx,Fy,
Fz],
[Fx,
Fy ,
Fz]|
T],TR),r
ev (
['
Fi
nall
y '
|
R],
RR),
di
splay(TR,RR).
j
ug(
Mx ,
X,My ,
Y,Mz ,
Z,Fx,
Fy,Fz ,
T,R):
-chk(Mx,X,My
,Y,
Nx,Ny),
not(member (
[Nx ,
Ny,Z]
,T))
,
j
ug(Mx,Nx ,
My ,
Ny ,
Mz ,
Z,Fx,Fy,Fz,
[[
X,Y,Z]|
T],[
'
Pourl
iquidfr
om jug1i nt
ojug2'|R])
.
j
ug(
Mx ,
X,My ,
Y,Mz ,
Z,Fx,
Fy,Fz ,
T,R):
-chk(Mx,X,Mz
,Z,
Nx,Nz),
not(member (
[Nx ,
Y,Nz],
T))
,
j
ug(Mx,Nx ,
My ,
Y,Mz,Nz ,
Fx,Fy,Fz,
[[
X,Y,Z]|
T],[
'
Pourl
iquidfrom jug1int
oj ug3'
|R])
.
j
ug(
Mx ,
X,My ,
Y,Mz ,
Z,Fx,
Fy,Fz ,
T,R):
-chk(My,
Y,Mz,Z,
Ny,Nz),
not(
member (
[X,Ny,
Nz ]
,T))
,
j
ug(Mx,X,My,Ny,Mz,Nz ,
Fx,Fy,Fz,
[[
X,Y,Z]|
T],[
'
Pourl
iquidfrom jug2int
oj ug3'
|R])
.
j
ug(
Mx ,
X,My ,
Y,Mz ,
Z,Fx,
Fy,Fz ,
T,R):
-chk(My,
Y,Mx,X,
Ny,Nx),
not(member (
[Nx ,
Ny,Z]
,T))
,
j
ug(Mx,Nx ,
My ,
Ny ,
Mz ,
Z,Fx,Fy,Fz,
[[
X,Y,Z]|
T],[
'
Pourl
iquidfr
om jug2i nt
ojug1'|R])
.
j
ug(
Mx ,
X,My ,
Y,Mz ,
Z,Fx,
Fy,Fz ,
T,R):
-chk(Mz,Z,
Mx,X,
Nz,Nx),
not(member (
[Nx ,
Y,Nz],
T))
,
j
ug(Mx,Nx ,
My ,
Y,Mz,Nz ,
Fx,Fy,Fz,
[[
X,Y,Z]|
T],[
'
Pourl
iquidfrom jug3int
oj ug1'
|R])
.
j
ug(
Mx ,
X,My ,
Y,Mz ,
Z,Fx,
Fy,Fz ,
T,R):
-chk(Mz,Z,
My,Y,
Nz,Ny),
not(
member (
[X,Ny,
Nz ]
,T))
,
j
ug(Mx,X,My,Ny,Mz,Nz ,
Fx,Fy,Fz,
[[
X,Y,Z]|
T],[
'
Pourl
iquidfrom jug3int
oj ug2'
|R])
.
211001506

di
spl
ay(
[]
,[
])
:-
!.
di
spl
ay(
[T1|
T],
[R1|
R])
:-
wri
te(
R1)
,wr
it
e('
:')
,wr
it
e(T1)
,nl
,
displ
ay(
T,R)
.

Out
put
:

Resul
t:

Thus,
thepr
ogr
am i
sent
eredandexecut
edsuccessf
ull
y
211001506

Exptno:9 I
MPLEMENTATI
ONSOFLI
NEARREGRESSI
ON

Dat
e:

Ai
m:ToWr
it
eapy
thonpr
ogr
am t
oimpl
ementl
i
nearr
egr
essi
on.

Al
gor
it
hm:

St
ep1:
OpenPy
thonnot
ebook

St
ep2:
impor
tthenecessar
ypackages

St
ep3:
per
for
m di
ff
erentoper
ati
onsandv
iewt
hedat
asetby

i
mpor
ti
ngi
tSt
ep4:
toper
for
mli
nearr
egr
essi
on,
impor
tthe

model

St
ep5:
Displ
ayt
her
esul
t

Pr
ogr
am:

i
mpor
tpandasas
pdi
mpor
tnumpy
asnp
i
mpor
tmat
plot
li
b.py
plotaspl
t
%mat
plot
li
binl
i
ne
dat
aset=
pd.
read_
csv
('
/cont
ent
/st
udent
_scor
es.
csv
')
dat
aset
.pl
ot(
x='
Hour
s',
y='
Scor
es'
,st
yle='
o'
)
pl
t.
ti
tl
e('
Hour
svsPer
cent
age'
)
pl
t.
xlabel
('
Hour
sSt
udi
ed'
)
pl
t.
yl
abel
('
Per
cent
age
Scor
e')pl
t.
show(
)
X=dat
aset
.i
loc[
:,
:-
1]
.val
uesy=dat
aset
.i
loc[
:,
1]
.val
ues
f
rom skl
ear
n.model
_sel
ect
ioni
mpor
ttr
ain_
test
_spl
i
t
X_
trai
n,X_
test
,y_
trai
n,y
_test=t
rai
n_t
est
_spl
i
t(X,
y,t
est
_si
ze=0.
2,
r
andom_
stat
e=0)f
rom skl
ear
n.l
i
near
_model
impor
tLi
near
Regr
essi
on
r
egr
essor=Li
near
Regr
essi
on(
)
r
egr
essor
.f
it
(X_
trai
n,y
_tr
ain)
pr
int
(r
egr
essor
.i
nter
cept
_)
pr
int
(r
egr
essor
.coef
_)
211001506

y
_pr
ed=r
egr
essor
.pr
edi
ct(
X_t
est
)
df=pd.
Dat
aFr
ame(
{'
Act
ual
'
:y_
test
,'
Predi
cted'
:y_
pred}
)
211001506

Out
put
:
211001506

Resul
t:

Thust
hepr
ogr
am i
sent
eredandexecut
edsuccessf
ull
y
211001506

Exptno:10 I
MPLEMENTATI
ONSOFCLASSI
FICATI
ON

Dat
e:

Ai
m:ToWr
it
eapy
thonpr
ogr
am t
oimpl
ementdi
ff
erentcl
assi
fi
cat
ionwi
tht
hedat
aset
.

Al
gor
it
hm:

St
ep1:
OpenPy
thonnot
ebook

St
ep2:
impor
tthenecessar
ypackages

St
ep3:
per
for
m di
ff
erentoper
ati
onsandv
iewt
hedat
asetby

i
mpor
ti
ngi
tSt
ep4:
per
for
m necessar
ycl
assi
fi
cat
iont
echni
ques

byi
mpor
ti tSt
ngi ep5:
Displ
ayt
her
esul
t

Pr
ogr
am:

%mat plotlibinline
i
mpor tpandasas
pd
i
mpor tmat plot l
ib.pyplotaspl t
fruits=pd. read_ excel('C:\\ User s\ \Lenov o\\Desktop\ \
fruit
s.xl
sx'
)
fruits.head( )
print(fruits.shape)
print(fruits['
f r
uit_name' ].uni que( ))
print(fruits.groupby ('
fr
ui t_name' ).size())
i
mpor tseabor nassns
sns. count plot(frui
ts['
fruit_name' ],l
abel ="Count")
plt.show( )
fruits.drop( 'frui
t _l
abel',axi s=1) .plot(kind='box'
, subplots=True,l
ayout
=(2,
2),
shar ex=Fal se, shar ey=Fal se, f
igsi ze=(9,9),
ti
tle=' BoxPl otf oreachi nputvariabl
e')
plt.sav efig('
f r
uits_box '
)
plt.show( )
i
mpor tpy labaspl
fruits.drop( 'frui
t _l
abel',axi s=1) .hist(bins=30, f
igsize=(9,9)
)
pl.supt itl
e( "
Hi stogr am foreachnumer i
cinputv ariabl
e")
plt.sav efig('
f r
uits_hist'
)
plt.show( )
feat ure_ names=[ '
mass' ,'wi dth','height',
'
col or_ scor e']X=f ruit
s[f eat ure_ names]
y=f ruits['
fruit_label']
from skl ear n.model _
sel ect ioni mpor ttrai
n_test_spli
t

X_trai
n,X_test,
y_trai
n,y_test=t r
ain_test_
spli
t(
X,y
,
random_ st
ate=0)from sklearn.pr
epr ocessi
ngimport
MinMaxScal er
scaler=MinMax Scaler(
)
X_trai
n=scal er.
fi
t_tr
ansform( X_t
rain)
X_test=scaler.
transfor
m( X_test)
211001506

f
rom skl
ear
n.l
i
near
_model
impor
tLogi
sti
cRegr
essi
on

l
ogr
eg=Logi
sti
cRegressi
on(
)
l
ogr
eg.
fi
t(
X_t
rai
n,y_
train)

pri
nt(
'AccuracyofLogi
sti
cregressi
oncl assi
fi
eront
rai
ningset:{
:.
2f}
'
.f
ormat (
logreg.
scor
e(X_t
rai
n,y_tr
ain)))
pri
nt(
'AccuracyofLogi
sti
cregressi
oncl assi
fi
eront
estset:
{:.
2f}
'
.f
ormat (
logreg.
scor
e(X_t
est,y_
test))
)
fr
om sklearn.
treei
mport

Deci
sionTr
eeCl
assi
fi
ercl
f=

Deci
sionTr
eeCl
assi
fi
er(
).
fi
t(
X_t
rai
n,y
_tr
ain)

pri
nt(
'AccuracyofDeci sionTreecl assifi
erontrai
ningset:{
:.2f}
'
.f
ormat (
clf.score(X_trai
n, y
_ tr
ain)))
pri
nt(
'AccuracyofDeci sionTreecl assifi
erontestset:{
:.
2f}
'
.f
ormat (
clf.score(X_test,y_test))
)
cl
f2=Deci sionTr eeClassif
ier(
max_ depth=3).
fi
t(
X_trai
n,y_
train)
pri
nt(
'AccuracyofDeci sionTreecl assifi
erontrai
ningset:{
:.2f}
'
.f
ormat (
clf2.score(X_train,y_tr
ain)))
pri
nt(
'AccuracyofDeci sionTreecl assifi
erontestset:{
:.
2f}
'
.f
ormat (
clf2.score(X_test,y_test)))
fr
om sklearn.neighbor simpor tKNei ghborsCl
assi
fi
er

knn=KNei ghborsClassif
ier
()
knn.fi
t(X_tr
ain,y_tr
ain)
print(
'AccuracyofK- NNclassi
fi
erontrai
ningset
:{:
.
2f}
'
.f
ormat (
knn.score(X_t
rai
n,y_t
rai
n))
)
print(
'AccuracyofK- NNclassi
fi
erontestset:
{:
.2f}'
.f
ormat (
knn.score(X_t
est,
y_test))
)from sklearn.
svmimpor t
SVC

svm =SVC( )
svm.fit(X_t
rain,y_t
rain)
pri
nt('
Accur acyofSVM cl assifi
eront rai
ningset:{
:.
2f}
'
.f
or mat (
sv m.score( X_t
rain,y_
train))
)
pri
nt('
Accur acyofSVM cl assifi
eront estset:
{:.
2f}
'
.f
or mat (
sv m.score( X_t
est,y_t
est ))
)
fr
om skl earn.metri
csi mpor t
cl
assificati
on_ r
epor tfrom sklearn.metri
cs
i
mpor tconf usion_mat rixpred=
knn.
pr edict
(X_ t
est)
pri
nt(confusion_mat rix(y_
test,pred))
pri
nt(classi
ficati
on_ report(
y_ t
est,pred))
k_r
ange=r ange( 1,20)
scores=[ ]

f
orki nk_r
ange:
knn=KNeighborsClassi
fi
er(
n_nei
ghbor
s=k)
knn.
fi
t(
X_tr
ain,
y_train)
211001506

scor
es.
append(
knn.
scor
e(X_
test
,y_
test
))
211001506

pl
t.
fi
gure()
pl
t.
xl
abel (
'
k')
pl
t.
yl
abel('
accur acy'
)
pl
t.
scatter(k_r
ange,scores)
pl
t.
xti
cks([0,5,
10,15,
20]
)

Out
put
:
211001506
211001506
211001506

Resul
t:

Thus,
thepr
ogr
am i
sent
eredandexecut
edsuccessf
ull
y
211001506

Exptno:11 I
MPLEMENTATI
ONSOFNEURALNETWORKS

Dat
e:

Ai
m:ToWr
it
eapy
thonpr
ogr
am t
oimpl
ementasi
mpl
eNeur
alNet
wor
k.

Al
gor
it
hm:

St
ep1:
OpenPy
thonnot
ebook

St
ep2:
impor
tthenecessar
ypackages

St
ep3:
per
for
m di
ff
erentoper
ati
onsandv
iewt
hedat
asetby

i
mpor
ti
ngi
tSt
ep4:
per
for
m necessar
ycl
assi
fi
cat
iont
echni
ques

byi
mpor
ti tSt
ngi ep5:
Displ
ayt
her
esul
t

Progr am:
i
mpor tnumpyasnp
fr
om sci py
.speciali
mpor texpi
tas
acti
v ati
on_functi
onfrom scipy
.st
atsi
mport
tr
uncnor m
deftruncated_normal(mean=0,sd=1,
low=0,
upp=10): ret
urntr
uncnor m(
(low-mean)/sd, (upp-mean)/sd,l
oc=mean,
scale=sd)classNnetwor k:

defini t(self,
no_of_in_nodes,
no_of_out _nodes,
no_of_hidden_ nodes,
l
earning_ rate):
sel
f.no_of_in_ nodes=no_ of_
in_nodes
sel
f.no_of_out _ nodes=no_ of_out_
nodes
sel
f.no_of_hi dden_ nodes=
no_of_hidden_ nodesself.
learni
ng_rat
e=
l
earning_ratesel f.
creat
e_weight_matr
ices(
)

defcreate_weight_ mat r
ices(
self)
:
"""Amet hodt oinit i
ali
zethewei ghtmat ri
cesoft heneural
network"""rad=1/np. sqrt
(self.
no_of_in_nodes)
X=t runcated_nor mal (mean=0, sd=1,low=-rad,upp=rad)
self.
weights_i
n_ hidden=X. r
vs((self
.no_of_hidden_nodes,
self.
no_of_i
n_nodes))
rad=1/
np.sqrt
(self.
no_ of_ hi
dden_nodes)
X=t runcated_nor mal (mean=0, sd=1,low=-rad,upp=rad)
self.
weights_hidden_ out=X. rv
s( (
self.
no_of_out_nodes,
sel
f.no_
of_hidden_nodes) )

deftr
ain(
sel
f,i
nput
_vect
or,
tar
get_
vector
):
pass#Moreworkisneededtotr
ainthenet
wor
k
211001506

defrun(
self,
input
_vect
or)
:
"
""
r
unningthenetworkwithaninputvector'
i
nput_vect
or'
.
'
input
_vector
'canbetuple,
li
storndar r
ay
"
""
i
nput_vect
or=np.arr
ay(i
nput_v
ector,ndmin=2)
.T

i
nput_
hidden=acti
vati
on_f
uncti
on(self
.weights_i
n_hi
dden@ i
nput
_vect
or)
out
put_vect
or=acti
vat
ion_
funct
ion(sel
f.weights_
hidden_
out@
i
nput_
hidden)r
etur
noutput_v
ector

si
mpl
e_net
wor
k=Nnetwork(no_
of _
in_
nodes=2,
no_
of_out_
nodes=2,
no_
of_hi
dden_nodes=4,
l
earni
ng_r
ate=0.6)

si
mpl
e_net
wor
k.r
un(
[(
3,4)
])

Out
put
:

Resul
t:

Thus,
thepr
ogr
am i
sent
eredandexecut
edsuccessf
ull
y

You might also like