<jupyter_start><jupyter_text>Bank Authentication Data Analysis We'll use the Bank Authentication Data Set. 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 indicates whether or not a Bank Note was authentic.This sort of task is perfectly suited for Neural Networks and Deep Learning.  Get the Data**Use pandas to read in the** `bank_note_data.csv` **file.**<jupyter_code>import pandas as pd
df = pd.read_csv("mk029-project_bank_authentication_data_analysis_with_tensorflow/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>Exploratory Data Analysis<jupyter_code>import matplotlib.pyplot as plt
import seaborn as sns
sns.set(palette="bright")<jupyter_output><empty_output><jupyter_text>**Create a** `countplot` **of the Classes (Authentic 1 vs Fake 0).**<jupyter_code>sns.countplot(x="Class", 
              data=df)<jupyter_output><empty_output><jupyter_text>**Create a** `pairplot` **and set** `hue="Class"`.<jupyter_code>sns.pairplot(df, hue="Class", 
             diag_kind="kde")<jupyter_output>C:\Users\MK\Anaconda\lib\site-packages\statsmodels\nonparametric\kde.py:488: RuntimeWarning: invalid value encountered in true_divide
  binned = fast_linbin(X, a, b, gridsize) / (delta * nobs)
C:\Users\MK\Anaconda\lib\site-packages\statsmodels\nonparametric\kdetools.py:34: RuntimeWarning: invalid value encountered in double_scalars
  FAC1 = 2*(np.pi*bw/RANGE)**2<jupyter_text>Data Preparation When using Neural Network and Deep Learning based systems, it is usually a good idea to Standardize the data. Standard Scaling<jupyter_code>from sklearn.preprocessing import StandardScaler<jupyter_output><empty_output><jupyter_text>**Create a** `StandardScaler()` **object instance.**<jupyter_code>ss = StandardScaler()<jupyter_output><empty_output><jupyter_text>**Fit** `ss` **to the features.**<jupyter_code>ss.fit(df.drop(["Class"], 
               axis=1))<jupyter_output><empty_output><jupyter_text>**Use the** `transform()` **method to transform the features to a scaled version.**<jupyter_code>scaled_features = ss.transform(df.drop(["Class"], 
                                       axis=1))
type(scaled_features)<jupyter_output><empty_output><jupyter_text>**Convert the scaled features to a dataframe and check the head of this dataframe to make sure the scaling worked.**<jupyter_code>sdf = pd.DataFrame(scaled_features, 
                   columns=df.columns[:-1])
sdf.head()<jupyter_output><empty_output><jupyter_text>Train Test Split**Create two objects X and y which are the scaled feature values and labels respectively.**<jupyter_code>X = sdf
y = df["Class"]
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, 
                                                    y, 
                                                    test_size=0.3)<jupyter_output><empty_output><jupyter_text>Tensorflow<jupyter_code>import tensorflow as tf<jupyter_output><empty_output><jupyter_text>**Create a list of feature column objects using** `tf.feature.numeric_column()`.<jupyter_code>feat_cols = []
[feat_cols.append(tf.feature_column.numeric_column(col)) for col in X.columns]
feat_cols<jupyter_output><empty_output><jupyter_text>**Create an object called** `classifier` **which is an instance of** `DNNClassifier` **. Set it to have 2 classes and a [10, 20, 10] hidden unit layer structure:**<jupyter_code>classifier = tf.estimator.DNNClassifier(hidden_units=[10, 20, 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\MK\AppData\Local\Temp\tmp9sigt3bo
INFO:tensorflow:Using config: {'_model_dir': 'C:\\Users\\MK\\AppData\\Local\\Temp\\tmp9sigt3bo', '_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 0x00000244EC6B50B8>, '_task_type': 'worker', '_task_id': 0, '_global_id_in_cluster': 0, '_master': '', '_evaluation_master': '', '_is_chief': True, '_num_ps_replicas': 0, '_num_worker_replicas': 1}<jupyter_text>**Create a** `tf.estimator.pandas_input_fn` **that takes in your** `X_train`, `y_train`, `batch_size` **and set** `shuffle=True`.<jupyter_code>tr_input_func = tf.estimator.inputs.pandas_input_fn(x=X_train, 
                                                    y=y_train, 
                                                    batch_size=20, 
                                                    shuffle=True)<jupyter_output><empty_output><jupyter_text>**Train** `classifier` **to the input function. Use** `steps=500`.<jupyter_code>classifier.train(input_fn=tr_input_func, 
                 steps=500)<jupyter_output>WARNING:tensorflow:From C:\Users\MK\Anaconda\lib\site-packages\tensorflow\python\framework\op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.
Instructions for updating:
Colocations handled automatically by placer.
WARNING:tensorflow:From C:\Users\MK\Anaconda\lib\site-packages\tensorflow_estimator\python\estimator\inputs\queues\feeding_queue_runner.py:62: QueueRunner.__init__ (from tensorflow.python.training.queue_runner_impl) is deprecated and will be removed in a future version.
Instructions for updating:
To construct input pipelines, use the `tf.data` module.
WARNING:tensorflow:From C:\Users\MK\Anaconda\lib\site-packages\tensorflow_estimator\python\estimator\inputs\queues\feeding_functions.py:500: add_queue_runner (from tensorflow.python.training.queue_runner_impl) is deprecated and will be removed in a future version.
Instructions for updating:
To construct input pipelines, use the `tf.data` module.
INFO[...]<jupyter_text>Model Evaluation **Create another** `pandas_input_func` **that takes in the** `X_test` **data for x. Set** `shuffle=False` **since we don't need to shuffle for predictions.**<jupyter_code>pred_input_func = tf.estimator.inputs.pandas_input_fn(x=X_test, 
                                                      batch_size=len(X_test), 
                                                      shuffle=False)<jupyter_output><empty_output><jupyter_text>**Use the** `predict` **method from the** `classifier` **model to create predictions from** `X_test`.<jupyter_code>predictions = list(classifier.predict(input_fn=pred_input_func))
predictions[:1]
final_predictions = []
[final_predictions.append(p["class_ids"][0]) for p in predictions]
final_predictions[:5]<jupyter_output><empty_output><jupyter_text>**Create a** `classification_report` **and a** `confusion_matrix`.<jupyter_code>from sklearn.metrics import classification_report, confusion_matrix, accuracy_score
print(classification_report(y_test, final_predictions))
print(confusion_matrix(y_test, final_predictions))
print(accuracy_score(y_test, final_predictions))<jupyter_output>precision    recall  f1-score   support

           0       0.99      0.96      0.98       227
           1       0.95      0.99      0.97       185

   micro avg       0.97      0.97      0.97       412
   macro avg       0.97      0.97      0.97       412
weighted avg       0.97      0.97      0.97       412

[[218   9]
 [  2 183]]
0.9733009708737864<jupyter_text>Optional ComparisonWe have extremely accurate results from the DNN model. Let's compare this to a Random Forest Classifier for a reality check!**Use SciKit-Learn to create a** `RandomForestClassifier` **and compare the** `classification_report` **and** `confusion_matrix` **to the DNN model.**<jupyter_code>from sklearn.ensemble import RandomForestClassifier
rfc = RandomForestClassifier(n_estimators=200)
rfc.fit(X_train, 
        y_train)
rfc_pred = rfc.predict(X_test)
print(classification_report(y_test, rfc_pred))
print(confusion_matrix(y_test, rfc_pred))
print(accuracy_score(y_test, rfc_pred))<jupyter_output>precision    recall  f1-score   support

           0       1.00      0.99      0.99       227
           1       0.99      0.99      0.99       185

   micro avg       0.99      0.99      0.99       412
   macro avg       0.99      0.99      0.99       412
weighted avg       0.99      0.99      0.99       412

[[225   2]
 [  1 184]]
0.9927184466019418
{'fraction_non_alphanumeric': 0.10612027674294838, 'fraction_numerical': 0.029377328366152207, 'mean_word_length': 4.098209441128595, 'pattern_counts': {'":': 0, '<': 77, '<?xml version=': 0, '>': 77, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 27, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '20', 'dir': 'jupyter-structured-clean-dedup', 'id': '428190', 'license': 'MIT', 'n_tokens_mistral': 3131, 'n_tokens_neox': 2889, 'n_words': 694, 'path': 'mk029-project_bank_authentication_data_analysis_with_tensorflow.ipynb', 'repo_name': 'karakose77/python-for-data-science-and-machine-learning-exercises-and-projects', 'size': '9395'}