Lecture 3 "k-nearest neighbors" -Cornell CS4780 SP17

Lecture 3 "k-nearest neighbors" -Cornell CS4780 SP17

Lecture Overview and Guidelines

Importance of Attending Lectures

  • The speaker warns against the temptation for students to skip attending lectures in favor of watching recorded videos at home, noting that many students fail to follow through with this plan.

Communication Regarding Exam Results

  • Students should have received emails regarding their exam performance, either an invitation or a recommendation to drop the class if results were poor. If not received, they are advised to check spam folders or contact TAs.

Classroom Etiquette

  • The instructor requests all laptops and iPads be closed during the lecture, emphasizing a rule against using electronic devices in class.

Machine Learning Concepts

Data Structure in Machine Learning

  • The discussion transitions into machine learning fundamentals, explaining that data consists of pairs of feature vectors and labels. The goal is to find a function that predicts outputs based on input vectors.

Hypothesis Class Explanation

  • A hypothesis class (denoted as curly H) is introduced as the set of all possible functions considered for predictions. An example involving decision trees illustrates how these functions can be structured.

Decision Trees as Hypotheses

  • Decision trees are described as algorithms that split data based on features (e.g., gender or age), leading to predictions about outcomes (e.g., patient return within 60 days).

Learning Process in Machine Learning

Minimizing Loss Function

  • The objective in machine learning is finding a function that minimizes loss on a dataset. The loss function quantifies prediction errors, guiding improvements in model accuracy.

Challenges with Overfitting

  • A zero-loss algorithm is discussed; while it perfectly memorizes training data, it fails to generalize beyond it. This highlights the need for models that perform well on unseen data rather than just fitting existing examples.

Expected Loss and Generalization

Concept of Expected Loss

Understanding Data Distribution and Model Evaluation

Importance of Data Distribution

  • The distribution from which data is sampled is often unknown, making it challenging to compute exact metrics. However, approximations can be made.
  • A dataset is typically divided into two parts: a training set (D training) and a test set (D test). The test set should be kept separate to ensure unbiased evaluation.

Training and Testing Process

  • The algorithm is trained solely on the training dataset, while the performance is evaluated using the test dataset to obtain an unbiased estimator of actual performance.
  • Mistakes in splitting datasets can lead to grave errors in machine learning outcomes. Proper methodology for splitting data is crucial.

Case Study: Spam Filter Example

  • An example from industry illustrates that random splits can lead to misleading results; if spam emails are similar, they may appear in both training and testing sets.
  • This overlap causes the model to perform well on tests but poorly in real-world applications since it has essentially memorized the data rather than learned generalizable patterns.

Temporal Aspects of Data

  • When dealing with temporal data, it's essential to split datasets based on time—training on past data and testing on future data—to avoid information leakage.
  • If no temporal component exists, shuffling the dataset randomly ensures a fair representation across both training and testing sets.

Common Pitfalls in Dataset Splitting

  • Care must be taken when sampling patients' data; entire patient samples should be moved together into either training or testing sets rather than individual samples being mixed randomly.
  • Uniformly random sampling applies only if the data is independent and identically distributed (iid); otherwise, careful consideration of how labels are distributed during splits is necessary.

The Role of Validation Sets

Need for Validation Sets

  • In practice, models are often split into three sets: training, validation, and testing. This helps prevent overfitting by providing a separate dataset for tuning model parameters.

Evaluating Model Performance

  • Evaluating a model's performance on a single test set provides an initial estimate of its generalization error. However, re-evaluating after tweaking algorithms can skew results due to reliance on specific datasets.

Risks of Overfitting

Understanding Overfitting and Model Evaluation in Machine Learning

The Risks of Overfitting

  • Many algorithms may appear to perform well by coincidentally making no mistakes, leading to overfitting on the test set rather than genuinely effective performance.
  • Frequent examination of a dataset can inadvertently train the algorithm on the test set, compromising its unbiased nature.

Model Selection Process

  • To select the best model, multiple algorithms should be evaluated on a validation set; the chosen model is then tested on a separate test set to report true error rates.
  • Typically, training error is lower than validation error, which in turn is lower than test error due to choices made during validation.

Importance of Test Set Integrity

  • It’s crucial to evaluate models strictly once on the test set after finalizing them; repeated evaluations can lead to misleading results when applied to new datasets.
  • Even if an algorithm performs well initially, it may fail when applied to different data due to prior exposure or adjustments based on earlier tests.

Handling Temporal Data and Future Information

  • When dealing with temporal data, it's essential that algorithms do not access future information during testing as this would invalidate results.
  • Adding noise or perturbations during validation can help mitigate overfitting issues but requires careful implementation.

Best Practices for Data Splits

  • After selecting a model based on validation results, retrain it using the entire dataset before testing; avoid any further adjustments post-testing.

Understanding Trade-offs in Machine Learning

The Importance of Test Sets

  • Discusses the trade-off between estimating translation error and data usage for training, emphasizing the need to balance these aspects.
  • Highlights that once a test set is examined, it should not influence further algorithm adjustments to avoid bias in results.

Iterative Testing and Overfitting

  • Explains the iterative process of testing algorithms on a dataset while maintaining transparency about which algorithms were tested.
  • Warns against adapting algorithms based on initial test results, as this can lead to overfitting.

Leave-One-Out Cross-Validation

  • Introduces a method where one data point is left out during training to assess model performance, particularly useful in expensive experiments like medical trials.
  • Describes averaging errors across multiple iterations of this method to obtain reliable estimates.

Achieving Desired Error Rates

Function Optimization for Spam Filters

  • Illustrates the process of refining a function to meet specific error thresholds (e.g., 0.5% for spam filters).
  • Emphasizes the importance of continuous tweaking until satisfactory performance metrics are achieved.

Statistical Foundations in Machine Learning

Understanding Loss Functions

  • Poses a question regarding loss calculation on large test sets and its convergence properties as sample size increases.

Law of Large Numbers

  • Engages students in discussing why loss converges with increasing sample sizes, leading into an explanation of statistical principles.
  • Clarifies that the weak law of large numbers assures us that averages will converge towards expected values as sample sizes grow.

Data Representation and Hypothesis Class Selection

Features and Labels in Data Sets

  • Recaps previous discussions about representing data through features and labels while defining functions mapping inputs to outputs.

Challenges in Algorithm Selection

  • Questions why simply identifying the best algorithm isn't sufficient for teaching or practical application.

Interactive Learning Exercise

Understanding Assumptions in Machine Learning Algorithms

The Importance of Assumptions

  • The speaker addresses the audience, highlighting a common issue where participants struggle with understanding the assumptions behind machine learning algorithms.
  • It is emphasized that many practitioners make assumptions about data without realizing it, which can lead to incorrect predictions.
  • The necessity of assuming a "nice" world for machine learning to function effectively is discussed; adversarial settings complicate predictions.
  • The "No Free Lunch" theorem is introduced, stating that no single algorithm works best for all problems due to varying assumptions required by different algorithms.
  • A reminder that if an algorithm's assumptions do not hold true for a dataset, its performance can be significantly compromised.

Analyzing Data Sets and Algorithm Selection

  • Practitioners often favor one algorithm (e.g., deep learning or random forests), neglecting to consider whether its underlying assumptions are valid for their specific dataset.
  • The goal of the class is to enhance students' ability to analyze datasets critically and understand the implications of various algorithms' assumptions on prediction accuracy.
  • Meta-learning is mentioned as a strategy where multiple algorithms are tested empirically against training and validation sets to find the best fit based on performance metrics.

Transitioning to Specific Algorithms

  • After discussing general principles, the speaker transitions into introducing specific algorithms used in machine learning.

Introduction to K Nearest Neighbors (KNN)

  • KNN is presented as one of the oldest machine learning algorithms, dating back to 1967.
  • A binary classification problem using KNN is illustrated with data points represented as crosses and circles; this visual aids understanding of how KNN operates.

How K Nearest Neighbors Works

  • The assumption made by KNN is that similar data points will have similar labels; this foundational concept drives its predictive capability.
  • When predicting a label for an unknown data point, KNN examines 'K' nearest neighbors (where 'K' could be any odd number like 1 or 3).

Voting Mechanism in KNN

Understanding the Nearest Neighbors Algorithm

Introduction to the Algorithm

  • The speaker introduces a simple yet effective algorithm for identifying K nearest neighbors, emphasizing its straightforward nature.
  • A test point X is defined alongside a dataset D, where a subset S(X) contains exactly K data points.

Defining Nearest Neighbors

  • For any point Y' in dataset D that is not part of S(X), its distance from the test point must be greater than or equal to the maximum distance of points in S(X).
  • In simpler terms, if a point is not among the K nearest neighbors, it should be further away than the farthest neighbor within that set.

Importance of Distance Metrics

  • The effectiveness of nearest neighbors heavily relies on the chosen distance metric; an accurate metric ensures better classification.
  • If two points share the same label, their distance will be zero; thus, using an appropriate metric can lead to perfect classification.

Common Distance Metrics

  • The Minkowski distance is highlighted as a commonly used metric. It calculates distances based on various dimensions raised to power P.
  • Special cases for Minkowski include:
  • P = 1: Manhattan distance.
  • P = 2: Euclidean distance.
  • As P to infty: Maximum difference across dimensions.

Understanding Large Values of P

  • As P increases, differences in dimensions become more pronounced; larger differences dominate smaller ones when calculating distances.
  • This leads to scenarios where only maximum differences matter, simplifying calculations and interpretations in high-dimensional spaces.

Conclusion on Metric Learning

  • The flexibility of adjusting parameter P allows for exploring various distances within algorithms implementing Minkowski metrics.
Video description

Cornell class CS4780. (Online version: https://tinyurl.com/eCornellML ) Lecture Notes: http://www.cs.cornell.edu/courses/cs4780/2018fa/lectures/lecturenote02_kNN.html Past 4780 exams are here: www.dropbox.com/s/zfr5w5bxxvizmnq/Kilian past Exams.zip?dl=0 Past 4780 homeworks are here: https://www.dropbox.com/s/tbxnjzk5w67u0sp/Homeworks.zip?dl=0 If you want to take the course for credit and obtain an official certificate, there is now a revamped version with 118 new high quality videos, made just for this course, offered through eCornell ( https://tinyurl.com/eCornellML ). Note, however, that eCornell does charge tuition for this version.