Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
3
3217CW2
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
XIAOYING DENG
3217CW2
Commits
f675ed4e
Commit
f675ed4e
authored
2 years ago
by
XIAOYING DENG
Browse files
Options
Downloads
Patches
Plain Diff
Upload New File
parent
a7eb385c
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
test2.py
+60
-0
60 additions, 0 deletions
test2.py
with
60 additions
and
0 deletions
test2.py
0 → 100644
+
60
−
0
View file @
f675ed4e
import
pandas
as
pd
from
sklearn.ensemble
import
RandomForestClassifier
from
sklearn.metrics
import
accuracy_score
# Load the training data
train_data
=
pd
.
read_csv
(
"
/Users/rebecca_dxy/Downloads/Machine/TrainingDataMulti.csv
"
)
# Split the features and labels
X_train
=
train_data
.
iloc
[:,
:
-
1
]
y_train
=
train_data
.
iloc
[:,
-
1
]
# Get the column names of the training data
feature_names
=
X_train
.
columns
# Create a Random Forest classifier
rf_classifier
=
RandomForestClassifier
()
# Train the classifier
rf_classifier
.
fit
(
X_train
,
y_train
)
# Load the testing data
test_data
=
pd
.
read_csv
(
"
/Users/rebecca_dxy/Downloads/Machine/TestingDataMulti.csv
"
)
# Set the column names of the testing data to match the training data
test_data
.
columns
=
feature_names
# Predict labels for the testing data
y_pred
=
rf_classifier
.
predict
(
test_data
)
# Create a DataFrame with the computed labels for testing data
testing_results
=
pd
.
DataFrame
(
y_pred
,
columns
=
[
"
Label
"
])
# Compute predictions on the training data
y_train_pred
=
rf_classifier
.
predict
(
X_train
)
# Calculate training error and accuracy
training_error
=
1
-
accuracy_score
(
y_train
,
y_train_pred
)
training_accuracy
=
accuracy_score
(
y_train
,
y_train_pred
)
# Print the training error and accuracy
print
(
"
Training Error:
"
,
training_error
)
print
(
"
Training Accuracy:
"
,
training_accuracy
)
# Show the computed labels for all testing data
print
(
"
Computed Labels for Testing Data:
"
)
print
(
testing_results
)
# Show the computed labels for all testing data
for
index
,
row
in
testing_results
.
iterrows
():
print
(
f
"
Computed Label for Trace
{
index
+
1
}
:
{
row
[
'
Label
'
]
}
"
)
# Save the computed labels to a file
testing_results
.
to_csv
(
"
TestingResultsMulti.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