Untitled
unknown
python
2 years ago
1.7 kB
9
Indexable
class MY_SparsePCA():
def __init__(self, n_components, alpha, max_iter=1000, tol=1e-6):
self.alpha = alpha
self.n_components = n_components
self.max_iter = max_iter
self.tol = tol
self.components_ = None
self.error_ = None
self.S = None
self.Vt = None
@staticmethod
def soft_threshold(x, alpha):
# GRADED CODE: Iterative Thresholding
### START CODE HERE ###
# Soft threshold function used for SPCA.
rt = None
### END CODE HERE ###
return rt
def fit_transform(self, x):
# GRADED CODE: SPARSE PCA FITTINT FUNCTION
### START CODE HERE ###
# Initialize components (5%)
_, S, Vt = np.linalg.svd(x, full_matrices=False)
self.components_ = None
self.S = S
self.Vt = Vt
for iteration in tqdm(range(self.max_iter)):
# Compute the transform in this iteration
x_projected = None
# Iterative Thresholding (update the components) (5%):
self.components_ = None
# Normalize components (5%)
norm = None
norm[norm < 1e-10] = 1.0
self.components_ /= None
# SSD Check
ssd = None
if ssd < self.tol:
break
# Transform the data (5%)
x_transformed = None
### END CODE HERE###
return x_transformed
def fit(self, x):
self.fit_transform(x)
return self
def transform(self, x):
return x@self.components_Editor is loading...
Leave a Comment