Skip to content

Commit

Permalink
kmeans example in cpp and python (#356)
Browse files Browse the repository at this point in the history
Signed-off-by: Alejandro Hernández <[email protected]>
  • Loading branch information
ahcorde authored Dec 30, 2021
1 parent 5e25570 commit 5fa6f78
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 0 deletions.
3 changes: 3 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,8 @@ target_link_libraries(rand_example ignition-math${IGN_MATH_VER}::ignition-math${
add_executable(graph_example graph_example.cc)
target_link_libraries(graph_example ignition-math${IGN_MATH_VER}::ignition-math${IGN_MATH_VER})

add_executable(kmeans kmeans.cc)
target_link_libraries(kmeans ignition-math${IGN_MATH_VER}::ignition-math${IGN_MATH_VER})

add_executable(color_example color_example.cc)
target_link_libraries(color_example ignition-math${IGN_MATH_VER}::ignition-math${IGN_MATH_VER})
64 changes: 64 additions & 0 deletions examples/kmeans.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright (C) 2021 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

#include <iostream>
#include <vector>

#include <ignition/math/Kmeans.hh>

int main(int argc, char **argv)
{
// Create some observations.
std::vector<ignition::math::Vector3d> obs;
obs.push_back(ignition::math::Vector3d(1.0, 1.0, 0.0));
obs.push_back(ignition::math::Vector3d(1.1, 1.0, 0.0));
obs.push_back(ignition::math::Vector3d(1.2, 1.0, 0.0));
obs.push_back(ignition::math::Vector3d(1.3, 1.0, 0.0));
obs.push_back(ignition::math::Vector3d(1.4, 1.0, 0.0));
obs.push_back(ignition::math::Vector3d(5.0, 1.0, 0.0));
obs.push_back(ignition::math::Vector3d(5.1, 1.0, 0.0));
obs.push_back(ignition::math::Vector3d(5.2, 1.0, 0.0));
obs.push_back(ignition::math::Vector3d(5.3, 1.0, 0.0));
obs.push_back(ignition::math::Vector3d(5.4, 1.0, 0.0));

// Initialize Kmeans with two partitions.
ignition::math::Kmeans kmeans(obs);

std::vector<ignition::math::Vector3d> obsCopy;
obsCopy = kmeans.Observations();

for (auto &elem : obsCopy)
elem += ignition::math::Vector3d(0.1, 0.2, 0.0);

// append more observations
kmeans.AppendObservations(obsCopy);

// cluster
std::vector<ignition::math::Vector3d> centroids;
std::vector<unsigned int> labels;
auto result = kmeans.Cluster(2, centroids, labels);

// Check that there are two centroids.
std::cout << "Is there a solution ? " << result << std::endl;
std::cout << "There are " << centroids.size() << " centroids" << std::endl;
std::cout << "labels: [";
for (auto &label: labels)
{
std::cout << label << ", ";
}
std::cout << "]" << '\n';
}
51 changes: 51 additions & 0 deletions examples/kmeans.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Copyright (C) 2021 Open Source Robotics Foundation
#
# Licensed under the Apache License, Version 2.0 (the "License")
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


from ignition.math import Kmeans, Vector3d

# Create some observations.
obs = list([])
obs.append(Vector3d(1.0, 1.0, 0.0))
obs.append(Vector3d(1.1, 1.0, 0.0))
obs.append(Vector3d(1.2, 1.0, 0.0))
obs.append(Vector3d(1.3, 1.0, 0.0))
obs.append(Vector3d(1.4, 1.0, 0.0))
obs.append(Vector3d(5.0, 1.0, 0.0))
obs.append(Vector3d(5.1, 1.0, 0.0))
obs.append(Vector3d(5.2, 1.0, 0.0))
obs.append(Vector3d(5.3, 1.0, 0.0))
obs.append(Vector3d(5.4, 1.0, 0.0))

# Initialize Kmeans with two partitions.
kmeans = Kmeans(obs)

# copy original Vector to add more data
obs_copy = list(kmeans.observations()).copy()
obs2 = list([])

for idx, a in enumerate(obs_copy):
obs_copy[idx] = a + Vector3d(0.1, 0.2, 0.0)
obs2.append(obs_copy[idx])

# Append more observations
kmeans.append_observations(obs2)

# Calling cluster
result, centroids, labels = kmeans.cluster(2)

# Check that there are two centroids.
print("Is there a solution ? {}".format(result));
print("There are {} centroids".format(len(centroids)))
print("labels:", labels)

0 comments on commit 5fa6f78

Please sign in to comment.