Module5 Dbscanv2
Module5 Dbscanv2
[1:import
]
numpyasnp
from
sklearn.clusterimportDBSCAN
fromsklearnimport
metrics
from
sklearn.datasetsimportmake_blobs
from
sklearnimport
metrics
fromsklearn.preprocessingimportLabelEncoder
importpandasas
pd
f rom
sklearn.preprocessingimport
StandardScalerimport
matplotlib.pyplotas
plt
from
matplotlib.colorsimport
ListedColormap
In [
2
] :#readthe
data
df
=
pd.read_csv'
(
ris.csv
i '
)
df.head()
0
5.1
3.5
1.4
0.2Setosa
1
4.9
3.0
1.4
0.2Setosa
2
4.7
3.2
1.3
0.2Setosa
3
4.6
3.1
1.5
0.2 Setosa
4
5.0
3.6
1.4
0.2Setosa
In
[
3
]
#select onlyfeature
:
columns
X=
df '
[[
sepal.length
'
,
'
s '
epal.width ,' '
petal.length '
, etal.width
p '
]
]
[4
n
I
]
:
sc
=
StandardScaler
()
Out
[4
] :
X
=
sc.fit_transform
(
X
)
X
:
[ 5
]
array
([
[
- 0.90068117
,
1.01900435
, -1.34022653, -1.3154443 ],
[-1.14301691
,
-0.13197948 , -1.34022653, -1.3154443],
[-1.38535265
, 0.32841405 ,
-1.39706395, -1.3154443
]
, [-1.50652052 , 0.09821729, -1.2833891
,
-1.3154443 ],
[
-
1.02184904
,
1.24920112
,
-1.34022653
,
-1.3154443]]
)
In
[5
]
:
ComputeDBSCAN
#
d =DBSCAN(
b ps
e =
0
.8,
min_samples = )
10 fit
. (X
)
core_samples_mask
=np.zeros_like(
db.labels_ ,dtype=
bool
)
core_samples_mask[
db.core_sample_indices_ ]=True
labels=
db.labels_
In
[6
]
:
labels
ده
ده
0,
Out
[6
]
:
1,
1
,
1
1
,
, 1
,
1
,
1
,
1
,
4444
array
([
0,0,0,
0
,0,
0
,0,0,0, 0,0,0,
-1 0,
,
1
,
1
,
1
,
1
,
1
,
1
,
1,
1
, -1,
1
,
1,
1
,
1
,
1
,
1
,
1,
1,
1
,
0
,
0
,0,0
0,
, 0
,
0,
ده
0
,
0,
0
,
0
,
0
,
0
,
0,
0,0,
0
,
-1
,
0
,
0
,
0,
0
,
1
,
1
,
PPP
1,
1
,
-1
,
1
,
1
,
लेते
1
,
PP
1
,
1
,
1.
1
,
1
, 1
,
1
,
PPPPP
1
,
1,
लेलेते
1,
1
,
1, 1
,
1
,
1
,
1,1,1,
1
,
1,
1
,
1
,
1
,
0
,
0
,
,0,
0
1
,
1
,
1
,
1
,
1,
1
,
,
1 1
,
1,
1,
1,
1
,
1
,
1,
1,
1
,
-1
,
-1
,
1
,
1
,
PP
1
,
लेते
1
,
-1,
1
,
1
,
1
,
1
,
1
,
1
,
],
1
1
,
1
,
1
,
1
,
1
,
,
-1
1
,
1
,
1
,
1
,
1
,
dtype
= )
int64
In
[7]
: le LabelEncoder
()
=
_actual
y
le.fit_transform
(
d
f'
[
ariety
v '
]
)
In
[8]
:
y_actual
Out
[8
]
:
array( ,0
[0
, 0,0,
0 0,0,
,
0,0,
0
, 0
,
0
, 0,
0
,0,0,
0
, 0,0,
0,
0
,0,0
0
, ,0,0,0
,0,0,
0,0,0,0,0
0,0,0,0,0, 0,0,0,0,0,
, ,0,
0 ,0
0
,0,0,
1
,
1
,
1
,
1
, 1
, 1
,
1
,
1,
1
,
1
,
1,
1
,
1
,
1
, 1,1
,
1
,
1
,
1
,
,1
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1,
1,
1,
1
,
1
,
1
,
1
,
1
,
,1
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
2
,
2
,
2
,
2
,
2
,
2
,
2
,
2
,
2
,
2
,
2
,
2
,
2
,
2
,
2
,
2
,
2
,
2
,
2
,
2
, 2,
2
,
2
,
2
,
2
,
2, 2
,
2
,
2
,
2
,
2
,
2
,
2
,
2,
2
,
2
,
2
,
2
,
2
,
2
,
2
,
2,
2,
2
,
2
,
2
,
2
,
2
,
2
,
2
]
)
n
I [
9
]
print(" Accuracy
:
:
"
,
metrics.accuracy_score
(
y
_actual
,
labels
))
print(
"Classificationreport
:
"
)
rint
p (
(
metrics.classification_report
y_actual
,
labels
))
Accuracy
:0.6466666666666666
Classificationreport :
precision
recall
f1
-
coresupport
s
-1
0.00
0.00
0.00
0
0
O
H
2
1.00
0.96
0.98
50
1
0.52
0.98
0.68
50
0.00
0.00
0.00
50
accuracy macroavg
weighted
avg
0.65
150
0.38
0.48
0.42
150
0.51
0.65
0.55
150
C :
\U
sers\R
ajeshA
\a
naconda3\envs\
tf
\
lib
\
s
ite -p
ackages\sklearn\m
etrics
\
_ :1
classification.py
34
4
:
UndefinedMetricWarning : Precision andF-
scoreare
ill
-
d
efined and being set
to inlabe
0.0 withno
ls
predicted samples .
zero_division`parameter
Usè
tocontrol thisbehavior.
_warn_prf(average
, ,
modifier msg_start
,
len(result
)
)
C : \U
sers\R
ajesh A\anaconda3\
nvs
e \
f
t \lib
\
s -p
ite
ackages\sklearn
\m
etrics
\
_
classification.py :1
34 4
:
UndefinedMetricWarning: Recall and score are
F
- ill
-
defined
and
being set
to
0.0
in
labels
with no true
samples
.
zero_division`parameter
Usè
tocontrolthisbehavior.
_warn_prf
(a
verage, modifier,msg_start
, (
len result
)
)
C
:
\U
sers\RajeshA\a
naconda3\e
nvs\
f
t \
lib
\
site-p
ackages\sklearn
\metrics
\
_
classification.py :1
344
:
UndefinedMetricWarning : Precision andF
-
scoreare
ill -d
efined
and beingset to
0.0
inlabe withno
ls
predicted samples. zero_division`parameter
Usè
to control thisbehavior.
_warn_prf
(a
verage
, ,
modifier msg_start, (
len esult))
r
C: \U
sers\
ajesh
R A\anaconda3\envs\
f
t \
l
ib \
site-
packages\sklearn
\
metrics
\
_classification.py
:
1
34 4
:
UndefinedMetricWarning
Recalland
: Fscoreare
- ill-
defined and beingset
to
0.0
in
labelswith
no
true .
samples zero_division`parameter
Usè
tocontrol
thisbehavior.
_warn_prf
(a
verage
, ,msg_start
modifier
, (
len esult
r )
)
C :
\Users\R
ajeshA\a
naconda3\envs\tf
\
lib
\
s -p
ite
ackages\s klearn
\
metrics
\
_classification.py: 1344
:
UndefinedMetricWarning:Precision and F
-
scoreare
ill
-
d
efined and beingset
to 0.0
in
labels withno
predicted samples .
zero_division`parameter
Usè
to control thisbehavior .
_warn_prf
(a
verage
,modifier, ,
msg_start len
(
r
esult
)
)
C : \U
sers\R
ajesh A
\ anaconda3\envs\tf\
ib
l \
s -p
ite
ackages\sklearn
\m
etrics
\
_ :1
classification.py
34 4
:
UndefinedMetricWarning: Recall and F
-
scoreare ill -
definedand beingsetto
0.0
in
labels
with notrue
samples
.
zero_division`parameter
Usè
tocontrol
thisbehavior.
_warn_prf
(a
verage
,
modifier
,
,len(
msg_start
result
)
)
In
[10
]
:
Setthe
# sizeofthe
plot
plt.figure
(
figsize
=
(10,7
))
#Createa
colormapfor
red,green and blue
cmap
=
ListedColormap 'r'
([
,'
g
' '
, '
b ])
#
Plot Sepal
(
plt.subplot
2 ,
2
,
1
)
(
lt.scatter
p f
d [
' '
sepal.length
],
df
[
' '
sepal.width ],
c
=
(y
cmap
_actual
),marker='0
'
,
s=
2
5
)
('
plt.xlabel
sepallength(cm)
'
)
lt.ylabel
p '
(
epalwidth
s
'
(cm) )
('
plt.title
epal (Actual)
S '
)
plt.subplot
(
2
,
2
,
2)
(
plt.scatter
f
d ['
'
sepal.length ,
] df
[
'
s '
epal.width ,
] c
=
c (
map abels
l ),marker=
' 0'
,
s
= 25)
plt.xlabel
(
' epallength(
s m)
c '
)
plt.ylabel
(
'
s
epalwidth (cm)
'
)
(
plt.title
'S )'
epal (Predicted
)
plt.show
()
In
[
]:
sepalwidth(cm)
4.0
3.5
3.0
2.5
Sepal(A
ctual)
Sepal(P
redicted)
4.5
4.5
sepalwidth(cm)
4.0-
3.5
3.0
2.5
2.0
2.0
4.5
5.0
5.5
6.06.5
7.5sepallength(cm)
7.0
8.0
4.5
5.0
5.5
6.0
6.5
7.0
7.5
8.0
sepallength(cm)