Does the following passage mention that the parameters 'perplexity=30, max_iter=300, random_state=0' should be given to 'TSNE'?
0 - Introduction
Welcome to the Foundations of Machine Learning (ECE5984_41) course!
This is the 4th lab practice. From now on, you will going to implement PCA, t-SNE
1. Packages
You have to install and use below packages for HW#4.
numpy: Fundamental package for matrix computation with python.
matplotlib: Package for visualization of the graph with python.
scikit-learn: Python module for machine learning built on top of SciPy
Do not use other machine learning packages in this homework, e.g., tensorflow, pytorch, jax, etc.And partial use of scikit-learn is permitted.
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as mplcolors
import sklearn.datasets as skdatasets
2. PCA(Principle Component Analysis)
2.1. Load iris dataset
def load_iris_dataset():
# import data
iris = skdatasets.load_iris()
# only take the first two features
X = iris.data[:, :2]
y = iris.target
return X, y
2.2. Implement a function for PCA (Principal Component Analysis).
def sort_eigenvalues_and_eigenvectors(eigenvalues, eigenvectors):
###################################################################################
# TODO: Implement the Sort eigenvalues and eigenvectors function using numpy #
###################################################################################
# ************* START OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE) ************* #
code
# ************* END OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE) ************* #
return sorted_eigenvalues, sorted_eigenvectors
Standardize the dataset
def standardize_data(X):
#########################################################################
# TODO: Implement the Standardize the dataset function using numpy #
#########################################################################
# ******** START OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE) ******** #
Assistant
The passage provided does not explicitly mention that the parameters perplexity=30, max_iter=300, random_state=0 should be given to TSNE. It outlines various segments on implementing PCA (Principal Component Analysis) and t-SNE (t-distributed Stochastic Neighbor Embedding) but there are placeholders for where these implementations should be, indicated by comments like # ******** START OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE) ********* #.
For t-SNE, it specifies a function shell for t_sne(X, n_componets=2) with comments indicating that t-SNE should be applied using scikit-learn, but no specific parameters are provided within these comments or the subsequent code sections. Here is what is outlined:
python
code
# ********* END OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE) ********* #
return X_standardized
Compute eigenvalues and eigenvectors
def compute_eigenvalues_and_eigenvectors(X):
######################################################################################
# TODO: Implement the Compute eigenvalues and eigenvectors function using numpy #
######################################################################################
# ************** START OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE) *************** #
code
# ************** END OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE) *************** #
return eigenvalues, eigenvectors
Transform the data
def transform_data(X, eigenvectors):
################################################################
# TODO: Implement the transform data function using numpy #
################################################################
# **** START OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE) **** #
code
# ***** END OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE) ***** #
return X_transformed
2.3. Implement PCA & Explained variance
def My_PCA(X, components=2):
##########################################################################
# TODO: Implement PCA using the function implemented in section 2.2 #
##########################################################################
# ******** START OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE) ********* #
code
# ********* END OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE) ********** #
return X_pca
2.4 Do PCA & Visulaize the result
X, y = load_iris_dataset()
X_pca = My_PCA(X, components=3)
def visualize_PCA_result(X_transformed, y):
# Use different colors for different classes
colors = ['r', 'b']
for i in range(len(colors)):
plt.scatter(X_transformed[y == i, 0], X_transformed[y == i, 1], c=colors[i], label=f'Class {i}')
plt.xlabel('Principal Component 1')
plt.ylabel('Principal Component 2')
plt.title('PCA Result')
plt.legend()
plt.show()
visualize_PCA_result(X_pca, y)
3. t-SNE
import matplotlib.pyplot as plt
import numpy as np
from sklearn import datasets
from sklearn.decomposition import PCA
from sklearn.manifold import TSNE
3.1 data load
import the dataset
iris = datasets.load_iris()
X = iris.data
y = iris.target
3.2 Apply PCA & t-SNE using scikit-learn
Apply PCA
def pca(X, n_componets=2):
##########################################################################
# TODO: Apply PCA using sckit-learn #
##########################################################################
# ******** START OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE) ********* #
code
# ******** END OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE) ********** #
return X_PCA
Apply t-SNE
def t_sne(X, n_componets=2):
##########################################################################
# TODO: Apply t-SNE using sckit-learn #
##########################################################################
# ******** START OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE) ********* #
code
# ******** END OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE) ********** #
return X_tsne
# Apply t-SNE
def t_sne(X, n_componets=2):
##########################################################################
# TODO: Apply t-SNE using sckit-learn #
##########################################################################
# ******** START OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE) ********* #
# ******** END OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE) ********** #
return X_tsne
Therefore, to summarize, the parameters perplexity=30, max_iter=300, random_state=0 are not mentioned anywhere in the passage.