27 Clustering
27.1 DBSCAN
27.2 k-Means Clustering
The \(k\)-means algorithm is an unsupervised learning algorithm that has a loose relationship to the \(k\)-nearest neighbor classifier. The \(k\)-means algorithm works as follows:
- Step 1: Randomly choose \(k\) centers. Assign points to cluster.
- Step 2: Determine the distances of each data point to the centroids and re-assign each point to the closest cluster centroid based upon minimum distance
- Step 3: Calculate cluster centroids again
- Step 4: Repeat steps 2 and 3 until we reach global optima where no improvements are possible and no switching of data points from one cluster to other.
The basic principle of the \(k\)-means algorithm is illustrated in Figure 27.1, Figure 27.2, Figure 27.3, and Figure 27.4.




- Video: K-means clustering
27.3 DDMO-Additional Videos
- Odds and Log(Odds), Clearly Explained!!!
- One-Hot, Label, Target and K-Fold Target Encoding, Clearly Explained!!!
- Maximum Likelihood for the Exponential Distribution, Clearly Explained!!!
- ROC and AUC, Clearly Explained!
- Entropy (for data science) Clearly Explained!!!
- Classification Trees in Python from Start to Finish: Long live video!
27.4 DDMO-Exercises
Exercise 27.1 (Smaller Bins) What happens when we use smaller bins in a histogram?
Exercise 27.2 (Density Curve) Why plot a curve to approximate a histogram?
Exercise 27.3 (TwoSDQuestion) How many samples are plus/minus two SD around the mean?
Exercise 27.4 (OneSDQuestion) How many samples are plus/minus one SD around the mean?
Exercise 27.5 (ThreeSDQuestion) How many samples are plus/minus three SD around the mean?
Exercise 27.6 (DataRangeQuestion) You have a mean at 100 and a SD of 10. Where are 95% of the data?
Exercise 27.7 (PeakHeightQuestion) If the peak is very high, is the SD low or high?
Exercise 27.8 (ProbabilityQuestion) If we have a certain curve and want to calculate the probability of values equal to 20 if the mean is 20.
Exercise 27.9 (MeanDifferenceQuestion) The difference between \(\mu\) and x-bar?
Exercise 27.10 (EstimateMeanQuestion) How do you calculate the sample mean?
Exercise 27.11 (SigmaSquaredQuestion) What is sigma squared?
Exercise 27.12 (EstimatedSDQuestion) What is the formula for the estimated standard deviation?
Exercise 27.13 (VarianceDifferenceQuestion) Difference between the variance and the estimated variance?
Exercise 27.14 (ModelBenefitsQuestion) What are the benefits of using models?
Exercise 27.15 (SampleDefinitionQuestion) What is a sample in statistics?
Exercise 27.16 (RejectHypothesisQuestion) What does it mean to reject a hypothesis?
Exercise 27.17 (NullHypothesisQuestion) What is a null hypothesis?
Exercise 27.18 (BetterDrugQuestion) How can you show that you have found a better drug?
Exercise 27.19 (PValueIntroductionQuestion) What is the reason for introducing the p-value?
Exercise 27.20 (PValueRangeQuestion) Is there any range for p-values? Can it be negative?
Exercise 27.21 (PValueRangeQuestion) Is there any range for p-values? Can it be negative?
Exercise 27.22 (TypicalPValueQuestion) What are typical values of the p-value and what does it mean? 5%?
Exercise 27.23 (FalsePositiveQuestion) What is a false-positive?
Exercise 27.24 (CalculatePValueQuestion) How to calculate p-value?
Exercise 27.25 (SDCalculationQuestion) What is the SD if the mean is 155 and in the range from 142 - 169 there are 95% of the data?
Exercise 27.26 (SidedPValueQuestion) When do we need the two-sided p-value and when the one-sided?
Exercise 27.27 (CoinTestQuestion) Test a coin with Tail-Head-Head. What is the p-value?
Exercise 27.28 (BorderPValueQuestion) If you get exactly the 0.05 border value, can you reject?
Exercise 27.29 (OneSidedPValueCautionQuestion) Why should you be careful with a one-sided p-test?
Exercise 27.30 (BinomialDistributionQuestion) What is the binomial distribution?
Exercise 27.31 (PHackingWaysQuestion) Name two typical ways of p-hacking.
Exercise 27.32 (AvoidPHackingQuestion) How can p-hacking be avoided?
Exercise 27.33 (MultipleTestingProblemQuestion) What is the multiple testing problem?
27.4.0.1 Covariance
Exercise 27.34 (CovarianceDefinitionQuestion) What is covariance?
Exercise 27.35 (CovarianceMeaningQuestion) What is the meaning of covariance?
Exercise 27.36 (CovarianceVarianceRelationshipQuestion) What is the relationship between covariance and variance?
Exercise 27.37 (HighCovarianceQuestion) If covariance is high, is there a strong relationship?
Exercise 27.38 (ZeroCovarianceQuestion) What if the covariance is zero?
Exercise 27.39 (NegativeCovarianceQuestion) Can covariance be negative?
Exercise 27.40 (NegativeVarianceQuestion) Can variance be negative?
Exercise 27.41 (CorrelationValueQuestion) What do you do if the correlation value is 10?
Exercise 27.42 (CorrelationRangeQuestion) What is the possible range of correlation values?
Exercise 27.43 (CorrelationFormulaQuestion) What is the formula for correlation?
Exercise 27.44 (UnderstandingStatisticalPower) What is the definition of power in a statistical test?
Exercise 27.45 (DistributionEffectOnPower) What is the implication for power analysis if the samples come from the same distribution?
Exercise 27.46 (IncreasingPower) How can you increase the power if the distributions are very similar?
Exercise 27.47 (PreventingPHacking) What should be done to avoid p-hacking when the distributions are close to each other?
Exercise 27.48 (SampleSizeAndPower) If there is overlap and the sample size is small, will the power be high or low?
Exercise 27.49 (FactorsAffectingPower) Which are the two main factors that affect power?
Exercise 27.50 (PurposeOfPowerAnalysis) What does power analysis tell us?
Exercise 27.51 (ExperimentRisks) What are the two risks faced when performing an experiment?
Exercise 27.52 (PerformingPowerAnalysis) How do you perform a power analysis?
Exercise 27.53 (CentralLimitTheoremExplanation) What does the Central Limit Theorem state?
Exercise 27.54 (MedianInBoxplot) What is represented by the middle line in a boxplot?
Exercise 27.55 (BoxContentInBoxplot) What does the box in a boxplot represent?
Exercise 27.56 (RSquaredDefinition) What is R-squared? Show the formula.
Exercise 27.57 (NegativeRSquared) Can the R-squared value be negative?
Exercise 27.58 (RSquaredCalculation) Perform a calculation involving R-squared.
Exercise 27.59 (LeastSquaresMeaning) What is the meaning of the least squares method?
Exercise 27.60 (RegressionVsClassification) What is the difference between regression and classification?
Exercise 27.61 (LikelihoodConcept) What is the idea of likelihood?
Exercise 27.62 (ProbabilityVsLikelihood) What is the difference between probability and likelihood?
Exercise 27.63 (TrainVsTestData) What is the difference between training and testing data?
Exercise 27.64 (SingleValidationIssue) What is the problem if you validate the model only once?
Exercise 27.65 (FoldDefinition) What is a fold in cross-validation?
Exercise 27.66 (LeaveOneOutValidation) What is leave-one-out cross-validation?
Exercise 27.67 (DrawingConfusionMatrix) Draw the confusion matrix.
Exercise 27.68 (SensitivitySpecificityCalculation1) Calculate the sensitivity and specificity for a given confusion matrix.
Exercise 27.69 (SensitivitySpecificityCalculation2) Calculate the sensitivity and specificity for a given confusion matrix.
Exercise 27.70 (BiasAndVariance) What are bias and variance?
Exercise 27.71 (MutualInformationExample) Provide an example and calculate if mutual information is high or low.
Exercise 27.72 (WhatIsPCA) What is PCA?
Exercise 27.73 (ScreePlotExplanation) What is a scree plot?
Exercise 27.74 (LeastSquaresInPCA) Does PCA use least squares?
Exercise 27.75 (PCASteps) Which steps are performed by PCA?
Exercise 27.76 (EigenvaluePC1) What is the eigenvalue of the first principal component?
Exercise 27.77 (DifferencesBetweenPoints) Are the differences between red and yellow the same as the differences between red and blue points?
Exercise 27.78 (ScalingInPCA) How to scale data in PCA?
Exercise 27.79 (DetermineNumberOfComponents) How to determine the number of principal components?
Exercise 27.80 (LimitingNumberOfComponents) How is the number of principal components limited?
Exercise 27.81 (WhyUseTSNE) Why use t-SNE?
Exercise 27.82 (MainIdeaOfTSNE) What is the main idea of t-SNE?
Exercise 27.83 (BasicConceptOfTSNE) What is the basic concept of t-SNE?
Exercise 27.84 (TSNESteps) What are the steps in t-SNE?
Exercise 27.85 (HowKMeansWorks) How does K-means clustering work?
Exercise 27.86 (QualityOfClusters) How can the quality of the resulting clusters be calculated?
Exercise 27.87 (IncreasingK) Why is it not a good idea to increase k too much?
Exercise 27.88 (CorePointInDBSCAN) What is a core point in DBSCAN?
Exercise 27.89 (AddingVsExtending) What is the difference between adding and extending in DBSCAN?
Exercise 27.90 (OutliersInDBSCAN) What are outliers in DBSCAN?
Exercise 27.91 (AdvantagesAndDisadvantagesOfK) What are the advantages and disadvantages of k = 1 and k = 100 in K-nearest neighbors?
Exercise 27.92 (NaiveBayesFormula) What is the formula for Naive Bayes?
Exercise 27.93 (CalculateProbabilities) Calculate the probabilities for a given example using Naive Bayes.
Exercise 27.94 (UnderflowProblem) Why is underflow a problem in Gaussian Naive Bayes?
Exercise 27.95 (Tree Usage) For what can we use trees?
Exercise 27.96 (Tree Usage) Based on a shown tree graph:
- How can you use this tree?
- What is the root node?
- What are branches and internal nodes?
- What are the leafs?
- Are the leafs pure or impure?
- Which of the leafs is more impure?
Exercise 27.97 (Tree Feature Importance) Is the most or least important feature on top?
Exercise 27.98 (Tree Feature Imputation) How can you fill a gap/missing data?
Solution 27.1 (Tree Feature Imputation).
- Mean
- Median
- Comparing to column with high correlation
Exercise 27.99 (Regression Tree Limitations) What are limitations?
Exercise 27.100 (Regression Tree Score) How is the tree score calculated?
Exercise 27.101 (Regression Tree Alpha Value Small) What can we say about the tree if the alpha value is small?
Exercise 27.102 (Regression Tree Increase Alpha Value) What happens if you increase alpha?
Exercise 27.103 (Regression Tree Pruning) What is the meaning of pruning?