Weighted Decision Trees

ML
Tutorial
Author

Chirag Sarda

Published

January 10, 2024

import numpy as np
import matplotlib.pyplot as plt
from latexify import latexify, format_axes
%matplotlib inline
%config InlineBackend.figure_format = 'retina'
!which latex
/usr/local/bin/latex
latexify(columns=2)
# Dummy Data
x1 = np.array([1, 3, 2, 5, 7, 8])
x2 = np.array([1.5, 3, 5, 2, 4, 4.5])
category = np.array([0, 1, 1, 1, 0, 0]) # 0 -> - class and 1 -> + class

# Separate data points for each class
class_0_x1 = x1[category == 0]
class_0_x2 = x2[category == 0]
class_1_x1 = x1[category == 1]
class_1_x2 = x2[category == 1]

# Create a scatter plot for - class with blue color
plt.scatter(class_0_x1, class_0_x2, color='blue', marker='_')

# Create a scatter plot for + class with red color
plt.scatter(class_1_x1, class_1_x2, color='red', marker='+')


plt.xlim(0, 10)
plt.ylim(0, 6)
plt.xlabel('$X1$')
plt.ylabel('$X2$')
format_axes(plt.gca())
plt.savefig("../figures/dt_weighted/fig1.pdf")

plt.show()

p = np.array([0.3, 0.1, 0.1, 0.3, 0.1, 0.1])

# Create a scatter plot for - class with blue color
plt.scatter(class_0_x1, class_0_x2, color='blue', marker='_')

# Create a scatter plot for + class with red color
plt.scatter(class_1_x1, class_1_x2, color='red', marker='+')

for i, txt in enumerate(p):
    plt.annotate(txt, (x1[i], x2[i]), textcoords="offset points", xytext=(0, 5), ha='center')

# Add labels and title
plt.xlim(0, 10)
plt.ylim(0, 6)
plt.xlabel('$X1$')
plt.ylabel('$X2$')

format_axes(plt.gca())
plt.savefig("../figures/dt_weighted/fig2.pdf")
plt.show()



# Create a scatter plot for - class with blue color
plt.scatter(class_0_x1, class_0_x2, color='blue', marker='_')

# Create a scatter plot for + class with red color
plt.scatter(class_1_x1, class_1_x2, color='red', marker='+')

plt.axvline(x=4, color='black', linestyle='--')

for i, txt in enumerate(p):
    plt.annotate(txt, (x1[i], x2[i]), textcoords="offset points", xytext=(0, 5), ha='center')

# Add labels and title
plt.xlim(0, 10)
plt.ylim(0, 6)
plt.xlabel('$X1$')
plt.ylabel('$X2$')
format_axes(plt.gca())
plt.savefig("../figures/dt_weighted/fig3.pdf")
plt.show()

p = np.array([0.3, 0.1, 0.1, 0.3, 0.1, 0.1])

# Create a scatter plot for - class with blue color
plt.scatter(class_0_x1, class_0_x2, color='blue', marker='_')

# Create a scatter plot for + class with red color
plt.scatter(class_1_x1, class_1_x2, color='red', marker='+')

plt.axvline(x=4, color='black', linestyle='--')

for i, txt in enumerate(p):
    plt.annotate(txt, (x1[i], x2[i]), textcoords="offset points", xytext=(0, 5), ha='center')


plt.fill_betweenx(y=np.arange(0, 6, 0.01), x1=0, x2= 4, color='red', alpha=0.3)

# Add labels and title
plt.xlim(0, 10)
plt.ylim(0, 6)
plt.xlabel('$X1$')
plt.ylabel('$X2$')
format_axes(plt.gca())
plt.savefig("../figures/dt_weighted/fig4.pdf")
plt.show()

p = np.array([0.3, 0.1, 0.1, 0.3, 0.1, 0.1])

# Create a scatter plot for - class with blue color
plt.scatter(class_0_x1, class_0_x2, color='blue', marker='_')

# Create a scatter plot for + class with red color
plt.scatter(class_1_x1, class_1_x2, color='red', marker='+')

plt.axvline(x=4, color='black', linestyle='--')

for i, txt in enumerate(p):
    plt.annotate(txt, (x1[i], x2[i]), textcoords="offset points", xytext=(0, 5), ha='center')


plt.fill_betweenx(y=np.arange(0, 6, 0.01), x1=4, x2= 10, color='blue', alpha=0.3)

# Add labels and title
plt.xlim(0, 10)
plt.ylim(0, 6)
plt.xlabel('$X1$')
plt.ylabel('$X2$')
format_axes(plt.gca())
plt.savefig("../figures/dt_weighted/fig5.pdf")
plt.show()