# -*- coding: utf-8 -*-
"""nnetwork.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1pMeLa_X6bXVDBF07Nf5ISyPfmGEpurvH
"""

import pandas as pd
import numpy as np
# first neural network with keras tutorial
from numpy import loadtxt
from sklearn.model_selection import train_test_split
from keras.models import Sequential
from keras.layers import Dense
import matplotlib.pyplot as plt

import tensorflow as tf
device_name = tf.test.gpu_device_name()
if device_name != '/device:GPU:0':
  raise SystemError('GPU device not found')
print('Found GPU at: {}'.format(device_name))

#data= pd.read_csv('/content/drive/MyDrive/Expert-Opinion.csv')
data = pd.read_csv('/content/drive/MyDrive/data_1000.csv',nrows = 1000)
#data = pd.read_csv('/content/drive/MyDrive/Expert_opinion_1000.csv')
dataset = data.drop(['Title'], axis=1)

data

#data = loadtxt('/content/drive/MyDrive/Expert-Opinion.csv',delimiter=',')
#Expert Opinion on Recipies

dataset.head(5)

import seaborn as sns
dataset['remarks'].value_counts().plot(kind='bar')

#input and output variables
X = dataset.iloc[:,0:6]
Y = dataset.iloc[:,6]

X

lst = list(dataset['energy'])
lst1 = list(dataset['fat'])
lst3 = list(dataset['sugars'])
lst2 = list(dataset['remarks'])

np.random.seed(100)

#create array of 50 random integers between 0 and 10
var1 = np.array(lst)+np.array(lst1)+np.array(lst3)

#create a positively correlated array with some random noise
var2 = var1 + np.array(lst2)

#calculate the correlation between the two arrays
np.corrcoef(var1, var2)

#create train and testing vars
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=.2)
print(X_train.shape, Y_train.shape)
print(X_test.shape, Y_test.shape)

#from sklearn.linear_model import LinearRegression
#lr = LinearRegression()
#lr.fit(X_train, Y_train)

#y_lr_train_pred = lr.predict(X_train)
#y_lr_test_pred = lr.predict(X_test)

#from sklearn.metrics import mean_squared_error, r2_score
#lr_train_mse = mean_squared_error(Y_train, y_lr_train_pred)
#lr_train_r2 = r2_score(Y_train, y_lr_train_pred)
#lr_test_mse = mean_squared_error(Y_test, y_lr_test_pred)
#lr_test_r2 = r2_score(Y_test, y_lr_test_pred)

#print(lr_train_mse)

# define the keras model layer by layer
from tensorflow.keras.layers import Dense
from tensorflow.keras.models import Sequential
from tensorflow.keras import initializers
model = Sequential()
model.add(Dense(12, input_dim=6, activation='relu'))
model.add(Dense(8, activation='relu'))
#model.add(Dense(4,init = 'uniform', activation='relu'))
model.add(Dense(1,  activation='sigmoid'))

# compile the keras model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

# fit the keras model on the dataset
model.fit(X_train, Y_train, epochs=200, batch_size=32)

# evaluate the keras model
_, accuracy = model.evaluate(X_train, Y_train)
print('Train_Accuracy: %.2f' % (accuracy*100))

y_hat= model.predict(np.array(X_test))
print('Test_Accuracy: %.2f' % (y_hat[0]))

model.save('ann-model.model.h5')

#from sklearn.metrics import mean_squared_error, r2_score
#lr_train_mse = mean_squared_error(Y_train, y_lr_train_pred)
#lr_train_r2 = r2_score(Y_train, y_lr_train_pred)
#lr_test_mse = mean_squared_error(Y_test, y_lr_test_pred)
#lr_test_r2 = r2_score(Y_test, y_lr_test_pred)#test accuracy check

#print('Test_Accuracy: %.2f' % (lr_test_r2*100))