Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
K
kmeans
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
dario.genga
kmeans
Commits
8c35d93a
Commit
8c35d93a
authored
2 years ago
by
dario.genga
Browse files
Options
Downloads
Patches
Plain Diff
Add values_utils files
parent
0078dbfb
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
kmeans.c
+14
-55
14 additions, 55 deletions
kmeans.c
kmeans.h
+1
-1
1 addition, 1 deletion
kmeans.h
makefile
+3
-1
3 additions, 1 deletion
makefile
output_data.txt
+7
-7
7 additions, 7 deletions
output_data.txt
values_utils.c
+50
-0
50 additions, 0 deletions
values_utils.c
values_utils.h
+40
-0
40 additions, 0 deletions
values_utils.h
with
115 additions
and
64 deletions
kmeans.c
+
14
−
55
View file @
8c35d93a
...
...
@@ -3,27 +3,7 @@
#include
"kmeans.h"
int
random_with_min_man_value
(
int
min
,
int
max
)
{
return
(
rand
()
%
(
max
-
min
+
1
))
+
min
;
}
void
swap
(
int
*
x
,
int
*
y
)
{
int
tmp
=
*
x
;
*
x
=
*
y
;
*
y
=
tmp
;
}
kmeans
*
kmeans_create_empty
()
{
kmeans
*
universe
=
malloc
(
sizeof
(
kmeans
));
universe
->
points_array
=
NULL
;
universe
->
nb_points
=
0
;
universe
->
k
=
0
;
universe
->
dimensions
=
0
;
universe
->
clusters_array
=
NULL
;
return
universe
;
}
/// Create empty clusters for the universe
/// \param universe The universe who contains the cluster to create.
...
...
@@ -82,6 +62,17 @@ point* create_point_from_string(char *line, int dimensions) {
return
p
;
}
kmeans
*
kmeans_create_empty
()
{
kmeans
*
universe
=
malloc
(
sizeof
(
kmeans
));
universe
->
points_array
=
NULL
;
universe
->
nb_points
=
0
;
universe
->
k
=
0
;
universe
->
dimensions
=
0
;
universe
->
clusters_array
=
NULL
;
return
universe
;
}
kmeans
*
kmeans_create
(
int
k
,
point
**
data
,
int
nb_points
)
{
kmeans
*
universe
=
malloc
(
sizeof
(
kmeans
));
universe
->
points_array
=
data
;
...
...
@@ -227,46 +218,14 @@ void init_clusters(kmeans *universe) {
free
(
random_index_possible
);
}
float
compute_euclidean_distance
(
point
*
p1
,
point
*
p2
)
{
float
sum
=
0
;
float
result
=
0
;
for
(
int
i
=
0
;
i
<
p1
->
dimensions
;
i
++
)
{
sum
+=
pow
(
p1
->
value
[
i
]
-
p2
->
value
[
i
],
2
);
}
result
=
sqrt
(
sum
);
return
result
;
}
float
compute_manhattan_distance
(
point
*
p1
,
point
*
p2
)
{
float
result
=
0
;
for
(
int
i
=
0
;
i
<
p1
->
dimensions
;
i
++
)
{
result
+=
fabs
(
p1
->
value
[
i
]
-
p2
->
value
[
i
]);
}
return
result
;
}
float
compute_chebyshev_distance
(
point
*
p1
,
point
*
p2
)
{
float
result
=
0
;
for
(
int
i
=
0
;
i
<
p1
->
dimensions
;
i
++
)
{
int
abs_diff
=
fabs
(
p1
->
value
[
i
]
-
p2
->
value
[
i
]);
if
(
abs_diff
>
result
)
{
result
=
abs_diff
;
}
}
return
result
;
}
float
compute_distance
(
point
*
p1
,
point
*
p2
)
{
if
(
p1
->
dimensions
!=
p2
->
dimensions
)
{
printf
(
"The points don't have the same dimensions!
\n
"
);
exit
(
EXIT_FAILURE
);
}
float
euclidean
=
compute_euclidean_distance
(
p1
,
p2
);
//float manhattan = compute_manhattan_distance(p1
, p2
);
//float chebyshev = compute_chebyshev_distance(p1
, p2
);
float
euclidean
=
compute_euclidean_distance
(
p1
->
value
,
p2
->
value
,
p1
->
dimensions
);
//float manhattan = compute_manhattan_distance(p1
->value, p2->value, p1->dimensions
);
//float chebyshev = compute_chebyshev_distance(p1
->value, p2->value, p1->dimensions
);
return
euclidean
;
}
...
...
This diff is collapsed.
Click to expand it.
kmeans.h
+
1
−
1
View file @
8c35d93a
...
...
@@ -8,8 +8,8 @@
#include
<stdlib.h>
#include
<stdbool.h>
#include
<string.h>
#include
<math.h>
#include
"files_utils.h"
#include
"values_utils.h"
#define LINE_INDEX_DIMENSIONS 0
#define LINE_INDEX_CLUSTER 1
...
...
This diff is collapsed.
Click to expand it.
makefile
+
3
−
1
View file @
8c35d93a
LIB
=
-lm
CC
=
gcc
-Wall
-Wextra
-g
main
:
files_utils.o kmeans.o main.o
main
:
files_utils.o
values_utils.o
kmeans.o main.o
$(
CC
)
$^
-fsanitize
=
address
-fsanitize
=
leak
-o
$@
$(
LIB
)
files_utils.o
:
files_utils.c files_utils.h
$(
CC
)
-c
$<
$(
LIB
)
values_utils.o
:
values_utils.c values_utils.h
$(
CC
)
-c
$<
$(
LIB
)
kmeans.o
:
kmeans.c kmeans.h
$(
CC
)
-c
$<
$(
LIB
)
main.o
:
main.c
...
...
This diff is collapsed.
Click to expand it.
output_data.txt
+
7
−
7
View file @
8c35d93a
2
3
*
-1.00,-5.00
-2.00,-4.00
-3.00,-3.00
-4.00,-2.00
-5.00,-1.00
-1.75,-2.25
*
0.00,0.00
-1.00,-5.00
3.10,-4.90
*
1.00,1.00
...
...
@@ -19,3 +13,9 @@
-2.25,4.75
2.20,4.40
4.00,2.00
*
-2.00,-4.00
-3.00,-3.00
-4.00,-2.00
-5.00,-1.00
-1.75,-2.25
This diff is collapsed.
Click to expand it.
values_utils.c
0 → 100644
+
50
−
0
View file @
8c35d93a
// Project : K-means
// Author : Dario GENGA
#include
"values_utils.h"
int
random_with_min_man_value
(
int
min
,
int
max
)
{
return
(
rand
()
%
(
max
-
min
+
1
))
+
min
;
}
/// Swap x and y values
/// \param x The first value
/// \param y The second value
void
swap
(
int
*
x
,
int
*
y
)
{
int
tmp
=
*
x
;
*
x
=
*
y
;
*
y
=
tmp
;
}
float
compute_euclidean_distance
(
float
*
p1
,
float
*
p2
,
size_t
dimensions
)
{
float
sum
=
0
;
float
result
=
0
;
for
(
size_t
i
=
0
;
i
<
dimensions
;
i
++
)
{
sum
+=
pow
(
p1
[
i
]
-
p2
[
i
],
2
);
}
result
=
sqrt
(
sum
);
return
result
;
}
float
compute_manhattan_distance
(
float
*
p1
,
float
*
p2
,
size_t
dimensions
)
{
float
result
=
0
;
for
(
size_t
i
=
0
;
i
<
dimensions
;
i
++
)
{
result
+=
fabs
(
p1
[
i
]
-
p2
[
i
]);
}
return
result
;
}
float
compute_chebyshev_distance
(
float
*
p1
,
float
*
p2
,
size_t
dimensions
)
{
float
result
=
0
;
for
(
size_t
i
=
0
;
i
<
dimensions
;
i
++
)
{
int
abs_diff
=
fabs
(
p1
[
i
]
-
p2
[
i
]);
if
(
abs_diff
>
result
)
{
result
=
abs_diff
;
}
}
return
result
;
}
This diff is collapsed.
Click to expand it.
values_utils.h
0 → 100644
+
40
−
0
View file @
8c35d93a
// Project : K-means
// Author : Dario GENGA
#ifndef _KMEANS_VALUES_UTILS_H
#define _KMEANS_VALUES_UTILS_H
#include
<math.h>
#include
<stdlib.h>
/// Get a random value in the min-max range.
/// \param min The minimal value (inclued).
/// \param max The maximal value (inclued).
/// \return A random int value.
int
random_with_min_man_value
(
int
min
,
int
max
);
/// Swap x and y values
/// \param x The first value
/// \param y The second value
void
swap
(
int
*
x
,
int
*
y
);
/// Compute the euclidean distance between two points of same dimensions.
/// \param p1 The values of the first point.
/// \param p2 The values of the second point.
/// \return The distance between the two points.
float
compute_euclidean_distance
(
float
*
p1
,
float
*
p2
,
size_t
dimensions
);
/// Compute the manhattan distance between two points of same dimensions.
/// \param p1 The values of the first point.
/// \param p2 The values of the second point.
/// \return The distance between the two points.
float
compute_manhattan_distance
(
float
*
p1
,
float
*
p2
,
size_t
dimensions
);
/// Compute the chebyshev distance between two points of same dimensions.
/// \param p1 The values of the first point.
/// \param p2 The values of the second point.
/// \return The distance between the two points.
float
compute_chebyshev_distance
(
float
*
p1
,
float
*
p2
,
size_t
dimensions
);
#endif
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