Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Movie Recommender
#1
Video 
Hello I'm trying to run this code, but I get this error on line 47 that,
Another way I can fix it is if I add an index column to the dataframe from 0 to the end of the row.
Please can someone tell me how to go about that?
Thanks
Error:
index 19995 is out of bounds for axis 0 with size 29
import pandas as pd
import numpy as np
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.metrics.pairwise import cosine_similarity
###### helper functions. Use them when needed #######
def get_title_from_index(id):
	return df[df.id == id]["title"].values[0]
 
def get_index_from_title(title):
	return df[df.title == title]["id"].values[0]

##################################################
# print(df.title.values[3])
##Step 1: Read CSV File
df = pd.read_csv("/content/tmdb_5000_movies1.csv", encoding= 'latin1')
#print df.columns
##Step 2: Select Features
# print(df.head(2))
features = ['keywords','genres']
##Step 3: Create a column in DF which combines all selected features
for feature in features:
	df[feature] = df[feature].fillna('')

def combine_features(row):
	# try:
  return row['keywords'] +" "+row["genres"]
	# except:
		# print "Error:", row	

df["combined_features"] = df.apply(combine_features,axis=1)

#print "Combined Features:", df["combined_features"].head()

##Step 4: Create count matrix from this new combined column
cv = CountVectorizer()

count_matrix = cv.fit_transform(df["combined_features"])

##Step 5: Compute the Cosine Similarity based on the count_matrix
cosine_sim = cosine_similarity(count_matrix) 
movie_user_likes = "Avatar"

## Step 6: Get index of this movie from its title
movie_index = get_index_from_title(movie_user_likes)
print(movie_index)

similar_movies =  list(enumerate(cosine_sim[movie_index]))

# Step 7: Get a list of similar movies in descending order of similarity score
sorted_similar_movies = sorted(similar_movies,key=lambda x:x[1],reverse=True)

## Step 8: Print titles of first 50 movies
i=0
for element in sorted_similar_movies:
		print(get_title_from_index(element[0]))
		i=i+1
		if i>5:
			break
Reply
#2
Try: similar_movies = list(enumerate(cosine_sim(movie_index)))
Reply
#3
(Mar-28-2021, 09:24 PM)Larz60+ Wrote: Try: similar_movies = list(enumerate(cosine_sim(movie_index)))

Error:
TypeError: 'numpy.ndarray' object is not callable
Reply
#4
try:
similar_movies = list(enumerate(cosine_sim(movie_index)))
Larz60+ write Feb-21-2023, 02:10 PM:
Note the post you are replying to is over 1 year old. I doubt that it will be seen
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  memory issue loading movie to numpy array djf123 1 2,295 Nov-07-2019, 06:19 AM
Last Post: ThomasL
  create a movie with python masoud 4 7,488 May-23-2017, 03:55 PM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020