"""
## Importing Libraries
"""
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline

"""
## Loading Dataset
"""
dataset = pd.read_csv("../input/wine-m/train.csv")
dataset.head()
dataset.info()
dataset.describe()
"""
## Data Visualisation
"""
print(dataset.target.value_counts())
dataset.target.value_counts().plot(kind='bar')
plt.show()
fig, ax = plt.subplots(5,3 , figsize = (15,20)) # Making Subplots

sns.barplot(data = dataset, y ="alcohol", x="target", ax=ax[0,0]);
sns.barplot(data = dataset, y ="malic_acid", x="target", ax=ax[0,1]);
sns.barplot(data = dataset, y ="ash", x="target", ax=ax[0,2]);
sns.barplot(data = dataset, y ="alcalinity_of_ash", x="target", ax=ax[1,0]);
sns.barplot(data = dataset, y ="magnesium", x="target", ax=ax[1,1]);
sns.barplot(data = dataset, y ="total_phenols", x="target", ax=ax[1,2]);
sns.barplot(data = dataset, y ="flavanoids", x="target", ax=ax[2,0]);
sns.barplot(data = dataset, y ="nonflavanoid_phenols", x="target", ax=ax[2,1]);
sns.barplot(data = dataset, y ="proanthocyanins", x="target", ax=ax[2,2]);
sns.barplot(data = dataset, y ="color_intensity", x="target", ax=ax[3,0]);
sns.barplot(data = dataset, y ="hue", x="target", ax=ax[3,1]);
sns.barplot(data = dataset, y ="od280/od315_of_diluted_wines", x="target", ax=ax[3,2]);
sns.barplot(data = dataset, y ="proline", x="target", ax=ax[4,0]);
"""
## Splitting Data in Train and Test
"""
# Splitting datset to X and y axis 

X=dataset.iloc[: , 1:14].values
y=dataset.iloc[: ,-1].values
X
y
from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.2, random_state=0)
#Feature Scaling

from sklearn.preprocessing import StandardScaler
sc=StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.fit_transform(X_test)
"""
# Model Selection
"""
"""
### Applying ANN
"""
# Adding layers and activation function

import tensorflow as tf
ann=tf.keras.models.Sequential()

ann.add(tf.keras.layers.Dense(units=6,activation='relu'))
ann.add(tf.keras.layers.Dense(units=6,activation='relu'))
ann.add(tf.keras.layers.Dense(units=1,activation='tanh'))

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

ann.fit(X_train,y_train,batch_size=32,epochs=200)
# Prediction on Test data

y_pred = ann.predict(X_test)
y_pred = (y_pred > 0.5)
print(np.concatenate((y_pred.reshape(len(y_pred),1), y_test.reshape(len(y_test),1)),1))
# Calculating quality for model

from sklearn.metrics import confusion_matrix, accuracy_score
cm = confusion_matrix(y_test, y_pred)
print(cm)
accuracy_score(y_test, y_pred)
"""
## Machine Learning Models
"""
"""
### Applying KNN
"""
# KNN:K Nearest Neighbour
from sklearn.neighbors import KNeighborsClassifier
classifier=KNeighborsClassifier(n_neighbors=5)
classifier.fit(X_train,y_train)
# Training the model
y_pred=classifier.predict(X_test)
print(np.concatenate((y_pred.reshape(len(y_pred),1), y_test.reshape(len(y_test),1)),1))
# Calculating quality for model

from sklearn.metrics import confusion_matrix, accuracy_score
cm = confusion_matrix(y_test, y_pred)
print(cm)
accuracy_score(y_test, y_pred)
"""
### Applying Decision Tree
"""
# Training the model
from sklearn.tree import DecisionTreeClassifier  
classifier= DecisionTreeClassifier(criterion='entropy', random_state=0)  
classifier.fit(X_train, y_train)  
# Predicting the values
y_pred=classifier.predict(X_test)
print(np.concatenate((y_pred.reshape(len(y_pred),1), y_test.reshape(len(y_test),1)),1))
# Calculating quality for model

from sklearn.metrics import confusion_matrix, accuracy_score
cm = confusion_matrix(y_test, y_pred)
print(cm)
accuracy_score(y_test, y_pred)
"""
### Applying Naive Bayes
"""
# Training the model  
from sklearn.naive_bayes import GaussianNB  
classifier = GaussianNB()  
classifier.fit(X_train, y_train)  
# Predicting the values
y_pred=classifier.predict(X_test)
print(np.concatenate((y_pred.reshape(len(y_pred),1), y_test.reshape(len(y_test),1)),1))
# Calculating quality for model

from sklearn.metrics import confusion_matrix, accuracy_score
cm = confusion_matrix(y_test, y_pred)
print(cm)
accuracy_score(y_test, y_pred)
"""
### Logistic Regression
"""
# Training the model
from sklearn.linear_model import LogisticRegression
logreg = LogisticRegression()
logreg = logreg.fit(X_train,y_train)

# Predicting the values
y_pred=classifier.predict(X_test)
print(np.concatenate((y_pred.reshape(len(y_pred),1), y_test.reshape(len(y_test),1)),1))
# Calculating quality for model

from sklearn.metrics import confusion_matrix, accuracy_score
cm = confusion_matrix(y_test, y_pred)
print(cm)
accuracy_score(y_test, y_pred)
"""
### Support Vector Machine
"""
# Training the model
from sklearn.svm import SVC
classifier = SVC(kernel='rbf', random_state = 1)
classifier.fit(X_train,y_train)
# Predicting the values
y_pred=classifier.predict(X_test)
print(np.concatenate((y_pred.reshape(len(y_pred),1), y_test.reshape(len(y_test),1)),1))
final_classifier = classifier    # since max quality is for SVM so we have stored this in a final variable
# Calculating quality for model

from sklearn.metrics import confusion_matrix, accuracy_score
cm = confusion_matrix(y_test, y_pred)
print(cm)
accuracy_score(y_test, y_pred)
"""
## Final Prediction Using most Efficient algorithm

"""
# In this case maximum quality is for SVM so we will use this algorithm

fin_test_data = pd.read_csv('../input/wine-m/test.csv')
test_data = fin_test_data.drop('id',axis=1)
test_data = sc.fit_transform(test_data)
final_pred = final_classifier.predict(test_data)
final_pred
sample_submit = pd.read_csv('../input/wine-m/sample_submit.csv')
sample_submit
output = pd.DataFrame({'id':fin_test_data.id,'target':final_pred})
output.to_csv('my_submission.csv', index=False)
filename = "my_submission.csv"
print("Your submission was successfully saved!")
# Download the CSV file created above

from IPython.display import HTML
import pandas as pd
import numpy as np

def create_download_link(title = "Download CSV file", filename = filename):  
    html = '<a href={filename}>{title}</a>'
    html = html.format(title=title,filename=filename)
    return HTML(html)

# create a link to download the dataframe which was saved with .to_csv method
create_download_link(filename=filename)