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
Commits
694ce504
Commit
694ce504
authored
2 years ago
by
ZHANG Zuoyu
Browse files
Options
Downloads
Plain Diff
Merge branch 'XiranZ' into 'main'
Fonctions_version1.py See merge request
!2
parents
8fa54afd
e6cbeb63
No related branches found
No related tags found
1 merge request
!2
Fonctions_version1.py
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
Fonctions.py
+59
-0
59 additions, 0 deletions
Fonctions.py
with
59 additions
and
0 deletions
Fonctions.py
+
59
−
0
View file @
694ce504
...
...
@@ -227,3 +227,62 @@ def test_decision_tree(data, max_depth):
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
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment