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
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 29import 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
