<jupyter_start><jupyter_text>Extract the target variablepredict variable Churn using a logistic regression on variables tenure, SeniorCitizen,MonthlyCharges.<jupyter_code># get to numericals
y = (data.Churn == 'Yes').astype(int)
y.head(5)
y.value_counts()<jupyter_output><empty_output><jupyter_text>Extract the independent variables and scale them<jupyter_code>X = data[["tenure", "SeniorCitizen", "MonthlyCharges"]]
X.info()
# rescaling using Standard Scaler
from sklearn.preprocessing import StandardScaler
transformer = StandardScaler().fit(X)
X_standardized = transformer.transform(X)
print(X_standardized.shape)<jupyter_output>(7043, 3)<jupyter_text>Building the logistic regression model, with imbalanced target<jupyter_code>from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4, random_state=40)
clf = LogisticRegression(random_state=0, solver='lbfgs',max_iter=400,multi_class='ovr').fit(X_train, y_train)
predictions = clf.predict(X_test)
accuracy_score(y_test, predictions)
from sklearn.metrics import confusion_matrix 
from sklearn.metrics import plot_confusion_matrix

cf_matrix = confusion_matrix(y_test,predictions)
sns.heatmap(cf_matrix/np.sum(cf_matrix), annot=True, 
            fmt='.2%', cmap='Blues');
group_names = ["True Neg","False Pos",'False Neg','True Pos']
group_counts = ['{0:0.0f}'.format(value) for value in
                cf_matrix.flatten()]
group_percentages = ['{0:.2%}'.format(value) for value in
                     cf_matrix.flatten()/np.sum(cf_matrix)]
labels = [f'{v1}\n{v2}\n{v3}' for v1, v2, v3 in
          zip(group_names,group_counts,group_percentages)]
labels = np.asarray(labels).reshape(2,2)
sns.heatmap(cf_matrix, annot=labels, fmt='', cmap='Blues');
from sklearn.metrics import roc_curve

# Make predictions with probabilities
y_probs = clf.predict_proba(X_test)

# Keep the probabilites of the positive class only
y_probs = y_probs[:, 1]

# Calculate fpr, tpr and thresholds
fpr, tpr, thresholds = roc_curve(y_test, y_probs)

# Check the false positive rate
fpr
import matplotlib.pyplot as plt

def plot_roc_curve(fpr, tpr):
    """
    Plots a ROC curve given the false positve rate (fpr) and 
    true postive rate (tpr) of a classifier.
    """
    # Plot ROC curve
    plt.plot(fpr, tpr, color='orange', label='ROC')
    # Plot line with no predictive power (baseline)
    plt.plot([0, 1], [0, 1], color='darkblue', linestyle='--', label='Guessing')
    # Customize the plot
    plt.xlabel('False Positive Rate')
    plt.ylabel('True Positive Rate')
    plt.title('Receiver Operating Characteristic (ROC) Curve')
    plt.legend()
    plt.show()
    
plot_roc_curve(fpr, tpr)
print(classification_report(y_test, predictions))<jupyter_output>precision    recall  f1-score   support

           0       0.82      0.91      0.86      2064
           1       0.65      0.46      0.54       754

    accuracy                           0.79      2818
   macro avg       0.74      0.68      0.70      2818
weighted avg       0.78      0.79      0.78      2818
{'fraction_non_alphanumeric': 0.11173358232181761, 'fraction_numerical': 0.029256146903205726, 'mean_word_length': 4.101587301587301, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '574252', 'license': 'Apache-2.0', 'n_tokens_mistral': 1158, 'n_tokens_neox': 1081, 'n_words': 259, 'path': 'churn.csv.ipynb', 'repo_name': 'aggysamy/Lab-imbalance-', 'size': '3213'}