(GDA: Gaussian Discriminant Analysis) We will build a binary classifier by using GDA.
3.1. Suppose someone gave you a decision rule (classifier) based on GDA, as shown below. Please
use “./data 1/train.npz” and write the code “train3 1.py” to estimate the four statistics in the
table below. N (x|μ, σ2) = 1√(2πσ2) exp { −1
2σ2 (x − μ)2}
δ(x) = +1 N (x|μpos, σ2
pos) ≥ N (x|μneg , σ2
neg )
δ(x) = −1 N (x|μpos, σ2
pos) < N (x|μneg , σ2
neg )
(2)
class mean
(μ)
var
(σ2)
class +
class −
3.2. Write the code “test3 2.py” to predict the class for the test data “./data 1/test.npz”. What
is the test accuracy?
3.3. How would you improve your classifier? Is the decision rule given above optimal? Write a
new code “test3 3.py” modifying “test3 2.py” and report new test accuracy. (hint: recall MAP
rule.)
3.4. Your will build a GDA classifier for 2D data. Use the data:“./data 2/train.npz” and write
the code “train3 4.py” to estimate the four statistics and complete the table below.
class mean
(⃗μ )
COV
(Σ)
class +
class −
answer these question clearly, start from 3.1 and end with 3.4
Assistant
3.1
To estimate the four statistics (means and variances for each class) using the training data ./data_1/train.npz, follow these steps in your script train3_1.py:
Load the Training Data:
python
import numpy as np
# Load training data
data = np.load('./data_1/train.npz')
x_train = data['x'] # Feature data (assumed to be 1D)
y_train = data['y'] # Labels (+1 or -1)
Separate Data by Class:
python
# Separate data for class +1
x_pos = x_train[y_train == +1]
# Separate data for class -1
x_neg = x_train[y_train == -1]
Compute Means and Variances:
python
# Compute mean and variance for class +1
mu_pos = np.mean(x_pos)
sigma2_pos = np.var(x_pos, ddof=1) # ddof=1 for sample variance
# Compute mean and variance for class -1
mu_neg = np.mean(x_neg)
sigma2_neg = np.var(x_neg, ddof=1)
Print or Save the Results:
python
print(f"Class +1 Mean (μ_pos): {mu_pos}")
print(f"Class +1 Variance (σ2_pos): {sigma2_pos}")
print(f"Class -1 Mean (μ_neg): {mu_neg}")
print(f"Class -1 Variance (σ2_neg): {sigma2_neg}")
Estimated Statistics:
(You need to run the script to obtain the actual numerical values.)
code
Class +1 Mean (μ_pos): [value]
Class +1 Variance (σ2_pos): [value]
Class -1 Mean (μ_neg): [value]
Class -1 Variance (σ2_neg): [value]
3.2
To predict the classes for the test data ./data_1/test.npz using the estimated statistics, and compute the test accuracy in test3_2.py, follow these steps:
Load Test Data and Estimated Parameters:
python
import numpy as np
from scipy.stats import norm
# Load test data
data = np.load('./data_1/test.npz')
x_test = data['x']
y_test = data['y']
# Use the estimated parameters from 3.1
# (You may save them to a file in 3.1 and load them here, or recompute them)
Compute Likelihoods Using Gaussian PDFs:
python
# Compute likelihoods for each class
likelihood_pos = norm.pdf(x_test, loc=mu_pos, scale=np.sqrt(sigma2_pos))
likelihood_neg = norm.pdf(x_test, loc=mu_neg, scale=np.sqrt(sigma2_neg))
Apply the Decision Rule:
python
# Predict classes based on the decision rule
y_pred = np.where(likelihood_pos >= likelihood_neg, +1, -1)
(You need to run the script to obtain the actual test accuracy.)
code
Test Accuracy: [value]%
3.3
To improve the classifier, consider incorporating prior probabilities of each class into the decision rule (Maximum A Posteriori, MAP rule). The original decision rule assumes equal priors, which may not be optimal if class distributions are imbalanced.
# Predict classes based on MAP rule
y_pred = np.where(posterior_pos >= posterior_neg, +1, -1)
Compute and Report New Test Accuracy:
python
# Calculate new accuracy
accuracy = np.mean(y_pred == y_test)
print(f"New Test Accuracy with MAP Rule: {accuracy * 100:.2f}%")
New Test Accuracy:
(You need to run test3_3.py to obtain the actual test accuracy.)
code
New Test Accuracy with MAP Rule: [value]%
Conclusion:
Including class prior probabilities often improves classifier performance, especially in cases of class imbalance. The MAP rule provides a more optimal decision boundary compared to using only likelihoods.
3.4
To build a GDA classifier for 2D data using ./data_2/train.npz, estimate the mean vectors and covariance matrices for each class in train3_4.py as follows:
Load Training Data:
python
import numpy as np
# Load training data
data = np.load('./data_2/train.npz')
X_train = data['X'] # Feature data (assumed to be 2D array of shape (n_samples, 2))
y_train = data['y'] # Labels (+1 or -1)
Separate Data by Class:
python
# Data for class +1
X_pos = X_train[y_train == +1]
# Data for class -1
X_neg = X_train[y_train == -1]
Compute Mean Vectors and Covariance Matrices:
python
# Compute mean vector and covariance matrix for class +1
mu_pos = np.mean(X_pos, axis=0)
Sigma_pos = np.cov(X_pos, rowvar=False, ddof=1)
# Compute mean vector and covariance matrix for class -1
mu_neg = np.mean(X_neg, axis=0)
Sigma_neg = np.cov(X_neg, rowvar=False, ddof=1)
(You need to run the script to obtain the actual numerical values.)
code
Class +1 Mean Vector (μ_pos): [μ_pos_vector]
Class +1 Covariance Matrix (Σ_pos):
[[σ11_pos, σ12_pos],
[σ21_pos, σ22_pos]]
Class -1 Mean Vector (μ_neg): [μ_neg_vector]
Class -1 Covariance Matrix (Σ_neg):
[[σ11_neg, σ12_neg],
[σ21_neg, σ22_neg]]
Notes:
Mean Vector (μ): A vector containing the mean of each feature for the class.
Covariance Matrix (Σ): A matrix representing the covariance between each pair of features.
By estimating these parameters, you have modeled the distribution of each class as a Gaussian in 2D space, which can be used for classification tasks.
Ensure to run your scripts with the provided datasets to obtain the numerical results and test accuracies.