# ---
# 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
# ---

# + [markdown] id="view-in-github" colab_type="text"
# <a href="https://colab.research.google.com/github/Madhav2204/LGMVIP-DataScience/blob/main/Task_1_Iris_Flowers_Classification_ML_Project.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + [markdown] id="8a9bdf11"
# # Author : <NAME>

# + [markdown] id="5810d787"
# ### TASK 1:Iris Flowers Classification ML Project 

# + [markdown] id="1123e586"
# ##### Problem Statement :This particular ML project is usually referred to as the “Hello World” of Machine Learning. The iris flowers dataset contains numeric attributes, and it is perfect for beginners to learn about supervised ML algorithms, mainly how to load and handle data. Also, since this is a small dataset, it can easily fit in memory without requiring special transformations or scaling capabilities.

# + [markdown] id="1825e1a6"
# Dataset link : http://archive.ics.uci.edu/ml/datasets/Iris

# + [markdown] id="02a4e0d3"
# #### Step-1 : Lets First import required liberies

# + id="da1a7931"
import pandas as pd
import numpy as np
import scipy.stats as st
import os
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
from sklearn.tree import DecisionTreeClassifier
from sklearn.linear_model import LinearRegression
from sklearn.metrics import accuracy_score
from sklearn.linear_model import LogisticRegression
from sklearn.neighbors import KNeighborsClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.preprocessing import PolynomialFeatures 
from sklearn import svm
from sklearn.metrics import classification_report

# + id="a595ac6b" outputId="f0e901de-b619-490d-b1c2-440b2f8d2c88"
df = pd.read_csv('./Datasets/iris.csv')
df.head(10)

# + id="c1237d32" outputId="e0c99446-26ae-479b-a360-9306f61a322e"
df.shape

# + id="972f186e" outputId="fc690c63-f417-4cc5-ff3c-c92cfb8c9f97"
df.tail(10)

# + id="006704f5" outputId="194fd627-80b6-4547-f12d-0e8fa09a0ff1"
df.sample(10)

# + id="30600be5" outputId="832686c4-b86d-410d-d5e8-9d5bee2ba0bf"
df["variety"].value_counts()

# + id="e4e68199" outputId="ecba6ce5-e18b-4c52-b037-40215e46be7e"
df.describe()

# + id="ceda418f" outputId="3f49d60f-2191-4e54-acc2-4ef486d020ed"
df.variety.unique()

# + id="2d3e2e09" outputId="2a9dbf44-929e-4785-fa22-53c9354db394"
df.nunique()

# + id="ef36cb24" outputId="ebb1f626-1add-4b22-e8e8-3d4802ee6d61"
df.isnull().sum()

# + id="8850f547" outputId="5b87e9d4-a2d2-4b3d-dcac-f78dd8e203aa"
df = df.rename(columns = {'sepal.length': 'sepal_length', 'petal.length': 'petal_length', 'sepal.width': 'sepal_width' , 'petal.width': 'petal_width', 'variety' : 'Species'})
print(df)

# + [markdown] id="04f53ca5"
# #### Lets Visualize the input data

# + id="0ced9c42" outputId="1c33ae53-c8ad-4551-9e7a-92a38e2d54a4"
sns.pairplot(df, hue = "Species")
plt.show()


# + id="71a36e94" outputId="cd9e49f0-a5e7-4193-d16b-49ae22e8eb5b"
def histplots():
    fig,axes=plt.subplots(2,2,figsize=(10,10))
    df['sepal_length'].hist(ax=axes[0][0])
    df['petal_length'].hist(ax=axes[0][1])
    df['petal_width'].hist(ax=axes[1][0])
    df['sepal_width'].hist(ax=axes[1][1])
    plt.show()
    
histplots()


# + id="7b424e44" outputId="7f7b3da4-8f26-445f-d4be-f51f2d74926d"
def barplots():
    fig,axes=plt.subplots(2,2,figsize=(10,10))
    sns.barplot(x=df.Species,y=df['sepal_length'],ax=axes[0][0])
    sns.barplot(x=df.Species,y=df['petal_length'],ax=axes[0][1])
    sns.barplot(x=df.Species,y=df['petal_width'],ax=axes[1][0])
    sns.barplot(x=df.Species,y=df['sepal_width'],ax=axes[1][1])
    plt.show()
    
barplots()

# + [markdown] id="eb4d8200"
# #### Correlation

# + id="2768fad3" outputId="21c30950-a082-408f-e69b-f271dbfd1442"
df.corr()

# + id="35110462" outputId="4c0b0ed9-88fb-468f-9b37-fe028cc9ec5f"
corr = df.corr()
fig, ax = plt.subplots(figsize=(5,5))
sns.heatmap(corr, annot=True, ax=ax)

# + id="6fe3cb4d" outputId="a9caa973-c816-445a-be69-fa8cb88f31e0"
le = LabelEncoder()
df['Species'] = le.fit_transform(df['Species'])
df.sample(15)

# + id="765d1cd3" outputId="5ac70553-b02c-479f-b8cb-984ec868a7c0"
x = df.iloc[:,:4].values
y = df.iloc[:,4].values

x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.3,random_state=10)

print(x_train.shape)
print(x_test.shape)
print(y_train.shape)
print(y_test.shape)

# + [markdown] id="c89b7bf1"
# # Supervised ML Algorithms

# + id="2008d2ee" outputId="78a770f6-6229-4a87-c7d7-34586d2f1f08"
print('-------------------------------------- LINEAR REGRESSION ----------------------------------------------')

model = LinearRegression()
model.fit(x_train,y_train)
y_pred = model.predict(x_test)
sc_lr = round(model.score(x_test, y_test) * 100 , 2)
print("Accuracy: ", str(sc_lr) , " %" )

# + id="f6f152a1" outputId="0936eb6f-599e-4a30-841d-7ff6383e7d49"
print('-------------------------------------- LOGISTIC REGRESSION ----------------------------------------------')

model2 = LogisticRegression()
model2.fit(x_train,y_train)
sc_logr = round(model2.score(x_test, y_test) * 100,2)
print("Accuracy: ", str(sc_logr) ,  " %")

# + id="3cf8ea86" outputId="ba975543-87d7-407d-9e3e-0ce54ab6e84f"
print('-------------------------------------- NAIVE BAYES ----------------------------------------------')

nb = GaussianNB()
nb.fit(x_train,y_train)
y_pred_nb = nb.predict(x_test)
score_nb = round(accuracy_score(y_pred_nb,y_test)*100,2)
print("Accuracy: "+str(score_nb)+" %")
print(classification_report(y_test, y_pred_nb))

# + id="6bac356d" outputId="9baa5718-e027-48a5-c81f-be3546a69884"
print('--------------------------------------KNN CLASSIFIER -----------------------------------------------')

model3 = KNeighborsClassifier()
model3.fit(x_train,y_train)
sc_knn = round(model3.score(x_test, y_test) * 100,2)
print("Accuracy: ", str(sc_knn) , " %")

# + id="4ab47d40" outputId="475686de-a6e2-4013-feb8-22450f0e7d61"
print('--------------------------------------DECISION TREE CLASSIFIER------------------------------------------------')
model4 = DecisionTreeClassifier()
model4.fit(x_train, y_train)
sc_dt= round(model4.score(x_test, y_test) * 100 , 2)
print("Accuracy: ", str(sc_dt) , "%")

# + id="0665afdd" outputId="01671e9a-b3e9-4855-8beb-d085d43c9e2a"
print('--------------------------------------SVM ------------------------------------------------')

sv = svm.SVC(kernel='linear')
sv.fit(x_train, y_train)
y_pred_svm = sv.predict(x_test)
sc_svm = round(accuracy_score(y_pred_svm,y_test)*100,2)
print("Accuracy: "+ str(sc_svm) +" %")
print(classification_report(y_test, y_pred_svm))

# + [markdown] id="9ed5d2ad"
# # Comparison

# + id="591e7438" outputId="1ec7a206-0dca-49a7-b812-badc198a02a2"
scores_plt = [sc_lr , sc_logr , score_nb, sc_dt, sc_svm, sc_knn]
algorithms = ["Linear Regression","Logistic Regression","Naive Bayes","Decision tree","Support Vector Machine", "KNN"]
sns.set(rc={'figure.figsize':(11,6)})
plt.xlabel("Algorithms")
plt.ylabel("Accuracy score")

sns.barplot(algorithms,scores_plt)
plt.show()

# + id="34959f72"


StarcoderdataJupyterScriptsDedupFiltered