KeegBarb commited on
Commit
4aa0a42
·
verified ·
1 Parent(s): 251e900

Create train_model.py

Browse files
Files changed (1) hide show
  1. train_model.py +33 -0
train_model.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import numpy as np
3
+ import joblib
4
+ from sklearn.linear_model import Ridge
5
+ from sklearn.model_selection import train_test_split
6
+
7
+ DATA_URL = "https://raw.githubusercontent.com/KeeganBarbee/KeeganBarbee.github.io/main/OnlineNewsPopularity.csv"
8
+
9
+ df = pd.read_csv(DATA_URL)
10
+ df.columns = df.columns.str.strip()
11
+
12
+ df['log_shares'] = np.log1p(df['shares'])
13
+
14
+ feature_cols = [
15
+ 'n_tokens_content', 'num_imgs', 'global_sentiment_polarity',
16
+ 'global_subjectivity', 'title_sentiment_polarity',
17
+ 'weekday_is_monday', 'weekday_is_tuesday', 'weekday_is_wednesday',
18
+ 'weekday_is_thursday', 'weekday_is_friday', 'weekday_is_saturday',
19
+ 'weekday_is_sunday', 'n_tokens_title', 'num_videos', 'num_hrefs'
20
+ ]
21
+
22
+ X = df[feature_cols]
23
+ y = df['log_shares']
24
+
25
+ X_train, X_test, y_train, y_test = train_test_split(
26
+ X, y, test_size=0.2, random_state=42
27
+ )
28
+
29
+ model = Ridge(alpha=1.0)
30
+ model.fit(X_train, y_train)
31
+
32
+ joblib.dump(model, 'popularity_model.pkl')
33
+ joblib.dump(feature_cols, 'model_features.pkl')