Installation and Use

Using OpenRank SDK - Hubs & Authorities to generate your first set of rankings.

Amazon often recommends items with the "Customers who bought this also bought" feature. This can be achieved by tracing a product-customer-product round trip: from the original product to all its purchasers and back to all products also purchased by them.

We can replicate this using the OpenRank SDK by focusing on actor-to-artifact relationships, mirroring them bidirectionally, and running EigenTrust with the original item as the seed peer.

In this example, we will check for book P by seeding trust in P and observing the resulting trust scores for other books.

Installation

pip install openrank-sdk
from openrank_sdk import EigenTrust

Here's the Github repo.

Parameters

Local Trust (lt)

i

From Peer ID: Address of the user initiating the interest arc

*(Required)

j

To Peer ID: Address of the entity receiving trust

*(Required)

v

Value of Interaction: Score/Weight/Value of the trust

*(Required)

Seed Peers (PreTrust / pt)

I

Seed Peers: Hub we want to get recommendations of

Defaults to using all i in the Local Trust as Seed Peers

V

Trust Score: set to 1

1/n (n = no of peers)

Parameters

Alpha

Needs to be 0

0.5 (50%) as the strength

max_iter

The maximum number of iterations. Needs to be 2

NA

Input:

We use the following CSV dataset representing user-to-book (vested interest) arcs. Here, users are represented by A, B, C, D, and E, while books are represented by P, Q, R, S, and T.

v value cannot be negative or 0.

Sample local trust data:

lt2 = [
    {"i": "A", "j": "P", "v": 10},
    {"i": "A", "j": "Q", "v": 5},
    {"i": "B", "j": "P", "v": 10},
    {"i": "B", "j": "Q", "v": 100},
    {"i": "B", "j": "R", "v": 55},
    {"i": "C", "j": "P", "v": 55},
    {"i": "C", "j": "Q", "v": 50},
    {"i": "C", "j": "R", "v": 30},
    {"i": "C", "j": "T", "v": 105},
    {"i": "D", "j": "R", "v": 100},
    {"i": "D", "j": "T", "v": 20},
    {"i": "E", "j": "P", "v": 130}
]

Here we have purposely named it localtrust2 as to build the final localtrust we need extend this array to also include arcs from books back to users

Sample Local Trust 2 CSV:

i,j,v
A,P,10
A,Q,5
B,P,10
B,Q,100
B,R,55
C,P,55
C,Q,50
C,R,30
C,T,105
D,R,100
D,T,20
E,P,130

In this example:

  • A, B, C, D, E: Users who have purchased books.

  • P, Q, R, S, T: Books that have been purchased by the users.

  • v: The strength of the connection between users and books, representing how many times the book was purchased or a similar metric.

Pre-trust Vector:

We initialize the pre-trust vector with a trust value for book P:

pretrust = [dict(i='P', v=1)]

Here, this will let us get rankings of other books as compared to P based on customer activity

Running:

The EigenTrust algorithm is executed by first transforming the input trust relationships. We start with localtrust2, a list of trust values from users to items, and generate localtrust by adding both the original and symmetric (item-to-user) trust entries.

Pretrust values, which provide initial trust levels, are also defined. After initializing the EigenTrust instance with specified parameters, the algorithm is run using localtrust and pretrust. Finally, the results are sorted by item identifier and printed. This process ensures both direct and mutual trust relationships are considered in the computation of global trust values.

Note: Please ensure the following settings are applied:

  • Set the alpha parameter to 0.

  • Set the pretrust score to 1.

  • Set the maximum number of iterations to 2.

from openrank_sdk import EigenTrust
# api_key = 'your_api_key'

# Inputs
localtrust2=[
    {"i": "A", "j": "P", "v": 10},
    {"i": "A", "j": "Q", "v": 5},
    ...
]
pretrust = [dict(i='P', v=1)]
# Initializing localtrust
localtrust=[]

# Process the arcs
for row in localtrust2:
    localtrust.append(row)
    # Create symmetric book-to-user arc
    localtrust.append({'i': row['j'], 'j': row['i'], 'v': row['v']})

# Initialize EigenTrust with alpha=0 and max_iter=2
eigentrust = EigenTrust(alpha=0, max_iter=2)

# Option A - Run EigenTrust algorithm - Use local variable
globaltrust = eigentrust.run_eigentrust(localtrust, pretrust)
# Sort results by item identifier
globaltrust.sort(key=lambda row: row['i'])

# Option B - Using CSV
# lt2.csv contains user-to-repo arcs from {A,B,C,D,E} to {P,Q,R,S,T}
import csv
with open('localtrust2.csv', newline='') as csvf:
    for row in csv.DictReader(csvf):
        row['v'] = float(row['v'])
        localtrust.append(row)
        # replicate user-to-repo arcs into symmetric repo-to-user arcs
        row['i'], row['j'] = row['j'], row['i']
        localtrust.append(row)
globaltrust = eigentrust.run_eigentrust('/localtrust2.csv', pretrust)

# Print the results
from pprint import pprint
pprint(globaltrust)

Output:

The output will be a list of dictionaries, where each dictionary contains an item (P, Q, R, T) and its corresponding trust score (v).

[{'i': 'P', 'v': 0.7311067997043607},
 {'i': 'Q', 'v': 0.10171840354767185},
 {'i': 'R', 'v': 0.049796747967479675},
 {'i': 'T', 'v': 0.11737804878048781}]
  • P has the highest trust score (0.731), indicating users with a vested interest in P are highly likely to be interested in it again.

  • Q and T also have significant trust scores (0.102 and 0.117 respectively), suggesting they are relevant recommendations.

  • R has a lower trust score (0.050), showing a lesser degree of interest.

  • S has no entries, so it naturally has no trust score.

This method effectively replicates the "Customers who bought this also bought" recommendation feature using the OpenRank SDK. Adjust the parameters alpha and max_iter as needed to suit different datasets or recommendation scenarios.

Note as max_iter is made to increase, the rankings converge more proportionally.

Last updated

Logo

Copyright 2024