|
import pandas as pd |
|
from surprise import Dataset, Reader |
|
|
|
laptop_df = pd.read_csv('laptop_data.csv') |
|
user_df = pd.read_csv('user_data.csv') |
|
|
|
laptop_df = laptop_df.fillna(0) |
|
user_df = user_df.fillna(0) |
|
|
|
|
|
reader = Reader(rating_scale=(1, 5)) |
|
data = Dataset.load_from_df(user_df[['User_ID', 'Laptop_ID', 'Rating']], reader) |
|
|
|
from surprise.model_selection import train_test_split |
|
from surprise import SVD |
|
from surprise import accuracy |
|
|
|
|
|
trainset, testset = train_test_split(data, test_size=0.2, random_state=42) |
|
|
|
|
|
model = SVD() |
|
model.fit(trainset) |
|
|
|
def recommend_laptops(age=None, category=None, gender=None, user_id=None, num_recommendations=5): |
|
if user_id is not None: |
|
|
|
user_ratings = user_df[user_df['User_ID'] == user_id] |
|
user_unrated_laptops = laptop_df[~laptop_df['Laptop_ID'].isin(user_ratings['Laptop_ID'])] |
|
user_unrated_laptops['Predicted_Rating'] = user_unrated_laptops['Laptop_ID'].apply(lambda x: model.predict(user_id, x).est) |
|
recommendations = user_unrated_laptops.sort_values(by='Predicted_Rating', ascending=False).head(num_recommendations) |
|
else: |
|
|
|
new_user_data = pd.DataFrame({ |
|
'User_ID': [10002], |
|
'Age': [age], |
|
'Category': [category], |
|
'Gender': [gender] |
|
}) |
|
new_user_data = new_user_data.merge(laptop_df, how='cross') |
|
new_user_data['Predicted_Rating'] = new_user_data.apply(lambda row: model.predict(10002, row['Laptop_ID']).est, axis=1) |
|
recommendations = new_user_data.sort_values(by='Predicted_Rating', ascending=False).head(num_recommendations) |
|
|
|
return recommendations |
|
|
|
|
|
import streamlit as st |
|
|
|
|
|
st.title("Laptop Recommendation System") |
|
|
|
|
|
user_type = st.radio("Are you a new user or an existing user?", ('New User', 'Existing User')) |
|
|
|
if user_type == 'New User': |
|
|
|
new_user_age = st.slider("Age:", min_value=12, max_value=89, value=25) |
|
new_user_category = st.selectbox("What best describes you:", ['Student', 'Professor', 'Banker', 'Businessman', 'Programmer']) |
|
new_user_gender = st.radio("Gender:", ['Male', 'Female']) |
|
|
|
|
|
if st.button("Get Recommendations"): |
|
recommendations = recommend_laptops(age=new_user_age, category=new_user_category, gender=new_user_gender) |
|
st.subheader("Top 5 Recommended Laptops:") |
|
|
|
recommendations_table = recommendations[['Laptop_Name', 'Price (in Indian Rupees)', 'Type', 'Dedicated Graphic Memory Capacity', |
|
'Processor Brand', 'SSD', 'RAM (in GB)', 'RAM Type', 'Expandable Memory', |
|
'Operating System', 'Touchscreen', 'Screen Size (in inch)', 'Weight (in kg)', |
|
'Refresh Rate', 'screen_resolution', 'company', 'Storage', 'Processor name', |
|
'CPU_ranking', 'battery_backup', 'gpu name ', 'gpu_benchmark', |
|
'ram_type_tokenized', 'gpu_processor_tokenized', 'link']] |
|
|
|
st.table(recommendations_table) |
|
|
|
|
|
elif user_type == 'Existing User': |
|
|
|
existing_user_id = st.text_input("Enter your user ID:", "") |
|
|
|
|
|
if st.button("Get Laptop Recommendations"): |
|
if existing_user_id: |
|
recommendations = recommend_laptops(user_id=int(existing_user_id)) |
|
st.subheader(f"Top 5 Recommended Laptops for User {existing_user_id}:") |
|
|
|
recommendations_table = recommendations[['Laptop_Name', 'Price (in Indian Rupees)', 'Type', 'Dedicated Graphic Memory Capacity', |
|
'Processor Brand', 'SSD', 'RAM (in GB)', 'RAM Type', 'Expandable Memory', |
|
'Operating System', 'Touchscreen', 'Screen Size (in inch)', 'Weight (in kg)', |
|
'Refresh Rate', 'screen_resolution', 'company', 'Storage', 'Processor name', |
|
'CPU_ranking', 'battery_backup', 'gpu name ', 'gpu_benchmark', |
|
'ram_type_tokenized', 'gpu_processor_tokenized', 'link']] |
|
|
|
st.table(recommendations_table) |
|
else: |
|
st.warning("Please enter a valid user ID.") |
|
|