Update app.py
Browse files
app.py
CHANGED
|
@@ -229,4 +229,54 @@ combined_df['post_score'] = combined_df.apply(rate_post, axis=1)
|
|
| 229 |
logging.info("Post Ratings:")
|
| 230 |
print(combined_df[['post_id', 'post_score']].head())
|
| 231 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 232 |
logging.info("Analysis complete!")
|
|
|
|
| 229 |
logging.info("Post Ratings:")
|
| 230 |
print(combined_df[['post_id', 'post_score']].head())
|
| 231 |
|
| 232 |
+
# Train models using all features
|
| 233 |
+
logging.info("Training models using all features...")
|
| 234 |
+
|
| 235 |
+
# Features for model training
|
| 236 |
+
features = [
|
| 237 |
+
'caption_length', 'hashtag_count', 'sentiment', 'engagement_rate', 'image_text'
|
| 238 |
+
]
|
| 239 |
+
|
| 240 |
+
# Viral Potential Prediction
|
| 241 |
+
logging.info("Training viral potential prediction model...")
|
| 242 |
+
combined_viral_threshold = combined_df['engagement_rate'].quantile(0.9)
|
| 243 |
+
combined_df['viral'] = combined_df['engagement_rate'].apply(lambda x: 1 if x >= combined_viral_threshold else 0)
|
| 244 |
+
|
| 245 |
+
X = combined_df[features]
|
| 246 |
+
y = combined_df['viral']
|
| 247 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
| 248 |
+
|
| 249 |
+
viral_model = RandomForestClassifier(random_state=42)
|
| 250 |
+
viral_model.fit(X_train, y_train)
|
| 251 |
+
y_pred = viral_model.predict(X_test)
|
| 252 |
+
accuracy = accuracy_score(y_test, y_pred)
|
| 253 |
+
logging.info(f"Viral Potential Model Accuracy: {accuracy:.4f}")
|
| 254 |
+
|
| 255 |
+
# Engagement Rate Prediction
|
| 256 |
+
logging.info("Training engagement rate prediction model...")
|
| 257 |
+
X = combined_df[features]
|
| 258 |
+
y = combined_df['engagement_rate']
|
| 259 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
| 260 |
+
|
| 261 |
+
engagement_model = XGBRegressor(random_state=42)
|
| 262 |
+
engagement_model.fit(X_train, y_train)
|
| 263 |
+
y_pred = engagement_model.predict(X_test)
|
| 264 |
+
mae = mean_absolute_error(y_test, y_pred)
|
| 265 |
+
logging.info(f"Engagement Rate Prediction Model - MAE: {mae:.4f}")
|
| 266 |
+
|
| 267 |
+
# Promotion Strategy
|
| 268 |
+
logging.info("Training promotion prediction model...")
|
| 269 |
+
promotion_threshold = combined_df['engagement_rate'].quantile(0.8)
|
| 270 |
+
combined_df['promote'] = combined_df['engagement_rate'].apply(lambda x: 1 if x >= promotion_threshold else 0)
|
| 271 |
+
|
| 272 |
+
X = combined_df[features]
|
| 273 |
+
y = combined_df['promote']
|
| 274 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
| 275 |
+
|
| 276 |
+
promotion_model = LogisticRegression(random_state=42)
|
| 277 |
+
promotion_model.fit(X_train, y_train)
|
| 278 |
+
y_pred = promotion_model.predict(X_test)
|
| 279 |
+
accuracy = accuracy_score(y_test, y_pred)
|
| 280 |
+
logging.info(f"Promotion Prediction Model Accuracy: {accuracy:.4f}")
|
| 281 |
+
|
| 282 |
logging.info("Analysis complete!")
|