<jupyter_start><jupyter_text>Visualizations<jupyter_code>data['LIMIT_BAL'].hist()
plt.title("Histogram LIMIT_BAL")
plt.xlabel("LIMIT_BAL")
plt.ylabel("frequency")


#X = data[['log_limit_bal' ]]  #  auc = 0.63 , coeff = -0.949
#X = data[['dummy_for_sex']]  #  auc = 0.53 , coeff = 0.168
#X = data[['dummy_for_grad_school' ]]  #  auc = 0.53 , coeff = -1.17
#X = data[['dummy_for_university']] # auc =  0.53 , coeff = +
#X = data[['dummy_for_high_school' ]] # auc = 0.51 , coeff = +
#X = data[['dummy_for_married']] # auc =  0.52 , coeff =  +
#X = data[['dummy_for_single' ]] # auc =  0.52 , coeff = -
#X = data[['PAY_0' ]] # auc = 0.69 , coeff = +
#X = data[['bill_over_credit' ]] # auc = 0.57 , coeff = +
#X = data[['aver_pay_over_balance' ]] # auc = 0.61 , coeff = -






X = data[['log_limit_bal','dummy_for_sex' ,   \
   'dummy_for_married',  'PAY_0', 'aver_pay_over_balance'   ]]  #auc = 0.72







y = data['default payment next month']
y=y.astype('int')
X_train, X_test, y_train, y_test = train_test_split(X, y, train_size = 0.75, random_state = 1)
model = LogisticRegression()
model.fit(X_train,y_train)
y_pred = model.predict(X_test)
print(model.coef_)
print(model.intercept_)
from sklearn import metrics
print("Accuracy:",metrics.accuracy_score(y_test, y_pred))
print("Precision:",metrics.precision_score(y_test, y_pred))
print("Recall:",metrics.recall_score(y_test, y_pred))
print("Accuracy_Score:",accuracy_score(y_test,y_pred))
confusion_matrix(y_test,y_pred)
#plot_confusion_matrix(confusion_matrix(y_test,y_pred),
#                      classes= class_names, normalize = True, 
#                      title='Normalized Confusion Matrix: Logistic Regression')

from sklearn import metrics
y_pred_proba = model.predict_proba(X_test)[::,1]
fpr, tpr, _ = metrics.roc_curve(y_test,  y_pred_proba)
auc = metrics.roc_auc_score(y_test, y_pred_proba)
plt.plot(fpr,tpr,label="data 1, auc=" + repr(auc))
plt.xlabel('False positive rate')
plt.ylabel('True positive rate')
plt.legend(loc=4)
plt.show()
data1 = data[['log_limit_bal','dummy_for_sex' ,   \
   'dummy_for_married',  'PAY_0', 'aver_pay_over_balance' , 'default payment next month'  ]] 
data1.head()
corr = data1.corr()
corr.style.background_gradient(cmap='coolwarm').set_precision(2)

#from sklearn import svm
#y = data1['default payment next month']
#y=y.astype('int')
#X_train, X_test, y_train, y_test = train_test_split(X, y, train_size = 0.75, random_state = 1)
#model = svm.SVR()
#model.fit(X_train,y_train)
#y_pred = model.predict(X_test)

#from sklearn import metrics
#print("Accuracy:",metrics.accuracy_score(y_test, y_pred))
#print("Precision:",metrics.precision_score(y_test, y_pred))
#print("Recall:",metrics.recall_score(y_test, y_pred))
#print("Accuracy_Score:",accuracy_score(y_test,y_pred))
"""
from sklearn.ensemble import RandomForestRegressor
y = data1['default payment next month']
y=y.astype('int')
X_train, X_test, y_train, y_test = train_test_split(X, y, train_size = 0.75, random_state = 1)
model = RandomForestRegressor()
model.fit(X_train,y_train)
y_pred = model.predict(X_test)

from sklearn import metrics
print("Accuracy:",metrics.accuracy_score(y_test, y_pred))
print("Precision:",metrics.precision_score(y_test, y_pred))
print("Recall:",metrics.recall_score(y_test, y_pred))
print("Accuracy_Score:",accuracy_score(y_test,y_pred))
"""
confusion_matrix(y_test,y_pred)
from sklearn import metrics
y_pred_proba = model.predict_proba(X_test)[::,1]
fpr, tpr, _ = metrics.roc_curve(y_test,  y_pred_proba)
auc = metrics.roc_auc_score(y_test, y_pred_proba)
plt.plot(fpr,tpr,label="data 1, auc=" + repr(auc))
plt.xlabel('False positive rate')
plt.ylabel('True positive rate')
plt.legend(loc=4)
plt.show()<jupyter_output><empty_output><jupyter_text>Logistic Regression<jupyter_code>#import xgboost as xgb
from sklearn.linear_model import LogisticRegression

X = data1[['log_limit_bal','dummy_for_sex' ,   \
   'dummy_for_married',  'PAY_0', 'aver_pay_over_balance'   ]]  
y = data1['default payment next month']
y=y.astype('int')
X_train, X_test, y_train, y_test = train_test_split(X, y, train_size = 0.75, random_state = 1)
#model = XGBClassifier()
model = LogisticRegression()
model.fit(X_train,y_train)
y_pred = model.predict(X_test)

from sklearn import metrics
print("Accuracy:",metrics.accuracy_score(y_test, y_pred))
print("Precision:",metrics.precision_score(y_test, y_pred))
print("Recall:",metrics.recall_score(y_test, y_pred))
print("Accuracy_Score:",accuracy_score(y_test,y_pred))

print(confusion_matrix(y_test,y_pred))


from sklearn import metrics
y_pred_proba = model.predict_proba(X_test)[::,1]
fpr, tpr, _ = metrics.roc_curve(y_test,  y_pred_proba)
auc = metrics.roc_auc_score(y_test, y_pred_proba)
plt.plot(fpr,tpr,label="data 1, auc=" + repr(auc))
plt.xlabel('False positive rate')
plt.ylabel('True positive rate')
plt.legend(loc=4)
plt.show()

X_test[:50]
y_test[:50]
model.predict(X_test[:50])
model.predict_proba(X_test[:50])<jupyter_output><empty_output><jupyter_text>XGBoost<jupyter_code>import xgboost as xgb
from xgboost import XGBClassifier
#from sklearn.linear_model import LogisticRegression

X = data1[['log_limit_bal','dummy_for_sex' ,   \
   'dummy_for_married',  'PAY_0', 'aver_pay_over_balance'   ]]  
y = data1['default payment next month']
y=y.astype('int')
X_train, X_test, y_train, y_test = train_test_split(X, y, train_size = 0.75, random_state = 1)
model = XGBClassifier()
#model = LogisticRegression()
model.fit(X_train,y_train)
y_pred = model.predict(X_test)

from sklearn import metrics
print("Accuracy:",metrics.accuracy_score(y_test, y_pred))
print("Precision:",metrics.precision_score(y_test, y_pred))
print("Recall:",metrics.recall_score(y_test, y_pred))
print("Accuracy_Score:",accuracy_score(y_test,y_pred))
#print("Score:",model.score(X_test, y_test))

print(confusion_matrix(y_test,y_pred))


from sklearn import metrics
y_pred_proba = model.predict_proba(X_test)[::,1]
fpr, tpr, _ = metrics.roc_curve(y_test,  y_pred_proba)
auc = metrics.roc_auc_score(y_test, y_pred_proba)
plt.plot(fpr,tpr,label="data 1, auc=" + repr(auc))
plt.xlabel('False positive rate')
plt.ylabel('True positive rate')
plt.legend(loc=4)
plt.show()

X_test[:50]
y_test[:50]
model.predict(X_test[:50])
model.predict_proba(X_test[:50])<jupyter_output><empty_output><jupyter_text>Naive Bayes<jupyter_code>from sklearn.naive_bayes import GaussianNB

X = data1[['log_limit_bal','dummy_for_sex' ,   \
   'dummy_for_married',  'PAY_0', 'aver_pay_over_balance'   ]]  
y = data1['default payment next month']
y=y.astype('int')
X_train, X_test, y_train, y_test = train_test_split(X, y, train_size = 0.75, random_state = 1)
model = GaussianNB()
#model = LogisticRegression()
model.fit(X_train,y_train)
y_pred = model.predict(X_test)

from sklearn import metrics
print("Accuracy:",metrics.accuracy_score(y_test, y_pred))
print("Precision:",metrics.precision_score(y_test, y_pred))
print("Recall:",metrics.recall_score(y_test, y_pred))
print("Accuracy_Score:",accuracy_score(y_test,y_pred))
#print("Score:",model.score(X_test, y_test))

print(confusion_matrix(y_test,y_pred))


from sklearn import metrics
y_pred_proba = model.predict_proba(X_test)[::,1]
fpr, tpr, _ = metrics.roc_curve(y_test,  y_pred_proba)
auc = metrics.roc_auc_score(y_test, y_pred_proba)
plt.plot(fpr,tpr,label="data 1, auc=" + repr(auc))
plt.xlabel('False positive rate')
plt.ylabel('True positive rate')
plt.legend(loc=4)
plt.show()<jupyter_output>Accuracy: 0.22626666666666667
Precision: 0.2225204670514025
Recall: 0.9940047961630696
Accuracy_Score: 0.22626666666666667
[[  39 5793]
 [  10 1658]]<jupyter_text>Random Forest<jupyter_code>from sklearn.ensemble import RandomForestClassifier

X = data1[['log_limit_bal','dummy_for_sex' ,   \
   'dummy_for_married',  'PAY_0', 'aver_pay_over_balance'   ]]  
y = data1['default payment next month']
y=y.astype('int')
X_train, X_test, y_train, y_test = train_test_split(X, y, train_size = 0.75, random_state = 1)
model = RandomForestClassifier()
#model = LogisticRegression()
model.fit(X_train,y_train)
y_pred = model.predict(X_test)

from sklearn import metrics
print("Accuracy:",metrics.accuracy_score(y_test, y_pred))
print("Precision:",metrics.precision_score(y_test, y_pred))
print("Recall:",metrics.recall_score(y_test, y_pred))
print("Accuracy_Score:",accuracy_score(y_test,y_pred))
#print("Score:",model.score(X_test, y_test))

print(confusion_matrix(y_test,y_pred))


from sklearn import metrics
y_pred_proba = model.predict_proba(X_test)[::,1]
fpr, tpr, _ = metrics.roc_curve(y_test,  y_pred_proba)
auc = metrics.roc_auc_score(y_test, y_pred_proba)
plt.plot(fpr,tpr,label="data 1, auc=" + repr(auc))
plt.xlabel('False positive rate')
plt.ylabel('True positive rate')
plt.legend(loc=4)
plt.show()<jupyter_output>c:\users\gabri\appdata\local\programs\python\python37\lib\site-packages\sklearn\ensemble\weight_boosting.py:29: DeprecationWarning: numpy.core.umath_tests is an internal NumPy module and should not be imported. It will be removed in a future NumPy release.
  from numpy.core.umath_tests import inner1d
c:\users\gabri\appdata\local\programs\python\python37\lib\site-packages\sklearn\ensemble\gradient_boosting.py:34: DeprecationWarning: `np.bool` is a deprecated alias for the builtin `bool`. To silence this warning, use `bool` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.bool_` here.
Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
  from ._gradient_boosting import predict_stages
c:\users\gabri\appdata\local\programs\python\python37\lib\site-packages\sklearn\ensemble\gradient_boosting.py:34: DeprecationWarning: `np.bool` is a deprecated alias [...]<jupyter_text>KNN<jupyter_code>from sklearn.neighbors import KNeighborsClassifier

X = data1[['log_limit_bal','dummy_for_sex' ,   \
   'dummy_for_married',  'PAY_0', 'aver_pay_over_balance'   ]]  
y = data1['default payment next month']
y=y.astype('int')
X_train, X_test, y_train, y_test = train_test_split(X, y, train_size = 0.75, random_state = 1)
model = KNeighborsClassifier(n_neighbors = 50)
#model = LogisticRegression()
model.fit(X_train,y_train)
y_pred = model.predict(X_test)

from sklearn import metrics
print("Accuracy:",metrics.accuracy_score(y_test, y_pred))
print("Precision:",metrics.precision_score(y_test, y_pred))
print("Recall:",metrics.recall_score(y_test, y_pred))
print("Accuracy_Score:",accuracy_score(y_test,y_pred))
#print("Score:",model.score(X_test, y_test))

print(confusion_matrix(y_test,y_pred))


from sklearn import metrics
y_pred_proba = model.predict_proba(X_test)[::,1]
fpr, tpr, _ = metrics.roc_curve(y_test,  y_pred_proba)
auc = metrics.roc_auc_score(y_test, y_pred_proba)
plt.plot(fpr,tpr,label="data 1, auc=" + repr(auc))
plt.xlabel('False positive rate')
plt.ylabel('True positive rate')
plt.legend(loc=4)
plt.show()<jupyter_output>c:\users\gabri\appdata\local\programs\python\python37\lib\site-packages\sklearn\neighbors\base.py:781: DeprecationWarning: `np.int` is a deprecated alias for the builtin `int`. To silence this warning, use `int` by itself. Doing this will not modify any behavior and is safe. When replacing `np.int`, you may wish to use e.g. `np.int64` or `np.int32` to specify the precision. If you wish to review your current use, check the release note link for additional information.
Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
  self._y = np.empty(y.shape, dtype=np.int)
{'fraction_non_alphanumeric': 0.145095662713185, 'fraction_numerical': 0.022249155917236602, 'mean_word_length': 7.022222222222222, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 22, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '386557', 'license': 'MIT', 'n_tokens_mistral': 4734, 'n_tokens_neox': 4440, 'n_words': 789, 'path': 'Credit_card_uci.ipynb', 'repo_name': 'gequitz/Issuing-Credit-Cards-and-SQL-Assignment', 'size': '11551'}