# -*- coding: utf-8 -*-
"""Homework 6 <NAME>

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1Jd4A3pPpg6KQr-xUkgwKePyJ2HzbPGK6
"""

import pandas as pd

"""Data for the book ratings is from http://www2.informatik.uni-freiburg.de/~cziegler/BX/

"""

df = pd.read_csv("BX-Book-Ratings.csv", sep = ";", encoding= 'unicode_escape' )

df.head(10)

#create a subset of users who left at least 30 reviews and movies with at least 30 reviews. This may seem high but with
#so many movies it was necessary for size
users = df.groupby('User-ID')['ISBN'].count()
keep = users[users > 30]
sample = df[df['User-ID'].isin(keep.index)]

movies = sample.groupby('ISBN')['User-ID'].count()
keep_movie = movies[movies > 30]
sample2 = sample[sample['ISBN'].isin(keep_movie.index)]
sample2.ISBN.nunique()

pivoted = sample2.pivot(columns = 'ISBN', index = "User-ID", values= "Book-Rating").fillna(0)

col = list(pivoted)

from sklearn.metrics.pairwise import euclidean_distances, cosine_distances

# recomendation matrix with book id as the index and column names
dist = pd.DataFrame(euclidean_distances(pivoted.transpose()), columns = col, index = col)

dist["000649840X"].sort_values()

names = pd.read_csv("BX-Books.csv", sep = ";", encoding= 'unicode_escape', error_bad_lines= False )

#the next three runs change the column and index names to match the book names so
#we know which ones we're talking about
col_id = list(dist)
idx_id = dist.index.tolist()
cols = pd.DataFrame(col_id)
idxs = pd.DataFrame(idx_id)
cols.columns = ["ISBN"]
idxs.columns = ["ISBN"]

m_cols = cols.merge(names, how = 'left', on = "ISBN")
m_idxs = idxs.merge(names, how = 'left', on = "ISBN")

c_titles = m_cols["Book-Title"]
i_titles = m_idxs['Book-Title']
dist.columns = c_titles
dist.index = i_titles
dist.head(10)

dist["Ender's Game (Ender Wiggins Saga (Paperback))"].sort_values()
#This is awesome because it recommends 2 books from the Ender Wiggins Saga (same as the book we searched)

# the rest just repeats with cosine distances instead 
cos_dist = pd.DataFrame(cosine_distances(pivoted.transpose()), columns = col, index = col)

col_id2 = list(cos_dist)
idx_id2 = cos_dist.index.tolist()
cols2 = pd.DataFrame(col_id2)
idxs2 = pd.DataFrame(idx_id2)
cols2.columns = ["ISBN"]
idxs2.columns = ["ISBN"]
m_cols2 = cols2.merge(names, how = 'left', on = "ISBN")
m_idxs2 = idxs2.merge(names, how = 'left', on = "ISBN")
c_titles2 = m_cols2["Book-Title"]
i_titles2 = m_idxs2['Book-Title']
cos_dist.columns = c_titles2
cos_dist.index = i_titles2

cos_dist.head(10)

cos_dist["Ender's Game (Ender Wiggins Saga (Paperback))"].sort_values()
# we get the same top sugguestions as the euclidean distances
# -*- coding: utf-8 -*-
"""Homework 6 <NAME>

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1Jd4A3pPpg6KQr-xUkgwKePyJ2HzbPGK6
"""

import pandas as pd

"""Data for the book ratings is from http://www2.informatik.uni-freiburg.de/~cziegler/BX/

"""

df = pd.read_csv("BX-Book-Ratings.csv", sep = ";", encoding= 'unicode_escape' )

df.head(10)

#create a subset of users who left at least 30 reviews and movies with at least 30 reviews. This may seem high but with
#so many movies it was necessary for size
users = df.groupby('User-ID')['ISBN'].count()
keep = users[users > 30]
sample = df[df['User-ID'].isin(keep.index)]

movies = sample.groupby('ISBN')['User-ID'].count()
keep_movie = movies[movies > 30]
sample2 = sample[sample['ISBN'].isin(keep_movie.index)]
sample2.ISBN.nunique()

pivoted = sample2.pivot(columns = 'ISBN', index = "User-ID", values= "Book-Rating").fillna(0)

col = list(pivoted)

from sklearn.metrics.pairwise import euclidean_distances, cosine_distances

# recomendation matrix with book id as the index and column names
dist = pd.DataFrame(euclidean_distances(pivoted.transpose()), columns = col, index = col)

dist["000649840X"].sort_values()

names = pd.read_csv("BX-Books.csv", sep = ";", encoding= 'unicode_escape', error_bad_lines= False )

#the next three runs change the column and index names to match the book names so
#we know which ones we're talking about
col_id = list(dist)
idx_id = dist.index.tolist()
cols = pd.DataFrame(col_id)
idxs = pd.DataFrame(idx_id)
cols.columns = ["ISBN"]
idxs.columns = ["ISBN"]

m_cols = cols.merge(names, how = 'left', on = "ISBN")
m_idxs = idxs.merge(names, how = 'left', on = "ISBN")

c_titles = m_cols["Book-Title"]
i_titles = m_idxs['Book-Title']
dist.columns = c_titles
dist.index = i_titles
dist.head(10)

dist["Ender's Game (Ender Wiggins Saga (Paperback))"].sort_values()
#This is awesome because it recommends 2 books from the Ender Wiggins Saga (same as the book we searched)

# the rest just repeats with cosine distances instead 
cos_dist = pd.DataFrame(cosine_distances(pivoted.transpose()), columns = col, index = col)

col_id2 = list(cos_dist)
idx_id2 = cos_dist.index.tolist()
cols2 = pd.DataFrame(col_id2)
idxs2 = pd.DataFrame(idx_id2)
cols2.columns = ["ISBN"]
idxs2.columns = ["ISBN"]
m_cols2 = cols2.merge(names, how = 'left', on = "ISBN")
m_idxs2 = idxs2.merge(names, how = 'left', on = "ISBN")
c_titles2 = m_cols2["Book-Title"]
i_titles2 = m_idxs2['Book-Title']
cos_dist.columns = c_titles2
cos_dist.index = i_titles2

cos_dist.head(10)

cos_dist["Ender's Game (Ender Wiggins Saga (Paperback))"].sort_values()
# we get the same top sugguestions as the euclidean distances
# -*- coding: utf-8 -*- Homework 6 <NAME>  Automatically generated by Colaboratory.  Original file is located at     https://colab.research.google.com/drive/1Jd4A3pPpg6KQr-xUkgwKePyJ2HzbPGK6 Data for the book ratings is from http://www2.informatik.uni-freiburg.de/~cziegler/BX/ #create a subset of users who left at least 30 reviews and movies with at least 30 reviews. This may seem high but with #so many movies it was necessary for size # recomendation matrix with book id as the index and column names #the next three runs change the column and index names to match the book names so #we know which ones we're talking about #This is awesome because it recommends 2 books from the Ender Wiggins Saga (same as the book we searched) # the rest just repeats with cosine distances instead # we get the same top sugguestions as the euclidean distances