Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
3
3217 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
kf2n21
3217 cw2
Commits
cb12b344
Commit
cb12b344
authored
3 years ago
by
kf2n21
Browse files
Options
Downloads
Patches
Plain Diff
Upload New File
parent
48004966
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
classifier.py
+76
-0
76 additions, 0 deletions
classifier.py
with
76 additions
and
0 deletions
classifier.py
0 → 100644
+
76
−
0
View file @
cb12b344
# coding=utf-8
import
numpy
as
np
import
pandas
as
pd
import
sklearn.metrics
from
sklearn.svm
import
SVC
from
sklearn.neighbors
import
KNeighborsClassifier
from
sklearn.model_selection
import
train_test_split
from
sklearn.discriminant_analysis
import
LinearDiscriminantAnalysis
"""
Reading training data
"""
train_data
=
pd
.
read_csv
(
"
TrainingData.txt
"
,
header
=
None
)
y
=
train_data
[
24
].
tolist
()
train_data
=
train_data
.
drop
(
24
,
axis
=
1
)
x
=
train_data
.
values
.
tolist
()
# Storing full training data before splitting
x
=
np
.
array
(
x
)
y
=
np
.
array
(
y
)
x_train_full
=
x
y_train_full
=
y
"""
Reading testing data to predict
"""
test_data
=
pd
.
read_csv
(
"
TestingData.txt
"
,
header
=
None
)
x_classify
=
test_data
.
values
.
tolist
()
# Splitting training data for testing algorithm
training1_data
,
validation_data
,
target_labels
,
validation_target_labels
=
train_test_split
(
x
,
y
,
test_size
=
0.2
,
random_state
=
0
)
"""
1. k nearest neighbours classifier
"""
KNN_clf
=
KNeighborsClassifier
()
KNN_clf
.
fit
(
training1_data
,
target_labels
)
KNN_results
=
KNN_clf
.
predict
(
validation_data
)
print
(
"
-
"
*
70
)
print
(
"
K Nearest Neighbours Classifier
"
)
print
(
sklearn
.
metrics
.
classification_report
(
KNN_results
,
validation_target_labels
))
"""
2. support vector classifier
"""
SVC_clf
=
SVC
()
SVC_clf
.
fit
(
training1_data
,
target_labels
)
SVC_results
=
SVC_clf
.
predict
(
validation_data
)
print
(
"
-
"
*
70
)
print
(
"
Support Vector Classifier
"
)
print
(
sklearn
.
metrics
.
classification_report
(
SVC_results
,
validation_target_labels
))
"""
3. Linear Discriminant Analysis
"""
lda_clf
=
LinearDiscriminantAnalysis
()
lda_clf
.
fit
(
training1_data
,
target_labels
)
lda_results
=
lda_clf
.
predict
(
validation_data
)
print
(
"
-
"
*
70
)
print
(
"
Linear Discriminant Analysis
"
)
print
(
sklearn
.
metrics
.
classification_report
(
lda_results
,
validation_target_labels
))
# %%
testDF
=
pd
.
read_csv
(
"
TestingData.txt
"
,
header
=
None
)
testing_data
=
testDF
.
values
.
tolist
()
# %%
pre
=
SVC_clf
.
predict
(
testing_data
)
pre
=
pd
.
DataFrame
(
pre
)
testing_data
=
pd
.
DataFrame
(
testing_data
)
all_result
=
pd
.
concat
([
testing_data
,
pre
],
axis
=
1
)
# print(all_result)
# %%
all_result
.
to_csv
(
"
TestingResults.txt
"
,
header
=
0
,
index
=
0
)
if
__name__
==
"
__main__
"
:
pass
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