<jupyter_start><jupyter_text>**WELCOME TO THIS NOTEBOOK**<jupyter_code>from google.colab import drive
drive.mount('/content/drive')<jupyter_output>Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount("/content/drive", force_remount=True).<jupyter_text>Let's install pyspark<jupyter_code>!pip install pyspark<jupyter_output>Requirement already satisfied: pyspark in /usr/local/lib/python3.6/dist-packages (3.0.1)
Requirement already satisfied: py4j==0.10.9 in /usr/local/lib/python3.6/dist-packages (from pyspark) (0.10.9)<jupyter_text>Importing the modules<jupyter_code>from pyspark.sql import SparkSession
from pyspark.sql.functions import count, desc , col, max
from pyspark.ml.feature import  StringIndexer
from pyspark.ml import Pipeline
from pyspark.ml.recommendation import ALS
from pyspark.ml.tuning import TrainValidationSplit, ParamGridBuilder<jupyter_output><empty_output><jupyter_text>Creating the spark session<jupyter_code>spark = SparkSession.builder.appName("lastfm").getOrCreate()<jupyter_output><empty_output><jupyter_text>Loading the dataset<jupyter_code>file_path = '/content/drive/MyDrive/datafiles/dataset/listenings.csv'
df_listening = spark.read.format('csv').option('header',True).option('inferSchema',True).load(file_path)
df_listening.show()<jupyter_output>+-----------+-------------+--------------------+---------------+--------------------+
|    user_id|         date|               track|         artist|               album|
+-----------+-------------+--------------------+---------------+--------------------+
|000Silenced|1299680100000|           Price Tag|       Jessie J|         Who You Are|
|000Silenced|1299679920000|Price Tag (Acoust...|       Jessie J|           Price Tag|
|000Silenced|1299679440000|Be Mine! (Ballad ...|          Robyn|            Be Mine!|
|000Silenced|1299679200000|            Acapella|          Kelis|            Acapella|
|000Silenced|1299675660000|   I'm Not Invisible|      The Tease|   I'm Not Invisible|
|000Silenced|1297511400000|Bounce (Feat NORE...|       MSTRKRFT|         Fist of God|
|000Silenced|1294498440000|Don't Stop The Mu...|        Rihanna|Addicted 2 Bassli...|
|000Silenced|1292438340000|               ObZen|      Meshuggah|               ObZen|
|000Silenced|1292437740000|   Yama's Messengers|      [...]<jupyter_text>Cleaning tables<jupyter_code>df_listening = df_listening.drop('date')
df_listening.show()
df_listening =df_listening.na.drop()
df_listening.show()<jupyter_output><empty_output><jupyter_text>Let's Perform some aggregationto see how many times each user has listened to specific track<jupyter_code>row_number=df_listening.count()
col_number=len(df_listening.columns)
print(row_number,col_number)
df_listening_agg = df_listening.select('user_id','track').groupby('user_id','track').agg(count('*').alias('count')).orderBy('user_id')
df_listening_agg.show()
row_numbers =df_listening_agg.count()
column_numbers =len(df_listening_agg.columns)
print(row_numbers,column_numbers)
df_listening_agg = df_listening_agg.limit(20000)<jupyter_output><empty_output><jupyter_text>Let's convert the user id and track columns into unique integers<jupyter_code>indexer = [StringIndexer(inputCol=col,outputCol = col+'_index').fit(df_listening_agg)for col in list(set(df_listening_agg.columns)-set(['count']))]
pipline =Pipeline(stages=indexer)
data=pipline.fit(df_listening_agg).transform(df_listening_agg)
data.show()
data =data.select('user_id_index','track_index','count').orderBy('user_id_index')
data.show()<jupyter_output><empty_output><jupyter_text>Train and Test data<jupyter_code>(training,testing)=data.randomSplit([0.5,0.5])<jupyter_output><empty_output><jupyter_text>Let's Create our Model<jupyter_code>USERID='user_id_index'

TRACK ='track_index'
COUNT='count'
als=ALS(maxIter=5,regParam=0.01,userCol=USERID,itemCol=TRACK,ratingCol=COUNT)
model=als.fit(training)

predictions=model.transform(testing)<jupyter_output><empty_output><jupyter_text>Generate top 10 Track recommendations for each user<jupyter_code>recs = model.recommendForAllUsers(10)
recs.show()
recs.take(1)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1577804583835947, 'fraction_numerical': 0.04318455971049457, 'mean_word_length': 4.774373259052925, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 42, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '330103', 'license': 'MIT', 'n_tokens_mistral': 1529, 'n_tokens_neox': 1406, 'n_words': 215, 'path': 'Music Recommendation/MusicRecommendations.ipynb', 'repo_name': 'anuraglahon16/Project-Baseline', 'size': '4145'}