<jupyter_start><jupyter_text>Graduation Rotational Internship Program - Task-4<jupyter_code>import pandas as pd
import numpy as np
import sklearn.datasets as datasets
import matplotlib.pyplot as plt
%matplotlib inline

# Loading the iris data set
df = pd.read_csv('Iris.csv')
df.head()
# Checking for null values in the datasets
df.isnull().sum()
# As all the categories aare equally classified, so we do not need to apply class imbalance techniques. 
df['Species'].value_counts()
df.info()
# Describing the dataset
df.describe()<jupyter_output><empty_output><jupyter_text>Pre-processing**Converting all the datatypes into nummeric values using label encoding.**<jupyter_code>from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()

factcols = df.select_dtypes(include='object')
df[factcols.columns] = df[factcols.columns].apply(le.fit_transform)
# Checking for Outliers in the dataset.
for i in df.columns:
    plt.figure()
    df.boxplot([i])
# Heat Map for checking the correlation between the columns
import seaborn as sns
plt.figure(figsize=(6,6))
sns.heatmap(df.corr(),annot=True)
# Checking the distribution of the data, whether it is skewed or not
for i in df.columns:
    g = sns.FacetGrid(data=df,legend_out=False)
    print(g.map(sns.distplot,i))<jupyter_output><seaborn.axisgrid.FacetGrid object at 0x0000021D5F85C9C8>
<seaborn.axisgrid.FacetGrid object at 0x0000021D5F8B1388>
<seaborn.axisgrid.FacetGrid object at 0x0000021D5F939608>
<seaborn.axisgrid.FacetGrid object at 0x0000021D5F97D248>
<seaborn.axisgrid.FacetGrid object at 0x0000021D5F44F7C8>
<seaborn.axisgrid.FacetGrid object at 0x0000021D5F353188><jupyter_text>Splitting the data into training and testing<jupyter_code># Shuffling the data to avoid biasing in the training
df = df.sample(frac=1)
X = df.iloc[:,1:-1]
y = df.iloc[:,-1]
X.columns
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)
X_train.shape
X_test.shape
y_train.shape
y_test.shape<jupyter_output><empty_output><jupyter_text>Building the Model<jupyter_code>from sklearn.tree import DecisionTreeClassifier

model_tree = DecisionTreeClassifier()
model_tree.fit(X_train, y_train)
# Predictions on testing data
y_pred = model_tree.predict(X_test)
y_pred
# Visualising using Confusion Matrix
from sklearn.metrics import confusion_matrix
from sklearn.metrics import classification_report

confuse_mtx = confusion_matrix(y_pred, y_test)
print(confuse_mtx)
print(classification_report(y_pred, y_test))<jupyter_output>precision    recall  f1-score   support

           0       1.00      1.00      1.00         4
           1       0.92      0.80      0.86        15
           2       0.77      0.91      0.83        11

    accuracy                           0.87        30
   macro avg       0.90      0.90      0.90        30
weighted avg       0.88      0.87      0.87        30<jupyter_text>**So we have a pretty good accuracy and confusion matrix.**<jupyter_code># Checking the accuracy using cross validation.
from sklearn.model_selection import cross_val_score

scores = cross_val_score(model_tree, X, y, cv=6)
scores
# Calculating accuracy
scores.mean()
from sklearn import tree

text_representation = tree.export_text(model_tree)
print(text_representation)
import graphviz
from IPython.display import Image
from io import StringIO
import pydotplus
import pydot
from sklearn.tree import export_graphviz

iris_x_train_names = X_train.columns
dot_data = StringIO()
fig = plt.figure()
export_graphviz(model_tree, out_file = dot_data,filled = True, feature_names = iris_x_train_names)
graph = pydotplus.graph_from_dot_data(dot_data.getvalue())
(graph,) = pydot.graph_from_dot_data(dot_data.getvalue())
Image(graph.create_png())<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08552631578947369, 'fraction_numerical': 0.041578947368421056, 'mean_word_length': 4.647845468053492, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '283734', 'license': 'Apache-2.0', 'n_tokens_mistral': 1395, 'n_tokens_neox': 1293, 'n_words': 324, 'path': 'Task-4/TSF_Task_4_Decision_Tree_Classification_IRIS_Dataset.ipynb', 'repo_name': 'vvHacker007/TSF_Internship', 'size': '3800'}