Skip to content

Instantly share code, notes, and snippets.

@Dminor7
Last active May 17, 2023 06:21
Show Gist options
  • Save Dminor7/4cb2e0bd69db8ce7d6d9b38a5463b426 to your computer and use it in GitHub Desktop.
Save Dminor7/4cb2e0bd69db8ce7d6d9b38a5463b426 to your computer and use it in GitHub Desktop.
Calculate Average of vectors in Presto
SELECT
customer_id,
reduce(
event_vectors,
repeat(0.0, 512), -- dimension of vector here it is 512
(sum_array, element_array) -> zip_with(sum_array, element_array, (s, e) -> s + e),
state_array -> transform(state_array, s -> s / cardinality(event_vectors))
) as mean_event_vector
FROM(
SELECT
customer_id,
ARRAY_AGG(
event_vector
) AS event_vectors
FROM customer_behaviour_features
WHERE date BETWEEN '{start_date}' AND '{end_date}'
GROUP BY customer_id
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment