raw_df_white = pd.read_csv("winequality-white.csv", sep =';')

"""
Explanation: Part2
In this part other models will be used in order to see if greater accuracy is achieved.
Importing Dataset:
End of explanation
"""


X = raw_df_white.iloc[:,:-1]
y = raw_df_white['quality']
encoder = LabelEncoder()
y = encoder.fit_transform(y)
y = y.reshape(-1, 1)
onehotencoder = OneHotEncoder()
y = onehotencoder.fit_transform(y).toarray()
y = pd.DataFrame(y)
y.columns = ['3', '4', '5', '6', '7', '8', '9']

X_train_white, X_test_white, y_train_white, y_test_white = cross_validation.train_test_split(X, y, test_size = 0.2, random_state = 0)



 y_train_white.describe()

"""
Explanation: Encoding target variable
Creating dummy variables and separate each category into one column
End of explanation
"""


scaler = StandardScaler()
#scaler =  MinMaxScaler()
#scaler = Normalizer()
X_train_white = scaler.fit(X_train_white).transform(X_train_white)
X_test_white = scaler.fit(X_test_white).transform(X_test_white)

"""
Explanation: Performing feature scaling on the dataset
End of explanation
"""


#classifier = Sequential()
#classifier.add(Dense(output_dim = 6, init = 'uniform', activation = 'relu', input_dim = 11))
# input_dim: number of independent var
#classifier.add(Dense(output_dim = 6,init = 'uniform', activation = 'relu'))

#adding layers with keras 2.0
model = Sequential()
model.add(Dense(units=11, input_dim=11))
model.add(Activation('relu'))
model.add(Dense(units=11))
model.add(Activation('relu'))
model.add(Dense(units=11))
model.add(Activation('relu'))
model.add(Dense(units=11))
model.add(Activation('relu'))
model.add(Dense(units=11))
model.add(Activation('relu'))
model.add(Dense(units=11))
model.add(Activation('relu'))
model.add(Dense(units=11))
model.add(Activation('relu'))
model.add(Dense(units=11))
model.add(Activation('relu'))
model.add(Dense(units=11))
model.add(Activation('relu'))
model.add(Activation('relu'))
model.add(Dense(units=11))
model.add(Activation('relu'))
model.add(Dense(units=11))
model.add(Activation('relu'))
model.add(Dense(units=11))
model.add(Activation('relu'))
model.add(Dense(units=11))
model.add(Activation('relu'))
model.add(Dense(units=11))
model.add(Activation('relu'))
model.add(Dense(units=11))
model.add(Activation('relu'))
model.add(Dense(units=11))
model.add(Activation('relu'))
model.add(Dense(units=11))
model.add(Activation('relu'))
model.add(Dense(units=7))
model.add(Activation('softmax'))

# 
model.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics = ['accuracy'])
model.fit(X_train_white, y_train_white, batch_size = 5, epochs = 100)

"""
Explanation: ANN
Performing Artificial Neural Networks. 
Instanciating the model adding layers and training the model:
End of explanation
"""


y_predict = model.predict(X_test_white)
y_test_white = y_test_white.astype(float)
y_predict = y_predict
y_predict = y_predict > 0.7

"""
Explanation: Performing predictions. if y_predict > 0.7 == True
End of explanation
"""


from sklearn.metrics import confusion_matrix
from numpy import argmax
cf = confusion_matrix(y_test_white.values.argmax(axis=1), y_predict.argmax(axis=1))
from sklearn.metrics import accuracy_score
#accuracy = accuracy_score(y_test_white, y_predict)
cf

"""
Explanation: Creating confusion matrix:
End of explanation
"""


from keras.layers import Dropout
odel = Sequential()
model.add(Dense(units=11, input_dim=11))
model.add(Activation('relu'))
model.add(Dropout(p = 0.2))
model.add(Dense(units=11, input_dim=11))
model.add(Activation('relu'))
model.add(Dropout(p = 0.2))
model.add(Dense(units=11, input_dim=11))
model.add(Activation('relu'))
model.add(Dropout(p = 0.2))
model.add(Dense(units=11, input_dim=11))
model.add(Activation('relu'))
model.add(Dropout(p = 0.2))
model.add(Dense(units=11, input_dim=11))
model.add(Activation('relu'))
model.add(Dropout(p = 0.2))
model.add(Dense(units=7))
model.add(Dense(units=11, input_dim=11))
model.add(Activation('relu'))
model.add(Dropout(p = 0.2))
model.add(Dense(units=11, input_dim=11))
model.add(Activation('relu'))
model.add(Dropout(p = 0.2))
model.add(Dense(units=11, input_dim=11))
model.add(Activation('relu'))
model.add(Dropout(p = 0.2))
model.add(Dense(units=11, input_dim=11))
model.add(Activation('relu'))
model.add(Dropout(p = 0.2))
model.add(Dense(units=11, input_dim=11))
model.add(Activation('relu'))
model.add(Dropout(p = 0.2))
model.add(Dense(units=7))
model.add(Activation('softmax'))
model.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics = ['accuracy'])
model.fit(X_train_white, y_train_white, batch_size = 5, epochs = 100)

y_predict = model.predict(X_test_white)
y_test_white = y_test_white.astype(float)
y_predict = y_predict
y_predict = y_predict > 0.7



cf = confusion_matrix(y_test_white.values.argmax(axis=1), y_predict.argmax(axis=1))
cf

"""
Explanation: The results are not accurate. 
Improving the model:
Improving the model
End of explanation
"""


from xgboost.sklearn import XGBClassifier
from xgboost.sklearn import XGBRegressor

xclas = XGBClassifier()  # for classifier
xclas.fit(X_train_white, y_train_white)
y_pred = xclas.predict(X_test_white)

cross_val_score(xclas, X_train_white, y_train_white)

from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test_white, y_pred)
print (cm)

"""
Explanation: The model accuracy was reduced.
xgboost
End of explanation
"""


