import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from matplotlib import cm
Linear Regression For Classification
Linear Regression isn’t suitable for classification due to its inability to predict probabilities and handle categorical outcomes; Logistic Regression addresses these issues by modeling probabilities and accommodating binary classification tasks effectively.
Libraries Required
Dataset
= np.array([0, 1, 2, 3, 4, 5, 6, 7, 8])
x_train = np.array([0, 0, 0, 1, 0, 1, 0, 1, 1]) y_train
= y_train == 1
pos = y_train == 0
neg
=(6, 4))
plt.figure(figsize
plt.scatter(x_train[pos], y_train[pos],='x', s=80, c='red', label="y=1")
marker='o',
plt.scatter(x_train[neg], y_train[neg], marker=100, label="y=0", facecolors='blue', edgecolors='black', linewidth=1)
s-0.08, 1.1)
plt.ylim('y', fontsize=12)
plt.ylabel('x', fontsize=12)
plt.xlabel('One Variable Plot')
plt.title(
plt.legend()
plt.tight_layout() plt.show()
Using Linear Regression
= x_train.reshape(-1, 1)
x_train = y_train.reshape(-1, 1)
y_train
= LinearRegression()
model
model.fit(x_train, y_train)
= model.predict(x_train)
y_pred
= np.array([0.5] * len(x_train))
y_line
= (0.5 - model.intercept_[0]) / model.coef_[0]
x_intersection
=(12, 6))
plt.figure(figsize='x', s=80, c='red', label="y=1")
plt.scatter(x_train[pos], y_train[pos], marker='o', s=100,
plt.scatter(x_train[neg], y_train[neg], marker="y=0", facecolors='blue', edgecolors='black', linewidth=1)
label
='red', label='Linear Regression Line')
plt.plot(x_train, y_pred, color
plt.plot([x_intersection, x_intersection],-0.2, 1.2], color='green', linestyle='--')
['X')
plt.xlabel('Y')
plt.ylabel(
plt.legend()'←', xy=(x_intersection, 0.5), xytext=(x_intersection - 0.5, 0.6), fontsize=25, color='blue')
plt.annotate('Y=0', xy=(x_intersection, 0.5), xytext=(x_intersection - 0.5, 0.5), fontsize=15, color='blue')
plt.annotate('→', xy=(x_intersection, 0.5), xytext=(x_intersection + 0.1, 0.6),fontsize=25, color='red')
plt.annotate('Y=1', xy=(x_intersection, 0.5), xytext=(x_intersection + 0.1, 0.5), fontsize=15, color='red')
plt.annotate( plt.show()
Logistic Square Error Cost
def compute_cost_logistic_sq_err(X, y, w, b):
= np.dot(X, w) + b
z = 1 / (1 + np.exp(-z))
f_wb = np.mean((f_wb - y) ** 2) / 2
cost return cost
= np.meshgrid(np.linspace(-6, 12, 50),
wx, by 10, -20, 40))
np.linspace(= np.c_[wx.ravel(), by.ravel()]
points = np.zeros(points.shape[0])
cost
for i in range(points.shape[0]):
= points[i]
w, b = compute_cost_logistic_sq_err(x_train, y_train, w, b)
cost[i]
= cost.reshape(wx.shape)
cost
= plt.figure(figsize=(18, 15))
fig = fig.add_subplot(1, 1, 1, projection='3d')
ax =0.6, cmap=cm.jet)
ax.plot_surface(wx, by, cost, alpha'w', fontsize=16)
ax.set_xlabel('b', fontsize=16)
ax.set_ylabel("Cost", rotation=90, fontsize=16)
ax.set_zlabel('"Logistic" Squared Error Cost vs (w, b)')
ax.set_title(
plt.show()