Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
COMP3217
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
sps2n22
COMP3217
Commits
9b4ee4d9
Commit
9b4ee4d9
authored
Jun 8, 2023
by
sps2n22
Browse files
Options
Downloads
Patches
Plain Diff
Upload New File
parent
c0d3aa79
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
task1.py
+86
-0
86 additions, 0 deletions
task1.py
with
86 additions
and
0 deletions
task1.py
0 → 100644
+
86
−
0
View file @
9b4ee4d9
# Importing libraries
import
matplotlib.pyplot
as
plt
import
pandas
as
pd
from
sklearn.model_selection
import
train_test_split
from
sklearn.metrics
import
accuracy_score
from
sklearn.svm
import
SVC
from
sklearn.preprocessing
import
StandardScaler
# Using pandas, reads a CSV file named "TrainingDataBinary.csv" and stores it in a dataframe named df.
df
=
pd
.
read_csv
(
"
TrainingDataBinary.csv
"
)
# Data preprocessing: Separating features and target variable
X
=
df
.
iloc
[:,
:
-
1
]
y
=
df
.
iloc
[:,
-
1
]
# Data splitting and model training
X_train
,
X_test
,
y_train
,
y_test
=
train_test_split
(
X
,
y
,
test_size
=
0.2
,
random_state
=
42
)
# Feature scaling
scaler
=
StandardScaler
()
X_train_scaled
=
scaler
.
fit_transform
(
X_train
)
X_test_scaled
=
scaler
.
transform
(
X_test
)
# Support Vector Classifier (SVC) initialization and training
svc
=
SVC
(
kernel
=
'
rbf
'
,
C
=
10.0
,
gamma
=
0.1
,
random_state
=
42
)
# Prediction on the scaled test data
svc
.
fit
(
X_train_scaled
,
y_train
)
y_pred
=
svc
.
predict
(
X_test_scaled
)
# Calculate and print accuracy
accuracy
=
accuracy_score
(
y_test
,
y_pred
)
print
(
"
Accuracy:
"
,
accuracy
)
# Calculate and print prediction error
prediction_error
=
1
-
accuracy
print
(
"
Prediction Error:
"
,
prediction_error
)
# Plotting the scatter plot
plt
.
scatter
(
X_test
.
iloc
[:,
0
],
X_test
.
iloc
[:,
1
],
c
=
y_pred
,
cmap
=
'
viridis
'
)
plt
.
xlabel
(
"
Feature 1
"
)
plt
.
ylabel
(
"
Feature 2
"
)
plt
.
title
(
"
Scatter Plot with Predicted Classes
"
)
plt
.
show
()
testingData
=
pd
.
read_csv
(
"
TestingDataBinary.csv
"
)
predicted
=
svc
.
predict
(
testingData
)
# Ensure the number of rows matches
resultData
=
pd
.
DataFrame
(
predicted
,
columns
=
[
'
PredictedMarker
'
])
resultData
=
resultData
[:
testingData
.
shape
[
0
]]
# Adding predicted markers to testing data
testingData
[
'
PredictedMarker
'
]
=
resultData
[
"
PredictedMarker
"
].
tolist
()
# Saving the predictions to a CSV file
testingData
.
to_csv
(
"
TestingResultsBinary.csv
"
,
index
=
False
)
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