← CVS Health Interview Insights
The normal case was fine, SS_res over SS_tot, nothing crazy.
Start by writing the r2_score formula from scratch using NumPy operations, then explicitly handle edge cases like perfect predictions and zero-variance targets. Test the function on a standard dataset and two edge cases where all true values are identical, demonstrating correct behavior and discussing trade-offs.
Pro tip: Mention that scikit-learn's r2_score returns 1.0 for perfect predictions and 0.0 for constant predictions (when the model predicts the mean), but raises a warning for zero-variance targets; replicate this behavior to show attention to detail.
Write the mathematical definition: R² = 1 - (SS_res / SS_tot), where SS_res = sum((y_true - y_pred)^2) and SS_tot = sum((y_true - y_true_mean)^2).
Use NumPy arrays and vectorized operations to compute SS_res and SS_tot efficiently, avoiding loops.
Check for perfect predictions (SS_res = 0) and zero-variance targets (SS_tot = 0). Decide on return values: 1.0 for perfect predictions, and for zero-variance, either 0.0 if predictions are perfect else -inf or raise an error, mirroring scikit-learn's behavior.
Create test cases: a standard case with varying y_true, and two edge cases where all y_true are identical (e.g., all zeros and all fives). Verify outputs and discuss expected behavior.
Explain why handling division by zero is important, and how your implementation compares to scikit-learn's, including any warnings or exceptions.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.
The second column in that matrix is in the hundreds while the third is under 1, so raw PCA is completely dominated by column 2.
Start by explaining the importance of scaling for PCA when features have different units or variances. Then outline the computational steps: center the data, compute covariance matrix, eigen-decomposition, sort eigenvalues, and compute explained variance ratios. Finally, compare the results and discuss why standardization changes the principal components.
Pro tip: Emphasize that PCA is sensitive to scale because it seeks directions of maximum variance; without standardization, features with larger scales dominate. Mention that in practice, standardization is almost always recommended unless features are already on comparable scales.
Load or generate the 6x3 matrix. For raw PCA, center each column by subtracting its mean. For standardized PCA, additionally divide each column by its standard deviation (z-score normalization).
Compute the covariance matrix using NumPy's np.cov on the centered data (raw) and standardized data. Ensure the correct orientation (rows as observations, columns as features).
Perform eigen-decomposition on each covariance matrix using np.linalg.eigh. Sort eigenvalues in descending order and reorder the corresponding eigenvectors accordingly.
Compute the explained variance ratio for each principal component by dividing each eigenvalue by the sum of all eigenvalues. Report the ratios for the first two components for both raw and standardized PCA.
Compare the principal components and explained variance ratios. Discuss how standardization equalizes the influence of each feature, leading to different principal directions that capture correlations rather than raw variance.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.