Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
COMP3217_Assignment
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
rw1u22
COMP3217_Assignment
Commits
60d0255c
Commit
60d0255c
authored
2 years ago
by
rw1u22
Browse files
Options
Downloads
Patches
Plain Diff
Update PartB.py
parent
d6fe32c9
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
PartB.py
+85
-57
85 additions, 57 deletions
PartB.py
with
85 additions
and
57 deletions
PartB.py
+
85
−
57
View file @
60d0255c
import
pandas
as
pd
from
sklearn.model_selection
import
train_test_split
from
sklearn.linear_model
import
LogisticRegression
from
sklearn.metrics
import
accuracy_score
from
sklearn.metrics
import
accuracy_score
,
f1_score
# Load the training data and testing data into a Pandas DataFrame
data
=
pd
.
read_csv
(
'
TrainingDataMulti.csv
'
,
header
=
None
)
...
...
@@ -14,6 +14,7 @@ y = data.iloc[:, -1]
X_train
,
X_test
,
y_train
,
y_test
=
train_test_split
(
X
,
y
,
test_size
=
0.1
,
random_state
=
0
)
def
Logistic_Regression
():
# Fit data using Logistic Regression model training
LR
=
LogisticRegression
(
C
=
1e5
,
max_iter
=
5000
)
LR
.
fit
(
X_train
,
y_train
)
...
...
@@ -21,11 +22,16 @@ LR.fit(X_train, y_train)
# Make predictions on the testing data
y_pred
=
LR
.
predict
(
X_test
)
# Evaluate
the accuracy of the model
# Testing
the accuracy of the model
accuracy
=
accuracy_score
(
y_test
,
y_pred
)
# Testing the F1 score of the model
f1
=
f1_score
(
y_test
,
y_pred
,
average
=
'
weighted
'
)
print
(
"
+-------------------------------------------+
"
)
print
(
f
"
LogisticRegression Accuracy:
{
accuracy
}
"
)
print
(
f
"
LogisticRegression F1 Score:
{
f1
}
"
)
def
Decision_Tree
():
# Fit data using Decision Tree model training
from
sklearn.tree
import
DecisionTreeClassifier
DT
=
DecisionTreeClassifier
(
random_state
=
0
)
...
...
@@ -36,36 +42,58 @@ y_pred = DT.predict(X_test)
# Evaluate the accuracy of the model
accuracy
=
accuracy_score
(
y_test
,
y_pred
)
# Testing the F1 score of the model
f1
=
f1_score
(
y_test
,
y_pred
,
average
=
'
weighted
'
)
print
(
"
+-------------------------------------------+
"
)
print
(
f
"
DecisionTree Accuracy:
{
accuracy
}
"
)
print
(
f
"
DecisionTree F1 Score:
{
f1
}
"
)
def
Random_Forest
():
# Fit data using Random Forest model training
from
sklearn.ensemble
import
RandomForestClassifier
RF
=
RandomForestClassifier
(
random_state
=
0
)
RF
.
fit
(
X_train
,
y_train
)
y_pred
=
RF
.
predict
(
X_test
)
accuracy
=
accuracy_score
(
y_test
,
y_pred
)
f1
=
f1_score
(
y_test
,
y_pred
,
average
=
'
weighted
'
)
print
(
"
+-------------------------------------------+
"
)
print
(
f
"
RandomForest Accuracy:
{
accuracy
}
"
)
print
(
f
"
RandomForest F1 Score:
{
f1
}
"
)
return
RF
def
svm
():
# Fit data using SVM model training
from
sklearn.svm
import
SVC
SVM
=
SVC
(
random_state
=
0
)
SVM
.
fit
(
X_train
,
y_train
)
y_pred
=
SVM
.
predict
(
X_test
)
accuracy
=
accuracy_score
(
y_test
,
y_pred
)
f1
=
f1_score
(
y_test
,
y_pred
,
average
=
'
weighted
'
)
print
(
"
+-------------------------------------------+
"
)
print
(
f
"
SVM Accuracy:
{
accuracy
}
"
)
print
(
f
"
SVM F1 Score:
{
f1
}
"
)
def
knn
():
# Fit data using KNN model training
from
sklearn.neighbors
import
KNeighborsClassifier
KNN
=
KNeighborsClassifier
()
KNN
.
fit
(
X_train
,
y_train
)
y_pred
=
KNN
.
predict
(
X_test
)
accuracy
=
accuracy_score
(
y_test
,
y_pred
)
f1
=
f1_score
(
y_test
,
y_pred
,
average
=
'
weighted
'
)
print
(
"
+-------------------------------------------+
"
)
print
(
f
"
KNN Accuracy:
{
accuracy
}
"
)
print
(
f
"
KNN F1 Score:
{
f1
}
"
)
if
__name__
==
"
__main__
"
:
Logistic_Regression
()
Decision_Tree
()
RF
=
Random_Forest
()
svm
()
knn
()
# Use Random Forest model to predict the test data and save the results to a CSV file
pridictions
=
RF
.
predict
(
test_data
)
test_data
[
'
Prediction
'
]
=
pridictions
...
...
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