Skip to content

Instantly share code, notes, and snippets.

@Dirga36
Created July 22, 2026 02:34
Show Gist options
  • Select an option

  • Save Dirga36/fff36ab09c5d53bf2e330908e132044e to your computer and use it in GitHub Desktop.

Select an option

Save Dirga36/fff36ab09c5d53bf2e330908e132044e to your computer and use it in GitHub Desktop.
import numpy as np
from sklearn.neighbors import NearestNeighbors
# Rows = patrons (anonymized), columns = books, values = checkout counts
checkout_matrix = np.array([
[3, 0, 1, 0, 2],
[0, 4, 0, 1, 0],
[2, 0, 3, 0, 1],
[0, 1, 0, 5, 0],
])
model = NearestNeighbors(n_neighbors=2, metric="cosine")
model.fit(checkout_matrix)
# Find patrons with similar borrowing patterns to patron index 0
distances, neighbor_indices = model.kneighbors(
[checkout_matrix[0]], n_neighbors=3
)
print("Most similar patron profiles:", neighbor_indices)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment