diff --git a/matching-model/.DS_Store b/matching-model/.DS_Store new file mode 100644 index 0000000..b06b76a Binary files /dev/null and b/matching-model/.DS_Store differ diff --git a/matching-model/convertDStoCSV.py b/matching-model/convertDStoCSV.py new file mode 100644 index 0000000..68cc9ef --- /dev/null +++ b/matching-model/convertDStoCSV.py @@ -0,0 +1,22 @@ +# Converts the datatset into csv file - i.e the donations table is being converted into .csv file + +import pandas as pd +from your_flask_app import app, db # Import your Flask app and SQLAlchemy objects +from models import Donation # Import the Donation model + +def export_to_csv(): + with app.app_context(): + donations = Donation.query.all() + data = [{ + 'food_type': donation.food_type, + 'quantity': donation.quantity, + 'location': donation.location, + 'timestamp': donation.timestamp + } for donation in donations] + + df = pd.DataFrame(data) + df.to_csv('donations_requests.csv', index=False) + +# Call this function periodically, e.g., using a cron job or a scheduled task +export_to_csv() + diff --git a/matching-model/main.py b/matching-model/main.py new file mode 100644 index 0000000..f461500 --- /dev/null +++ b/matching-model/main.py @@ -0,0 +1,50 @@ +import pandas as pd +from sklearn.model_selection import train_test_split +from sklearn.neighbors import KNeighborsClassifier +from sklearn.metrics.pairwise import cosine_similarity + +# Load the CSV file into a pandas DataFrame (assuming data is already loaded) +df = pd.read_csv('convertDStoCSV.csv') + +# Preprocess the data (e.g., handle missing values, encode categorical variables) +# Assuming 'timestamp' is in string format, convert it to datetime if needed +df['timestamp'] = pd.to_datetime(df['timestamp']) + +# Split the data into features (X) and target variable (y) +X = df[['food_type', 'location', 'quantity', 'timestamp']] # Features: food_type, location, quantity, timestamp +y = df['food_preferences'] # Target variable: food_preferences + +# Encode categorical variables (if needed) +X_encoded = pd.get_dummies(X, columns=['food_type', 'location'], drop_first=True) # One-hot encoding for categorical variables + +# Split the data into training and testing sets +X_train, X_test, y_train, y_test = train_test_split(X_encoded, y, test_size=0.2, random_state=42) + +# Initialize the KNN classifier +knn = KNeighborsClassifier(n_neighbors=3) # Adjust hyperparameters as needed + +# Train the classifier +knn.fit(X_train, y_train) + +# Calculate similarity between donors and recipients +def calculate_similarity(row): + donor_food = row['food_type'].split(', ') + recipient_food = row['food_preferences'].split(', ') + vector_donor = [1 if food in donor_food else 0 for food in set(donor_food + recipient_food)] + vector_recipient = [1 if food in recipient_food else 0 for food in set(donor_food + recipient_food)] + similarity = cosine_similarity([vector_donor], [vector_recipient])[0][0] + return similarity + +# Calculate similarity scores for the test set +X_test_similarity = X_test.apply(calculate_similarity, axis=1) + +# Set a similarity threshold to filter matches +threshold = 0.5 +matched_indices = X_test_similarity[X_test_similarity >= threshold].index + +# Get the matched donor-recipient pairs +matched_pairs = df.iloc[matched_indices] + +# Output matched pairs along with their similarity scores +print("Matched Donor-Recipient Pairs:") +print(matched_pairs[['donor_id', 'recipient_id', 'location', 'quantity']])