<jupyter_start><jupyter_text>**Decision Tree**  **Importing Libraries**<jupyter_code># Data Analysis Library

import numpy as np
import pandas as pd
!pip install dtreeviz
# Libraries to plot graph

import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
import seaborn as sns
import graphviz
from dtreeviz.trees import dtreeviz
# Libraries for implementation and prediction of algo

from sklearn import tree
from sklearn.tree import DecisionTreeClassifier  
from sklearn.model_selection import train_test_split
from sklearn import metrics
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import confusion_matrix<jupyter_output><empty_output><jupyter_text>**About Dataset**<jupyter_code>df = pd.read_csv("/content/Social_Network_Ads.csv")
df.describe()
df.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 400 entries, 0 to 399
Data columns (total 5 columns):
 #   Column           Non-Null Count  Dtype 
---  ------           --------------  ----- 
 0   User ID          400 non-null    int64 
 1   Gender           400 non-null    object
 2   Age              400 non-null    int64 
 3   EstimatedSalary  400 non-null    int64 
 4   Purchased        400 non-null    int64 
dtypes: int64(4), object(1)
memory usage: 15.8+ KB<jupyter_text>**Data Visulization** **sns.lmplot():** Plot data and regression model fits across a FacetGrid.<jupyter_code>sns.lmplot(x='Age',y ='Purchased', data=df)
sns.lmplot(x='EstimatedSalary',y ='Purchased', data=df)
fig = plt.figure()
ax = plt.axes(projection = '3d')
z = np.linspace(0, 15, 400)
zdata = 1 * np.random.random(400)
ax.scatter3D(df['Age'], df['EstimatedSalary'], df['Purchased'],cmap = 'Blues')
ax.set_xlabel('Age', fontsize= 10)
ax.set_ylabel("EstimatedSalary", fontsize=10)
ax.set_zlabel("Purchased", fontsize=10)<jupyter_output><empty_output><jupyter_text>**Spliting Data Into Train and Test Sets**<jupyter_code># Replacing Column names with numbers

df.columns = [num for num in range(len(df.columns))]
df.head()
X_df = df.drop([0, 1, 4], axis = 1)
Y_df = df[4]
# Splitinh dataset

X_train, X_test, Y_train, Y_test = train_test_split(X_df, Y_df, test_size=0.33, random_state=42)<jupyter_output><empty_output><jupyter_text>**Feature Scaling** **StandardScaler():** Standardize features by removing the mean and scaling to unit variance. The standard score of a sample **x** is calculated as:> z = (x - u) / sHere, **u:** mean, **s:** standard deviation**fit_transform():** Fit to data, then transform it.**transform():** Perform standardization by centering and scaling.<jupyter_code>sc_X = StandardScaler()
X_train = sc_X.fit_transform(X_train)
X_test = sc_X.transform(X_test)<jupyter_output><empty_output><jupyter_text>**Training Model**  **Model Instance**<jupyter_code>model = DecisionTreeClassifier(criterion='entropy', random_state=0)<jupyter_output><empty_output><jupyter_text>**Fitting Data in Model** **fit():** Compute the mean and std to be used for later scaling.<jupyter_code>model = model.fit(X_train, Y_train)<jupyter_output><empty_output><jupyter_text>**Ploting Tree**  **Using plot_tree()**<jupyter_code>tree.plot_tree(model, filled = True)<jupyter_output><empty_output><jupyter_text>**Using graphviz**<jupyter_code>plot = tree.export_graphviz(model, out_file=None, filled=True)
graph = graphviz.Source(plot, format="png") 
graph<jupyter_output><empty_output><jupyter_text>**Using dtreeviz**<jupyter_code>plot = dtreeviz(model, X_train, Y_train)
plot
plot.save("train_decision_tree.svg")
plot = dtreeviz(model, X_test, Y_test)
plot
plot.save("test_decision_tree.svg")<jupyter_output>/usr/local/lib/python3.7/dist-packages/numpy/core/_asarray.py:83: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray
  return array(a, dtype, copy=False, order=order)<jupyter_text>**Model Prediction**<jupyter_code>train_pred = model.predict(X_train)
pred = model.predict(X_test)<jupyter_output><empty_output><jupyter_text>**Model Evaluation For Training Data**  **Confusion Matrix**<jupyter_code>cm = confusion_matrix(Y_train, train_pred)
print(cm)<jupyter_output>[[177   0]
 [  1  90]]<jupyter_text>**Accuracy**<jupyter_code>accuracy = (cm[0][0] + cm[1][1])/(np.sum(cm))
print("Train Accuracy:", accuracy)<jupyter_output>Train Accuracy: 0.996268656716418<jupyter_text>**Calculating Error**<jupyter_code>print("Mean Absolute Error:", metrics.mean_absolute_error(Y_train, train_pred))
print("Mean Squared Error:", metrics.mean_squared_error(Y_train, train_pred))
print("Root Mean Square Error:", np.sqrt(metrics.mean_squared_error(Y_train, train_pred)))<jupyter_output>Mean Absolute Error: 0.0037313432835820895
Mean Squared Error: 0.0037313432835820895
Root Mean Square Error: 0.06108472217815261<jupyter_text>**Precision, Recall, F1 Score**<jupyter_code>precision = cm[0][0]/(cm[0][0] + cm[0][1])
recall = cm[0][0]/(cm[0][0] + cm[1][0])
f_score = 2/(1/recall + 1/precision)
print("Precision:", precision)
print("Recall:", recall)
print("F-Score:", f_score)<jupyter_output>Precision: 1.0
Recall: 0.9943820224719101
F-Score: 0.9971830985915492<jupyter_text>**Ploting Graph**<jupyter_code>plt.scatter(Y_train,train_pred)
plt.xlabel('Y Train')
plt.ylabel('Train Predicted Y')
plt.title("Regression Line")
plt.xlabel("Test Values")
plt.ylabel("Predicted Value")
plt.plot(Y_train, train_pred, color='blue', linewidth=1)
plt.scatter(Y_train,train_pred)
plt.show()
plt.figure(1)

X_set, Y_set = X_train, Y_train

X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() -1, stop = X_set[:, 0].max() + 1, step = 0.01), np.arange(start = X_set[:, 1].min() -1, stop = X_set[:, 1].max() + 1, step = 0.01))

plt.contourf(X1, X2, model.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape), alpha = 0.75, cmap = ListedColormap(('red', 'green')))

plt.xlim(X1.min(), X1.max())
plt.ylim(X2.min(), X2.max())

for i, j in enumerate(np.unique(Y_set)):
    plt.scatter(X_set[Y_set == j, 0], X_set[Y_set == j , 1], c = ListedColormap(('red', 'green'))(i), label = j, linewidths = 1, edgecolor = 'black')

plt.title('Prediction boundary and training examples plotted')
plt.legend()
plt.show()<jupyter_output>*c* argument looks like a single numeric RGB or RGBA sequence, which should be avoided as value-mapping will have precedence in case its length matches with *x* & *y*.  Please use the *color* keyword-argument or provide a 2-D array with a single row if you intend to specify the same RGB or RGBA value for all points.
*c* argument looks like a single numeric RGB or RGBA sequence, which should be avoided as value-mapping will have precedence in case its length matches with *x* & *y*.  Please use the *color* keyword-argument or provide a 2-D array with a single row if you intend to specify the same RGB or RGBA value for all points.<jupyter_text>**Model Evaluation For Testing Data**  **Confusion Matrix**<jupyter_code>cm = confusion_matrix(Y_test, pred)
print(cm)<jupyter_output>[[71  9]
 [ 9 43]]<jupyter_text>**Accuracy**<jupyter_code>accuracy = (cm[0][0] + cm[1][1])/(np.sum(cm))
print("Accuracy:", accuracy)<jupyter_output>Accuracy: 0.8636363636363636<jupyter_text>**Calculating Error**<jupyter_code>print("Mean Absolute Error:", metrics.mean_absolute_error(Y_test, pred))
print("Mean Squared Error:", metrics.mean_squared_error(Y_test, pred))
print("Root Mean Square Error:", np.sqrt(metrics.mean_squared_error(Y_test, pred)))<jupyter_output>Mean Absolute Error: 0.13636363636363635
Mean Squared Error: 0.13636363636363635
Root Mean Square Error: 0.3692744729379982<jupyter_text>**Precision, Recall, F1 Score**<jupyter_code>precision = cm[0][0]/(cm[0][0] + cm[0][1])
recall = cm[0][0]/(cm[0][0] + cm[1][0])
f_score = 2/(1/recall + 1/precision)
print("Precision:", precision)
print("Recall:", recall)
print("F-Score:", f_score)<jupyter_output>Precision: 0.8875
Recall: 0.8875
F-Score: 0.8875<jupyter_text>**Ploting Graph**<jupyter_code>plt.scatter(Y_test,pred)
plt.xlabel('Y Test')
plt.ylabel('Predicted Y')
plt.title("Regression Line")
plt.xlabel("Test Values")
plt.ylabel("Predicted Value")
plt.plot(Y_test, pred, color='blue', linewidth=1)
plt.scatter(Y_test,pred)
plt.show()<jupyter_output><empty_output><jupyter_text>**np.meshgrid():** Return coordinate matrices from coordinate vectors.**ListedColormap():** Used to create color maps from the given list of colors.**plt.contourf():** Draw filled contour.**plt.arange():** Return evenly spaced values within a given interval.<jupyter_code>plt.figure(2)

X_set, Y_set = X_test, Y_test

X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() -1, stop = X_set[:, 0].max() + 1, step = 0.01), np.arange(start = X_set[:, 1].min() -1, stop = X_set[:, 1].max() + 1, step = 0.01))

plt.contourf(X1, X2, model.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape), alpha = 0.75, cmap = ListedColormap(('red', 'green')))

plt.xlim(X1.min(), X1.max())
plt.ylim(X2.min(), X2.max())

for i, j in enumerate(np.unique(Y_set)):
    plt.scatter(X_set[Y_set == j, 0], X_set[Y_set == j , 1], c = ListedColormap(('red', 'green'))(i), label = j, linewidths = 1, edgecolor = 'black')

plt.title('Prediction boundary and testing examples plotted')
plt.legend()
plt.show()<jupyter_output>*c* argument looks like a single numeric RGB or RGBA sequence, which should be avoided as value-mapping will have precedence in case its length matches with *x* & *y*.  Please use the *color* keyword-argument or provide a 2-D array with a single row if you intend to specify the same RGB or RGBA value for all points.
*c* argument looks like a single numeric RGB or RGBA sequence, which should be avoided as value-mapping will have precedence in case its length matches with *x* & *y*.  Please use the *color* keyword-argument or provide a 2-D array with a single row if you intend to specify the same RGB or RGBA value for all points.
StarcoderdataJupyterStructuredCleanDedup