<jupyter_start><jupyter_text>Collaborative Filtering : <NAME><jupyter_code>import pandas as pd
import numpy as np
df = pd.read_csv('views.csv')
df2 = pd.read_csv('users.csv',index_col = '_id')
df1 = pd.read_csv('posts.csv',index_col = '_id')
#df.head()

z = np.array(df['timestamp'])
ts = [1 for i in z]
df.insert(3,"ts",ts)
df.head()
rec = df.pivot_table(index= 'post_id',columns='user_id',values = 'ts').fillna(0)
rec.head()
from scipy.sparse import csr_matrix
rec_matrix = csr_matrix(rec.values)
from sklearn.neighbors import NearestNeighbors

model_knn = NearestNeighbors(metric = 'cosine', algorithm = 'brute')
model_knn.fit(rec_matrix)
def post_id(query_index):
  distances, indices = model_knn.kneighbors(rec.iloc[query_index,:].values.reshape(1, -1), n_neighbors = 6)
  x= []
  for i in range(0, len(distances.flatten())):
    x.append(rec.index[indices.flatten()[i]])

  x.remove(x[0])
  return(x)
def post_recommend(df1,rec,query_index,x):
  print('Recommendations for post id {0}'.format(rec.index[query_index]))
  print(df1.loc[rec.index[query_index]].head())
  print("*************************************************************************************")
  df1 = pd.read_csv('posts.csv',index_col = '_id')
  return df1.loc[x]  
rec2 = df.pivot_table(index= 'user_id',columns='post_id',values = 'ts').fillna(0)
rec2_matrix = csr_matrix(rec2.values)
model_knn1 = NearestNeighbors(metric = 'cosine', algorithm = 'brute')
model_knn1.fit(rec2_matrix)
def user_id(query_index2):
  distances2, indices2 = model_knn1.kneighbors(rec2.iloc[query_index2,:].values.reshape(1, -1), n_neighbors = 6)
  y= []
  for i in range(0, len(distances2.flatten())):
    y.append(rec2.index[indices2.flatten()[i]])

  y.remove(y[0])
  return y
def user_post_id(df,rec2,query_index2,y):
  post_ = []
  for i in y:
    post_list = []
    post = df.loc[df['user_id'] == i]
    post2 = post['post_id']
    a = np.array(post2)
    post_.append(a)
  user = df.loc[df['user_id'] ==rec2.index[query_index2]]
  user_post = user['post_id']
  b = np.array(user_post)
  c = []
  for j in post_:
    for i in j:
      if i in b:
        continue
      else:
        c.append(i)
  c = c[0:5]
  return c
def user_recommend(df2,df1,rec2,c,query_index2):
  print('Recommendations for user id {0}'.format(rec2.index[query_index2]))
  print(df2.loc[rec2.index[query_index2]].head())
  print("*************************************************************************************")
  #df1 = pd.read_csv('posts.csv',index_col = '_id')
  return df1.loc[c]<jupyter_output><empty_output><jupyter_text>Post-based Recommendation<jupyter_code>post = input('Enter post_id : ')
count1 = 0
for i in rec.index:
  if i == post:
    break
  else:
    count1+=1
query_index = count1
rec.index[query_index]
post_recommend(df1,rec,query_index,post_id(query_index))<jupyter_output>Enter post_id : 5ed1ff0276027d35905cc60d
Recommendations for post id 5ed1ff0276027d35905cc60d
title         AWS services and how to launch OS on AWS Cloud
category                 Computer Technology|Cloud Computing
 post_type                                              blog
Name: 5ed1ff0276027d35905cc60d, dtype: object
*************************************************************************************<jupyter_text>User-based Recommendation<jupyter_code>user = input('Enter user_id : ')
count2 = 0
for i in rec2.index:
  if i == user:
    break
  else:
    count2+=1
query_index2 = count2
rec2.index[query_index2]
user_recommend(df2,df1,rec2,user_post_id(df,rec2,query_index2,user_id(query_index2)),query_index2)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.20748394303267242, 'fraction_numerical': 0.03518570231778833, 'mean_word_length': 5.175862068965517, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '270975', 'license': 'MIT', 'n_tokens_mistral': 1397, 'n_tokens_neox': 1333, 'n_words': 235, 'path': 'Collaborative_filtering_debashishmalakar.ipynb', 'repo_name': 'hsihsabed/Recommendation-System', 'size': '3592'}