9951 explained code solutions for 126 technologies


python-scikit-learnUsing SVD to reduce dimensions example


from sklearn import decomposition, datasets

X, y = datasets.load_iris(return_X_y=True)

svd = decomposition.TruncatedSVD(n_components=2)
svd.fit(X)
X = svd.transform(X)ctrl + c
from sklearn import

import module from scikit-learn

load_iris

loads Iris dataset

.TruncatedSVD(

creates dimensionality reduction model based on truncated SVD

.fit(

train reduction model

.transform(

transform original data and return reduced dimensions data


Usage example

from sklearn import decomposition, datasets

X, y = datasets.load_iris(return_X_y=True)
print('Original:', X.shape)

svd = decomposition.TruncatedSVD(n_components=2)
svd.fit(X)
X = svd.transform(X)

print('Reduced: ', X.shape)
output
Original: (150, 4)
Reduced:  (150, 2)