<jupyter_start><jupyter_text>convert pandas to numpy<jupyter_code>rp_mat=rp.as_matrix()
m, n = rp.shape
from scipy.spatial.distance import cosine
# User similarity matrix
mat_users = np.zeros((m, m))#Return an array of zeros with the same shape and type as a given array.
for i in range(m):
    for j in range(m):
        if i != j:
            mat_users[i][j] = (1- cosine(rp_mat[i,:], rp_mat[j,:]))
        else:
            mat_users[i][j] = 0.
            
pd_users = pd.DataFrame(mat_users,index =rp.index ,columns= rp.index )
pd_users.head(n=7)
# Finding similar users
def topn_simusers(uid = 16,n=5):
    users = pd_users.loc[uid,:].sort_values(ascending = False)
    topn_users = users.iloc[:n,]
    topn_users = topn_users.rename('score')    
    print ("Similar users as user:",uid)
    return pd.DataFrame(topn_users)

print (topn_simusers(uid=17,n=10))  
# Finding most rated movies of a user
def topn_movieratings(uid = 355,n_ratings=10):    
    uid_ratings = ratings.loc[ratings['userId']==uid]
    uid_ratings = uid_ratings.sort_values(by='rating',ascending = [False])
    print ("Top",n_ratings ,"movie ratings of user:",uid)
    return uid_ratings.iloc[:n_ratings,]    

print (topn_movieratings(uid=596,n_ratings=10))<jupyter_output>Top 10 movie ratings of user: 596
       userId  movieId  rating                                   title
89645     596     4262     5.0                         Scarface (1983)
89732     596     6874     5.0                Kill Bill: Vol. 1 (2003)
89353     596      194     5.0                            Smoke (1995)
89546     596     2329     5.0               American History X (1998)
89453     596     1193     5.0  One Flew Over the Cuckoo's Nest (1975)
89751     596     8132     5.0                        Gladiator (1992)
89579     596     2858     5.0                  American Beauty (1999)
89365     596      296     5.0                     Pulp Fiction (1994)
89587     596     2959     5.0                       Fight Club (1999)
89368     596      318     5.0        Shawshank Redemption, The (1994)
{'fraction_non_alphanumeric': 0.09970958373668926, 'fraction_numerical': 0.09825750242013553, 'mean_word_length': 2.030791788856305, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '175269', 'license': 'MIT', 'n_tokens_mistral': 829, 'n_tokens_neox': 698, 'n_words': 187, 'path': 'section8/.ipynb_checkpoints/movie1-checkpoint.ipynb', 'repo_name': 'sanjanapackt/PacktPublishing-Clustering-and-Classification-with-Machine-Learning-in-Python', 'size': '2066'}