(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
3.5 Write the code “test3 5.py” to predict the class for the test data: ‘./data 2/test.npz”. What
is the test accuracy?
3.6 [Extra Points: 10 points] The 2D data is generated by the densities specified below. Write the
code “test3 6.py” based on the information and compute the new accuracy. Determine whether
there is any significant change compared to the accuracy obtained in 3.5. Based on your accuracy
comparison, discuss how GDA provides a reasonable framework for classification.
xpos ∼ N ([0, 0]t, I)
xneg ∼ 0.5 × N ([0, 2]t, I) + 0.5 × N ([0, −2]t, I)
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
To address this problem, we'll develop a new classifier in test3_6.py that leverages the exact data-generating distributions specified. We'll compute the likelihoods using these distributions and classify the test data accordingly. Then, we'll compare the new accuracy to that obtained in 3.5 and discuss the implications regarding GDA's effectiveness.
Compute the posterior probabilities (since priors are equal, we can compare the likelihoods directly):
Assign ( y = +1 ) if ( p(x \mid y = +1) > p(x \mid y = -1) )
Assign ( y = -1 ) otherwise.
Here's the code for test3_6.py:
python
import numpy as np
from scipy.stats import multivariate_normal
# Load test data
data = np.load('./data_2/test.npz')
X_test = data['X_test']
y_test = data['y_test']
# Define means and covariance matrices
mean_pos = np.array([0, 0])
cov_pos = np.eye(2)
mean_neg1 = np.array([0, 2])
mean_neg2 = np.array([0, -2])
cov_neg = np.eye(2)
# Compute the likelihoods
likelihood_pos = multivariate_normal.pdf(X_test, mean=mean_pos, cov=cov_pos)
# For negative class, since it's a mixture
likelihood_neg1 = multivariate_normal.pdf(X_test, mean=mean_neg1, cov=cov_neg)
likelihood_neg2 = multivariate_normal.pdf(X_test, mean=mean_neg2, cov=cov_neg)
likelihood_neg = 0.5 * likelihood_neg1 + 0.5 * likelihood_neg2
# Classify based on likelihoods
y_pred = np.where(likelihood_pos > likelihood_neg, 1, -1)
# Calculate accuracy
accuracy = np.mean(y_pred == y_test)
print(f'Test Accuracy: {accuracy * 100:.2f}%')
Computing the New Accuracy
Running the above code will yield the new test accuracy. For instance, suppose it outputs:
code
Test Accuracy: 95.00%
(Note: The actual accuracy may vary depending on the test dataset.)
Comparing with Accuracy from 3.5
Assuming the accuracy obtained in 3.5 was:
code
Test Accuracy: 92.00%
We observe an improvement in accuracy when using the true data-generating distributions.
Discussion on GDA’s Effectiveness
The Gaussian Discriminant Analysis (GDA) classifier assumes that the feature vectors of each class are normally distributed. In 3.5, we used GDA under the assumption that both classes are generated from Gaussian distributions with class-specific means and a shared covariance matrix estimated from the data.
In 3.6, we utilized the true underlying distributions:
The positive class is indeed normally distributed as assumed by GDA.
The negative class, however, is a mixture of two Gaussians, which deviates from GDA's single Gaussian assumption.
Implications:
GDA Performance: Despite the negative class not strictly following a single Gaussian distribution, GDA can still perform reasonably well because it captures the central tendency of the data. In 3.5, the estimated mean for the negative class would be around the average of the two means, and the covariance would reflect the spread.
Improved Accuracy: By using the true distributions in 3.6, particularly modeling the negative class as a mixture of Gaussians, we more accurately represent the data's structure. This leads to better likelihood estimates and, consequently, improved classification accuracy.
Model Assumptions: GDA's effectiveness depends on how well the model assumptions align with the actual data distribution. When the assumptions are violated (as with the negative class in this case), GDA may not capture all the nuances, potentially leading to reduced performance.
Conclusion:
Reasonable Framework: GDA provides a reasonable framework for classification, especially when classes are approximately normally distributed. It is simple to implement and often performs well even when the normality assumption is moderately violated.
Limitations: In scenarios where class distributions are complex (e.g., mixtures, multimodal distributions), GDA may not be optimal. Alternative models that can capture such complexities, like mixture models or non-parametric methods, might yield better performance.
Practical Considerations: In practice, even when the data doesn't perfectly meet the assumptions, GDA can serve as a good baseline due to its simplicity and ease of interpretation. It is essential, however, to be aware of its limitations and consider more sophisticated models when necessary.