<jupyter_start><jupyter_text>BackgroundThis mini project will explore the [Bank Authentication Data Set](https://archive.ics.uci.edu/ml/datasets/banknote+authentication) from the UCI repository in order to perform a typical classification task using a simple deep neural network model (DNN) on TensorFlow. In order to investigate the effectiveness of DNN, a roughly constructed DNN model will be evaluated and compared to the highly-tuned logistic regression model by their accuracy.From the authors:>Data were extracted from images that were taken from genuine and forged banknote-like specimens. For digitization, an industrial camera usually used for print inspection was used. The final images have 400x 400 pixels. Due to the object lens and distance to the investigated object gray-scale pictures with a resolution of about 660 dpi were gained. Wavelet Transform tool were used to extract features from images.The data consists of 5 columns:* variance of Wavelet Transformed image (continuous)* skewness of Wavelet Transformed image (continuous)* curtosis of Wavelet Transformed image (continuous)* entropy of image (continuous)* class (integer)Where `class` is the target variable that indicates whether or not a Bank Note was authentic.  Libraries<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline

from sklearn.model_selection import train_test_split
from sklearn.model_selection import GridSearchCV
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import classification_report, confusion_matrix
import tensorflow as tf<jupyter_output><empty_output><jupyter_text>Data overview<jupyter_code>import pandas as pd
df = pd.read_csv('Data/bank_note_data.csv')<jupyter_output><empty_output><jupyter_text>** Check the head of the Data **<jupyter_code>df.head()<jupyter_output><empty_output><jupyter_text>EDA Distribution of the target variable<jupyter_code>df_pp = df.copy()
df_pp['Class'] = np.where(df['Class']==0,'Fake','Authentic')
sns.countplot(df_pp['Class'])
plt.show()<jupyter_output><empty_output><jupyter_text>Pairplot of the data by Class<jupyter_code>sns.pairplot(df_pp,hue='Class')
plt.show()<jupyter_output><empty_output><jupyter_text>Data preprocessing  Train-test split and standarization<jupyter_code>X = df.drop('Class',axis=1)
y = df['Class']
X_train,X_test,y_train,y_test = train_test_split(X,y,random_state=100)
scaler = StandardScaler().fit(X_train)
X_train_scaled = scaler.transform(X_train)
X_test_scaled = scaler.transform(X_test)

X_train_scaled_df = pd.DataFrame(X_train_scaled,columns=X_train.columns)
X_test_scaled_df = pd.DataFrame(X_test_scaled,columns=X_train.columns)<jupyter_output><empty_output><jupyter_text>Model_1: Tensorflow DNN model construction Creating a list of feature column objects<jupyter_code>feat_cols = []
for col in X_train_scaled_df.columns:
    feat_cols.append(tf.feature_column.numeric_column(col))<jupyter_output><empty_output><jupyter_text>Defining a classifier<jupyter_code>classifier = tf.estimator.DNNClassifier(hidden_units=[10,10],n_classes=2,feature_columns=feat_cols)<jupyter_output>INFO:tensorflow:Using default config.
WARNING:tensorflow:Using temporary folder as model directory: C:\Users\Sean\AppData\Local\Temp\tmphvgsnnpr
INFO:tensorflow:Using config: {'_model_dir': 'C:\\Users\\Sean\\AppData\\Local\\Temp\\tmphvgsnnpr', '_tf_random_seed': None, '_save_summary_steps': 100, '_save_checkpoints_steps': None, '_save_checkpoints_secs': 600, '_session_config': allow_soft_placement: true
graph_options {
  rewrite_options {
    meta_optimizer_iterations: ONE
  }
}
, '_keep_checkpoint_max': 5, '_keep_checkpoint_every_n_hours': 10000, '_log_step_count_steps': 100, '_train_distribute': None, '_device_fn': None, '_protocol': None, '_eval_distribute': None, '_experimental_distribute': None, '_service': None, '_cluster_spec': <tensorflow.python.training.server_lib.ClusterSpec object at 0x0000023BCA889198>, '_task_type': 'worker', '_task_id': 0, '_global_id_in_cluster': 0, '_master': '', '_evaluation_master': '', '_is_chief': True, '_num_ps_replicas': 0, '_num_worker_replicas':[...]<jupyter_text>Defining an input function<jupyter_code>input_func = tf.estimator.inputs.pandas_input_fn(X_train,y_train,batch_size=20,shuffle=True)<jupyter_output><empty_output><jupyter_text>Training a classifier with input function<jupyter_code>classifier.train(input_fn=input_func,steps=500)<jupyter_output>INFO:tensorflow:Calling model_fn.
INFO:tensorflow:Done calling model_fn.
INFO:tensorflow:Create CheckpointSaverHook.
INFO:tensorflow:Graph was finalized.
INFO:tensorflow:Running local_init_op.
INFO:tensorflow:Done running local_init_op.
INFO:tensorflow:Saving checkpoints for 0 into C:\Users\Sean\AppData\Local\Temp\tmphvgsnnpr\model.ckpt.
INFO:tensorflow:loss = 28.02778, step = 1
INFO:tensorflow:Saving checkpoints for 52 into C:\Users\Sean\AppData\Local\Temp\tmphvgsnnpr\model.ckpt.
INFO:tensorflow:Loss for final step: 0.032023825.<jupyter_text>Evaluation<jupyter_code>pred_func = tf.estimator.inputs.pandas_input_fn(X_test,batch_size=len(X_test),shuffle=False)
note_predictions = list(classifier.predict(input_fn=pred_func))
final_preds = []
for pred in note_predictions:
    final_preds.append(pred['class_ids'][0])
pd.DataFrame(confusion_matrix(y_test,final_preds),columns=['Predicted Fake','Predicted Authentic'],index=['True Fake','Predicted Authentic'])
print(classification_report(y_test,final_preds))<jupyter_output>precision    recall  f1-score   support

           0       1.00      1.00      1.00       198
           1       1.00      1.00      1.00       145

   micro avg       1.00      1.00      1.00       343
   macro avg       1.00      1.00      1.00       343
weighted avg       1.00      1.00      1.00       343<jupyter_text>Model_2: Highly tuned logistic regression<jupyter_code>grid_param ={'C':[0.01,0.03,0.1,0.3,1,3,10,30]}
grid_lr = GridSearchCV(LogisticRegression(solver='lbfgs'),grid_param,cv=3,).fit(X_train,y_train)
print('best param: {}'.format(grid_lr.best_params_))
print('best score: {}'.format(grid_lr.best_score_))
y_predict_lr = grid_lr.predict(X_test)
pd.DataFrame(confusion_matrix(y_test,y_predict_lr),columns=['Predicted Fake','Predicted Authentic'],index=['True Fake','Predicted Authentic'])
print(classification_report(y_test,y_predict_lr))<jupyter_output>precision    recall  f1-score   support

           0       1.00      0.98      0.99       198
           1       0.97      1.00      0.99       145

   micro avg       0.99      0.99      0.99       343
   macro avg       0.99      0.99      0.99       343
weighted avg       0.99      0.99      0.99       343
{'fraction_non_alphanumeric': 0.10214094558429973, 'fraction_numerical': 0.033303597977995834, 'mean_word_length': 5.836382113821138, 'pattern_counts': {'":': 0, '<': 46, '<?xml version=': 0, '>': 47, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '553441', 'license': 'MIT', 'n_tokens_mistral': 2364, 'n_tokens_neox': 2208, 'n_words': 486, 'path': 'Mini capstone projects/Bank authentication prediction_DNN buildup on TensorFlow.ipynb', 'repo_name': 'sungsujaing/DataScience_MachineLearning_Portfolio', 'size': '6726'}