Update Netflix_Recommendation_Notebook_Code
Browse files
Netflix_Recommendation_Notebook_Code
CHANGED
@@ -8,35 +8,35 @@ import numpy as np
|
|
8 |
import pandas as pd
|
9 |
from tqdm import tqdm # For tracking progress in batches
|
10 |
|
11 |
-
#
|
12 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
13 |
print(f"Using device: {device}")
|
14 |
|
15 |
-
#
|
16 |
dataset = pd.read_csv('/kaggle/input/d/infamouscoder/dataset-netflix-shows/netflix_titles.csv')
|
17 |
|
18 |
-
#
|
19 |
model = SentenceTransformer("all-MiniLM-L6-v2").to(device)
|
20 |
|
21 |
-
#
|
22 |
def combine_description_title_and_genre(description, listed_in, title):
|
23 |
return f"{description} Genre: {listed_in} Title: {title}"
|
24 |
|
25 |
-
#
|
26 |
dataset['combined_text'] = dataset.apply(lambda row: combine_description_title_and_genre(row['description'], row['listed_in'], row['title']), axis=1)
|
27 |
|
28 |
-
#
|
29 |
batch_size = 32
|
30 |
embeddings = []
|
31 |
|
32 |
for i in tqdm(range(0, len(dataset), batch_size), desc="Generating Embeddings"):
|
33 |
batch_texts = dataset['combined_text'][i:i+batch_size].tolist()
|
34 |
batch_embeddings = model.encode(batch_texts, convert_to_tensor=True, device=device)
|
35 |
-
embeddings.extend(batch_embeddings.cpu().numpy()) #
|
36 |
|
37 |
-
#
|
38 |
embeddings = np.array(embeddings)
|
39 |
|
40 |
-
#
|
41 |
np.save("/kaggle/working/netflix_embeddings.npy", embeddings)
|
42 |
dataset[['show_id', 'title', 'description', 'listed_in']].to_csv("/kaggle/working/netflix_metadata.csv", index=False)
|
|
|
8 |
import pandas as pd
|
9 |
from tqdm import tqdm # For tracking progress in batches
|
10 |
|
11 |
+
# check if GPU is available
|
12 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
13 |
print(f"Using device: {device}")
|
14 |
|
15 |
+
# load dataset
|
16 |
dataset = pd.read_csv('/kaggle/input/d/infamouscoder/dataset-netflix-shows/netflix_titles.csv')
|
17 |
|
18 |
+
# load model to GPU if available
|
19 |
model = SentenceTransformer("all-MiniLM-L6-v2").to(device)
|
20 |
|
21 |
+
# combine fields (title, genre, description) for embeddings
|
22 |
def combine_description_title_and_genre(description, listed_in, title):
|
23 |
return f"{description} Genre: {listed_in} Title: {title}"
|
24 |
|
25 |
+
# create combined text column
|
26 |
dataset['combined_text'] = dataset.apply(lambda row: combine_description_title_and_genre(row['description'], row['listed_in'], row['title']), axis=1)
|
27 |
|
28 |
+
# generate embeddings in batches to save memory
|
29 |
batch_size = 32
|
30 |
embeddings = []
|
31 |
|
32 |
for i in tqdm(range(0, len(dataset), batch_size), desc="Generating Embeddings"):
|
33 |
batch_texts = dataset['combined_text'][i:i+batch_size].tolist()
|
34 |
batch_embeddings = model.encode(batch_texts, convert_to_tensor=True, device=device)
|
35 |
+
embeddings.extend(batch_embeddings.cpu().numpy()) # move to CPU to save memory
|
36 |
|
37 |
+
# convert list to numpy array
|
38 |
embeddings = np.array(embeddings)
|
39 |
|
40 |
+
# save embeddings and metadata
|
41 |
np.save("/kaggle/working/netflix_embeddings.npy", embeddings)
|
42 |
dataset[['show_id', 'title', 'description', 'listed_in']].to_csv("/kaggle/working/netflix_metadata.csv", index=False)
|