<jupyter_start><jupyter_text>Data Loading PIMA Dataset<jupyter_code>def load_pima_dataset():
  data = pd.read_csv('diabetes.csv')
  pd.value_counts(data['Outcome']).plot.bar()
  plt.title('Pima Dataset')
  plt.xlabel('Class')
  plt.ylabel('Frequency')
  plt.show()

  X = data.drop(data.columns.values[-1:],axis=1)
  y = data.drop(data.columns.values[:-1],axis=1)

  return X.to_numpy(), y.to_numpy()<jupyter_output><empty_output><jupyter_text>Credit Card Dataset<jupyter_code>def load_credit_card_dataset():
  data = pd.read_csv('creditcard.csv')
  pd.value_counts(data['Class']).plot.bar()
  plt.title('Credit Card Dataset')
  plt.xlabel('Class')
  plt.ylabel('Frequency')
  plt.show()
  data = data.drop(['Time', 'Amount'], axis=1)

  X = data.drop(data.columns.values[-1:],axis=1)
  y = data.drop(data.columns.values[:-1],axis=1)

  return X.to_numpy(),y.to_numpy()

load_credit_card_dataset()<jupyter_output><empty_output><jupyter_text>Synthetic Datasets<jupyter_code>def load_synthetic_dataset(num_samples=100, num_features=4, num_classes=2, weights_list=[0.9, 0.1], random_state=0, test_size=0.2):
  X,y = make_classification(n_samples=num_samples,n_features=num_features,n_classes=num_classes,weights=weights_list,random_state=random_state)

  plt.scatter(X[:,0][y==1],X[:,1][y==1],c='g',marker='X')
  plt.scatter(X[:,0][y==0],X[:,1][y==0],c='r',marker='.')
  plt.show()

  y = np.array([[float(i)] for i in y])

  return X, y<jupyter_output><empty_output><jupyter_text>Evaluation Metrics<jupyter_code>def get_evaluation_metrics(X_test, y_test, y_predict):
  fn = len(y_test[(y_predict==0) & (y_test==1)])
  tp = len(y_predict[(y_predict==1) & (y_test==1)])
  fp = len(y_predict[(y_predict==1) & (y_test==0)])
  tn = len(y_predict[(y_predict==0) & (y_test==0)])
  majority_acc = len(y_test[(y_predict==1) & (y_test==1)])/(len(y_test[(y_predict==0) & (y_test==1)])+len(y_predict[(y_predict==1) & (y_test==1)]))
  minority_acc = len(y_test[(y_predict==0) & (y_test==0)])/(len(y_test[(y_predict==1) & (y_test==0)])+len(y_predict[(y_predict==0) & (y_test==0)]))

  return fn, tp, fp, tn, majority_acc, minority_acc<jupyter_output><empty_output><jupyter_text>SMOTE<jupyter_code>def get_knn(X, y, y_minority, k):
  neighbours = []
  for index_1 in range(len(X[:][y==y_minority])):
    neighbours.append([])
    for index_2 in range(len(X[:][y==y_minority])):
      if index_2 != index_1:
        dist = np.sum(np.square(X[:][y==y_minority][index_1]-X[:][y==y_minority][index_2]))
        neighbours[index_1].append((dist, index_2))
    neighbours[index_1].sort()
  neighbours = np.array(neighbours)
  k_neighbours = neighbours[:, :k]
  return k_neighbours
def nn_vect_select(X, y, y_minority, nnarray, k, os_percent):
  init_size = len(X[:][y==y_minority])
  factor = os_percent // 100
  for index_1 in range(init_size):
    neigh_nums = random.sample(range(0, k), factor)
    for i in neigh_nums:
      index_2 = int(nnarray[index_1][i][1])
      ratio = random.random()
      new_vals = X[:][y==y_minority][index_1] + ratio * (X[:][y==y_minority][index_2] - X[:][y==y_minority][index_1])
      X = np.append(X,[new_vals],axis=0)
      y = np.append(y,[y_minority])
  return X, y
def apply_smote(X, y, y_minority, test_size=0.3, random_state=0, os_percent=200, k=5):
  y = y.ravel()

  nnarray = get_knn(X, y, y_minority, k)
  X, y = nn_vect_select(X, y, y_minority, nnarray, k, os_percent)

  X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=test_size, random_state=random_state)

  clf = KNeighborsClassifier()
  clf.fit(X_train,y_train)
  y_predict = clf.predict(X_test)

  y_scores = clf.predict_proba(X_test)
  fpr, tpr, threshold = roc_curve(y_test, y_scores[:, 1])
  roc_auc = auc(fpr, tpr)

  fn, tp, fp, tn, majority_acc, minority_acc = get_evaluation_metrics(X_test, y_test, y_predict)

  print('Over-sampling %: ', os_percent)
  print('Precision: ', tp/(tp+fp))
  print('Recall: ', tp/(tp+fn))
  # print('Accuracy: ', (tp+tn)/(tp+fp+tn+fn))

  print()

  return X, y, fpr, tpr, roc_auc<jupyter_output><empty_output><jupyter_text>SMOTEBoost<jupyter_code>def apply_smote_boost(X, y, y_minority, test_size=0.3, random_state=0):
  y = y.ravel()
  X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=test_size, random_state=random_state)
  sm_boost = SMOTEBoostClassifier(random_state=random_state)
  sm_boost.fit(X_train, y_train)  
  y_predict = sm_boost.predict(X_test)

  y_scores = sm_boost.decision_function(X_test)
  fpr, tpr, threshold = roc_curve(y_test, y_scores)
  roc_auc = auc(fpr, tpr)

  fn, tp, fp, tn, majority_acc, minority_acc = get_evaluation_metrics(X_test, y_test, y_predict)
  print()
  print('Precision: ', tp/(tp+fp))
  print('Recall: ', tp/(tp+fn))
  # print('Accuracy: ', (tp+tn)/(tp+fp+tn+fn))
  return fpr, tpr, roc_auc<jupyter_output><empty_output><jupyter_text>Analysis<jupyter_code>oversampling_percentage = [0,100,200,300]
colours = ['b', 'g', 'y', 'r']
def evaluate_smote(X, y, test_size=0.3, random_state=0, os_percent=200, k=5):
  newX, newy, fpr, tpr, roc_auc = apply_smote(X, y, 1, test_size=test_size, os_percent=os_percent, k=k)
  return newX, newy, fpr, tpr, roc_auc
def evaluate_smote_boost(X, y, test_size=0.3, random_state=0):
  fpr, tpr, roc_auc = apply_smote_boost(X, y, 1, test_size=test_size)
  return fpr, tpr, roc_auc<jupyter_output><empty_output><jupyter_text>SMOTE+KNN on Pima<jupyter_code>X, y = load_pima_dataset()
for i in range(0, len(oversampling_percentage)):
  plt.title('Receiver Operating Characteristic')
  plt.plot([0,1],[0,1],'r--')
  plt.xlim([-0.1,1.0])
  plt.ylim([-0.1,1.01])
  plt.ylabel('True Positive Rate')
  plt.xlabel('False Positive Rate')
  
  newX, newY, fpr, tpr, roc_auc = evaluate_smote(X, y, os_percent=oversampling_percentage[i])

  plt.plot(fpr, tpr, colours[i],label='Oversampling = %d%%, AUC = %0.3f'% (oversampling_percentage[i], roc_auc))

plt.legend()
plt.show()<jupyter_output><empty_output><jupyter_text>SMOTEBoost on Pima<jupyter_code>X, y = load_pima_dataset()
plt.title('Receiver Operating Characteristic')
plt.plot([0,1],[0,1],'r--')
plt.xlim([-0.1,1.0])
plt.ylim([-0.1,1.01])
plt.ylabel('True Positive Rate')
plt.xlabel('False Positive Rate')
fpr, tpr, roc_auc = evaluate_smote_boost(X, y)

plt.plot(fpr, tpr, 'b',label='AUC = %0.3f'% (roc_auc))
print('\n\n')

plt.legend()
plt.show()<jupyter_output><empty_output><jupyter_text>SMOTEBoost on Credit Fraud<jupyter_code>X, y = load_credit_card_dataset()
plt.title('Receiver Operating Characteristic')
plt.plot([0,1],[0,1],'r--')
plt.xlim([-0.1,1.0])
plt.ylim([-0.1,1.01])
plt.ylabel('True Positive Rate')
plt.xlabel('False Positive Rate')
fpr, tpr, roc_auc = evaluate_smote_boost(X, y)

plt.plot(fpr, tpr, 'b',label='AUC = %0.3f'% (roc_auc))
print('\n\n')

plt.legend()
plt.show()<jupyter_output><empty_output><jupyter_text>SMOTE+KNN on Synthetic Data<jupyter_code>number_samples = [1000]
oversampling_percentage = [0,100,200,300]
ir = [5, 10, 50]
for j in range(len(ir)):
  print('Imbalance Ratio: ', ir[j])
  for num_samp in number_samples:
    print('Number of Samples: ', num_samp)
    X, y = load_synthetic_dataset(num_samples=num_samp, weights_list=[ir[j]/(ir[j]+1), 1/(ir[j]+1)], random_state=2)
    for i in range(0, len(oversampling_percentage)):
      plt.title('Receiver Operating Characteristic')
      plt.plot([0,1],[0,1],'r--')
      plt.xlim([-0.1,1.0])
      plt.ylim([-0.1,1.01])
      plt.ylabel('True Positive Rate')
      plt.xlabel('False Positive Rate')
      newX, newy, fpr, tpr, roc_auc = evaluate_smote(X, y, os_percent=oversampling_percentage[i])
      plt.plot(fpr, tpr, colours[i],label='Oversampling = %d%%, AUC = %0.3f'% (oversampling_percentage[i], roc_auc))

    plt.legend()
    plt.show()
for j in range(len(ir)):
  print('Imbalance Ratio: ', ir[j])
  for num_samp in number_samples:
    print('Number of Samples: ', num_samp)
    X, y = load_synthetic_dataset(num_samples=num_samp, weights_list=[ir[j]/(ir[j]+1), 1/(ir[j]+1)], random_state=2)
    for i in range(0, len(oversampling_percentage)):
      newX, newy, fpr, tpr, roc_auc = evaluate_smote(X, y, os_percent=oversampling_percentage[i])
      print('Over-sampling %: ', oversampling_percentage[i])
      plt.scatter(X[:,0][y[:,0]==1],X[:,1][y[:,0]==1],c='g',marker='X')
      plt.scatter(X[:,0][y[:, 0]==0],X[:,1][y[:, 0]==0],c='r',marker='.')
      plt.show()<jupyter_output>Imbalance Ratio:  5
Number of Samples:  1000<jupyter_text>SMOTEBoost on Synthetic Data<jupyter_code>for j in range(len(ir)):
  print('Imbalance Ratio: ', ir[j])
  for num_samp in number_samples:
    print('Number of Samples: ', num_samp)
    X, y = load_synthetic_dataset(num_samples=num_samp, weights_list=[ir[j]/(ir[j]+1), 1/(ir[j]+1)], random_state=2)
    plt.title('Receiver Operating Characteristic')
    plt.plot([0,1],[0,1],'r--')
    plt.xlim([-0.1,1.0])
    plt.ylim([-0.1,1.01])
    plt.ylabel('True Positive Rate')
    plt.xlabel('False Positive Rate')
    fpr, tpr, roc_auc = evaluate_smote_boost(X, y)
    plt.plot(fpr, tpr, 'b',label='AUC = %0.3f'% (roc_auc))
    print('\n\n')

    plt.legend()
    plt.show()<jupyter_output>Imbalance Ratio:  5
Number of Samples:  1000
{'fraction_non_alphanumeric': 0.18772563176895307, 'fraction_numerical': 0.02559894978667542, 'mean_word_length': 5.802083333333333, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 47, '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': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '505774', 'license': 'MIT', 'n_tokens_mistral': 4079, 'n_tokens_neox': 3895, 'n_words': 600, 'path': 'code/smote/smote_and_smoteboost_analysis.ipynb', 'repo_name': 'vs666/SMAI-Project', 'size': '9141'}