Project Code
# Import libraries
import numpy as np
import [Link] as plt
from [Link] import load_iris
from sklearn.model_selection import train_test_split
from [Link] import StandardScaler
# Task 1
# Use only first two features for visualization
# Sepal length & Sepal width
iris = load_iris()
X = [Link][:, :2]
y = [Link]
# Data Splitting
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.20, random_state=0
# features Scaling
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = [Link](X_test)
# perceptron trick
from [Link] import PolynomialFeatures
poly = PolynomialFeatures(degree=2)
X_train_poly = poly.fit_transform(X_train)
X_test_poly = [Link](X_test)
# Training the Model
from sklearn.linear_model import LogisticRegression
classifier = LogisticRegression(multi_class='ovr', solver='lbfgs', random_state=0)
[Link](X_train_poly, y_train)
# Mesh Grid
x_min, x_max = X_train[:, 0].min() - 1, X_train[:, 0].max() + 1
y_min, y_max = X_train[:, 1].min() - 1, X_train[:, 1].max() + 1
x, y = [Link](
[Link](x_min, x_max, 0.01),
[Link](y_min, y_max, 0.01)
grid = np.c_[[Link](), [Link]()]
grid_poly = [Link](grid)
# Decision boudary plotting
Z = [Link](grid_poly)
Z = [Link]([Link])
[Link](figsize=(8,6))
[Link](x, y, Z, alpha=0.5)
colors = ['red', 'green', 'blue']
labels = iris.target_names
for i, color, label in zip(range(3), colors, labels):
[Link](
X_train[y_train == i, 0],
X_train[y_train == i, 1],
c=color, label=label, edgecolor='k'
[Link]('Sepal Length (scaled)')
[Link]('Sepal Width (scaled)')
[Link]('Decision Boundary for Iris Classes with percepron trick')
[Link]()
[Link]()
#Task 2
# Use only first two features for visualization
# Sepal length & Sepal width
iris = load_iris()
X = [Link][:, :2]
y = [Link]
# Data Splitting
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.20, random_state=0
# features Scaling
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = [Link](X_test)
# Training the Model without perceptron trick
from sklearn.linear_model import LogisticRegression
classifier = LogisticRegression(multi_class='ovr', solver='lbfgs', random_state=0)
[Link](X_train, y_train)
# Mesh Grid
x_min, x_max = X_train[:, 0].min() - 1, X_train[:, 0].max() + 1
y_min, y_max = X_train[:, 1].min() - 1, X_train[:, 1].max() + 1
x, y = [Link](
[Link](x_min, x_max, 0.01),
[Link](y_min, y_max, 0.01)
grid = np.c_[[Link](), [Link]()]
# Decision boudary plotting
Z2 = [Link](grid)
Z2 = [Link]([Link])
[Link](figsize=(8,6))
[Link](x, y, Z2, alpha=0.5)
colors = ['red', 'green', 'blue']
labels = iris.target_names
for i, color, label in zip(range(3), colors, labels):
[Link](
X_train[y_train == i, 0],
X_train[y_train == i, 1],
c=color, label=label, edgecolor='k'
[Link]('Sepal Length (scaled)')
[Link]('Sepal Width (scaled)')
[Link]('Decision Boundary with only logistic Regression')
[Link]()
[Link]()
# Task 3
# sigmoid function
# Use only first two features for visualization
# Sepal length & Sepal width
iris = load_iris()
X = [Link][:, :2]
y = [Link]
# Data Splitting
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.20, random_state=0
# features Scaling
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = [Link](X_test)
# Training the Model
from sklearn.linear_model import LogisticRegression
classifier = LogisticRegression(multi_class='ovr', random_state=0)
[Link](X_train, y_train)
def sigmoid(z1):
return 1 / (1 + [Link](-z1))
# Create mesh grid
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = [Link](
[Link](x_min, x_max, 300),
[Link](y_min, y_max, 300)
)
# Compute decision scores
grid = np.c_[[Link](), [Link]()]
grid = [Link](grid)
z1 = classifier.decision_function(grid)
# Convert scores to probabilities using sigmoid
probabilities = sigmoid(z1)
# Pick class with highest probability
z1 = [Link](probabilities, axis=1)
z1 = [Link]([Link])
# Plot decision boundary
[Link](figsize=(8, 6))
[Link](xx, yy, z1, alpha=0.5)
[Link](X[:, 0], X[:, 1], c=y, edgecolor='k')
[Link]("Sepal Length")
[Link]("Sepal Width")
[Link]("Decision Boundary of iris using Sigmoid)")
[Link]()
[Link]()
# Task 4
# Import libraries
import numpy as np
import [Link] as plt
from [Link] import load_iris
from sklearn.model_selection import train_test_split
from [Link] import StandardScaler, PolynomialFeatures
from sklearn.linear_model import LogisticRegression
# Load dataset (2 features only)
iris = load_iris()
X = [Link][:, :2]
y = [Link]
# Train-test split
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.20, random_state=0
# Feature scaling
sc = StandardScaler()
X_train_scaled = sc.fit_transform(X_train)
X_test_scaled = [Link](X_test)
# Perceptron trick (Polynomial Features)
poly = PolynomialFeatures(degree=2)
X_train_poly = poly.fit_transform(X_train_scaled)
# Train Logistic Regression (OvR)
classifier = LogisticRegression(multi_class='ovr', random_state=0)
[Link](X_train_poly, y_train)
# Sigmoid function
def sigmoid(z):
return 1 / (1 + [Link](-z))
# Mesh grid (in scaled feature space)
x_min, x_max = X_train_scaled[:, 0].min() - 1, X_train_scaled[:, 0].max() + 1
y_min, y_max = X_train_scaled[:, 1].min() - 1, X_train_scaled[:, 1].max() + 1
xx, yy = [Link](
[Link](x_min, x_max, 300),
[Link](y_min, y_max, 300)
# Prepare grid
grid = np.c_[[Link](), [Link]()]
grid_poly = [Link](grid)
# Decision function → Sigmoid
scores = classifier.decision_function(grid_poly)
probabilities = sigmoid(scores)
# Final class prediction
Z = [Link](probabilities, axis=1)
Z = [Link]([Link])
# Plot decision boundary
[Link](figsize=(8, 6))
[Link](xx, yy, Z, alpha=0.5)
colors = ['red', 'green', 'blue']
labels = iris.target_names
for i, color, label in zip(range(3), colors, labels):
[Link](
X_train_scaled[y_train == i, 0],
X_train_scaled[y_train == i, 1],
c=color, label=label, edgecolor='k'
[Link]("Sepal Length (scaled)")
[Link]("Sepal Width (scaled)")
[Link]("Decision Boundary using Perceptron Trick + Sigmoid ")
[Link]()
[Link]()
# Pie Chart (Class Distribution)
import [Link] as plt
from [Link] import load_iris
import numpy as np
iris = load_iris()
y = [Link]
labels = iris.target_names
sizes = [Link](y)
[Link](figsize=(6,6))
[Link](sizes, labels=labels, autopct='%1.1f%%', startangle=90)
[Link]("Pie Chart of Iris Class Distribution")
[Link]()
# Histogram (Feature Distribution)
import [Link] as plt
from [Link] import load_iris
iris = load_iris()
X = [Link]
feature_names = iris.feature_names
[Link](figsize=(8,5))
[Link](X[:, 0], bins=20, alpha=0.7)
[Link](feature_names[0])
[Link]("Frequency")
[Link]("Histogram of Sepal Length")
[Link]()
# Gaussian Distribution (Normal Curve)
import numpy as np
import [Link] as plt
from [Link] import load_iris
from [Link] import norm
iris = load_iris()
data = [Link][:, 0] # Sepal length
mean, std = [Link](), [Link]()
x = [Link]([Link](), [Link](), 100)
pdf = [Link](x, mean, std)
[Link](figsize=(8,5))
[Link](data, bins=20, density=True, alpha=0.6)
[Link](x, pdf)
[Link]("Sepal Length")
[Link]("Probability Density")
[Link]("Gaussian Distribution of Sepal Length")
[Link]()
# Correlation Heatmap
import seaborn as sns
import [Link] as plt
import pandas as pd
from [Link] import load_iris
iris = load_iris()
df = [Link]([Link], columns=iris.feature_names)
corr = [Link]()
[Link](figsize=(8,6))
[Link](corr, annot=True, cmap='coolwarm')
[Link]("Correlation Heatmap of Iris Features")
[Link]()