<jupyter_start><jupyter_text>MVPMVP for predicting buy or not buy based on a web session at an e-commerce site.<jupyter_code>import pandas as pd
import numpy as np

from sklearn.model_selection import train_test_split
from sklearn.model_selection import cross_val_score
from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.ensemble import RandomForestClassifier

from sklearn.metrics import f1_score<jupyter_output><empty_output><jupyter_text>Read in observation created from the feature generation process.<jupyter_code>observations_df = pd.read_pickle('../data/design.pkl')
observations_df.head()<jupyter_output><empty_output><jupyter_text>Create `X` / `y`, and the train/test split<jupyter_code>X, y = observations_df.drop(columns='buy_event'), observations_df.buy_event
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3, random_state = 1234)<jupyter_output><empty_output><jupyter_text>Create baseline classification models<jupyter_code>log_model = LogisticRegression()
svm_model = SVC()
svm_linear_model = SVC(kernel='linear')
rf_model = RandomForestClassifier()
gboost_model = GradientBoostingClassifier()<jupyter_output><empty_output><jupyter_text>Fit all models and calcualte F1 score<jupyter_code>models = {'Logistic': log_model, 
#           'SVM RBF': svm_model, 
#           'SVM Linear': svm_linear_model, 
          'Random Forrest': rf_model, 
          'Gradient Boost': gboost_model}

for k, m in models.items():
    m.fit(X_train, y_train)
    y_pred = m.predict(X_train)
    print(f'{k} F1 score: {f1_score(y_train, y_pred)}')
rf_model.feature_importances_
cv_rf_model = cross_val_score(rf_model, X_train, y_train, scoring='f1', cv=10)

print(f'10=fold F1 score: {cv_rf_model.mean():.2%} +- {cv_rf_model.std():.2%}')
cv_gboost_model = cross_val_score(gboost_model, X_train, y_train, scoring='f1', cv=10)

print(f'10-fold F1 score: {cv_gboost_model.mean():.2%} +- {cv_gboost_model.std():.2%}')<jupyter_output>Avg F1 score: 62.90% +- 4.13%
{'fraction_non_alphanumeric': 0.11469879518072289, 'fraction_numerical': 0.0163855421686747, 'mean_word_length': 6.576642335766423, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '643291', 'license': 'MIT', 'n_tokens_mistral': 788, 'n_tokens_neox': 761, 'n_words': 153, 'path': 'notebooks/old/MVP.ipynb', 'repo_name': 'jason-sa/classification', 'size': '2075'}