Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
Development project in Machine Learning_Group 9
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ZHANG Zuoyu
Development project in Machine Learning_Group 9
Merge requests
!2
Fonctions_version1.py
Code
Review changes
Check out branch
Download
Patches
Plain diff
Expand sidebar
Merged
Fonctions_version1.py
XiranZ
into
main
Overview
0
Commits
1
Pipelines
0
Changes
1
Merged
ZHANG Zuoyu
requested to merge
XiranZ
into
main
2 years ago
Overview
0
Commits
1
Pipelines
0
Changes
1
merge 1
0
0
Merge request reports
Compare
main
main (base)
and
latest version
latest version
e6cbeb63
1 commit,
2 years ago
1 file
+
59
−
0
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Fonctions.py
+
59
−
0
View file @ e6cbeb63
Edit in single-file editor
Open in Web IDE
Show full file
@@ -226,4 +226,63 @@ def test_decision_tree(data, max_depth):
plt
.
xlabel
(
'
False Positive Rate
'
)
plt
.
ylabel
(
'
True Positive Rate
'
)
plt
.
title
(
'
The ROC curve of the decision tree model
'
)
plt
.
show
()
class
Gaussian_NB
:
def
__init__
(
self
):
self
.
mean0
,
self
.
mean1
=
0
,
0
self
.
var0
,
self
.
var1
=
1
,
1
self
.
p0
,
self
.
p1
=
[
0
],[
0
]
def
fit
(
self
,
trainMatrix
,
trainCategory
):
numTrainData
=
len
(
trainMatrix
)
numFeatures
=
len
(
trainMatrix
[
0
])
p_c1
=
sum
(
trainCategory
)
/
float
(
numTrainData
)
self
.
mean0
=
np
.
mean
(
trainMatrix
[
trainCategory
==
0
],
axis
=
0
)
self
.
mean1
=
np
.
mean
(
trainMatrix
[
trainCategory
==
1
],
axis
=
0
)
self
.
var0
=
np
.
var
(
trainMatrix
[
trainCategory
==
0
],
axis
=
0
)
self
.
var1
=
np
.
var
(
trainMatrix
[
trainCategory
==
1
],
axis
=
0
)
return
self
def
_get_proba
(
self
,
testMatrix
):
p0Vect
=
((
2
*
np
.
pi
*
self
.
var0
)
**
0.5
)
*
np
.
exp
(
-
(
testMatrix
-
self
.
mean0
)
**
2
/
(
2
*
self
.
var0
**
2
))
p1Vect
=
((
2
*
np
.
pi
*
self
.
var1
)
**
0.5
)
*
np
.
exp
(
-
(
testMatrix
-
self
.
mean1
)
**
2
/
(
2
*
self
.
var1
**
2
))
p_condition0
=
reduce
(
operator
.
mul
,
p0Vect
.
T
)
p_condition1
=
reduce
(
operator
.
mul
,
p1Vect
.
T
)
self
.
p0
=
p_condition0
*
(
1
-
p_c1
)
self
.
p1
=
p_condition1
*
p_c1
return
def
predict
(
self
,
testMatrix
):
self
.
_get_proba
(
testMatrix
)
label
=
np
.
zeros
(
len
(
self
.
p1
))
for
i
in
range
(
len
(
label
)):
label
[
i
]
=
0
if
self
.
p0
[
i
]
>
self
.
p1
[
i
]
else
1
return
label
.
reshape
([
-
1
,
1
])
def
predict_proba
(
self
,
testMatrix
):
self
.
_get_proba
(
testMatrix
)
return
np
.
array
([
self
.
p0
,
self
.
p1
]).
T
def
score
(
self
,
y_true
,
y_pred
):
acc
=
np
.
mean
([
1
if
y_true
[
i
]
==
y_pred
[
i
]
else
0
for
i
in
range
(
len
(
y_true
))])
return
acc
def
test_Gaussian_NB
(
data
):
X_train
,
X_test
,
y_train
,
y_test
=
train_test_split
(
data
[:,
0
:
-
1
],
data
[:,
-
1
],
test_size
=
0.25
,
random_state
=
42
)
gnb
=
Gaussian_NB
()
y_pred
=
gnb
.
fit
(
X_train
,
y_train
).
predict
(
X_test
)
acc_gnb
=
gnb
.
score
(
y_test
,
y_pred
)
P_score
=
precision_score
(
y_test
,
y_pred
)
R_score
=
recall_score
(
y_test
,
y_pred
)
y_score
=
(
gnb
.
predict_proba
(
X_test
))[:,
1
]
fpr
,
tpr
,
thresholds
=
roc_curve
(
y_test
,
y_score
)
Area_Under_Curve
=
auc
(
fpr
,
tpr
)
print
(
"
The accuracy of the logistic regression model is:
"
,
acc_gnb
,
"
\n
"
,
"
The precision of the logistic regression model is:
"
,
P_score
,
"
\n
"
,
"
The recall of the logistic regression model is:
"
,
R_score
,
"
\n
"
,
"
The AUC of the logistic regression model is:
"
,
Area_Under_Curve
,
"
\n
"
)
plt
.
plot
(
fpr
,
tpr
,
'
b
'
,
label
=
'
AUC = %0.2f
'
%
Area_Under_Curve
)
plt
.
legend
(
loc
=
'
lower right
'
)
plt
.
plot
([
0
,
1
],[
0
,
1
],
'
r--
'
)
plt
.
xlim
([
-
0.1
,
1.1
])
plt
.
ylim
([
-
0.1
,
1.1
])
plt
.
xlabel
(
'
False Positive Rate
'
)
plt
.
ylabel
(
'
True Positive Rate
'
)
plt
.
title
(
'
The ROC curve of the Gaussian Naive Bayes classifier
'
)
plt
.
show
()
\ No newline at end of file
Loading