Skip to content

Instantly share code, notes, and snippets.

@Dbhardwaj99
Created December 2, 2022 12:27
Show Gist options
  • Save Dbhardwaj99/721f5243c33fb4b08006aa76768b59c2 to your computer and use it in GitHub Desktop.
Save Dbhardwaj99/721f5243c33fb4b08006aa76768b59c2 to your computer and use it in GitHub Desktop.
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
# Load the data for the recommendation system
data = pd.read_csv("posts.csv")
# Select the relevant columns for the ML model
X = data[["hashtags", "time_spent"]]
# The target variable is the category of the post
y = data["category"]
# Split the data into a training set and a test set
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
# Create the k-nearest neighbors model
model = KNeighborsClassifier()
# Train the model using the training set
model.fit(X_train, y_train)
# Evaluate the performance of the model on the test set
accuracy = model.score(X_test, y_test)
print(f"Model accuracy: {accuracy:.2f}")
# Use the model to make recommendations for a new user
new_user = [["#fitness", "#wellness"], 120]
prediction = model.predict([new_user])
print(f"Recommended category for new user: {prediction[0]}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment