Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
COMP3217-CW2
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor 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
Jay Caunhye
COMP3217-CW2
Commits
4dcecf0f
Commit
4dcecf0f
authored
2 years ago
by
Jaralus
Browse files
Options
Downloads
Patches
Plain Diff
update Part_A.py and Part_B.py
parent
a83d569e
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
COMP3217 Machine Learning Coursework.docx
+0
-0
0 additions, 0 deletions
COMP3217 Machine Learning Coursework.docx
Part_A.py
+8
-2
8 additions, 2 deletions
Part_A.py
Part_B.py
+8
-8
8 additions, 8 deletions
Part_B.py
with
16 additions
and
10 deletions
COMP3217 Machine Learning Coursework.docx
0 → 100644
+
0
−
0
View file @
4dcecf0f
File added
This diff is collapsed.
Click to expand it.
Part_A.py
+
8
−
2
View file @
4dcecf0f
# Import libraries and modules
import
pandas
as
pd
import
numpy
as
np
import
matplotlib.pyplot
as
plt
from
skopt
import
BayesSearchCV
from
skopt.space
import
Real
,
Categorical
,
Integer
from
sklearn.model_selection
import
cross_val_score
from
sklearn.utils
import
shuffle
from
sklearn.preprocessing
import
StandardScaler
from
sklearn.metrics
import
accuracy_score
,
f1_score
from
sklearn.metrics
import
accuracy_score
,
f1_score
,
confusion_matrix
,
ConfusionMatrixDisplay
from
sklearn.linear_model
import
LogisticRegression
from
sklearn.neural_network
import
MLPClassifier
from
sklearn.svm
import
SVC
...
...
@@ -56,7 +57,7 @@ def main():
# Train the classifiers
classifiers
=
[
(
"
Logistic
al
Regression
"
,
LogisticRegression
(
max_iter
=
10000
,
solver
=
"
newton-cg
"
,
C
=
9.416
).
fit
(
train_features_scaled
,
train_labels
)),
(
"
Logistic Regression
"
,
LogisticRegression
(
max_iter
=
10000
,
solver
=
"
newton-cg
"
,
C
=
9.416
).
fit
(
train_features_scaled
,
train_labels
)),
(
"
Multi-layer Perceptron
"
,
MLPClassifier
(
max_iter
=
10000
,
solver
=
"
adam
"
,
activation
=
"
tanh
"
,
learning_rate
=
"
constant
"
).
fit
(
train_features_scaled
,
train_labels
)),
(
"
C-Support Vector
"
,
SVC
(
C
=
7.989999999999979
,
kernel
=
"
linear
"
).
fit
(
train_features_scaled
,
train_labels
)),
...
...
@@ -92,9 +93,14 @@ def main():
best_classifier_name
=
classifier_name
best_classifier
=
classifier
best_classifier_average
=
cv_average
best_classifier_predictions
=
train_predicted_labels
print
(
f
"
The best classifier is
{
best_classifier_name
}
.
\n
"
)
# Draw confusion matrix for the best classifier
ConfusionMatrixDisplay
(
confusion_matrix
=
confusion_matrix
(
train_labels
,
best_classifier_predictions
)).
plot
()
plt
.
show
()
# Read the training data
test_features
=
pd
.
read_csv
(
"
TestingDataBinary.csv
"
,
header
=
None
)
...
...
This diff is collapsed.
Click to expand it.
Part_B.py
+
8
−
8
View file @
4dcecf0f
# Import libraries and modules
import
pandas
as
pd
import
numpy
as
np
import
matplotlib.pyplot
as
plt
from
skopt
import
BayesSearchCV
from
skopt.space
import
Real
,
Categorical
,
Integer
from
sklearn.model_selection
import
cross_val_score
from
sklearn.utils
import
shuffle
from
sklearn.preprocessing
import
StandardScaler
from
sklearn.metrics
import
accuracy_score
,
f1_score
from
sklearn.metrics
import
accuracy_score
,
f1_score
,
confusion_matrix
,
ConfusionMatrixDisplay
from
sklearn.linear_model
import
LogisticRegression
from
sklearn.neural_network
import
MLPClassifier
from
sklearn.svm
import
SVC
...
...
@@ -56,7 +57,7 @@ def main():
# Train the classifiers
classifiers
=
[
(
"
Logistic
al
Regression
"
,
LogisticRegression
(
max_iter
=
10000
,
solver
=
"
newton-cg
"
,
C
=
9.088000000000001
).
fit
(
train_features_scaled
,
train_labels
)),
(
"
Logistic Regression
"
,
LogisticRegression
(
max_iter
=
10000
,
solver
=
"
newton-cg
"
,
C
=
9.088000000000001
).
fit
(
train_features_scaled
,
train_labels
)),
(
"
Multi-layer Perceptron
"
,
MLPClassifier
(
max_iter
=
10000
,
solver
=
"
adam
"
,
activation
=
"
tanh
"
,
learning_rate
=
"
adaptive
"
).
fit
(
train_features_scaled
,
train_labels
)),
(
"
C-Support Vector
"
,
SVC
(
C
=
9.59
,
kernel
=
"
linear
"
).
fit
(
train_features_scaled
,
train_labels
))
]
...
...
@@ -87,15 +88,14 @@ def main():
best_classifier_name
=
classifier_name
best_classifier
=
classifier
best_classifier_average
=
cv_average
# Update the best classifier if the current classifier has a better average score
if
(
best_classifier_average
<
cv_average
):
best_classifier_name
=
classifier_name
best_classifier
=
classifier
best_classifier_average
=
cv_average
best_classifier_predictions
=
train_predicted_labels
print
(
f
"
The best classifier is
{
best_classifier_name
}
.
\n
"
)
# Draw confusion matrix for the best classifier
ConfusionMatrixDisplay
(
confusion_matrix
=
confusion_matrix
(
train_labels
,
best_classifier_predictions
)).
plot
()
plt
.
show
()
# Read the training data
test_features
=
pd
.
read_csv
(
"
TestingDataMulti.csv
"
,
header
=
None
)
...
...
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