本文整理汇总了Python中sklearn.decomposition.RandomizedPCA.inverse_transform方法的典型用法代码示例。如果您正苦于以下问题:Python RandomizedPCA.inverse_transform方法的具体用法?Python RandomizedPCA.inverse_transform怎么用?Python RandomizedPCA.inverse_transform使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.decomposition.RandomizedPCA
的用法示例。
在下文中一共展示了RandomizedPCA.inverse_transform方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_sparse_randomized_pca_inverse
# 需要导入模块: from sklearn.decomposition import RandomizedPCA [as 别名]
# 或者: from sklearn.decomposition.RandomizedPCA import inverse_transform [as 别名]
def test_sparse_randomized_pca_inverse():
"""Test that RandomizedPCA is inversible on sparse data"""
rng = np.random.RandomState(0)
n, p = 50, 3
X = rng.randn(n, p) # spherical data
X[:, 1] *= 0.00001 # make middle component relatively small
# no large means because the sparse version of randomized pca does not do
# centering to avoid breaking the sparsity
X = csr_matrix(X)
# same check that we can find the original data from the transformed signal
# (since the data is almost of rank n_components)
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always", DeprecationWarning)
pca = RandomizedPCA(n_components=2, random_state=0).fit(X)
assert_equal(len(w), 1)
assert_equal(w[0].category, DeprecationWarning)
Y = pca.transform(X)
Y_inverse = pca.inverse_transform(Y)
assert_almost_equal(X.todense(), Y_inverse, decimal=2)
# same as above with whitening (approximate reconstruction)
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always", DeprecationWarning)
pca = RandomizedPCA(n_components=2, whiten=True, random_state=0).fit(X)
assert_equal(len(w), 1)
assert_equal(w[0].category, DeprecationWarning)
Y = pca.transform(X)
Y_inverse = pca.inverse_transform(Y)
relative_max_delta = (np.abs(X.todense() - Y_inverse) / np.abs(X).mean()).max()
# XXX: this does not seam to work as expected:
assert_almost_equal(relative_max_delta, 0.91, decimal=2)
示例2: test_randomized_pca_inverse
# 需要导入模块: from sklearn.decomposition import RandomizedPCA [as 别名]
# 或者: from sklearn.decomposition.RandomizedPCA import inverse_transform [as 别名]
def test_randomized_pca_inverse():
"""Test that RandomizedPCA is inversible on dense data"""
rng = np.random.RandomState(0)
n, p = 50, 3
X = rng.randn(n, p) # spherical data
X[:, 1] *= 0.00001 # make middle component relatively small
X += [5, 4, 3] # make a large mean
# same check that we can find the original data from the transformed signal
# (since the data is almost of rank n_components)
pca = RandomizedPCA(n_components=2, random_state=0).fit(X)
Y = pca.transform(X)
Y_inverse = pca.inverse_transform(Y)
assert_almost_equal(X, Y_inverse, decimal=2)
# same as above with whitening (approximate reconstruction)
pca = RandomizedPCA(n_components=2, whiten=True, random_state=0).fit(X)
Y = pca.transform(X)
Y_inverse = pca.inverse_transform(Y)
relative_max_delta = (np.abs(X - Y_inverse) / np.abs(X).mean()).max()
assert_almost_equal(relative_max_delta, 0.11, decimal=2)
示例3: gap_statistic
# 需要导入模块: from sklearn.decomposition import RandomizedPCA [as 别名]
# 或者: from sklearn.decomposition.RandomizedPCA import inverse_transform [as 别名]
def gap_statistic(x, random_datasets=64):
"""
Returns the gap statistic of the data set. Keeps increasing the number of clusters until the maximum gap statistic is more than double the current gap statistic.
http://blog.echen.me/2011/03/19/counting-clusters/
"""
assert isinstance(x, np.ndarray)
assert len(x.shape) == 2
if x.shape > GAP_STATISTIC.RANDOMIZED_PCA_THRESHOLD:
pca = RandomizedPCA(GAP_STATISTIC.RANDOMIZED_PCA_THRESHOLD)
else:
pca = PCA()
pca.fit(x)
transformed = pca.transform(x)
reference_datasets = [pca.inverse_transform(generate_random_dataset(transformed)) for _ in range(random_datasets)]
max_gap_statistic = -1
best_num_clusters = 1
for num_clusters in range(1, x.shape[0] + 1):
kmeans = MiniBatchKMeans(num_clusters)
kmeans.fit(x)
trained_dispersion = dispersion(kmeans, x)
random_dispersions = [dispersion(kmeans, data) for data in reference_datasets]
gap_statistic = np.log(sum(random_dispersions) / random_datasets) - np.log(trained_dispersion)
if gap_statistic > max_gap_statistic:
max_gap_statistic = gap_statistic
best_num_clusters = num_clusters
if gap_statistic < max_gap_statistic * GAP_STATISTIC.MAXIMUM_DECLINE:
break
if num_clusters > best_num_clusters + GAP_STATISTIC.NUM_CLUSTERS_WITHOUT_IMPROVEMENT:
break
return best_num_clusters
示例4: plotImageGrid
# 需要导入模块: from sklearn.decomposition import RandomizedPCA [as 别名]
# 或者: from sklearn.decomposition.RandomizedPCA import inverse_transform [as 别名]
plotImageGrid(X[sample_patches_ind, ...],
image_size=(patch_size, patch_size, 3), nrow=6, ncol=6)
#plt.savefig('patches16.png')
plt.show()
#perform whitening
# 590 components = 99% explained varience
pca = RandomizedPCA(n_components=590, whiten=True, random_state=seed)
w_X = pca.fit_transform(X)
print("==== PCA fitted =====")
print("variance explained:")
print(pca.explained_variance_ratio_)
#plot whitened patches after inverse transform
orig_X = pca.inverse_transform(w_X[sample_patches_ind, ...])
plotImageGrid(orig_X, image_size=(patch_size, patch_size, 3), nrow=6, ncol=6)
#plt.savefig('patches_whitened16.png')
plt.show()
###KMEANS
k_means = cluster.KMeans(n_clusters=50, n_jobs=3)
k_means.fit(X)
print("==== K-Means fitted ====")
# get centroids and transform them to original space
#tmp = pca.inverse_transform(k_means.cluster_centers_.copy())
tmp = k_means.cluster_centers_.copy()
plotImageGrid(tmp, image_size=(16, 16, 3), nrow=5, ncol=10 )
plt.savefig('centroids_nw.png')
示例5: range
# 需要导入模块: from sklearn.decomposition import RandomizedPCA [as 别名]
# 或者: from sklearn.decomposition.RandomizedPCA import inverse_transform [as 别名]
# range(2,74) means its goes from col 2 to col 73
df_input_data = df_input[list(range(2, 74))]
df_input_target = df_input[list(range(0, 1))]
colors = numpy.random.rand(len(df_input_target))
# Randomized PCA
from sklearn.decomposition import RandomizedPCA
pca = RandomizedPCA(n_components=6) #from optimal pca components chart n_components=6
proj1 = pca.fit_transform(df_input_data)
# Relative weights on features
print pca.explained_variance_ratio_
print pca.components_
# Plotting
mpyplot.figure(1)
p1 = mpyplot.scatter(proj1[:, 0], proj1[:, 1], c=colors)
mpyplot.colorbar(p1)
mpyplot.show(p1)
# Randomized PCA using inverse transform - to make it linear
proj2 = pca.inverse_transform(proj1)
# Plotting
mpyplot.figure(2)
# p1 = mpyplot.scatter(proj1[:, 0], proj1[:, 1], c=colors, alpha=0.2)
p2 = mpyplot.scatter(proj2[:, 0], proj2[:, 1], c=colors, alpha=0.8)
mpyplot.colorbar(p1)
mpyplot.show(p2)
开发者ID:nishantnath,项目名称:MusicPredictiveAnalysis_EE660_USCFall2015,代码行数:32,代码来源:ml_classification_randomized_pca.py
示例6: KMeans
# 需要导入模块: from sklearn.decomposition import RandomizedPCA [as 别名]
# 或者: from sklearn.decomposition.RandomizedPCA import inverse_transform [as 别名]
kmeans = KMeans(k=49, n_init=1)
if not os.path.exists(MODEL_NAME):
print "Training on ", train_set_x.shape
print "Fitting PCA"
X_tr = pca.fit_transform(X_tr)
X_tst = pca.transform(X_tst)
print "Fitted PCA"
# Train KMeans on whitened data
print "Transforming data"
X_tr_white = pca.fit_transform(X_tr)
print "Fitting KMEANS"
kmeans.fit(X_tr_white)
filters_kmeans = pca.inverse_transform(kmeans.cluster_centers_)
else:
pca, kmeans = cPickle.load(open(MODEL_NAME, "r"))
import matplotlib.pylab as plt
if Visualise:
for i, f in enumerate(F):
plt.subplot(7, 7, i + 1)
plt.imshow(f.reshape(ImageSideFinal, ImageSideFinal), cmap="gray")
plt.axis("off")
plt.show()
N=min(1000, test_set_x.shape[0])
x_plt, y_plt, clr_plt = [0]*int(N), [0]*int(N), [0]*int(N)