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
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ma8g20
COMP3217-CW2
Commits
8ef027fc
Commit
8ef027fc
authored
2 years ago
by
Matt Avis
Browse files
Options
Downloads
Patches
Plain Diff
Added Part B, and accompanying results.
parent
b15083d9
No related branches found
No related tags found
No related merge requests found
Changes
3
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
part-a/part_a.py
+3
-3
3 additions, 3 deletions
part-a/part_a.py
part-b/TestingResultsMutli.csv
+100
-0
100 additions, 0 deletions
part-b/TestingResultsMutli.csv
part-b/part_b.py
+58
-0
58 additions, 0 deletions
part-b/part_b.py
with
161 additions
and
3 deletions
part-a/part_a.py
+
3
−
3
View file @
8ef027fc
...
@@ -24,15 +24,15 @@ print("Created classifier with linear kernal.")
...
@@ -24,15 +24,15 @@ print("Created classifier with linear kernal.")
# test the accuracy of the model
# test the accuracy of the model
print
(
"
Beginning accuracy testing...
"
)
print
(
"
Beginning accuracy testing...
"
)
averageAccuracy
=
0
averageAccuracy
=
0
for
i
in
range
(
10
):
for
i
in
range
(
5
):
print
(
"
Testing accuracy - iteration
"
+
str
(
i
)
+
"
/
10
"
)
print
(
"
Testing accuracy - iteration
"
+
str
(
i
)
+
"
/
5
"
)
X_train
,
X_test
,
y_train
,
y_test
=
train_test_split
(
X
,
y
,
test_size
=
0.0167
)
X_train
,
X_test
,
y_train
,
y_test
=
train_test_split
(
X
,
y
,
test_size
=
0.0167
)
classifier
.
fit
(
X_train
,
y_train
)
classifier
.
fit
(
X_train
,
y_train
)
y_pred
=
classifier
.
predict
(
X_test
)
y_pred
=
classifier
.
predict
(
X_test
)
accuracy
=
metrics
.
accuracy_score
(
y_test
,
y_pred
)
accuracy
=
metrics
.
accuracy_score
(
y_test
,
y_pred
)
print
(
"
Accuracy:
"
,
accuracy
)
print
(
"
Accuracy:
"
,
accuracy
)
averageAccuracy
+=
accuracy
averageAccuracy
+=
accuracy
print
(
"
Average accuracy:
"
,
(
averageAccuracy
/
10
))
print
(
"
Average accuracy:
"
,
(
averageAccuracy
/
5
))
# re-train the model with whole data set
# re-train the model with whole data set
classifier
.
fit
(
X
,
y
)
classifier
.
fit
(
X
,
y
)
...
...
This diff is collapsed.
Click to expand it.
part-b/TestingResultsMutli.csv
0 → 100644
+
100
−
0
View file @
8ef027fc
This diff is collapsed.
Click to expand it.
part-b/part_b.py
+
58
−
0
View file @
8ef027fc
import
csv
from
sklearn
import
svm
,
metrics
from
sklearn.model_selection
import
train_test_split
# load training data - array of size (6000, 128) - and labels (1d array of length 6000)
print
(
"
Loading training data...
"
)
X
=
[]
y
=
[]
# read the training csv file line by line and parse the data
trainingFile
=
open
(
"
TrainingDataMulti.csv
"
)
reader
=
csv
.
reader
(
trainingFile
)
for
line
in
reader
:
# remove the label from the system trace and add it to the training data
y
.
append
(
line
.
pop
())
# add the features of the system trace to the training data
X
.
append
(
line
)
print
(
"
Finished reading training data - read
"
+
str
(
len
(
X
))
+
"
system traces.
"
)
# create a SVM classifier with the linear kernel
classifier
=
svm
.
SVC
(
kernel
=
"
linear
"
)
print
(
"
Created classifier with linear kernel.
"
)
# test the accuracy of the model
print
(
"
Beginning accuracy testing...
"
)
averageAccuracy
=
0
for
i
in
range
(
5
):
print
(
"
Testing accuracy - iteration
"
+
str
(
i
)
+
"
/5
"
)
X_train
,
X_test
,
y_train
,
y_test
=
train_test_split
(
X
,
y
,
test_size
=
0.0167
)
classifier
.
fit
(
X_train
,
y_train
)
y_pred
=
classifier
.
predict
(
X_test
)
accuracy
=
metrics
.
accuracy_score
(
y_test
,
y_pred
)
print
(
"
Accuracy:
"
,
accuracy
)
averageAccuracy
+=
accuracy
print
(
"
Average accuracy:
"
,
(
averageAccuracy
/
5
))
# re-train the model with whole data set
classifier
.
fit
(
X
,
y
)
print
(
"
Trained model.
"
)
# read the test file
testData
=
[]
testFile
=
open
(
"
TestingDataMulti.csv
"
);
reader
=
csv
.
reader
(
testFile
)
for
line
in
reader
:
# add the system trace to the list of test data
testData
.
append
(
line
)
print
(
"
Read test sytem traces.
"
)
# predict the classifications for the test system traces and write them to the results file
print
(
"
Writing to results file...
"
)
resultsFile
=
open
(
"
TestingResultsMutli.csv
"
,
"
w
"
,
newline
=
""
)
writer
=
csv
.
writer
(
resultsFile
)
for
trace
in
testData
:
trace
.
append
(
classifier
.
predict
([
trace
])[
0
])
writer
.
writerow
(
trace
)
resultsFile
.
close
()
print
(
"
Finished writing to results 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