# ---
# 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 numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from scipy import stats
# %matplotlib inline
import pandas as pd
from scipy.stats import entropy
import math
import keras
from sklearn.model_selection import train_test_split
from keras.callbacks import EarlyStopping
from sklearn.preprocessing import StandardScaler,MinMaxScaler
from keras import optimizers
from sklearn.preprocessing import OneHotEncoder
import matplotlib.pyplot as plt

data=pd.read_csv(r"C:\Users\rajan\OneDrive\Documents\FullDataset.csv",sep=",")
X=data.iloc[:,0:9]
print(X)
Y=data.iloc[:,9]
print(Y)

# data=pd.read_csv(r"C:\Users\rajan\OneDrive\Documents\Returns.csv",sep=",")
# X=data.iloc[:,0]
# print(X.quantile(0.3))
# print(X.quantile(0.7))

scaler = MinMaxScaler(feature_range=(0, 1))
X = scaler.fit_transform(X)
print(X[0])

from sklearn.utils import class_weight
class_weights = class_weight.compute_class_weight('balanced',np.unique(Y),Y)
print(class_weights)

# onehot_encoder = OneHotEncoder(sparse=False)
# Y=Y.to_numpy()
# Y = Y.reshape(len(Y), 1)
# Y = onehot_encoder.fit_transform(Y)
# print(Y)

# print(len(Y))
# print((len(Y[Y==0])/len(Y))*100)
# print((len(Y[Y==1])/len(Y))*100)
# print((len(Y[Y==2])/len(Y))*100)

# +
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2, random_state=42,stratify=Y)

onehot_encoder = OneHotEncoder(sparse=False)
Y_train=Y_train.to_numpy()
Y_train = Y_train.reshape(len(Y_train), 1)
Y_train = onehot_encoder.fit_transform(Y_train)
old=Y_test
Y_test =Y_test.to_numpy()
Y_test = Y_test.reshape(len(Y_test), 1)
Y_test = onehot_encoder.transform(Y_test)

# c_weight = {0: 1.75, 1: 1, 2: 2}

opt=optimizers.Adam(lr=0.001);
# 0.001
model1 = keras.models.Sequential()
model1.add(keras.layers.Dense(units=256, activation='relu',input_dim=X_train.shape[1]))
# model1.add(keras.layers.Dense(units=256, activation='relu'))
model1.add(keras.layers.Dropout(0.1))
model1.add(keras.layers.Dense(units=128, activation='relu'))
model1.add(keras.layers.Dropout(0.1))
model1.add(keras.layers.Dense(units=64, activation='relu'))
model1.add(keras.layers.Dropout(0.1))
model1.add(keras.layers.Dense(units=32, activation='relu'))
model1.add(keras.layers.Dropout(0.1))
model1.add(keras.layers.Dense(units=16, activation='relu'))
model1.add(keras.layers.Dropout(0.1))
model1.add(keras.layers.Dense(units=8, activation='relu'))
model1.add(keras.layers.Dropout(0.1))
model1.add(keras.layers.Dense(units=5, activation='softmax'))
model1.summary()
model1.compile(loss='categorical_crossentropy',
  optimizer=opt,
  metrics=['accuracy'],)
history=model1.fit(X_train, Y_train,
  batch_size=128,
  epochs=3000,
  verbose=1,
  class_weight=class_weights,
  validation_data=(X_test, Y_test),
#   callbacks = [EarlyStopping(monitor='val_loss', patience=5)],
  shuffle=True)
# -

plt.plot(history.history['acc'])
plt.plot(history.history['val_acc'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
# summarize history for loss
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()

# +
score = model1.evaluate(X_test, Y_test, verbose=0)
print('Test accuracy:', score[1]*100)
print(model1.metrics_names)

Y_pred=model1.predict(X_test)


# print(len(Y_test[Y_test==1]))
print(len(Y_pred))
print(len(old[old==4]))
Y_pred = np.argmax(Y_pred, axis=1)
print(len(Y_pred[Y_pred==4]))

# print(len(Y_pred[Y_pred<0.5]))
# print(len(Y_pred[Y_pred<0.5]))

# -

# print(Y_pred)
# Y_pred = np.argmax(Y_pred, axis=1)
print(Y_pred)
new=Y_pred
Y_pred = Y_pred.reshape(len(Y_pred), 1)
Y_pred = onehot_encoder.transform(Y_pred)

# Y_pred=(Y_pred > 0.5).astype(np.int)
#
# # print(Y_pred)
# Y_pred=np.ravel(Y_pred)
# # print(len(Y_pred[Y_pred==0]))

from sklearn.metrics import confusion_matrix,classification_report
cm = confusion_matrix(old, new)
print(cm)
print(classification_report(Y_test, Y_pred))



StarcoderdataJupyterScriptsDedupFiltered