<jupyter_start><jupyter_text>Q  To predict whether the client will subscribe a term deposit or not  Importing libraries<jupyter_code>import pandas as pd

import matplotlib.pyplot as plt
import seaborn as sns

from sklearn.linear_model import LogisticRegression<jupyter_output><empty_output><jupyter_text>Importing data<jupyter_code>bank = pd.read_csv('bank-full.csv', sep= ';')
bank<jupyter_output><empty_output><jupyter_text>Checking for null value<jupyter_code>bank.info()
bank.columns<jupyter_output><empty_output><jupyter_text>Checking frequencies<jupyter_code>sns.countplot(x='y',data=bank)
%matplotlib inline
pd.crosstab(bank.job,bank.y).plot(kind='bar')
plt.title('Subsribed Frequency for Job Title')
plt.xlabel('Job')
plt.ylabel('Frequency of Purchase')<jupyter_output><empty_output><jupyter_text>All occupations have  different type of subscription to term deposit and so this is an important feature<jupyter_code>table=pd.crosstab(bank.marital,bank.y)
table.div(table.sum(1).astype(float), axis=0).plot(kind='bar', stacked=False)
plt.title('Stacked Bar Chart of Marital Status vs Subscribed')
plt.xlabel('Marital Status')
plt.ylabel('Proportion of Customers')<jupyter_output><empty_output><jupyter_text>People from all status levels, have the same frequency. So, this is not an important feature<jupyter_code>table=pd.crosstab(bank.education,bank.y)
table.div(table.sum(1).astype(float), axis=0).plot(kind='bar')
plt.title('Stacked Bar Chart of Education vs Subscribed')
plt.xlabel('Education')
plt.ylabel('Proportion of Customers')<jupyter_output><empty_output><jupyter_text>People with tertiary education, tend to get subscription to term deposit, a little more than people from other education background<jupyter_code>pd.crosstab(bank.month,bank.y).plot(kind='bar')
plt.title('Purchase Frequency for Month')
plt.xlabel('Month')
plt.ylabel('Frequency of Subsribe')<jupyter_output><empty_output><jupyter_text>People tend to get term deposit mostly in the month of May  Checking for outliers<jupyter_code>bank.boxplot(column='age', by='y')
pd.crosstab(bank.poutcome,bank.y).plot(kind='bar')
plt.title('Subscribe Frequency for Poutcome')
plt.xlabel('Poutcome')
plt.ylabel('Frequency of Subscribe')
bank.head()<jupyter_output><empty_output><jupyter_text>Dropping all the unimportant features.<jupyter_code>bank.drop(["month","education","pdays","day","campaign","age",'loan',"housing",'marital'],axis=1,inplace=True)
bank.head()<jupyter_output><empty_output><jupyter_text>Creating dummy variable for categorical data<jupyter_code>contactd=pd.get_dummies(bank['contact'],drop_first=True)
jobd=pd.get_dummies(bank['job'],drop_first=True)
poutcomed=pd.get_dummies(bank['poutcome'],drop_first=True)
defaultd = pd.get_dummies(bank['default'],drop_first=True)
bank = pd.concat([bank,defaultd,poutcomed,jobd,contactd],axis=1)
bank.head()
bank.drop(['default','poutcome','job',"contact"],axis=1,inplace = True)
bank.head()
bank_new= bank.copy()
bank_new['y'] = bank_new['y'].map({'no':0,'yes':1})
bank_new<jupyter_output><empty_output><jupyter_text>Independent and Target variables<jupyter_code>X = bank.drop("y",axis=1)
Y = bank["y"]
from sklearn.linear_model import LogisticRegression
classifier = LogisticRegression()
classifier.fit(X,Y)<jupyter_output><empty_output><jupyter_text>Prediction<jupyter_code>y_pred = classifier.predict(X)
y_pred_prob=classifier.predict_proba(X)
y_pred_prob<jupyter_output><empty_output><jupyter_text>Evaluation metrics<jupyter_code>from sklearn.metrics import confusion_matrix
from sklearn.metrics import accuracy_score
cm=confusion_matrix = confusion_matrix(Y,y_pred)
accuracy=accuracy_score(Y,y_pred)
print (cm,accuracy)<jupyter_output>[[39005   917]
 [ 3605  1684]] 0.8999800933401163<jupyter_text>Accuray of the model is 90%<jupyter_code>from sklearn.metrics import classification_report
print(classification_report(Y,y_pred))<jupyter_output>precision    recall  f1-score   support

          no       0.92      0.98      0.95     39922
         yes       0.65      0.32      0.43      5289

    accuracy                           0.90     45211
   macro avg       0.78      0.65      0.69     45211
weighted avg       0.88      0.90      0.88     45211<jupyter_text>ROC Curve<jupyter_code>from sklearn import preprocessing
label_encoder = preprocessing.LabelEncoder() 
  
y_predict= label_encoder.fit_transform(y_pred) 

y_actual= label_encoder.fit_transform(bank_new['y']) 
from sklearn.metrics import roc_curve
from sklearn.metrics import roc_auc_score

fpr, tpr, thresholds = roc_curve(y_actual, classifier.predict_proba (X)[:,1])

area_under_curve = roc_auc_score(y_actual, y_predict)

import matplotlib.pyplot as plt
plt.plot(fpr, tpr, color='red', label='logit model ( area  = %0.2f)'%area_under_curve)
plt.plot([0, 1], [0, 1], 'k--')
plt.xlabel('False Positive Rate or [1 - True Negative Rate]')
plt.ylabel('True Positive Rate')
area_under_curve<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12599065230644177, 'fraction_numerical': 0.023775655354602722, 'mean_word_length': 7.203333333333333, 'pattern_counts': {'":': 0, '<': 59, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '15', 'dir': 'jupyter-structured-clean-dedup', 'id': '507539', 'license': 'Apache-2.0', 'n_tokens_mistral': 1850, 'n_tokens_neox': 1729, 'n_words': 313, 'path': 'Logistic Regression/Logistic Regression (Bank).ipynb', 'repo_name': 'santaaaaaa/Data-science---Illustrative-cases', 'size': '4921'}