Who is this workflow for? This n8n workflow processes a collection of vectors provided in JSON format, ensures they share the same dimensions, and computes their centroid. Designed for versatility, it can be seamlessly integrated into various projects requiring vector analysis..

What does this workflow do?

Step 1: Receive Vectors (Webhook)

  • Function: Acts as the entry point of the workflow by accepting a GET request.
  • Input: Expects a vectors parameter containing an array of vectors in JSON format.
  • Example Request: /webhook/centroid?vectors=[[2,3,4],[4,5,6],[6,7,8]]
  • Output: Passes the received data to the next node for processing.

Step 2: Extract & Parse Vectors (Set Node)

  • Function: Converts the incoming vector data from a string to a proper JSON array.
  • Validation: Checks if the vectors parameter exists and is a valid array.
  • Output Example:
  {    "vectors": [[2,3,4],[4,5,6],[6,7,8]]  }

Step 3: Validate & Compute Centroid (Code Node)

  • Function: Ensures all vectors have the same number of dimensions and calculates the centroid.
  • Validation:
  • Confirms that the vectors array is not empty and contains only arrays.
  • Checks that each vector has the same length (dimensional consistency).
  • Computation:
  • Calculates the average for each dimension across all vectors.
  • Output:
  • Success: Returns the computed centroid.json{ "centroid": [4,5,6]}
  • Error: Provides a descriptive error message if validation fails.json{ "error": "Vectors have inconsistent dimensions."}
  • Script Example:
  const input = items[0].json;  const vectors = input.vectors;  if (!Array.isArray(vectors) 
  • Create a New Workflow:
  • Open n8n and create a new workflow.
  • Add Webhook Node:
  • Configure a Webhook node named Receive Vectors to handle GET requests with the vectors parameter.
  • Add Set Node:
  • Insert a Set node named Extract & Parse Vectors to convert the incoming string into a JSON array.
  • Add Code Node:
  • Add a Code node named Validate & Compute Centroid and input the provided script to validate and compute the centroid.
  • Add Respond to Webhook Node:
  • Include a Respond to Webhook node named Return Centroid Response to send the computed centroid or error message back to the client.
  • Activate and Test:
  • Activate the workflow and use tools like Postman or the n8n interface to send test requests and verify responses.

By following these steps, you can integrate this workflow into your projects, enabling seamless centroid calculations for your vector data.

🤖 Why Use This Automation Workflow?

  • Automated Validation: Ensures all input vectors are consistent in dimensions, preventing computational errors.
  • Reusable Template: Easily adaptable for different datasets and projects without significant modifications.
  • Efficient Centroid Calculation: Quickly computes the average position of a set of vectors, essential for data analysis and machine learning tasks.

👨‍💻 Who is This Workflow For?

This workflow is ideal for developers, data analysts, and engineers who need to automate the process of calculating the centroid of multiple vectors. Whether you're working on data preprocessing, spatial analysis, or any project requiring vector computations, this workflow streamlines the process.

🎯 Use Cases

  1. Data Preprocessing for Machine Learning: Aggregate feature vectors to determine central tendencies before model training.
  2. Geospatial Analysis: Calculate the central point of multiple geographical coordinates for mapping applications.
  3. Sensor Data Aggregation: Combine readings from multiple sensors to identify average states or conditions.

TL;DR

This n8n workflow provides an automated and reliable method for calculating the centroid of a set of vectors. By validating input dimensions and efficiently computing the average, it ensures accurate results suitable for various analytical applications.

Help us find the best n8n templates

About

A curated directory of the best n8n templates for workflow automations.

© n8n Templates | This is an independent community resource for n8n workflows. Not affiliated with, sponsored by, or endorsed by n8n.io or n8n.io GmbH.