-1
def computefeatures(node_id):
    return [ord(node_id), len(node_id)]

I am computing features for my node ids which are a combo of a letter and a number. ord will not work, is there another work around for this.

my list is:

ln0
Out[88]: 
0     C1
1     C2
2     C3
3     C4
4     C5
5     C6
6     G1
7     I1
8     O1
9     P1
10    P2
11    P3
12    R1
13    R2
14    R3
15    R4
16    R5
17    R6
dtype: object
  • 3
    It is unclear what you are trying to do. What is the input? What should be the output? – bb1 Feb 16 '21 at 01:34
  • The first issue is that it seems like you're passing a string like 'C1' to the 'ord' function. ord() takes a char (single character), not a string. Beyond that you'll need to provide more information for any meaningful help – Boskosnitch Feb 16 '21 at 01:46
  • Apologies, each of the letter number combos is the label of a node in the graph. I am trying to compute a simple node feature using the node id. A float is required. –  Feb 16 '21 at 01:47
  • Are you trying to come up with an essentially arbitrary hashing of node id's to floats? – John Coleman Feb 16 '21 at 02:03

1 Answers1

0

If your node consist of a single letter followed by an integer, and all you want to do is map them to floats, this can be done in various ways.

One way is to convert your node_id into a hex string of the sort that is returned by the float method hex (for example, (3.14).hex() = '0x1.91eb851eb851fp+1'). Take the ord of the letter, convert it to a hexstring, and use it as the decimal part of the mantissa. Take the integer part and use it as the exponent. Once you create the string, map it to a float using the class method float.fromhex:

def compute_feature(node_id):
    n = ord(node_id[0])
    i = node_id[1:]
    hex_string = '0x1.' + hex(n)[2:] + 'p+' + i
    return float.fromhex(hex_string)

For example,

>>> compute_feature('C1')
2.5234375

This approach has the nice feature that you can use the float method hex and a small amount of parsing to recover the node id from the float.

John Coleman
  • 51,337
  • 7
  • 54
  • 119