Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Numpy 2d array with non-integer index

Background: I'm trying to build affinity matrix to feed into sklearn spectral clustering.

In this problem, I encounter the problem where numpy array indexes are 0-based integer, and for my application I'm using some sort of application specific ID (string-based, a random example "abc123"). I would like to create a 2d numpy array indexed by all the data points I have. For instance, given two points points = ["abc123", "xyz456"], I would have 2d numpy array whose row indices and column indexes are points. So that I could easily specify the distance between two points by something similar to arr["abc123"]["xyz456"] = dist

How could I achieve that? Thank you.

like image 374
clwen Avatar asked Sep 18 '26 23:09

clwen


1 Answers

Pandas can do this and much much more...

In [41]: import pandas as pd

In [122]: a = np.random.randint(100, size=(5, 3))

In [123]: a
Out[123]:
array([[53,  7, 34],
       [54, 56, 85],
       [ 0, 11, 83],
       [63, 28, 88],
       [65, 19, 44]])

In [124]: df = pd.DataFrame(a, index=list('abcde'), columns=list('xyz'))

In [125]: df
Out[125]:
    x   y   z
a  53   7  34
b  54  56  85
c   0  11  83
d  63  28  88
e  65  19  44

In [126]: df.loc[['a','d'], ['x','y']]
Out[126]:
    x   y
a  53   7
d  63  28

we can always get a Numpy array from the DataFrame using .values accessor:

In [127]: df.values
Out[127]:
array([[53,  7, 34],
       [54, 56, 85],
       [ 0, 11, 83],
       [63, 28, 88],
       [65, 19, 44]])

In [128]: df.loc[['a','d'], ['x','y']].values
Out[128]:
array([[53,  7],
       [63, 28]])
like image 60
MaxU - stop WAR against UA Avatar answered Sep 21 '26 12:09

MaxU - stop WAR against UA