1

当我试图了解 LSA 时,我发现我无法使用 SVD 从 TruncatedSVD 重现结果。为什么这不起作用。

谢谢您的帮助。

import pandas as pd
import numpy as np
from sklearn.decomposition import TruncatedSVD

# Iris data
df = pd.read_csv(filepath_or_buffer='https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data', header=None, sep=',')
df.columns=['sepal_len', 'sepal_wid', 'petal_len', 'petal_wid', 'class']
df.dropna(how="all", inplace=True)
X = df.ix[:,0:4].values

# TruncatedSVD
lsa = TruncatedSVD(n_components=3)
X_proj = lsa.fit_transform(X)
#print(lsa.singular_values_)                  <------- error
print('Components:\n', lsa.components_)
print('Explained variance:\n', lsa.explained_variance_, '\n')

# SVD
u, s, vt = np.linalg.svd(X)
print('Components:\n', vt)
exp_var = (s**2) / (X.shape[0]-1)
print('Explained variance:\n', exp_var)
4

0 回答 0