<jupyter_start><jupyter_text>Support Vector Classifier for Healthcare Fraud Detection <NAME>, <NAME> and <NAME>Import pandas and numpy packages to be able to transform and manipulation data.<jupyter_code>import numpy as np
import pandas as pd<jupyter_output><empty_output><jupyter_text>Read in modified feature file from post_providerinout_mods.py, with a large number of features.<jupyter_code>x_train_inout_mod = pd.read_csv('x_train_inout_mod.csv')
x_train_inout_mod.columns<jupyter_output><empty_output><jupyter_text>Remove junk columns created during read in.  Set 'potential fraud' as target (already converted to 1s and 0s.) Scale the input variables and ensure there are no NAs.  Split into test and train sets.<jupyter_code>from sklearn import model_selection as ms
x_train_inout_mod = x_train_inout_mod.drop(columns = 'Unnamed: 0')
y = x_train_inout_mod['PotentialFraud']
X = x_train_inout_mod.drop(columns = ['Provider', 'PotentialFraud'])
X_scaled = (X-X.min(axis=0))/(X.max(axis=0)-X.min(axis=0))
X_scaled=X_scaled.fillna(0)
from sklearn import model_selection as ms
X_train, X_test, y_train, y_test = ms.train_test_split(X_scaled, y, test_size=0.20, random_state=42)<jupyter_output><empty_output><jupyter_text>Import packages, including classification report and confusion matrix to be able to assess model classification performance.<jupyter_code>from sklearn import ensemble
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix<jupyter_output><empty_output><jupyter_text>Import Support Vector Machine, and set up dictionary of values to examine for kernals, scales and penalty parameter.  Set class weight in model parameters as balanced since the classification is so imbalanced (90/10)<jupyter_code>from sklearn import svm
svm_model = svm.SVC(class_weight='balanced')<jupyter_output><empty_output><jupyter_text>Fit model with default parameters and assess performance on test set.  Performance isn't outstanding.  We are using the F1 score as a proxy for overall performance given the imbalanced ratio of classes.<jupyter_code>svm_model.fit(X_train,y_train)
print(classification_report(y_test,svm_model.predict(X_test)))
grid_para_svc = [
    {'C': np.linspace(0.75,3,8),
     'kernel': ['linear', 'rbf'],
     'gamma': ['scale']}]
grid_search_svm = ms.GridSearchCV(svm_model, grid_para_svc, scoring='f1_weighted', cv=5, iid=True,  n_jobs=3)
grid_search_svm.fit(X_train, y_train)
grid_search_svm.best_params_<jupyter_output><empty_output><jupyter_text>Noticed that optimal values were at ends of ranges specified, so will shift range.<jupyter_code>grid_para_svc = [
    {'C': np.linspace(1.5,10,4),
     'kernel': ['rbf','linear'],
     'gamma': ['scale']}]
grid_search_svm = ms.GridSearchCV(svm_model, grid_para_svc, scoring='f1_weighted', cv=5,  n_jobs=3)
grid_search_svm.fit(X_train, y_train)
grid_search_svm.best_params_<jupyter_output><empty_output><jupyter_text>Again see that optimal parameters are toward end of specified range, but will examine performance at these values.  Again, using confusion matrix and F1 as the target score to maximize for imbalanced classes<jupyter_code>svm_model.set_params(C= grid_search_svm.best_params_['C'], gamma = grid_search_svm.best_params_['gamma'], \
                     kernel = grid_search_svm.best_params_['kernel'])
svm_model.fit(X_train, y_train)
print('   0    1    predicted is columns')
print(confusion_matrix(y_test, svm_model.predict(X_test)))
print(classification_report(y_test, svm_model.predict(X_test)))<jupyter_output>0    1    predicted is columns
[[887  90]
 [ 19  86]]
              precision    recall  f1-score   support

           0       0.98      0.91      0.94       977
           1       0.49      0.82      0.61       105

    accuracy                           0.90      1082
   macro avg       0.73      0.86      0.78      1082
weighted avg       0.93      0.90      0.91      1082<jupyter_text>Decided to try one more grid search with higher C values.  This is a second iteration after a first (erased) iteration, to find an optimal C value for the rbf kernel.<jupyter_code>grid_para_svc = [
    {'C': [21,21.5,22],
     'kernel': ['rbf'],
     'gamma': ['scale','auto']}]
grid_search_svm = ms.GridSearchCV(svm_model, grid_para_svc, scoring='f1_weighted', cv=5,  n_jobs=3)
grid_search_svm.fit(X_train, y_train)
grid_search_svm.best_params_
svm_model.set_params(C= 22, gamma = grid_search_svm.best_params_['gamma'], \
                     kernel = grid_search_svm.best_params_['kernel'])
svm_model.fit(X_train, y_train)
print('   0    1    predicted is columns')
print(confusion_matrix(y_test, svm_model.predict(X_test)))
print(classification_report(y_test, svm_model.predict(X_test)))<jupyter_output>0    1    predicted is columns
[[897  80]
 [ 24  81]]
              precision    recall  f1-score   support

           0       0.97      0.92      0.95       977
           1       0.50      0.77      0.61       105

    accuracy                           0.90      1082
   macro avg       0.74      0.84      0.78      1082
weighted avg       0.93      0.90      0.91      1082
{'fraction_non_alphanumeric': 0.09515435717076984, 'fraction_numerical': 0.03751465416178194, 'mean_word_length': 4.023552502453386, 'pattern_counts': {'":': 0, '<': 38, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '653865', 'license': 'MIT', 'n_tokens_mistral': 1817, 'n_tokens_neox': 1711, 'n_words': 436, 'path': 'doug/Provider_support_vector_classifier.ipynb', 'repo_name': 'DebLeong/HealthCareCapstone', 'size': '5136'}