MSML603 HW Assignment 5
MSML603 HW Assignment 5
Mounted at /content/drive
import pandas as pd
df = pd.read_csv('/content/drive/MyDrive/Pizza (1).csv')
print(df.head())
import pandas as pd
data = pd.read_csv('/content/drive/MyDrive/Pizza (1).csv')
# Example: A vs B classification
ab_data = data[data['brand'].isin(['A', 'B'])] # Correct column name
is 'brand'
X_ab = ab_data[['mois', 'prot', 'fat', 'ash', 'sodium', 'carb',
'cal']].values # Correct column names for features
y_ab = (ab_data['brand'] == 'A').astype(int).values # A as 1, B as 0
# Display the first few rows to confirm the data is loaded correctly
print(ab_data.head())
1. A vs B:
from sklearn.linear_model import Perceptron
# Train Perceptron
clf_ab = Perceptron(max_iter=1000, tol=1e-3)
clf_ab.fit(X_ab, y_ab)
# Hyperplane parameters for A vs B
weights_ab = clf_ab.coef_[0]
bias_ab = clf_ab.intercept_[0]
Hyperplane for A vs B:
Weights: [-74.48 20.17 53.74 6.69 2.75 -6.12 5.41]
Bias: 0.0
1. A vs C:
# Filter data for A vs C
ac_data = data[data['brand'].isin(['A', 'C'])]
X_ac = ac_data[['mois', 'prot', 'fat', 'ash', 'sodium', 'carb',
'cal']].values
y_ac = (ac_data['brand'] == 'A').astype(int).values # A as 1, C as 0
# Train Perceptron
clf_ac = Perceptron(max_iter=1000, tol=1e-3)
clf_ac.fit(X_ac, y_ac)
Hyperplane for A vs C:
Weights: [-21.36 -8.12 27.69 1.09 1.15 0.7 2.19]
Bias: 0.0
1. B vs C:
# Filter data for B vs C
bc_data = data[data['brand'].isin(['B', 'C'])]
X_bc = bc_data[['mois', 'prot', 'fat', 'ash', 'sodium', 'carb',
'cal']].values
y_bc = (bc_data['brand'] == 'B').astype(int).values # B as 1, C as 0
# Train Perceptron
clf_bc = Perceptron(max_iter=1000, tol=1e-3)
clf_bc.fit(X_bc, y_bc)
Hyperplane for B vs C:
Weights: [ 6.19 -31.96 17.23 0.63 1.49 7.91 0.6 ]
Bias: 0.0
To compute the margins for each of the classifiers, you can use the formula for the margin,
which is given by Margin = 2/|ω|
import numpy as np
def compute_margin(weights):
return 2 / np.linalg.norm(weights)
# Compute margins
margin_ab = compute_margin(weights_ab)
margin_ac = compute_margin(weights_ac)
margin_bc = compute_margin(weights_bc)
print("Margins:")
print("Margin A vs B:", margin_ab)
print("Margin A vs C:", margin_ac)
print("Margin B vs C:", margin_bc)
Margins:
Margin A vs B: 0.021127526507728367
Margin A vs C: 0.055540198413972064
Margin B vs C: 0.05303387598998595
Fusion Rule for Classification Using majority voting based on the predictions from the classifiers,
we can classify new samples.
# Get predictions
pred_ab = clf_ab.predict(sample)
pred_ac = clf_ac.predict(sample)
pred_bc = clf_bc.predict(sample)
# Majority voting
votes = [pred_ab[0], pred_ac[0], pred_bc[0]] # Convert
predictions to brand labels
# Translate binary predictions to brand labels
labels = ['A' if vote == 1 else 'B' if i == 1 else 'C' for i, vote
in enumerate(votes)]