# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# +
import comet_ml
# import comet_ml in the top of your file
from comet_ml import Experiment
    
# Add the following code anywhere in your machine learning file
experiment = Experiment(api_key="<KEY>",
                        project_name="unsupervised-rm2", workspace="team-jhb-ss3",log_code=True)
# -

import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from wordcloud import WordCloud

train = pd.read_csv(r"C:\Users\Dell\Documents\Dev\EDSA\Unsupervised_learning\train.csv")
train.head()

movies = pd.read_csv(r"C:\Users\Dell\Documents\Dev\EDSA\Unsupervised_learning\movies.csv")
movies.head()

df = pd.merge(train,movies, on='movieId')

df.drop(['genres'], axis =1, inplace=True)

df = df.dropna(axis=0, subset=['title'])
rating_count = (df.groupby(by=['title'])['rating'].count().reset_index().rename(columns={'rating':'totalRatingCount'})[['title','totalRatingCount']])
rating_count.head()

rating_with_totalRatingCount = df.merge(rating_count,left_on='title',right_on='title', how='left')
rating_with_totalRatingCount.head()

pd.set_option('display.float_format' ,lambda x: '%.3f' % x)
print(rating_count['totalRatingCount'].describe())

data = train

from surprise import Reader
from surprise import Dataset
from surprise.model_selection import KFold
from surprise.model_selection import cross_validate
from surprise import NormalPredictor
from surprise import KNNBasic
from surprise import KNNWithMeans
from surprise import KNNWithZScore
from surprise import KNNBaseline
from surprise import SVD
from surprise import SVDpp
from surprise import NMF
from surprise import SlopeOne
from surprise import CoClustering
from surprise.accuracy import rmse
from surprise import accuracy
from surprise.model_selection import train_test_split
from surprise.model_selection import GridSearchCV
from collections import defaultdict

# +
ratings_dict = {'itemID': list(train.movieId),
                'userID': list(train.userId),
                'rating': list(train.rating)}

df = pd.DataFrame(ratings_dict)
df.shape

# +

reader = Reader(line_format='user item rating timestamp', sep='\t')

reader = Reader(rating_scale=(0.5, 5.0))

data = Dataset.load_from_df(df[['userID', 'itemID', 'rating']], reader)


# +
from surprise.model_selection import train_test_split

from surprise.model_selection import KFold

kf = KFold(n_splits=10)
kf.split(data)
algo = SVD(n_factors=35, n_epochs=25, lr_all=0.008, reg_all=0.08)
trainset = data.build_full_trainset()
algo.fit(trainset)
# -

experiment.log_model('SVD',algo)

test = pd.read_csv(r"C:\Users\Dell\Documents\Dev\EDSA\Unsupervised_learning\test.csv")
test.head()

test = test.rename(columns={'movieId': 'itemId'})

test['Id'] = test[['userId', 'itemId']].apply(tuple, axis=1)

test['rating'] = test['Id'].apply(lambda x: algo.predict(x[0],x[1])[3])

test['Id'] = test['Id'].apply(lambda x: str(x[0])+'_'+ str(x[1]))

sub = test[['Id','rating']]

sub.to_csv('submission.csv', index=False)

experiment.end()

StarcoderdataJupyterScriptsDedupFiltered