TransWikia.com

Have several 128 elements lists, need to obtain distance between them

Database Administrators Asked by misterghost on December 7, 2020

Is storing them in a CUBE type field and then getting euclidean distance, (using <->) the only way?

I can not save them as a list of floats, since i get: “can’t adapt type numpyndarray”

Can not cast a type bytea to type cube, either.

Cube extension is installed, i can convert the list to type bytea using psycopg2, but nothing seems to work.

What am i missing?

I’m using postgresql 10.4, python 3, postgresapp 2.1.4 and postico

One Answer

I can not save them as a list of floats, since i get: "can't adapt type numpyndarray"

Quick google search shows numpyndarray as being SciPy's "N-dimensional array". This can hold what PostgreSQL calls a cube and vise-versa.

You assume that there is a translation layer that takes numpyndarray and converts the types to a cube for you. There likely isn't. You could provide such a layer, or extend your PostgreSQL connector/driver to provide that layer (see this for information on psycopg), but shy of that you'll have to go by way of text.

That means you'll be calling cube() or providing a text string and a cast to cube.

SELECT '(0,1,2,3,4,5)'::cube;
SELECT CAST ( '(0,1,2,3,4,5)' AS cube );

Your job is to convert numpyndarray to a textual representation like the above, and then to convert it back again.

Cubes can also be constructed as float[] which you may find easier if your DB layer supports that,

SELECT cube(ARRAY[0,1,2,3,4,5]::float[]); ## Array-constructor for float[]
SELECT cube('{1,2,3,4,5}'::float[]);      ## Text-constructor for float[]

Answered by Evan Carroll on December 7, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP