| import pandas as pd |
| from sklearn.linear_model import LogisticRegression |
| from sklearn.model_selection import train_test_split |
| from sklearn.metrics import accuracy_score |
| import joblib |
| from utils.preprocessing import preprocess_data |
| from huggingface_hub import HfApi, login |
| import os |
|
|
| def train_promotion_strategy(): |
| """Train the promotion strategy model.""" |
| |
| df = pd.read_json("data/raw/engagement_metrics.json") |
| df = preprocess_data(df) |
|
|
| |
| promotion_threshold = df['engagement_rate'].quantile(0.8) |
| df['promote'] = df['engagement_rate'].apply(lambda x: 1 if x >= promotion_threshold else 0) |
|
|
| X = df[['caption_length', 'hashtag_count', 'sentiment']] |
| y = df['promote'] |
| X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) |
|
|
| promotion_model = LogisticRegression(random_state=42) |
| promotion_model.fit(X_train, y_train) |
| y_pred = promotion_model.predict(X_test) |
| accuracy = accuracy_score(y_test, y_pred) |
| print(f"Promotion Prediction Model Accuracy: {accuracy:.4f}") |
|
|
| |
| joblib.dump(promotion_model, "promotion_strategy_model.pkl") |
|
|