-
Notifications
You must be signed in to change notification settings - Fork 0
/
reccomendation_system.py
94 lines (63 loc) · 3.58 KB
/
reccomendation_system.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# -*- coding: utf-8 -*-
"""Reccomendation system.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1LcXsKpxEThboSebnaA45-Y-f7IKfujGK
"""
import pandas as pd
import numpy as np
df = pd.read_csv("/content/MOCK_DATA (1).csv")
df
import pandas as pd
from sklearn.metrics.pairwise import cosine_similarity
# Convert categorical variables into numerical representations
df['College_ID'] = df['College'].astype('category').cat.codes
df['Project_Domain_ID'] = df['Project Domain'].astype('category').cat.codes
# Create a user-item matrix
user_item_matrix = df.pivot_table(index='College_ID', columns='Project_Domain_ID', aggfunc=lambda x: 1, fill_value=0)
# Calculate cosine similarity
cosine_sim = cosine_similarity(user_item_matrix)
# Function to get project domain recommendations for a college
def get_project_domain_recommendations(college_id, user_item_matrix, cosine_sim):
college_vector = user_item_matrix.loc[college_id].values.reshape(1, -1)
sim_scores = cosine_similarity(college_vector, user_item_matrix)
sim_scores = sim_scores.flatten()
# Get indices of project domains with the highest similarity scores
similar_project_domains = sim_scores.argsort()[::-1][1:]
# Get recommendations based on similar project domains
recommendations = user_item_matrix.columns[similar_project_domains]
return recommendations
# Example: Get recommendations for College ID 0 (JOHNS)
college_id = 1
project_domain_recommendations = get_project_domain_recommendations(college_id, user_item_matrix, cosine_sim)
print(f"Recommendations for College ID {college_id}:\n{project_domain_recommendations}")
from sklearn.model_selection import train_test_split
from sklearn.metrics import precision_score, recall_score
df['College_ID'] = df['College'].astype('category').cat.codes
df['Project_Domain_ID'] = df['Project Domain'].astype('category').cat.codes
# Create a user-item matrix
user_item_matrix = df.pivot_table(index='College_ID', columns='Project_Domain_ID', aggfunc=lambda x: 1, fill_value=0)
# Split the data into training and testing sets
train_data, test_data = train_test_split(df[['College_ID', 'Project_Domain_ID']], test_size=0.2, random_state=42)
def get_project_domain_recommendations(college_id, user_item_matrix, cosine_sim):
college_vector = user_item_matrix.loc[college_id].values.reshape(1, -1)
sim_scores = cosine_similarity(college_vector, user_item_matrix)
sim_scores = sim_scores.flatten()
# Get indices of project domains with the highest similarity scores
similar_project_domains = sim_scores.argsort()[::-1][1:]
# Get recommendations based on similar project domains
recommendations = user_item_matrix.columns[similar_project_domains]
return recommendations
# Evaluate precision and recall on the test set
precision_values = []
recall_values = []
for college_id in test_data['College_ID'].unique():
# Get actual liked project domains for the college in the test set
actual_liked_project_domains = set(test_data[test_data['College_ID'] == college_id]['Project_Domain_ID'])
# Get recommended project domains
recommended_project_domains = set(get_project_domain_recommendations(college_id, user_item_matrix, cosine_sim))
# Calculate precision and recall
precision = len(actual_liked_project_domains.intersection(recommended_project_domains)) / len(recommended_project_domains)
recall = len(actual_liked_project_domains.intersection(recommended_project_domains)) / len(actual_liked_project_domains)
precision_values.append(precision)
recall_values.append(recall)