importmatplotlib.pyplotaspltfromsklearn.feature_extraction.imageimportgrid_to_graphfromsklearn.clusterimportAgglomerativeClustering# ############################################################################## Generate datatry:# SciPy >= 0.16 have face in miscfromscipy.miscimportfaceface=face(gray=True)exceptImportError:face=sp.face(gray=True)# Resize it to 10% of the original size to speed up the processingface=sp.misc.imresize(face,0.10)/255.X=np.reshape(face,(-1,1))# ############################################################################## Define the structure A of the data. Pixels connected to their neighbors.connectivity=grid_to_graph(*face.shape)# #############################################################################
>>> # Create a signal with only 2 useful dimensions>>> x1=np.random.normal(size=100)>>> x2=np.random.normal(size=100)>>> x3=x1+x2>>> X=np.c_[x1,x2,x3]>>> fromsklearnimportdecomposition>>> pca=decomposition.PCA()>>> pca.fit(X)PCA(copy=True, iterated_power='auto', n_components=None, random_state=None, svd_solver='auto', tol=0.0, whiten=False)>>> print(pca.explained_variance_)[ 2.18565811e+00 1.19346747e+00 8.43026679e-32]>>> # As we can see, only the 2 first components are useful>>> pca.n_components=2>>> X_reduced=pca.fit_transform(X)>>> X_reduced.shape(100, 2)