KX Community

Find answers, ask questions, and connect with our KX Community around the world.
KX Community Guidelines

Home Forums PyKX How to transform pykx list of lists to .np() matrix

  • How to transform pykx list of lists to .np() matrix

    Posted by marcosvm13 on August 29, 2023 at 12:00 am

    Hi!  I am trying to transform a list of lists to a numpy matrix. For example, this array:

     

    arr = kx.toq(np.random.rand(3,2))

     

    We tried to transform it with the .np method:

     

    arr.np()

     

    But the result of this was an array of objects, which cannot be easily translated to a normal array:

     

    array([array([0.9092564 , 0.40209283]), array([0.06786111, 0.50098914]), array([0.04279995, 0.50821769])], dtype=object)

     

    Now we are using:

    np.array(arr.py())

     It works but this solution is very slow. Is there a way to transform lists of q’s lists into numpy arrays without going through python?

    Thanks for your help!

    Marcos

    marcosvm13 replied 9 months, 3 weeks ago 2 Members · 1 Reply
  • 1 Reply
  • cmccarthy1

    Member
    August 29, 2023 at 12:00 am

    Hey,

    There isn’t presently a way for multi-dimensional arrays to be returned in a different representation by default, if the returned data is a ‘rectangular’ data representation you can call np.stack on the data post processing to flatten it’s representation if needed

    >>> import pykx as kx 
    >>> import numpy as np 
    >>> arr = kx.toq(np.random.rand(3,2))
    >>> np.stack(arr.np()) 
    array([[0.98669419, 0.47121857], 
    [0.27329073, 0.15091775], 
    [0.30553385, 0.32638803]])

     

Log in to reply.