<jupyter_start><jupyter_text>Importing Modules and Loading Iris Flower Dataset<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline

# importing the dataset
from sklearn.datasets import load_iris<jupyter_output><empty_output><jupyter_text>**dataset in the variable**<jupyter_code>flowers = load_iris()
dir(flowers)<jupyter_output><empty_output><jupyter_text>**Checking no of rows and columns**<jupyter_code>print(f'rows = {len(flowers.data)}')
print(f'cols = {flowers.feature_names}')
print(f'cols = {flowers.target_names}')<jupyter_output>rows = 150
cols = ['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)']
cols = ['setosa' 'versicolor' 'virginica']<jupyter_text>**Inference**: We have 150 datapoints, 4 columns, and 3 category of flowers  Making Pandas Dataframe and Preprocessing<jupyter_code>df = pd.DataFrame(data=flowers.data, columns=flowers.feature_names)
df['target'] = flowers.target
df.head()<jupyter_output><empty_output><jupyter_text>**checking datatype and null values**<jupyter_code>temp = pd.DataFrame()
temp['null'] = df.isnull().sum()
temp['dtype'] = df.dtypes
temp<jupyter_output><empty_output><jupyter_text>**Inference**: Data is good to work as no null values and dtype are also in float64  Univarate Analysis and Visualizing **Varialtion of each column(category) with respect to the target(Dependent Variable)**<jupyter_code>for feature in flowers.feature_names:
  sns.boxplot(y=feature,x='target',data=df)
  plt.title('Petal Lengths of Iris Species')
  plt.show()<jupyter_output><empty_output><jupyter_text>**Inference**: there is clear distribution for each category  Bivarate Analysis and Visualization **Visualizing the pair plot**<jupyter_code>sns.pairplot(data=df, hue = 'target')<jupyter_output><empty_output><jupyter_text>**Petel Length has high correlation with the petel width** **Distribution of Petal**<jupyter_code>color = ['red' if v == 0 else 'green' if v == 1 else 'blue' for v in df.target]
plt.scatter(x = df['petal length (cm)'],y =  df['petal width (cm)'], c=color)
plt.xlabel('petal length (cm)')
plt.ylabel('petal width (cm)')
plt.grid()
plt.show()<jupyter_output><empty_output><jupyter_text>**Sepals distribution is not highly correlated** **Distribution of Sepels**<jupyter_code>color = ['red' if v == 0 else 'green' if v == 1 else 'blue' for v in df.target]
plt.scatter(x = df['sepal length (cm)'],y = df['sepal width (cm)'], c=color)
plt.xlabel('sepal length (cm)')
plt.ylabel('sepal width (cm)')
plt.grid()
plt.show()<jupyter_output><empty_output><jupyter_text>train-test and Dependent and Independent Splitting of dataset<jupyter_code>from sklearn.model_selection import train_test_split<jupyter_output><empty_output><jupyter_text>**Dependent and Independent Splitting**<jupyter_code>X = df.drop(['target'], axis=1)
y = df['target']
X.head()
y.head()<jupyter_output><empty_output><jupyter_text>**train test splitting**<jupyter_code>X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=56)
X_train.shape
X_test.shape<jupyter_output><empty_output><jupyter_text>Decision Tree Calssifier with Gini-Impurity<jupyter_code>from sklearn.tree import DecisionTreeClassifier
model = DecisionTreeClassifier()
model.fit(X_train,y_train)
model.score(X_test, y_test)<jupyter_output><empty_output><jupyter_text>**tuning with depth**<jupyter_code>depths = range(1, 5)
test_score = []
train_score = []
for i in depths:
  model = DecisionTreeClassifier(max_depth=i)
  model.fit(X_train,y_train)
  test_score.append(model.score(X_train,y_train))
  train_score.append(model.score(X_test, y_test))<jupyter_output><empty_output><jupyter_text>**plotting it**<jupyter_code>plt.plot(list(depths), test_score, color='r', label='test_score')
plt.plot(list(depths), train_score, color='g', label='train_score')
plt.legend()
plt.xlabel('depth')
plt.ylabel('score')
plt.show()<jupyter_output><empty_output><jupyter_text>**Inference**: max-depth should be 3 or 4 **Tuning with the min_samples_leaf**<jupyter_code>min_leaf = range(5, 20, 2)
test_score = []
train_score = []
for i in min_leaf:
  model = DecisionTreeClassifier(max_depth=3, min_samples_leaf=i)
  model.fit(X_train,y_train)
  test_score.append(model.score(X_train,y_train))
  train_score.append(model.score(X_test, y_test))<jupyter_output><empty_output><jupyter_text>**Plotting the score**<jupyter_code>plt.plot(list(min_leaf), test_score, color='r', label='test_score')
plt.plot(list(min_leaf), train_score, color='g', label='train_score')
plt.legend()
plt.xlabel('min sample leaf')
plt.ylabel('score')
plt.show()<jupyter_output><empty_output><jupyter_text>**Inference**: min_sample_leaf will be < 10 **Accuracy with min_samples_leaf = 8**<jupyter_code>model = DecisionTreeClassifier(max_depth=3, min_samples_leaf=8)
model.fit(X_train, y_train)
model.score(X_test, y_test)<jupyter_output><empty_output><jupyter_text>**Accuracy with min_samples_leaf = 9**<jupyter_code>model = DecisionTreeClassifier(max_depth=3, min_samples_leaf=9)
model.fit(X_train, y_train)
model.score(X_test, y_test)<jupyter_output><empty_output><jupyter_text>Gini Impurity: classification report, Confusion Metric , and Decision Tree Splitting<jupyter_code>#for checking testing results
from sklearn.metrics import classification_report, confusion_matrix
#for visualizing tree 
from sklearn.tree import plot_tree
# Predicting the values of test data
y_pred = model.predict(X_test)
print("Classification report - \n", classification_report(y_test,y_pred))
cm = confusion_matrix(y_test, y_pred)
sns.heatmap(data=cm, annot=True)
plt.ylabel('Actual label')
plt.xlabel('Predicted label')
all_sample_title = 'Accuracy Score: {0}'.format(model.score(X_test, y_test))
plt.title(all_sample_title, size = 15)<jupyter_output><empty_output><jupyter_text>**Visualization of Discision Tree**<jupyter_code># Visualising the graph without the use of graphviz
plt.figure(figsize = (20,20))
dec_tree = plot_tree(model, 
                   feature_names=flowers.feature_names,  
                   class_names=flowers.target_names,
                   filled=True)<jupyter_output><empty_output><jupyter_text>Decision Tree with Entropy **With Entropy**<jupyter_code>from sklearn.tree import DecisionTreeClassifier
model2 = DecisionTreeClassifier(criterion='entropy')
model2.fit(X_train, y_train)
model2.score(X_test, y_test)<jupyter_output><empty_output><jupyter_text>**tuning the model**<jupyter_code>depths = range(1, 8)
test_score = []
train_score = []
for i in depths:
  temp_model = DecisionTreeClassifier(max_depth=i, criterion='entropy')
  temp_model.fit(X_train,y_train)
  test_score.append(temp_model.score(X_train,y_train))
  train_score.append(temp_model.score(X_test, y_test))
plt.plot(list(depths), test_score, color='r', label='test_score')
plt.plot(list(depths), train_score, color='g', label='train_score')
plt.legend()
plt.xlabel('depth')
plt.ylabel('score')
plt.show()<jupyter_output><empty_output><jupyter_text>**Here maximum test score will be 0.93**  Entropy: classification report, Confusion Metric , and Decision Tree Splitting **Classification Report**<jupyter_code># Predicting the values of test data
y_pred = model2.predict(X_test)
print("Classification report - \n", classification_report(y_test,y_pred))<jupyter_output>Classification report - 
               precision    recall  f1-score   support

           0       1.00      1.00      1.00        10
           1       1.00      0.80      0.89        10
           2       0.83      1.00      0.91        10

    accuracy                           0.93        30
   macro avg       0.94      0.93      0.93        30
weighted avg       0.94      0.93      0.93        30<jupyter_text>**Confusion Metric**<jupyter_code>cm = confusion_matrix(y_test, y_pred)
sns.heatmap(data=cm, annot=True)
plt.ylabel('Actual label')
plt.xlabel('Predicted label')
all_sample_title = 'Accuracy Score: {0}'.format(model2.score(X_test, y_test))
plt.title(all_sample_title, size = 15)<jupyter_output><empty_output><jupyter_text>**Visualizing the Decision Tree**<jupyter_code># Visualising the graph without the use of graphviz
plt.figure(figsize = (20,20))
dec_tree = plot_tree(model, 
                   feature_names=flowers.feature_names,  
                   class_names=flowers.target_names,
                   filled=True)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12770970782280866, 'fraction_numerical': 0.014608859566446749, 'mean_word_length': 5.818473895582329, 'pattern_counts': {'":': 0, '<': 104, '<?xml version=': 0, '>': 103, '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': '26', 'dir': 'jupyter-structured-clean-dedup', 'id': '126717', 'license': 'MIT', 'n_tokens_mistral': 3137, 'n_tokens_neox': 3029, 'n_words': 579, 'path': 'IrisFlower(DT)/IrisFlower_DT.ipynb', 'repo_name': 'akrish4/datascience-mashup', 'size': '8488'}