2

I have a function

def get_similar_row(rows, target):
    """Return the index of the most similar row"""
    return np.argmax(cosine_similarity(rows, [target]))


get_similar_row([[1191, 3, 0, 1, 1], 
                 [3251, 2, 1, 0, 0], 
                 [1641, 1, 1, 1, 0]], [2133, 3, 0, 0, 1])

Instead of manually inputting numbers while calling the function, I want to pass all rows of my data frame df such that I skip the id and pass in all other variables for all rows. This is for the rows parameter of the function.

id  size    numberOfPlants  balcony   available  publicTransport    
0   1191    3               0         1           1
1   3251    2               1         0           0
2   1641    1               1         1           0
3   2133    3               0         0           1

1 Answer 1

2

Use DataFrame.drop for remove id column, convert to numpy array and pass to function:

#target id
id1 = 3

#convert id to index if necessary
df1 = df.set_index('id')

#selected row by id
target = df1.loc[id1]

#removed target row from original data
get_similar_row(df1.drop(id1).to_numpy(), target.to_numpy())
Sign up to request clarification or add additional context in comments.

3 Comments

For example, if I want to check which row is the most similar to my third row. Here, the third row is target. The id is 2. So in my function, get_similar_row(rows, target) I want to pass the the third row just be specifying the id 2. But I want to ensure that while calculating the similarity within the function, the idfield is not included.
Then I need to compare my target with all other rows of the dataframe. So I need to pass all other rows in the rows parameter. But I want to exclude the third row since that's the original row that I am using as target. I also want to include the idfield from similarity calculation
@x89 - for me mor clear, can you check edit?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.