Created
July 22, 2026 02:34
-
-
Save Dirga36/fff36ab09c5d53bf2e330908e132044e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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