Food recommendation system
Introduction
Recommender systems have revolutionized the way we discover new products, recipes, and more. In this blog post, we'll walk through the steps of creating a food recommendation system using the decision tree algorithm, a powerful yet straightforward machine learning technique.
Step 1: Understanding the Dataset
First, we need a dataset to train our model. Our hypothetical dataset includes features such as:
- Age: Age of the user.
- Gender: Gender of the user.
- Dietary Preferences: Dietary restrictions or preferences (e.g., vegetarian, vegan, gluten-free).
- Cuisine Preference: Preferred cuisine (e.g., Italian, Chinese, Indian).
- Meal Type: Type of meal (e.g., breakfast, lunch, dinner).
- Favorite Ingredients: Preferred ingredients in meals.
- Previous Likes: Historical data on liked recipes.
Step 2: Data Preprocessing
Before we can train our model, we need to preprocess the data. This includes handling missing values, encoding categorical variables, and splitting the dataset into training and test sets.
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
# Load the dataset
data = pd.read_csv('food_data.csv')
# Handle missing values
data.fillna(method='ffill', inplace=True)
# Encode categorical variables
label_encoders = {}
categorical_columns = ['Gender', 'Dietary Preferences', 'Cuisine Preference', 'Meal Type', 'Favorite Ingredients', 'Previous Likes']
for col in categorical_columns:
label_encoders[col] = LabelEncoder()
data[col] = label_encoders[col].fit_transform(data[col])
# Split the data into features and target
X = data.drop('Favorite Ingredients', axis=1)
y = data['Favorite Ingredients']
# Split into training and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
Step 3: Building the Decision Tree Model
Next, we'll build our decision tree model using the scikit-learn library.
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score
# Initialize the decision tree classifier
clf = DecisionTreeClassifier()
# Train the model
clf.fit(X_train, y_train)
# Predict on the test set
y_pred = clf.predict(X_test)
# Evaluate the model
accuracy = accuracy_score(y_test, y_pred)
print(f'Accuracy: {accuracy * 100:.2f}%')
Step 4: Making Recommendations
With our trained model, we can now make food recommendations. Let's create a function that takes user data as input and returns the recommended ingredients.
def recommend_food(user_data):
# Preprocess the input data
for col in categorical_columns:
if col in user_data:
user_data[col] = label_encoders[col].transform([user_data[col]])[0]
# Predict the favorite ingredients
favorite_ingredients_code = clf.predict([list(user_data.values())])[0]
# Decode the favorite ingredients
favorite_ingredients = label_encoders['Favorite Ingredients'].inverse_transform([favorite_ingredients_code])[0]
return favorite_ingredients
# Example user data
user_data = {
'Age': 28,
'Gender': 'Female',
'Dietary Preferences': 'Vegan',
'Cuisine Preference': 'Italian',
'Meal Type': 'Lunch',
'Previous Likes': 'Yes'
}
# Get the recommendation
recommended_ingredients = recommend_food(user_data)
print(f'Recommended Ingredients: {recommended_ingredients}')
Conclusion
In this blog post, we built a food recommendation system using a decision tree algorithm. We covered data preprocessing, model building, and making recommendations. Decision trees are a great choice for this type of problem due to their simplicity and interpretability.
Thank you for reading! Stay tuned for more posts on machine learning and data science applications.
Comments
Post a Comment