# This Python 3 environment comes with many helpful analytics libraries installed
# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python
# For example, here's several helpful packages to load

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

# Input data files are available in the read-only "../input/" directory
# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory

import os
for dirname, _, filenames in os.walk('/kaggle/input'):
    for filename in filenames:
        print(os.path.join(dirname, filename))

# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using "Save & Run All" 
# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session
"""
<h1>Classification</h1>
"""
import os
import pandas as pd
import math
import matplotlib.pyplot as plt
import seaborn as sns #Visualization
from sklearn.linear_model import LinearRegression
df_class = pd.read_csv('/kaggle/input/iris/Iris.csv')
print('\nNumber of rows and columns in the data set: ',df_class.shape)
print('')
print(df_class.head())
"""
Check the features will null using heat map
"""
plt.figure(figsize=(12,4))
sns.heatmap(df_class.isnull(),cbar=False,cmap='coolwarm',yticklabels=False)
plt.title('Missing value in the dataset')
"""
Find the sum of null values in each feature/column
"""
df_class.isnull().sum()
print(df_class.info())
df_class.nunique()
df_class.info()
# Scaling for Numerical data set
#from sklearn.preprocessing import StandardScaler
#sc=StandardScaler()
#data_numerical.iloc[:,1:] =sc.fit_transform(data_numerical.iloc[:,1:])  
# Lets verify the dummay variable process
print('Columns in original data frame:\n',df_class.columns.values) #original dataframe column names
print('\nNumber of rows and columns in the dataset:',df_class.shape) #previous dataframe shape
#print('\nColumns in data frame after encoding dummy variable:\n',df_Loan.columns.values) #original dataframe column names
#print('\nNumber of rows and columns in the dataset:',df_Loan.shape) #updated data frame shape
"""
Distributing data in **training and testing**

"""
from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test =train_test_split(df_class.drop(['Species'],axis=1),df_class.Species,test_size=0.30,random_state=0)# chane zero to 20,10
"""
Importing SVM and creating the model
"""
from sklearn.svm import SVC
svc_model=SVC()
"""
<h1>SUPPORT VECTOR CLASSIFIER</h1>
Creating the **SVM model** and run prediction on testing data
"""
svc_model.fit(X_train,y_train)
svm_pred=svc_model.predict(X_test)

"""
Importing **evaluation** libraries
"""
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score
"""
**Confusion matrix** and **Accuracy** score
"""

cm=confusion_matrix(y_test,svm_pred)
print(cm)

from sklearn.metrics import accuracy_score
print(accuracy_score(y_test,svm_pred))
print("SVM  -> ",accuracy_score(y_test,svm_pred)*100)
"""
**Classification Report**
"""
print(confusion_matrix(y_test,svm_pred))
print(classification_report(y_test,svm_pred))
print(accuracy_score(y_test, svm_pred))
"""
<h1>Naive Bayes</h1>
"""
from sklearn import naive_bayes
Naive = naive_bayes.GaussianNB()
# fit the training dataset on the NB classifier
Naive.fit(X_train,y_train)# predict the labels on validation dataset
predictions_NB= Naive.predict(X_test)# Use accuracy_score function to get the accuracy
print("Naive Bayes Accuracy Score -> ",accuracy_score(predictions_NB, y_test)*100)
print(confusion_matrix(y_test,predictions_NB))
print(classification_report(y_test,predictions_NB))
print(accuracy_score(y_test,predictions_NB))
"""
<h1>K-nearest Neighbour<h1>
"""
from sklearn.neighbors import KNeighborsClassifier
knn = KNeighborsClassifier(n_neighbors=3)
knn.fit(X_train, y_train)
knn_pred = knn.predict(X_test)
print("Accuracy:",accuracy_score(y_test, knn_pred))
print(confusion_matrix(y_test,knn_pred))
print(classification_report(y_test,knn_pred))
print(accuracy_score(y_test,knn_pred))
print('erroe')

print(1-accuracy_score(y_test,knn_pred))
"""
Try KNN model with different number of K and perform evaluation.
"""
"""
<h1> Decision Tree <h1>
"""
from sklearn.tree import DecisionTreeClassifier
dtc = DecisionTreeClassifier()
# Train Decision Tree Classifer
dtc.fit(X_train,y_train)

#Predict the response for test dataset
dtc_pred = dtc.predict(X_test)
print(confusion_matrix(y_test,dtc_pred))
print(classification_report(y_test,dtc_pred))
print(accuracy_score(y_test,dtc_pred))
"""
<H1> ENSEMBLE <H1>
"""
from sklearn.ensemble import RandomForestClassifier, VotingClassifier
"""
<h3>Stacking</h3>
"""
"""
<h2>Voting Classifier</h2>
"""
voting_clf = VotingClassifier(estimators=[('SVC', SVC()), ('DTree', dtc), ('NaiveBayes', Naive),('KNN',knn)], voting='hard')
voting_clf.fit(X_train, y_train)


preds = voting_clf.predict(X_test)
print(confusion_matrix(y_test,preds))
print(classification_report(y_test,preds))
print(accuracy_score(y_test,preds))
print('error')
print(1-accuracy_score(y_test,preds))
"""
<h3>Bagging</h3>
"""
"""
<h2> Random Forest Classifier</h2>
"""
random_forest = RandomForestClassifier(n_estimators=10, random_state=12)
random_forest.fit(X_train,y_train)
rf=random_forest.predict(X_test)
print(confusion_matrix(y_test,rf))
print(classification_report(y_test,rf))
print(accuracy_score(y_test,rf))
print('error')
print(1-accuracy_score(y_test,rf))
from sklearn.model_selection import KFold
#kf=KFold(n_splits=5,shuffle=True)
kf=KFold(n_splits=10,shuffle=True)
data=df_class.drop(['Species'],axis=1)
print(data.head)
plt.figure(figsize=(12,4))
sns.heatmap(data.isnull(),cbar=False,cmap='coolwarm',yticklabels=False)
plt.title('Missing value in the dataset')
rf_k=RandomForestClassifier(n_estimators=10)
from sklearn.model_selection import cross_val_score
acc_rf=cross_val_score(rf_k,data,df_class.Species,scoring='accuracy',cv=10)
print(acc_rf.mean())
print('error')
print(1-(acc_rf.mean()))


array=[10,20,40,60,80,100]
for i in range(0,6):
  random_forest = RandomForestClassifier(n_estimators=array[i])
  random_forest.fit(X_train,y_train)
  rf_predict=random_forest.predict(X_test)
  print('Accuracy of Rain Forest when estimate is ',array[i], ' : ',accuracy_score(y_test,rf_predict))
"""
<h3> Boosting</h3>
"""
k_folds = KFold(n_splits=20,shuffle=True)
from sklearn.ensemble import AdaBoostClassifier
ada_boost = AdaBoostClassifier(n_estimators=10, random_state=12)
results = cross_val_score(ada_boost, X_train, y_train, cv=k_folds)
print(results.mean()*100)
ada_boost.fit(X_train,y_train)
ada_predict=ada_boost.predict(X_test)
print(confusion_matrix(y_test,ada_predict))
print(classification_report(y_test,ada_predict))
print(accuracy_score(y_test,ada_predict))
print('error')
print(1-accuracy_score(y_test,ada_predict))
array=[10,20,40,60,80,100]
for i in range(0,6):
  ada_boost = AdaBoostClassifier(n_estimators=array[i], random_state=12)
  ada_boost.fit(X_train,y_train)
  ada_predict=ada_boost.predict(X_test)
  print('Accuracy when estimate is ',array[i], ' : ',accuracy_score(y_test,ada_predict))