<jupyter_start><jupyter_text>OVERVIEW---* Classify if Bank notes are fake or real* Data Preprocessing* HyperParameter Tuning by using RandomizedSearchCV* Comparisson between Keras Sequential Model and XGB Classifier<jupyter_code>#VIZ LIBRARY
import pandas as pd
from pandas import plotting
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from matplotlib import pyplot
plt.style.use('fivethirtyeight')

#Preprocessing
from sklearn import feature_extraction, linear_model, model_selection, preprocessing
from sklearn.model_selection import train_test_split, GridSearchCV, RandomizedSearchCV
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler, MinMaxScaler, OneHotEncoder, LabelEncoder

#MODELS
import xgboost as xgb
from xgboost import XGBClassifier, XGBRegressor
from sklearn.linear_model import SGDClassifier

#deep learning libs
import keras
from keras.models import Sequential
from keras.layers import LSTM, Activation, Dense, Dropout, Input, Embedding, Masking, Flatten
from keras.models import Model
from keras.optimizers import RMSprop
from keras.callbacks import EarlyStopping


#METRICS
from sklearn.metrics import accuracy_score, confusion_matrix,classification_report, r2_score,mean_absolute_error,mean_squared_error


import warnings 
warnings.filterwarnings('ignore')
#Load the data
df = pd.read_csv('data/bank_note_data.csv')<jupyter_output><empty_output><jupyter_text>DATA BASIC INFORMATION---<jupyter_code>df.head(3)
#Check the shape
print('DATASET SHAPE: ', df.shape)
#check the feature datatypes
df.info()
#get some statistical info by grouping them into class
df.groupby('Class').describe()<jupyter_output><empty_output><jupyter_text>CHECK NULL DATA<jupyter_code>df.isnull().sum()
# Seems like the data has no null data<jupyter_output><empty_output><jupyter_text>EDA---<jupyter_code>plt.rcParams['figure.figsize'] = (10,5)
sns.countplot(df.Class, palette='viridis')
plt.title('COUNTPLOT OF CLASSES')
plt.ylabel('Frequency')<jupyter_output><empty_output><jupyter_text>PAIRPLOT OF DATAFRAME<jupyter_code>sns.pairplot(df, hue='Class')<jupyter_output><empty_output><jupyter_text>DATA PREPARATION---<jupyter_code>X = df.drop('Class', axis=1)
y = df['Class']

#scale the data
scaler = StandardScaler()
scaled_features = scaler.fit_transform(X)
#Converting the scaled_features back to original df
df_scaled_features = pd.DataFrame(scaled_features, columns=df.columns[:-1])<jupyter_output><empty_output><jupyter_text>PREDICTIVE MODELLING---  SPLIT THE DATA<jupyter_code>X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=101)<jupyter_output><empty_output><jupyter_text>KERAS MODEL---<jupyter_code>#building the keras Sequential model
model = Sequential()
model.add(Dense(units = 100 , activation = 'relu', input_dim=X_train.shape[1]))
model.add(Dense(units = 50 , activation = 'relu'))
model.add(Dense(units = 25 , activation = 'relu'))
model.add(Dense(units = 10 , activation = 'relu'))
model.add(Dense(units = 1 , activation = 'sigmoid'))
model.compile(optimizer = 'adam' , loss = 'binary_crossentropy' , metrics = ['accuracy'])
model.summary()
#train the model with 5 epochs
model.fit(X_train, y_train, epochs=5)
#Get predictions of X_test
predictions = model.predict_classes(X_test)
predictions = predictions.reshape(predictions.shape[0])
#print Classification report
print(classification_report(y_test, predictions))<jupyter_output>precision    recall  f1-score   support

           0       1.00      1.00      1.00       238
           1       1.00      1.00      1.00       174

    accuracy                           1.00       412
   macro avg       1.00      1.00      1.00       412
weighted avg       1.00      1.00      1.00       412<jupyter_text>*  The model looks good, it was able to predict the classes perfectly with 100% accuracy!<jupyter_code>#let's try XGBoost<jupyter_output><empty_output><jupyter_text>XGBOOST CLASSIFIER---<jupyter_code>from scipy.stats import uniform

#RandomSearchCV
# Hyperparameters to tune and their ranges
param_dist = {"learning_rate": uniform(0, 2),
              "gamma": uniform(1, 0.000001),
              "max_depth": range(1,50),
              "n_estimators": range(1,300),
              "min_child_weight": range(1,10),
              'n_jobs': range(1,5)}
#instance of RansearchCV
rs = RandomizedSearchCV(XGBClassifier(), param_distributions=param_dist, n_iter=25) #25 iterations
#fit the model
rs.fit(X_train, y_train)
#Predict the test data
predictions = rs.predict(X_test)
#accuracy test
xgb_accu = accuracy_score(y_test, predictions)
print('Model Accuracy: ', round(xgb_accu*100, 2), '%')
#print classification report
print(classification_report(y_test, predictions))<jupyter_output>precision    recall  f1-score   support

           0       1.00      1.00      1.00       238
           1       0.99      0.99      0.99       174

    accuracy                           1.00       412
   macro avg       1.00      1.00      1.00       412
weighted avg       1.00      1.00      1.00       412
{'fraction_non_alphanumeric': 0.09922909665941886, 'fraction_numerical': 0.03419648151808658, 'mean_word_length': 4.382978723404255, 'pattern_counts': {'":': 5, '<': 39, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '452769', 'license': 'MIT', 'n_tokens_mistral': 1790, 'n_tokens_neox': 1664, 'n_words': 402, 'path': 'Classifying Bank Notes with (TensorFlow, Keras)/banknotes_keras.ipynb', 'repo_name': 'iamwendellbalagot/dsprojects', 'size': '5059'}