<jupyter_start><jupyter_text>Task 4: Credit Card Default Prediction  **Run the following two cells before you begin.**<jupyter_code>%autosave 10
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split<jupyter_output><empty_output><jupyter_text>______________________________________________________________________**First, import your data set and define the sigmoid function.**    Hint:    The definition of the sigmoid is $f(x) = \frac{1}{1 + e^{-X}}$.<jupyter_code># Import the data set
filepath="C:/Users/uttam/anaconda3/Technocolabs/MinorProj2/Datasets/cleaned_data.csv"
df= pd.read_csv(filepath)
df.head()
# Define the sigmoid function
def sigmoid(x):
    return 1 /( np.exp(-x) + 1 )<jupyter_output><empty_output><jupyter_text>**Now, create a train/test split (80/20) with `PAY_1` and `LIMIT_BAL` as features and `default payment next month` as values. Use a random state of 24.**<jupyter_code># Create a train/test split
# I renamed default payment next month to DEFAULT for easier readablity
X_train, X_test, y_train, y_test = train_test_split(
                            df[['PAY_1', 'LIMIT_BAL']].values, df['DEFAULT'].values,
                            test_size=0.2, random_state=24)<jupyter_output><empty_output><jupyter_text>______________________________________________________________________**Next, import LogisticRegression, with the default options, but set the solver to `'liblinear'`.**<jupyter_code>from sklearn.linear_model import LogisticRegression
LogisticRegressor= LogisticRegression(solver='liblinear')
LogisticRegressor<jupyter_output><empty_output><jupyter_text>______________________________________________________________________**Now, train on the training data and obtain predicted classes, as well as class probabilities, using the testing data.**<jupyter_code># Fit the logistic regression model on training data
LogisticRegressor.fit(X_train, y_train)
# Make predictions using `.predict()`
y_predict = LogisticRegressor.predict(X_test)
# Find class probabilities using `.predict_proba()`
y_predict_proba = LogisticRegressor.predict_proba(X_test)<jupyter_output><empty_output><jupyter_text>______________________________________________________________________**Then, pull out the coefficients and intercept from the trained model and manually calculate predicted probabilities. You'll need to add a column of 1s to your features, to multiply by the intercept.**<jupyter_code>print( LogisticRegressor.intercept_,LogisticRegressor.coef_)
# Add column of 1s to features
one_array = np.ones((X_test.shape[0],1))
ones_with_feature = np.hstack([one_array, X_test])
ones_with_feature
# Get coefficients and intercepts from trained model
coefficients_and_intercept = np.concatenate([LogisticRegressor.intercept_.reshape(1,1), LogisticRegressor.coef_], axis=1)
coefficients_and_intercept
# Manually calculate predicted probabilities
X_linear = np.dot(coefficients_and_intercept, np.transpose(ones_with_feature))
# For checking: X_linear.shape
y_predict_prob_manual = sigmoid(X_linear)<jupyter_output><empty_output><jupyter_text>______________________________________________________________________**Next, using a threshold of `0.5`, manually calculate predicted classes. Compare this to the class predictions output by scikit-learn.**<jupyter_code># Manually calculate predicted classes
y_predict_manual = (y_predict_prob_manual >= 0.5)
#Shapes of both predicted y values
print("Sklearn Predicted Shape: ",y_predict.shape)
print("Manual Predicted Shape: ",y_predict_manual.shape)
# Compare to scikit-learn's predicted classes
np.array_equal(y_predict.reshape(1,-1), y_predict_manual)<jupyter_output><empty_output><jupyter_text>______________________________________________________________________**Finally, calculate ROC AUC using both scikit-learn's predicted probabilities, and your manually predicted probabilities, and compare.**<jupyter_code># Use scikit-learn's predicted probabilities to calculate ROC AUC
from sklearn.metrics import roc_auc_score
print(roc_auc_score(y_test, y_predict_proba[:,1]))
# Use manually calculated predicted probabilities to calculate ROC AUC
print(roc_auc_score(y_test, y_predict_prob_manual.reshape(y_predict_prob_manual.shape[1],)))<jupyter_output>0.627207450280691
{'fraction_non_alphanumeric': 0.0870582728761994, 'fraction_numerical': 0.012169435993447227, 'mean_word_length': 7.812371134020618, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 33, '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': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '319337', 'license': 'Apache-2.0', 'n_tokens_mistral': 1284, 'n_tokens_neox': 1204, 'n_words': 327, 'path': 'DataScience/Logistic_regression/Fitting_a_Logistic_Regression_Model.ipynb', 'repo_name': 'PranavHegde99/OpenOctober', 'size': '4273'}