<jupyter_start><jupyter_text>Initial Imports<jupyter_code>#Imports
import category_encoders as ce
import pandas as pd
import joblib
from joblib import dump, load
import json
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
from sklearn.metrics import  f1_score, roc_auc_score, roc_curve, precision_score, recall_score, accuracy_score
from xgboost import XGBClassifier
# read data previously cleaned and reduced to neccessary columns
# these csv files can be found in 'data'files of this github repository
df1a = pd.read_csv('data/Clean_Data_1.csv')
df1b = pd.read_csv('data/Clean_Data_2.csv')
df1 = pd.concat([df1a, df1b], ignore_index=True)
df1.reset_index(drop=True, inplace=True)
print(df1.shape)
df1.head()
df1['category'].value_counts()
# Assign datatypes for processing purposes
df1['blurb'] = df1['blurb'].astype(str)
df1['country'] = df1['country'].astype(str)
df1['campaign_success'] = df1['campaign_success'].astype(int)<jupyter_output><empty_output><jupyter_text>Data Splits  Train, Validate, Test Split<jupyter_code>df1['launched_at'].describe()
# Choose cutoffs based on unix time, 75th & 50th percentile
cutoff1 = 1466003000
cutoff2 = 1530120000

test = df1[df1['launched_at']>=cutoff2]
train = df1[df1['launched_at']<cutoff2]
val = train[train['launched_at']>cutoff1] 
train = train[train['launched_at']<=cutoff1]

# Drop launched_at column, not used in model
train = train.drop(columns=['launched_at'])
val = val.drop(columns=['launched_at']) 
test = test.drop(columns=['launched_at'])

print(train.shape)
print(val.shape)
print(test.shape)
# check that data is balanced
train['campaign_success'].value_counts(normalize=True)<jupyter_output><empty_output><jupyter_text>Target Vector / Features Matrix Split<jupyter_code># Target, is the client a defaulter
target = 'campaign_success'

# Features
features = list(train.drop(columns = [target]))
# Arrange data into X features matrix and y target vector 
X_train = train[features]
y_train = train[target]
X_val = val[features]
y_val = val[target]
X_test = test[features]
y_test = test[target]<jupyter_output><empty_output><jupyter_text>Wrangle Data  Create Encoder<jupyter_code>#Create and train transformations
# Encode catagorical features on X_train
encoder = ce.OrdinalEncoder(cols=['country', 'category', 'subcategory'])
encoder.fit(X_train)
print('')
def wrangle(X, encoder):

    # copy to avoid errors
    X = X.reset_index(drop=True).copy()

    X = encoder.transform(X)

    X['blurb_length'] = X['blurb'].apply(lambda x: len(x))
    X['blurb_words'] = X['blurb'].apply(lambda x: len(x.split()))
    X['blurb_uppers'] = X['blurb'].apply(lambda x: sum(map(str.isupper, x.split())))
    X['blurb_qmarks'] = X['blurb'].apply(lambda x: x.count("?"))
    X['blub_exclamation'] = X['blurb'].apply(lambda x: x.count("!"))
    X = X.drop(columns=['blurb'])

    return(X)
X_train_e = wrangle(X_train, encoder)
X_val_e = wrangle(X_val, encoder)
X_test_e = wrangle(X_test, encoder)
print(X_train_e.shape)
print(X_val_e.shape)
print(X_test_e.shape)<jupyter_output>(86636, 10)
(45201, 10)
(47619, 10)<jupyter_text>XGBoost Model<jupyter_code>""" Define Model"""
# First XGBoost Model
booster= XGBClassifier(n_estimators=100, random_state=42, n_jobs=-1)

# XBG Simple
xgb_simple= booster.fit(X_train_e, y_train) #fit on train
""" Run Model XGBoost Simple """
xs_y_pred_v = booster.predict(X_val_e)

m_name = 'XGBoost Simple'
y = y_val
pred = xs_y_pred_v

print('Classification Report:\n\n', classification_report(y, pred))
""" Run Model"""
xs_y_pred_test = booster.predict(X_test_e)

m_name = 'XGBoost Simple TEST'
model =  booster.fit(X_train_e, y_train)
X = X_test_e
y = y_test
pred = xs_y_pred_test

print('Classification Report:\n\n', classification_report(y, pred))<jupyter_output>Classification Report:

               precision    recall  f1-score   support

           0       0.65      0.64      0.64     15914
           1       0.82      0.83      0.82     31705

    accuracy                           0.76     47619
   macro avg       0.73      0.73      0.73     47619
weighted avg       0.76      0.76      0.76     47619<jupyter_text>Creating Pickles via JobLib<jupyter_code># Pickling encoder created above, no changes required
dump(encoder, 'encoder_m.joblib' )
encoder_m = load('encoder_m.joblib')
# Wrangle Function to pickle
# allows for state of incoming data
def wrangler(X, encoder):
  X = pd.DataFrame.from_dict(X, orient='index')
  X = X.T
  X = X.reset_index(drop=True).copy()
  X.rename(columns={'x1':'goal',
                    'x2':'campaign_length',
                    'x3':'country',
                    'x4':'category',
                    'x5':'subcategory',
                    'x6':'blurb'}, inplace=True)

  X = encoder.transform(X)
  X = X[['country', 'goal', 'category', 'subcategory',
         'campaign_length', 'blurb']]

  X['blurb_length'] = X['blurb'].apply(lambda x: len(x))
  X['blurb_words'] = X['blurb'].apply(lambda x: len(x.split()))
  X['blurb_uppers'] = X['blurb'].apply(lambda x: sum(map(str.isupper, x.split())))
  X['blurb_qmarks'] = X['blurb'].apply(lambda x: x.count("?"))
  X['blub_exclamation'] = X['blurb'].apply(lambda x: x.count("!"))
  X = X.drop(columns=['blurb'])
  X = X.astype(int)
  return(X)
dump(wrangler, 'wrangler_m.joblib' )
wrangler_m = load('wrangler_m.joblib')
# XGBoost Model to Pickle
def kick_boost(X):
  prediction = booster.predict_proba(X)[0][1]
  prediction = round(prediction, 3)
  
  return(prediction)
dump(kick_boost, 'kick_boost_m.joblib' )
kick_boost_m = load('kick_boost_m.joblib')<jupyter_output><empty_output><jupyter_text>Testing data through pickled model<jupyter_code># Dummy Data
test_1 = {
  "x1": 10000,
  "x2": 8,
  "x3": "Canada",
  "x4": "Science",
  "x5": "Material Thread Science",
  "x6": "I am making somthing that will do something awesome!!! Answer the question 'will it be amazing?'"
}
result = wrangler_m(test_1, encoder_m)
prediction = kick_boost_m(result)
prediction<jupyter_output><empty_output>