<jupyter_start><jupyter_text>**Assignment 11**Predict type of animal based on height and weight.**Assignment Requirements:**- Use the following dataset for your assignment today: Link- Create a Decision Tree Classifier model on your training data.- Report your results on the test data.- Report the precision, recall, and accuracy of your training and test sets.- Try some of the preprocessing techniques learned last week where applicable to improve your model.- Plot your Decision Tree at the end of your notebook showing the tree you created.- Submit your notebook to the Learn platform when finished.<jupyter_code>import pandas as pd
import matplotlib.pyplot as plt
from sklearn import tree 
import seaborn as sns
sns.set_palette('rocket_r')
sns.set_style('white')
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.metrics import accuracy_score, recall_score, precision_score, classification_report
from sklearn.preprocessing import StandardScaler

# read data from the driver csv file
URL = 'https://drive.google.com/file/d/1p-0OicAm-AMFeCdaM9G53gai4Fqsqy1P/view?usp=sharing'
path = 'https://drive.google.com/uc?export=download&id='+URL.split('/')[-2]
df = pd.read_csv(path)
df.head()
df.info()
# as we see in the scatterplot there are two separated group

sns.scatterplot(data = df, x = 'height', y='weight',hue='type')
sns.despine(bottom = True, left = True);
# split data

X = df[['height','weight']]
y = df[['target']]
X_train, X_test, y_train, y_test = train_test_split(
    X, 
    y, 
    test_size=0.33, 
    random_state=42)
# baseline model 
y_train.target.value_counts(normalize=True)<jupyter_output><empty_output><jupyter_text>Create a Decision Tree Classifier model<jupyter_code># create a model 
dt = DecisionTreeClassifier()
dt.fit(X_train, y_train)
dt.score(X_train, y_train)
# show the model paramters
dt
# Classification report for train data
print(classification_report(y_train, dt.predict(X_train)))
# Classification report for test data
print(classification_report(y_test, dt.predict(X_test)))
# plot the tree for this model 
plt.figure(figsize=(15,8))
tree.plot_tree(dt, filled=True, class_names= df.type.unique());<jupyter_output><empty_output><jupyter_text>Improve model using Features Scaling<jupyter_code># scale features use Standard Scaler
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
# create a Decision Tree Classifier and fit with train
dt2 = DecisionTreeClassifier()
dt2.fit(X_scaled, y_train)
dt2.score(X_scaled, y_train)
# Classification report for train data
print(classification_report(y_train, dt2.predict(X_scaled)))
# Classification report for test data
print(classification_report(y_test, dt2.predict(X_test_scaled)))
# plot the tree for model
plt.figure(figsize=(15,8))
tree.plot_tree(dt2, filled=True, class_names=df.type.unique());<jupyter_output><empty_output><jupyter_text>Improve model using GridSearchCV<jupyter_code># create a dict of Decision Tree parameters 
param_grid = {
    'criterion': ['gini', 'entropy'],
    'max_depth': [5,10,15,20],
    'max_features': ['auto', 'sqrt', 'log2']

}

# create Grid Search Cross object
clf = GridSearchCV(dt, param_grid, cv=3)
clf.fit(X_train, y_train) # fit model
clf.score(X_train, y_train) # calculate score
# print the best parameters for model
clf.best_params_
# Classification report for train data
print(classification_report(y_train, clf.best_estimator_.predict(X_train)))
# Classification report for test data
print(classification_report(y_test, clf.best_estimator_.predict(X_test)))

# plot tree for model
plt.figure(figsize=(15,8))
tree.plot_tree(clf.best_estimator_, filled=True, class_names= df.type.unique()) ;<jupyter_output><empty_output><jupyter_text>**Finally,**Based on the above results, we can summarize these:1. There is a clear relation between height and weight (that's separating data into two groups).2. The size of data affects the final results. 3. Used improvement techniques in this data has improved the result a bit.<jupyter_code><jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10247093023255814, 'fraction_numerical': 0.010174418604651164, 'mean_word_length': 6.507272727272727, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '528300', 'license': 'MIT', 'n_tokens_mistral': 1360, 'n_tokens_neox': 1310, 'n_words': 387, 'path': 'Machine Learning/Week 3/Day11_ML_DecisionTreeClassifier.ipynb', 'repo_name': 'ZarahShibli/Data-Science-Bootcamp', 'size': '4128'}