0% found this document useful (0 votes)
10 views7 pages

Adobe Scan 30-Sept-2024

Uploaded by

srisowmya
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)
10 views7 pages

Adobe Scan 30-Sept-2024

Uploaded by

srisowmya
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/ 7

2292408 0036 - k-Sreekanth MBA CDS)

Machine learning Using Python - 22Ds 6| 09a


END SEM AB EX AMINATIO NS

9.) Write the Python code foY the


Logistc Reqression agorthm
A: mport numpy as np
class Logishi cReqvessien
det int(self learnig rate
=l000);
-ooliterotiors
Self. lenning rate :
set. iteyations =
terotions
learning_tate
self. voeiqhts Nme
Self bias = None
+ Sigmoid tunchion
des
Samoid (self 2):
Yeturnd (4np.exp(-z)
# Training the mode )
def fit (selt x,y:
# Number of
samples and deatures
n-gamples , nfeotures Xshape
+ Initialize welghts
and bias
Selt, selghts =
npZeras (n.featues
Self. bias =
# Gradient decent
for_ in rage (self. iterati ons):
# Lncar model z xtb
linear_ mo del =np. dot (X, slf oeight) +
Selfbías
ApPy slg mord to get predicti en.
Y- predicled - self . slgmoid Clirearmodel)
t compute grodients
dus e (itn- samplu+ np"xT,
(Y-predidedy
db: (uln.sampler np Sum CY -predr
# Updot we, hts and blas
ed-)
selt.waa hts : selt. learnirg-Tate +do
selfbos Self-learning atetdb
+ predicion tuncton
det predict Cseltx)
linear- nmodel = np -dot (x,se(f - oeights) +
Self. bias
Y- predided : Self. sig moid (ineor - model)
4 Return birarg ela predict
y- predicted_cls =se4i s0-s else otor
iin Y pedict ed ]
return nparray (Y- predict ed -ds)
# Example usaget
name ma'n
Geneating Some random binary elost
model selection
ai enon
Cati doto trom skleam
test- Splt from sklearn- datotek
import train -
t0ort make- clositotn from sklearn
etics import acuracy- Sore
metries
Vmake Claiiation (n.samples -D00
n_teaturel =lo, nclaes 2, rordom stote
=42)
Ntra'n, X_tety Y-traln, Ytest talntet

s pit (x,Y, tet size : 02,


Yandon State zl
2
Creating and taining the model
Ymodel logisthcReqresion (leoming-rate -ool,
iteroti en l00o)
model -fit(x_train, Y_train)
# Making preditions
predich on s model'predicd Xtet
4 Evalucting -the model
print (4" Accuraty ": faccuracy -sore
(1utet,
predichi ons )?°)
Output
0825
zs,50c08 +tom tromimport assodaed meoun model
thattralned ts
S Supervised ond 3,
tromImport
Anslgsls
CPcA) 4 1
deaning
sample sklearn. Derente
sklearnskleam. k-m Ursuperised lasb
train numpy ate
o, LA5, eans
labeled
.metris model-
Linear- tet label
Loth a In between the
, 0000], as
clustering,
np
earming Machine
L,, sele Supervised dilierente
(50, doto
importacturaly-score. model split. cion
L65 60003]. eample
training s
000o), labeled
dta Sugrvsed
, LAge, : im leaming
[3s, port princpa
L55, learning betoeen
[4o;ôoog) solag and
00oo), Logishe
Componen ohch the , unsutv Superme
Ragreu
y 0P aay (lo,0,0, l,1,t,, 1,t,1])
Lobels (o Not Buy, (: Buy)
t splttng the doto tnto raun and test ses
xtrain Ktet, Ytrain, Ytest- train_test
Split (X, Y, tet_sizez0 2
random- State2)
supervised ain: togisthe Ragpeui an model
medel tit (Ktraln Ytrain
coith labeled doto .

+preditirg and evolucting the model


Y_preda model- predic (ret)
prtnt("supeviscd leanlrg
n acuralysor
-AccuraC,
elYtest , Y-pred)3")
ole
Supervised heornlng - Acura ey i r0l'0
a: Unsuperv'sed
Tn thisthe
tsained dato that ha no nmodel is
abe ls. The
model ties to leam the
struchure or
potterns trom the data tself. Such as
clutering of di men sT onality
treduchien.
k-Means clustering
import umpy a np
tiom skearn. cluter import kMeans
in port nat plotli b PH plt as plf
+ sampleunlabeled data: [Age, Salay )
X=np-amy ([ 2s,
so00,[3o, Cocoo],
[50, 10000o),L55, l0ooo,(4o,t20g
es, 1300o0]., o,14
D006]1)
unsupervied learring3 : k-Means' clutesing
kmeans. kMeans( n_custers=2 andom stote
# Assuming 2 cut ers

KmeonsC
# eting the eluter labels.
clusters - hmeans- labe
print (+ Unsupervi's ed teoning -cater
Assignments : cluuters3)
# Vis ualistne the cuuters
ptt scatterlx L',o,als,), ezcuters,
Cmap'viridis)
plt xlabel ('Age)
plt- Ylabel 's
alary'
plt ttle (k-Means
)
etering (unsupervsed
leoming))
pltshoo)

Unlupervisd leomig - crter Assignmerfs :


[o00011)

You might also like