# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# +
import tensorflow as tf
import keras
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# %matplotlib inline

import seaborn as sns
sns.set()

from keras.models import Sequential
from keras.layers import Dense
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix
# -

dataset = pd.read_csv(r"C:\Users\DIU\Desktop\Banknote authentication.csv")

dataset.head()

dataset.describe()

dataset.info()

dataset.isnull().sum()

plt.subplots(figsize=(8,5))
dataset.boxplot()

X = dataset.iloc[:, :-1]

scaler = StandardScaler()
scaler.fit(X)
scaled_features = pd.DataFrame(data=scaler.transform(X), columns=X.columns)

scaled_features.head()

sns.pairplot(data = dataset, hue="class")

sns.countplot(x = "class", data=dataset)

sns.heatmap(X.corr())

# +
class_map = {0 : "Forged", 1 : "Authentic"}
dataset["class"] = dataset["class"].map(class_map)

y = dataset["class"]
y = pd.get_dummies(y)
# -

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

learning_rate = 0.01
training_epochs = 100
batch_size = 100

n_hidden_1 = 4 
n_hidden_2 = 4 
n_input = 4 
n_classes = 2 
n_samples = X_train.shape[0]

x = tf.placeholder(tf.float32, [None, n_input])
y = tf.placeholder(tf.float32, [None, n_classes])

# +
weights = {
    'h1': tf.Variable(tf.random_normal([n_input, n_hidden_1])),
    'h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2])),
    'out': tf.Variable(tf.random_normal([n_hidden_2, n_classes]))
}

biases = {
    'b1': tf.Variable(tf.random_normal([n_hidden_1])),
    'b2': tf.Variable(tf.random_normal([n_hidden_2])),
    'out': tf.Variable(tf.random_normal([n_classes]))
}


# -

def multilayer_perceptron(x, weights, biases):
    
    '''
    x: Placeholder for data input
    weights: Dictionary of weights
    biases: Dictionary of biases
    
    '''
    
    layer_1 = tf.add(tf.matmul(x, weights['h1']), biases['b1'])
    layer_1 = tf.nn.relu(layer_1)
    
    layer_2 = tf.add(tf.matmul(layer_1, weights['h2']), biases['b2'])
    layer_2 = tf.nn.relu(layer_2)
    
    out_layer = tf.matmul(layer_2, weights['out'] + biases['out'])
    
    return out_layer


preds = multilayer_perceptron(x, weights, biases)

cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=preds))
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)

# +
sess = tf.InteractiveSession()
sess.run(tf.initialize_all_variables())
costs = []
for epoch in range(training_epochs):
    avg_cost = 0.0
    total_batch = int(n_samples/batch_size)
    for batch in range(total_batch):
        batch_x = X_train[batch*batch_size : (1+batch)*batch_size]
        batch_y = y_train[batch*batch_size : (1+batch)*batch_size]
        _, c = sess.run([optimizer, cost], feed_dict={x: batch_x, y: batch_y})
        avg_cost += c / total_batch
    
    print("Epoch: {} cost={:.4f}".format(epoch+1,avg_cost))
    costs.append(avg_cost)
    
print("Model has completed {} epochs of training.".format(training_epochs))
# -

correct_predictions = tf.cast(tf.equal(tf.argmax(preds, 1), tf.argmax(y, 1)), tf.float32)

accuracy = tf.reduce_mean(correct_predictions)
print("Accuracy:", accuracy.eval(feed_dict={x: X_test, y: y_test}))

rfc = RandomForestClassifier(n_estimators=10) 
rfc.fit(X_train, y_train)
preds_rfc = rfc.predict(X_test)

print(classification_report(y_test, preds_rfc))

print(confusion_matrix(y_test.values.argmax(axis=1), preds_rfc.argmax(axis=1)))

accuracy_score(y_test, preds_rfc)



StarcoderdataJupyterScriptsDedupFiltered