Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Image classification
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
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Deep Learning
Image classification
Commits
cad0a258
Commit
cad0a258
authored
8 months ago
by
pierre-cau
Browse files
Options
Downloads
Patches
Plain Diff
split_data
parent
2d858147
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/main.py
+11
-1
11 additions, 1 deletion
src/main.py
src/utils/__init__.py
+1
-0
1 addition, 0 deletions
src/utils/__init__.py
src/utils/split_data.py
+62
-0
62 additions, 0 deletions
src/utils/split_data.py
with
74 additions
and
1 deletion
src/main.py
+
11
−
1
View file @
cad0a258
...
...
@@ -4,3 +4,13 @@ from utils import *
if
__name__
==
"
__main__
"
:
# Load CIFAR data
data
,
labels
=
read_cifar
(
r
"
../data/cifar-10-batches-py
"
)
print
(
f
"
Data shape:
{
data
.
shape
}
, Labels shape:
{
labels
.
shape
}
\n
"
)
# Split the dataset
data_train
,
labels_train
,
data_test
,
labels_test
=
split_dataset
(
data
,
labels
,
0.8
)
print
(
f
"
Split the dataset with a
{
0.8
}
split factor:
"
)
print
(
f
"
- Training data shape:
{
data_train
.
shape
}
, Training labels shape:
{
labels_train
.
shape
}
"
)
print
(
f
"
- Testing data shape:
{
data_test
.
shape
}
, Testing labels shape:
{
labels_test
.
shape
}
"
)
\ No newline at end of file
This diff is collapsed.
Click to expand it.
src/utils/__init__.py
+
1
−
0
View file @
cad0a258
...
...
@@ -3,6 +3,7 @@
ROOT
=
r
"
../../..
"
from
.read_cifar
import
*
from
.split_data
import
*
This diff is collapsed.
Click to expand it.
src/utils/split_data.py
0 → 100644
+
62
−
0
View file @
cad0a258
# Author: Pierre CAU
# Date: 2024
import
numpy
as
np
def
split_dataset
(
data
,
labels
,
split
):
"""
Split the dataset into a training set and a test set.
Parameters
----------
data : np.ndarray
Array of data samples.
labels : np.ndarray
Array of labels corresponding to the data samples.
split : float
A float between 0 and 1 which determines the split factor of the training set with respect to the test set.
Returns
-------
data_train : np.ndarray
Training data.
labels_train : np.ndarray
Corresponding labels for the training data.
data_test : np.ndarray
Testing data.
labels_test : np.ndarray
Corresponding labels for the testing data.
"""
assert
data
.
shape
[
0
]
==
labels
.
shape
[
0
],
"
Data and labels must have the same number of samples
"
assert
0
<
split
<
1
,
"
Split must be a float between 0 and 1
"
# Shuffle the data and labels in unison
indices
=
np
.
arange
(
data
.
shape
[
0
])
np
.
random
.
shuffle
(
indices
)
data
=
data
[
indices
]
labels
=
labels
[
indices
]
# Calculate the split index
split_index
=
int
(
data
.
shape
[
0
]
*
split
)
# Split the data and labels
data_train
=
data
[:
split_index
]
labels_train
=
labels
[:
split_index
]
data_test
=
data
[
split_index
:]
labels_test
=
labels
[
split_index
:]
return
data_train
,
labels_train
,
data_test
,
labels_test
# Example usage
if
__name__
==
"
__main__
"
:
# Example data and labels
data
=
np
.
random
.
rand
(
100
,
10
)
labels
=
np
.
random
.
randint
(
0
,
2
,
100
)
print
(
f
"
Data shape:
{
data
.
shape
}
, Labels shape:
{
labels
.
shape
}
"
)
# Split the dataset
data_train
,
labels_train
,
data_test
,
labels_test
=
split_dataset
(
data
,
labels
,
0.8
)
print
(
f
"
Training data shape:
{
data_train
.
shape
}
, Training labels shape:
{
labels_train
.
shape
}
"
)
print
(
f
"
Testing data shape:
{
data_test
.
shape
}
, Testing labels shape:
{
labels_test
.
shape
}
"
)
\ No newline at end of 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