Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use scikit learn inverse_transform with new values

I have a set of data that I have used scikit learn PCA. I scaled the data before performing PCA with StandardScaler().

variance_to_retain = 0.99
np_scaled = StandardScaler().fit_transform(df_data)
pca = PCA(n_components=variance_to_retain)
np_pca = pca.fit_transform(np_scaled)

# make dataframe of scaled data
# put column names on scaled data for use later
df_scaled = pd.DataFrame(np_scaled, columns=df_data.columns)
num_components = len(pca.explained_variance_ratio_)
cum_variance_explained = np.cumsum(pca.explained_variance_ratio_)

eigenvalues = pca.explained_variance_
eigenvectors = pca.components_

I then ran K-Means clustering on the scaled dataset. I can plot the cluster centers just fine in scaled space.

My question is: how do I transform the locations of the centers back into the original data space. I know that StandardScaler.fit_transform() make the data have zero mean and unit variance. But with the new points of shape (num_clusters, num_features), can I use inverse_transform(centers) to get the centers transformed back into the range and offset of the original data?

Thanks, David

like image 372
David McCormick Avatar asked Apr 17 '18 18:04

David McCormick


People also ask

What does scaler Inverse_transform do?

scaler remembers that you passed it a 2D input with two columns, and works under the assumption that all subsequent data passed to it will have the same number of features/columns. If it's only the first column you want, you will still need to pass inverse_transform an input with the same number of columns.

What is difference between Fit_transform and transform?

The fit(data) method is used to compute the mean and std dev for a given feature to be used further for scaling. The transform(data) method is used to perform scaling using mean and std dev calculated using the . fit() method. The fit_transform() method does both fits and transform.


1 Answers

you can get cluster_centers on a kmeans, and just push that into your pca.inverse_transform

here's an example

import numpy as np
from sklearn import decomposition
from sklearn import datasets
from sklearn.cluster import KMeans
from sklearn.preprocessing import StandardScaler



iris = datasets.load_iris()
X = iris.data
y = iris.target

scal = StandardScaler()
X_t = scal.fit_transform(X)

pca = decomposition.PCA(n_components=3)
pca.fit(X_t)
X_t = pca.transform(X_t)

clf = KMeans(n_clusters=3)
clf.fit(X_t)

scal.inverse_transform(pca.inverse_transform(clf.cluster_centers_))

Note that sklearn has multiple ways to do the fit/transform. You can do StandardScaler().fit_transform(X) but you lose the scaler, and can't reuse it; nor can you use it to create an inverse.

Alternatively, you can do scal = StandardScaler() followed by scal.fit(X) and then by scal.transform(X)

OR you can do scal.fit_transform(X) which combines the fit/transform step

like image 143
Mohammad Athar Avatar answered Sep 28 '22 17:09

Mohammad Athar