I'm comparing a query embedding with my document embeddings using cosine_similarity and keep getting shape errors.
Why this happens
sklearn.metrics.pairwise.cosine_similarity(X, Y) compares every row of X with every row of Y. Both must be 2D, with samples as rows and features as columns. The two errors mean different things.
"Expected 2D array, got 1D array" means you passed a single vector of shape (d,) rather than (1, d).
"Incompatible dimension for X and Y matrices: X.shape[1] == 384 while Y.shape[1] == 768" means X and Y have different numbers of features, which almost always means they came from different embedding models.
Answer 1: Reshape a single vector to one row
The error message suggests reshape(-1, 1), which is wrong for this case. That turns your vector into 384 samples with one feature each. For one embedding, you want one sample with many features:
from sklearn.metrics.pairwise import cosine_similarity
query = query.reshape(1, -1) # (384,) -> (1, 384)
scores = cosine_similarity(query, doc_matrix)[0]
top5 = scores.argsort()[::-1][:5]
The [0] takes the single row of results, giving one score per document.
