代做ECE 219 Winter 2025 Project 2: Data Representations and Clustering代写Python语言

Large-Scale Data Mining:  Models and Algorithms

ECE 219 Winter 2025

Project 2: Data Representations and Clustering

Due February 07, 2025 by 11:59 pm

Introduction

Machine learning algorithms are applied to a wide variety of data, including text and images. Before applying these algorithms, one needs to convert the raw data into feature representa- tions that are suitable for downstream algorithms. In project 1, we studied feature extraction from text data, and the downstream task of classification.  We also learned that reducing the dimension of the extracted features often helps with a downstream task.

In this project, we explore the concepts of feature extraction and clustering together.  In an ideal world, all we need are data points – encoded using certain features– and AI should be able to find what is important to learn, or more specifically, determine what are the underlying modes or categories in the dataset.  This is the ultimate goal of General AI: the machine is able to bootstrap a knowledge base, acting as its own teacher and interacting with the outside world to explore to be able to operate autonomously in an environment.

We first explore this field of unsupervised learning using textual data, which is a continuation of concepts learned in Project 1. We ask if a combination of feature engineering and clustering techniques can automatically separate a document set into groups that match known labels.

Next we focus on a new type of data, i.e.   images.   Specifically,  we first explore how to use “deep learning” or “deep neural networks (DNNs)” to obtain image features.  Large neural networks have been trained on huge labeled image datasets to recognize objects of different types from images.  For example, networks trained on the Imagenet dataset can classify more than one thousand different categories of objects.  Such networks can be viewed as comprising two parts:  the first part maps a given RGB image into a feature vector using convolutional filters, and the second part then classifies this feature vector into an appropriate category, using a fully-connected multi-layered neural network (we will study such NNs in a later lecture).  Such pre-trained networks could be considered as experienced agents that have learned to discover features that are salient for image understanding.   Can one use the experience of such pre- trained agents in understanding new images that the machine has never seen before?  It is akin to asking a human expert on forensics to explore a new crime scene.  One would expect such an expert to be able to  transfer their domain knowledge into a new scenario.   In a similar vein, can a pre-trained network for image understanding be used for transfer learning? One could use the output of the network in the last few layers as expert features.  Then, given a multi-modal dataset –consisting of images from categories that the DNN was not trained for– one can use feature engineering (such as dimensionality reduction) and clustering algorithms to automatically extract unlabeled categories from such expert features.

For both the text and image data, one can use a common set of multiple evaluation metrics to compare the groups extracted by the unsupervised learning algorithms to the corresponding ground truth human labels.

Clustering Methods

Clustering is the task of grouping a dataset in such a way that data points in the same group (called a cluster) are more similar (in some sense) to each other than to those in other groups (clusters). Thus, there is an inherent notion of a metric that is used to compute similarity among data points, and different clustering algorithms differ in the type of similarity measure they use, e.g., Euclidean vs Riemannian geometry.  Clustering algorithms are considered  “unsupervised learning”,i.e. they do not require labels during training. In principle, if two categories of objects or concepts are distinct from some perspective (e.g. visual or functional), then data points from these two categories – when properly coded in a feature space and augmented with an associated distance metric – should form. distinct clusters.  Thus, if one can perform. perfect clustering then one can discover and obtain computational characterizations of categories without any labeling. In practice, however, finding such optimal choices of features and metrics has proven to be a computationally intractable task, and any clustering result needs to be validated against tasks for which one can measure performance.  Thus, we use labeled datasets in this project, which allows us to evaluate the learned clusters by comparing them with ground truth labels.

Below, we summarize several clustering algorithms:

K-means: K-means clustering is a simple and popular clustering algorithm.  Given a set of data points {x1,..., xN } in multidimensional space, and a hyperparameter K denoting the number of clusters, the algorithm finds the K cluster centers such that each data point belongs to exactly one cluster. This cluster membership is found by minimizing the sum of the squares of the distances between each data point and the center of the cluster it belongs to. If we define μk  to be the “center” of the kth cluster, and

Then our goal is to find rnk ’s and μk ’s that minimize  The approach of K-means algorithm is to repeatedly perform the following two steps until convergence:

1.  (Re)assign each data point to the cluster whose center is nearest to the data point.

2.  (Re)calculate the position of the centers of the clusters:  setting the center of the cluster to the mean of the data points that are currently within the cluster.

The center positions may be initialized randomly.

Hierarchical Clustering Hierarchical clustering is a general family of clustering algorithms that builds nested clusters by merging or splitting them successively.  This hierarchy of clusters is represented as a tree (or dendrogram).  A flat clustering result is obtained by cutting the dendrogram at a level that yields a desired number of clusters.

DBSCAN DBSCAN or Density-Based Spatial Clustering of Applications with Noise findscore samples of high density and expands clusters from them. It is a density-based clustering non-parametric algorithm:  Given  a set of points, the algorithm groups together points that are closely packed together (points with many nearby neighbors), marking as outliers points that lie alone in low-density regions (whose nearest neighbors are too far away).

HDBSCAN HDBSCAN extends DBSCAN by converting it into a hierarchical clustering al- gorithm, and then using an empirical technique to extract a flat clustering based on the stability of clusters (similar to the elbow method in k-Means).  The resulting algorithm gets rid of the hyperparameter  “epsilon”, which is necessary in DBSCAN  (see here for more on that).

Common Clustering Evaluation Metrics

In order to evaluate a clustering pipeline, one can use the ground-truth class labels and compare them with the cluster labels.  This analysis determines the quality of the clustering algorithm in recovering the ground-truth underlying labels.  It also indicates if the adopted feature extrac- tion and dimensionality reduction methods retain enough information about the ground-truth classes. Below we provide several evaluation metrics available in sklearn . metrics. Note that for the  clustering sub-tasks,  you  do  not  need to  separate your data to  training  and test sets. Question 25 requires you to split the data.

Homogeneity is a measure of how  “pure” the clusters are.  If each cluster contains only data points from a single class, the homogeneity is satisfied.

Completeness indicates how much of the data points of a class are assigned to the same cluster.

V-measure is the harmonic average of homogeneity score and completeness score.

Adjusted Rand Index is similar to accuracy, which computes similarity between the clus- tering labels and ground truth labels. This method counts all pairs of points that both fall either in the same cluster and the same class or in different clusters and different classes.

Adjusted mutual information score measures the mutual information between the cluster label distribution and the ground truth label distributions.

Dimensionality Reduction Methods

In project 1, we studied SVD/PCA and NMF as linear dimensionality reduction techniques. Here, we consider some additional non-linear methods.

Uniform Manifold Approximation and Projection  (UMAP) The UMAP algorithm constructs a graph-based representation of the high-dimensional data manifold, and learns a low-dimensional representation space based on the relative inter-point distances.  UMAP allows more choices of distance metrics besides Euclidean distance. In particular, we are interested  in  “cosine  distance”  for  text  data,  because  as  we  shall  see  it  bypasses  the magnitude of the vectors, meaning that the length of the documents does not affect the distance metric.

Autoencoders An autoencoder  is a special type of neural network that is trained to copy its input to its output. For example, given an image of a handwritten digit, an autoencoder first encodes the image into a lower dimensional latent representation, then decodes the latent representation back to animage.  An autoencoder learns to compress the data while minimizing the reconstruction error. Further details can be found in chapter 14 of [4].

Part 1 - Clustering on Text Data

In this part of the project, we will work with “20 Newsgroups” dataset, which is a collection of approximately 20, 000 documents, partitioned (nearly) evenly across 20 different newsgroups (newsgroups are discussion groups like forums, which originated during the early age of the Internet), each corresponding to a different topic.  Use the fetch 20newsgroups function from scikit-learn to load the dataset.  Detailed usage of the dataset and sample code can be found at this link.

To get started with asimple clustering task, we work with a well-separable subset of samples from the larger dataset. Specifically, we define two classes comprising of the following categories.

Clustering with Sparse Text Representations

1. Generate sparse TF-IDF representations:   Following the steps in Project 1, trans- form the documents into TF-IDF vectors.  Use min df  =  3, exclude the stopwords  (no need to do stemming or lemmatization), and remove the headers and footers. No need to do any additional data cleaning.

QUESTION 1: Report the dimensions of the TF-IDF matrix you obtain.

2. Clustering:  Apply K-means clustering with k = 2 using the TF-IDF data.  Note that the KMeans class in sklearn has parameters named random   state, max   iter and n   init. Please use random   state=0, max   iter ≥ 1000 and n   init ≥ 30. You can refer to sklearn - Clustering text documents using k-means for a basic work flow.

(a) Given the clustering result and ground truth labels, contingency table A is the matrix whose entries Aij  is the number of data points that belong to the i’th class and the j’th cluster.

QUESTION 2: Report the contingency table of your clustering result.  You  may use the provided plotmat . py to visualize the matrix.  Does the contingency matrix have to be square-shaped?

QUESTION 3: Report the 5 clustering measures explained in the introduction for K- means clustering.

Clustering with Dense Text Representations

As you may have observed, high-dimensional sparse TF-IDF vectors do not yield a good clus- tering result, especially when K-Means clustering is used.   One  of the  reasons is that in  a high-dimensional space, the Euclidean distance is not a good metric anymore, in the sense that the distances between data points tends to be almost the same (see [1]).

K-means clustering has other limitations.   Since its objective is to minimize the sum of  within-cluster l2  distances, it implicitly assumes that the clusters are isotropically shaped, i.e. round-shaped.   When  the clusters  are not round-shaped,  K-means may fail to identify the clusters properly.  Even when the clusters are round, K-means algorithm may also fail when the clusters have unequal variances. A direct visualization for these problems can be found at sklearn - Demonstration of k-means assumptions.

In this part we try to find a  “better” representation tailored to the performance of the downstream task of K-means clustering. Towards finding a better representation, we can reduce the dimension of our data with different methods before clustering.

1. Generate dense representations for better K-Means Clustering

(a) First we want to find the effective dimension of the data through inspection of the top singular values of the TF-IDF matrix and see how many of them are significant in reconstructing the matrix with the truncated SVD representation. A guideline is to see what ratio of the variance of the original data is retained after the dimensionality reduction.

QUESTION 4: Report the plot of the percentage of variance that the top r  principle components retain v.s. r, for r = 1 to 1000.

Hint: explained variance ratio   of TruncatedSVD objects.  See sklearn document. (b) Now, use the following two methods to reduce the dimension of the data.  Sweep over

the dimension parameters for each method, and choose one that yields better results in terms of clustering purity metrics.

 Truncated SVD / PCA

Note that you don’t need to perform SVD multiple times:  performing SVD with r = 1000 gives you the data projected on all the top 1000 principle components, so for smaller r’s, you just need to exclude the least important features.

 NMF

QUESTION 5:

Let r be the dimension that we want to reduce the data to (i.e. n   components).

Try r = 1 10, 20, 50, 100, 300, and plot the 5 measure scores v.s.  r for both SVD and NMF.

Report a good choice of r for SVD and NMF respectively.

Note:  In the choice of r, there  is a trade-off between the  information preservation, and better performance of k-means in lower dimensions.

QUESTION 6: How do you explain the non-monotonic behavior of the measures as r increases?

QUESTION 7: Are these measures on average better than those computed in Question 3?

2. Visualize the clusters

We can visualize the clustering results by projecting the dimension-reduced data points onto a 2-D plane by once again using SVD, and coloring the points according to the:

 Ground truth class label;

• Clustering label respectively.

QUESTION 8: Visualize the clustering results for:

 SVD with your optimal choice of r for K-Means clustering;

•  NMF with your choice of r for K-Means clustering.

To  recap, you can accomplish this  by first creating the dense  representations and then once again  projecting these representations into a 2-D plane for visualization.

QUESTION 9: What do you observe in the visualization?  How are the data points of the two classes distributed?  Is distribution of the data ideal for  K-Means clustering?

3. Clustering of the Entire 20 Classes

We have been dealing with a relatively simple clustering task with only two well-separated classes.  Now let’s face a more challenging one:  clustering for the entire 20 categories in the 20newsgroups dataset.

QUESTION  10: Load documents with the same configuration as  in Question 1,  but for ALL 20 categories. Construct the TF-IDF matrix, reduce its dimensionality using BOTH NMF and SVD (specify settings you choose and why), and perform K-Means clustering with  k=20 .

Visualize the contingency matrix and report the five clustering metrics (DO BOTH NMF AND SVD).

There is a mismatch between cluster labels and class labels.  For example, the cluster #3 may correspond to the class #8.  As a result, the high-value entries of the 20 × 20 contingency matrix can be scattered around, making it messy to inspect, even if the clustering result is not bad.

One  can  use   scipy . optimize . linear_sum_assignment   to  identify  the  best-matching cluster-class pairs, and permute the columns of the contingency matrix accordingly.  See below for an example:

import  numpy  as  np

from plotmat  import plot_mat  #  using  the  provided plotmat.py

from  scipy . optimize  import  linear_sum_assignment

from  sklearn . metrics  import  confusion_matrix

cm  =  confusion_matrix(labels,  clustering_labels)

rows,  cols  =  linear_sum_assignment(cm, maximize=True)

plot_mat(cm[rows[:, np . newaxis],  cols],  xticklabels=cols,

→      yticklabels=rows,  size=(15 ,15))

4. UMAP

The clustering performance is poor for the 20 categories data.  To see if we can improve this performance, we consider UMAP for dimensionality reduction.  UMAP uses cosine distances to compare representations.  Consider two documents that are about the same topic and are similar, but one is very long while the other is short.  The magnitude of the TF-IDF vector will be high for the long document and low for the short one, even though the orientation of their TF-IDF vectors might be very close.  In this case, the cosine distance adopted by UMAP will correctly identify the similarity, whereas Euclidean distance might fail.

QUESTION 11: Reduce the dimension of your dataset with UMAP. Consider the following settings: n   components = [5, 20, 200], metric = ”cosine” vs. ”euclidean” .  If ”cosine” metric fails, please look at the FAQ at the end of this spec.

Report the permuted contingency matrix and the five clustering evaluation metrics for the different combinations (6 combinations).

QUESTION  12: Analyze the contingency  matrices.  Which setting works best and why? What about for each metric choice?

QUESTION  13: So far, we have attempted K-Means clustering with 4 different representation learning techniques (sparse TF-IDF representation, PCA-reduced, NMF-reduced, UMAP-reduced). Compare and contrast the clustering results across the 4 choices, and suggest an approach that is best for the K-Means clustering task on the 20-class text data.  Choose any choice of clustering metrics for your comparison.

Clustering Algorithms that do not explicitly rely on the Gaussian distribution per cluster

While we have successfully shown in the previous section that some representation learning techniques perform better than others for the task of K-Means clustering on this text dataset, this sweep only covers a half of the end-to-end solution for representation learning.  What if we changed the clustering method?  In this section we introduce 2 additional clustering algorithms.

1. Agglomerative Clustering

The AgglomerativeClusteringobject performs a hierarchical clustering using a bottom up approach:  each observation starts in its own cluster,  and  clusters  are successively merged together. There are 4 linkage criteria that determines the merge strategy.

QUESTION  14: Use UMAP to reduce the dimensionality properly, and perform Agglom- erative clustering with  n_clusters=20 .  Compare the  performance of  “ward” and  “single” linkage criteria.

Report the five clustering evaluation metrics for each case.

2. HDBSCAN

QUESTION 15: Apply HDBSCAN on UMAP-transformed 20-category data. Use  min_cluster_size=100 .

Vary the min cluster size among 20, 100, 200 and report your findings in terms of the five clustering evaluation metrics - you will plot the best contingency matrix in the next question.   Feel  free to try  modifying other  parameters  in  HDBSCAN to get better performance.

QUESTION 16: Contingency matrix

Plot the contingency matrix for the best clustering model from Question 15.

How many clusters are given by the model? What does  “-1” mean for the clustering labels?

Interpret the contingency matrix considering the answer to these questions.

QUESTION 17: Based on your experiments, which dimensionality reduction technique and clus- tering methods worked best together for 20-class text data and why?  Follow the table below.  If UMAP takes too long to converge, consider running it once and saving the intermediate results in a pickle file.

Hint:  DBSCAN and  HDBSCAN do not accept the number of clusters as an input parameter.  So pay close attention to how the different clustering metrics are being computed for these methods.

QUESTION  18: Extra credit:   If you can find creative ways to further enhance the clustering performance, report your method and the results you obtain.

Part 2 - Deep Learning and Clustering of Image Data

In this part, we aim to cluster the images of the tf flowers dataset. This dataset consists of images of five types of flowers.  Explore this link to see actual samples of the data.

Extracting meaningful features from images has along history in computer vision. Instead of considering the raw pixel values as features, researchers have explored various hand-engineered feature extraction methods, e.g.  [5]. With the recent rise of “deep learning”, these methods are replaced with using appropriate neural networks. Particularly, one can adopt a neural network already trained to classify another large dataset of images.  These pre-trained networks have been trained to morph the highly non-smooth scatter of images in the higher dimension, into smooth lower-dimensional manifolds.

In this project, we use a VGG network  [6] pre-trained on the ImageNet dataset  [7].  We provide a  helper  codebase  (check Week 4  in BruinLearn), which guides you through the necessary steps for loading the VGG network and for using it for feature extraction.

QUESTION  19: In a brief paragraph discuss:  If the VGG  network  is trained on a dataset with perhaps totally different classes as targets, why would one expect the features derived from such a network to have discriminative power for a custom dataset?

Use the helper code to load the flowers dataset, and extract their features.   To perform computations on deep neural networks fast enough, GPU resources are often required.  GPU resources can be freely accessed through “Google Colab” .

QUESTION  20: In  a  brief  paragraph explain  how the  helper  code  base  is  performing  feature extraction.

QUESTION  21: How  many  pixels are there  in the original images?   How  many features  does the VGG network extract per image; i.e what is the dimension of each feature vector for an image sample?

QUESTION  22: Are  the  extracted  features  dense  or  sparse?   (Compare  with  sparse  TF-IDF features in text.)

QUESTION 23: In order to inspect the high-dimensional features,t-SNE is a popular off-the-shelf choice for visualizing Vision features.  Map the features you have extracted onto 2 dimensions with t-SNE. Then plot the mapped feature vectors along x and y axes.  Color-code the data points with ground-truth labels.  Describe your observation.

While PCA is a powerful method for dimensionality reduction, it is limited to  “linear” transformations.  This might not be particularly good if a dataset is distributed non-linearly. An alternative approach is use of an “autoencoder” or UMAP. The helper has implemented an autoencoder which is ready to use.

QUESTION 24: Report the best result (in terms of rand score) within the table below.

For HDBSCAN, introduce a conservative parameter grid over min   cluster   size and min   samples.

Lastly, we can conduct an experiment to ensure that VGG features are rich enough in information about the data classes.  In particular, we can train a fully-connected neural network classifier to predict the labels of data. For this task, you may use the MLP  module provided in the helper code base.

QUESTION  25: Report the test accuracy of the MLP classifier on the original VGG features. Report the same when  using the  reduced-dimension features  (you  have  freedom  in  choosing the dimensionality reduction algorithm and its parameters).  Does the  performance of the model suffer with the  reduced-dimension  representations?   Is  it  significant?   Does  the  success  in  classification make sense in the context of the clustering results obtained for the same features in Question 24.

Part 3 - Clustering using both image and text

In part  1  and  part  2,  we  have  practived  the  art of clustering text  and images separately. However, can we map image and text to the same space?   In the Pokemon world, Pokedex catalogs Pokemon’s appearances and various metadata. We will build our Pokedex from image dataset link and meta metadata link. Fortunately, ECE 219 Gym kindly provides new Pokemon trainers with the helper code for data preprocessing and inferencing.  Please find the code on Bruinlearn modules Week 4.

Each Pok´emon may be represented by multiple images and up to two types (for example, Bulbasaur is categorized as both Grass and Poison types).  In this section, we will focus on the first image (named 0.jpg) in each folder for our analysis.

We will use the pre-trained CLIP [8] to illustrate the idea of multimodal clustering.  CLIP (Contrastive Language–Image Pretraining) is an innovative model developed by OpenAI, de- signed to understand and connect concepts from both text and images.  CLIP is trained on a vast array of internet-sourced text-image pairs.  This extensive training enables the model to understand a broad spectrum of visual concepts and their textual descriptions. 

Figure 1: CLIP training summary

CLIP consists of two primary components: a text encoder and an image encoder. The text encoder processes textual data, converting sentences and phrases into numerical representa- tions.   Simultaneously, the image encoder transforms visual inputs into a corresponding set of numerical values.  These encoders are trained to map both text and images into a shared embedding space, allowing the model to compare and relate the two different types of data di- rectly. The training employs a contrastive learning approach, where the model learns to match corresponding text and image pairs against numerous non-matching pairs.  This approach helps the model in accurately associating images with their relevant textual descriptions and vice versa.


热门主题

课程名

mktg2509 csci 2600 38170 lng302 csse3010 phas3226 77938 arch1162 engn4536/engn6536 acx5903 comp151101 phl245 cse12 comp9312 stat3016/6016 phas0038 comp2140 6qqmb312 xjco3011 rest0005 ematm0051 5qqmn219 lubs5062m eee8155 cege0100 eap033 artd1109 mat246 etc3430 ecmm462 mis102 inft6800 ddes9903 comp6521 comp9517 comp3331/9331 comp4337 comp6008 comp9414 bu.231.790.81 man00150m csb352h math1041 eengm4100 isys1002 08 6057cem mktg3504 mthm036 mtrx1701 mth3241 eeee3086 cmp-7038b cmp-7000a ints4010 econ2151 infs5710 fins5516 fin3309 fins5510 gsoe9340 math2007 math2036 soee5010 mark3088 infs3605 elec9714 comp2271 ma214 comp2211 infs3604 600426 sit254 acct3091 bbt405 msin0116 com107/com113 mark5826 sit120 comp9021 eco2101 eeen40700 cs253 ece3114 ecmm447 chns3000 math377 itd102 comp9444 comp(2041|9044) econ0060 econ7230 mgt001371 ecs-323 cs6250 mgdi60012 mdia2012 comm221001 comm5000 ma1008 engl642 econ241 com333 math367 mis201 nbs-7041x meek16104 econ2003 comm1190 mbas902 comp-1027 dpst1091 comp7315 eppd1033 m06 ee3025 msci231 bb113/bbs1063 fc709 comp3425 comp9417 econ42915 cb9101 math1102e chme0017 fc307 mkt60104 5522usst litr1-uc6201.200 ee1102 cosc2803 math39512 omp9727 int2067/int5051 bsb151 mgt253 fc021 babs2202 mis2002s phya21 18-213 cege0012 mdia1002 math38032 mech5125 07 cisc102 mgx3110 cs240 11175 fin3020s eco3420 ictten622 comp9727 cpt111 de114102d mgm320h5s bafi1019 math21112 efim20036 mn-3503 fins5568 110.807 bcpm000028 info6030 bma0092 bcpm0054 math20212 ce335 cs365 cenv6141 ftec5580 math2010 ec3450 comm1170 ecmt1010 csci-ua.0480-003 econ12-200 ib3960 ectb60h3f cs247—assignment tk3163 ics3u ib3j80 comp20008 comp9334 eppd1063 acct2343 cct109 isys1055/3412 math350-real math2014 eec180 stat141b econ2101 msinm014/msing014/msing014b fit2004 comp643 bu1002 cm2030
联系我们
EMail: 99515681@qq.com
QQ: 99515681
留学生作业帮-留学生的知心伴侣!
工作时间:08:00-21:00
python代写
微信客服:codinghelp
站长地图