Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,10 +9,8 @@ from sklearn.feature_extraction.text import TfidfVectorizer
|
|
| 9 |
# Initialize Flask
|
| 10 |
app = Flask(__name__)
|
| 11 |
|
| 12 |
-
#
|
| 13 |
df = pd.read_csv("ecommerce_dataset_updated.csv")
|
| 14 |
-
|
| 15 |
-
# Clean and process data as in your previous code
|
| 16 |
df['Category'] = df['Category'].str.replace('&', ' and ')
|
| 17 |
df['Purchase_Date'] = pd.to_datetime(df['Purchase_Date'], format='%d-%m-%Y')
|
| 18 |
df.dropna(inplace=True)
|
|
@@ -30,13 +28,13 @@ interaction_matrix = df.pivot_table(
|
|
| 30 |
user_similarity = cosine_similarity(interaction_matrix)
|
| 31 |
user_similarity_df = pd.DataFrame(user_similarity, index=interaction_matrix.index, columns=interaction_matrix.index)
|
| 32 |
|
| 33 |
-
# TF-IDF on categories
|
| 34 |
tfidf = TfidfVectorizer(stop_words='english')
|
| 35 |
tfidf_matrix = tfidf.fit_transform(df['Category'])
|
| 36 |
product_similarity = cosine_similarity(tfidf_matrix, tfidf_matrix)
|
| 37 |
product_similarity_df = pd.DataFrame(product_similarity, index=df['Product_ID'], columns=df['Product_ID'])
|
| 38 |
|
| 39 |
-
# Collaborative Filtering Recommendation Function
|
| 40 |
def recommend_collaborative(user_id, top_n=5):
|
| 41 |
user_idx = interaction_matrix.index.get_loc(user_id)
|
| 42 |
sim_scores = user_similarity_df.iloc[user_idx]
|
|
@@ -45,7 +43,7 @@ def recommend_collaborative(user_id, top_n=5):
|
|
| 45 |
recommended_products = interaction_matrix.columns[product_indices]
|
| 46 |
return recommended_products.tolist()
|
| 47 |
|
| 48 |
-
# Content-Based Recommendation Function
|
| 49 |
def recommend_content_based(product_id, top_n=5):
|
| 50 |
sim_scores = product_similarity_df.loc[product_id].sort_values(ascending=False)
|
| 51 |
recommended_products = sim_scores.index[1:top_n+1] # Exclude the product itself
|
|
|
|
| 9 |
# Initialize Flask
|
| 10 |
app = Flask(__name__)
|
| 11 |
|
| 12 |
+
# Dataset is loaded for preprocessing
|
| 13 |
df = pd.read_csv("ecommerce_dataset_updated.csv")
|
|
|
|
|
|
|
| 14 |
df['Category'] = df['Category'].str.replace('&', ' and ')
|
| 15 |
df['Purchase_Date'] = pd.to_datetime(df['Purchase_Date'], format='%d-%m-%Y')
|
| 16 |
df.dropna(inplace=True)
|
|
|
|
| 28 |
user_similarity = cosine_similarity(interaction_matrix)
|
| 29 |
user_similarity_df = pd.DataFrame(user_similarity, index=interaction_matrix.index, columns=interaction_matrix.index)
|
| 30 |
|
| 31 |
+
# TF-IDF vectors on categories
|
| 32 |
tfidf = TfidfVectorizer(stop_words='english')
|
| 33 |
tfidf_matrix = tfidf.fit_transform(df['Category'])
|
| 34 |
product_similarity = cosine_similarity(tfidf_matrix, tfidf_matrix)
|
| 35 |
product_similarity_df = pd.DataFrame(product_similarity, index=df['Product_ID'], columns=df['Product_ID'])
|
| 36 |
|
| 37 |
+
# Collaborative Filtering Recommendation Function(Method-1)
|
| 38 |
def recommend_collaborative(user_id, top_n=5):
|
| 39 |
user_idx = interaction_matrix.index.get_loc(user_id)
|
| 40 |
sim_scores = user_similarity_df.iloc[user_idx]
|
|
|
|
| 43 |
recommended_products = interaction_matrix.columns[product_indices]
|
| 44 |
return recommended_products.tolist()
|
| 45 |
|
| 46 |
+
# Content-Based Recommendation Function(Method-2)
|
| 47 |
def recommend_content_based(product_id, top_n=5):
|
| 48 |
sim_scores = product_similarity_df.loc[product_id].sort_values(ascending=False)
|
| 49 |
recommended_products = sim_scores.index[1:top_n+1] # Exclude the product itself
|