(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 −
3
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
For each test sample, compute the likelihood under each class's Gaussian distribution.
python
from scipy.stats import norm
# Likelihood under class +
likelihood_pos = norm.pdf(X_test, loc=mu_pos, scale=np.sqrt(sigma2_pos))
# Likelihood under class -
likelihood_neg = norm.pdf(X_test, loc=mu_neg, scale=np.sqrt(sigma2_neg))
Apply the Decision Rule:
Predict the class based on which likelihood is greater.
The output will display the test accuracy as a percentage.
3.3 Improving the Classifier
Is the Decision Rule Given Optimal?
The initial decision rule compares only the likelihoods ( P(x | \text{class}) ) and does not consider the prior probabilities ( P(\text{class}) ). This is equivalent to the Maximum Likelihood (ML) decision rule. The optimal decision rule under the Bayesian framework is the Maximum A Posteriori (MAP) rule, which considers both the likelihood and the prior:
accuracy = np.mean(y_pred == y_test)
print(f'New Test Accuracy with MAP: {accuracy * 100:.2f}%')
Report the New Test Accuracy:
After incorporating the priors, you may find that the test accuracy improves if the classes are imbalanced (i.e., the prior probabilities are not equal).
3.4 Estimating Parameters for 2D Data
To estimate the class mean vectors and covariance matrices for the 2D data in ./data_2/train.npz:
Scipy's norm.pdf: In one-dimensional cases, scipy.stats.norm.pdf is used to compute the Gaussian probability density function. In higher dimensions, consider using scipy.stats.multivariate_normal.pdf.
Covariance Matrix: In practice, using ddof=0 (population covariance) is appropriate when estimating parameters for Gaussian distributions in a Maximum Likelihood framework.
Implementing the Classifier for 2D Data:
For the 2D case, when predicting new samples, compute the multivariate Gaussian likelihoods for each class.
Use the parameters estimated to implement the decision rule for classification.
Testing Your Classifier:
After estimating the parameters, you can proceed to test your classifier on test data (if provided) using similar steps as in 3.2 and 3.3, adjusted for 2D data.
Remember to verify the correctness of your code and ensure that you handle array shapes and dimensions appropriately, especially when working with vectors and matrices.