Monday, 27 June 2022

Recommendations System - Course for +2 Students

 Recommendation System using pandas and Sklearn

Student Dataset1.csv

id,gender,stream,subject,marks,course,specialization
1,m,science,physics,78,btech,civil
2,f,commerce,maths,56,bsc,maths
3,m,humanities,history,79,ba,history
4,f,commerce,economics,88,bcom,hons
5,m,science,chemistry,79,bsc,biology
6,m,science,maths,98,btech,mech
7,f,commerce,physics,56,bsc,maths
8,m,humanities,history,79,ba,history
9,f,commerce,commerce,76,bcom,hons
10,m,science,physics,89,bsc,biology

Code snippet (Thanks to  Siddharth Sachdeva — August 25, 2021, Analytics Vidya)

#S1 importing necessary libraries.
import pandas as pd
data = pd.read_csv(
r"dataset1.csv")
#s2
descriptions =data['gender'] +' '+ data['subject'] + ' ' + data['stream'] +' '+ data['marks'].apply(str)
#Printing the first description
print(descriptions[0])
# s3 similarity matrix
#import sklearn.TfidfVector
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import linear_kernel
def create_similarity_matrix(new_description, overall_descriptions):
overall_descriptions.append(new_description)
# Define a tfidf [Term Frequency Inverse document frequency] vectorizer and remove all stopwords.
tfidf = TfidfVectorizer(stop_words="english")
tfidf_matrix = tfidf.fit_transform(overall_descriptions)
tfidf_matrix.shape
cosine_sim = linear_kernel(tfidf_matrix
,tfidf_matrix)
return cosine_sim
#s5 Sim Matrix reco
def get_recommendations(new_description,overall_descriptions):

cosine_sim = create_similarity_matrix(new_description
,overall_descriptions)
sim_scores =
list(enumerate(cosine_sim[-1]))
sim_scores =
sorted(sim_scores,key =lambda x:x[1],reverse= True )
sim_scores = sim_scores[
1:10]
indices = [i[
0]for i in sim_scores]
return data.iloc[indices]
# s6 Recommendations
new_description = pd.Series('m physics science 78')
recommendation= get_recommendations(new_description
,descriptions)
print(recommendation[0:3])
"""
id gender stream subject marks course specialization 0 1 m science physics 78 btech civil 6 7 f commerce physics 56 bsc maths 4 5 m science chemistry 79 bsc biology
"""

Simple not refined. But throw some light on basics of Recommendation System.

No comments:

Post a Comment

Making Prompts for Profile Web Site

  Prompt: Can you create prompt to craft better draft in a given topic. Response: Sure! Could you please specify the topic for which you...