Reputation: 3
I'm Implementing SLP on boston house dataset that has 13 features. I chose 'rm' and 'zn' for X and 'medv' for target Y. I also implemented a Perceptron class from scratch. in this class I have a function called plot_losses
that plots predicted line(2d) and losses in one window and also plots predicted plane for 3d plots and thats the problem, the 3d part.
the plane doesnt show on 3d scatter plot.
Perceptron class implementation:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter
class Perceptron:
def __init__(self, input_size, lr, epochs):
self.w = np.zeros(input_size)
self.b = 0
self.lr = lr
self.epochs = epochs
self.losses = []
def fit(self, X_train, Y_train):
for _ in range(self.epochs):
for x_i in range(X_train.shape[0]):
x = X_train[x_i]
y = Y_train[x_i]
y_pred = np.dot(x, self.w) + self.b
error = y - y_pred
self.w = self.w + (error * x * self.lr)
self.b = self.b + (error * self.lr)
loss = np.mean(np.abs(error))
self.losses.append(loss)
def predict(self, X_test):
return np.dot(X_test, self.w) + self.b
def plot_losses(self, X_train, Y_train, ax1_title, ax2_title, plot_3d=False, plot_3d_title='3D Plot'):
for _ in range(self.epochs):
for x_i in range(X_train.shape[0]):
x = X_train[x_i]
y = Y_train[x_i]
Y_pred = np.dot(x, self.w) + self.b
if plot_3d:
fig = plt.figure(figsize=(10, 7))
ax = fig.add_subplot(111, projection='3d')
X_feature1 = X_train[:, 0]
X_feature2 = X_train[:, 1]
ax.scatter(X_feature1, X_feature2, Y_train, color='blue', label='True Values')
X1_grid, X2_grid = np.meshgrid(
np.linspace(X_feature1.min(), X_feature1.max(), 20),
np.linspace(X_feature2.min(), X_feature2.max(), 20)
)
Z_pred = self.w[0] * X1_grid + self.w[1] * X2_grid + self.b
ax.plot_surface(X1_grid, X2_grid, Z_pred, color='red', alpha=0.5)
ax.set_xlabel("Feature 'rm'")
ax.set_ylabel("Feature 'zn'")
ax.set_zlabel("Target 'medv'")
ax.set_title(plot_3d_title)
ax.legend()
plt.show()
else:
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(11, 5))
ax1.scatter(X_train[:, 0], Y_train, color='blue', label='True Values')
ax1.plot(X_train[:, 0], Y_pred, color='red', label='Predicted Line')
ax1.set_title(ax1_title)
ax1.legend()
ax2.plot(self.losses)
ax2.set_title(ax2_title)
ax2.set_xlabel("Epochs")
ax2.set_ylabel("Mean Squared Error (MSE)")
plt.tight_layout()
plt.show()
linear regression on Boston house dataset:
%matplotlib qt
import matplotlib.pyplot as plt
from sklearn.datasets import make_regression
from sklearn.model_selection import train_test_split
import numpy as np
import pandas as pd
from perceptron import Perceptron
df_boston = pd.read_csv('input/BostonHousing.csv')
X = df_boston[['rm','zn']].values
Y = df_boston['medv'].values
X_train, X_test, Y_train, Y_test = train_test_split(X,Y,test_size=.2)
slp = Perceptron(2, .01, 100)
slp.fit(X_train, Y_train)
slp.plot_losses(X_train,Y_train, 'Employees salary and experience perceptron', 'Loss value', plot_3d=True, plot_3d_title='Boston housing perceptron')
Upvotes: 0
Views: 29