1
$\begingroup$

I am not to figure out how to form a ndarray from existing numpy arrays. Suppose I have three arrays - index, distA, and distB. Now the I want to form a ndarray of these three arrays

What I have tried -

>>> indices = np.array([[5,7,4], [6,4,8]])
>>> distances = np.array([[0.2, 0.3, 0.4], [0.3, 0.4, 0.5]])
>>> np.column_stack((indices, distances))
array([[5. , 7. , 4. , 0.2, 0.3, 0.4],
   [6. , 4. , 8. , 0.3, 0.4, 0.5]])

But I want to form a table-like structure so that I can retrieve values in distA and distB.

5, 0.2, 0
7, 0.3, 0
4, 0.4, 0.4
6, 0, 0.3
8, 0, 0.5

Is it possible to do this with numpy?

$\endgroup$
  • 1
    $\begingroup$ I can't see how you aim to obtain a 5x3 matrix from two 2x3 arrays $\endgroup$ – yatu Dec 11 '18 at 8:55
  • $\begingroup$ @nixon Neither me, I was thinking there would be some to accomplish this. $\endgroup$ – Shivam Srivastava Dec 11 '18 at 9:02
  • 1
    $\begingroup$ Its just that I can't see the logic on how to build this ndarray, can you better explain please? $\endgroup$ – yatu Dec 11 '18 at 9:15
  • $\begingroup$ There would not be a direct way to do this, but if we could do a union of indices and find the corresponding distance in a and in b, then using pandas dataframe we can build this. Not sure if this a good question to ask, still a noob. $\endgroup$ – Shivam Srivastava Dec 11 '18 at 9:24
3
$\begingroup$

If your purpose is to deal with some data analysis from your arrays and have a nice view of it, try this using pandas:

import pandas as pd


df = pd.DataFrame({'distA' : np.array([0.2, 0.3, 0.4]),
                   'distB' : np.array([0.3, 0.4, 0.5])})


df

enter image description here

In the case you want to make computation with vectors, matrixes, tensors, etc .. NumPy is probably a good option. I didn't understand exactly what you've asked, though. Can you explain me what you're asking?

$\endgroup$
  • $\begingroup$ Yes, this is what I required, but it neither analysis nor computation on vectors, matrices, or tensors. It about the computation specific to distance calculation an efficient manner, and the algorithm I wrote, this formation was an integral part. Thanks. $\endgroup$ – Shivam Srivastava Dec 24 '18 at 4:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.