sadII_klastrowanie_spektralne

2387 days ago by macieksk

#install.packages("kernlab") library(kernlab) 
       
data(spirals) sc <- specc(spirals, centers=2) sc centers(sc) size(sc) withinss(sc) plot(spirals, col=sc) dev.off() 
       
Spectral Clustering object of class "specc" 

 Cluster memberships: 
 
1 1 2 2 1 2 2 2 1 2 2 1 1 2 2 1 1 1 1 1 2 2 1 2 2 2 2 1 1 1 2 1 2 2
1 2 1 2 1 1 2 2 2 2 1 1 1 1 1 2 1 2 1 1 2 2 2 1 1 1 1 1 2 2 1 2 1 1
1 2 2 1 2 2 2 1 1 1 1 2 1 2 1 2 1 1 1 1 1 1 2 1 1 2 2 2 1 2 2 2 2 1
1 1 2 2 1 2 2 2 1 2 1 1 1 1 2 2 1 1 2 1 1 1 2 1 2 1 1 1 1 1 2 2 2 2
2 1 1 1 2 2 1 2 1 1 1 2 2 2 1 2 2 2 2 2 2 1 1 1 1 2 1 2 1 1 1 2 1 1
1 1 2 1 2 1 1 1 2 2 1 1 1 2 2 2 1 1 2 2 2 2 2 2 2 2 1 1 1 1 2 1 2 2
1 2 1 2 2 2 2 2 1 2 1 2 1 2 1 1 1 2 2 2 2 1 1 1 2 1 1 2 2 2 2 2 1 1
2 2 2 2 1 1 1 2 2 2 2 2 2 1 1 2 2 1 1 1 1 1 1 1 2 2 2 2 1 2 1 2 1 1
2 2 2 1 2 1 1 1 1 2 2 1 2 1 2 2 2 1 2 1 2 2 1 1 2 2 2 1 
 
Gaussian Radial Basis kernel function. 
 Hyperparameter : sigma =  368.689121906976 

Centers:  
            [,1]       [,2]
[1,]  0.01997201 -0.1761483
[2,] -0.01770984  0.1775137

Cluster size:  
[1] 150 150

Within-cluster sum of squares:  
[1] 117.3429 118.1182

            [,1]       [,2]
[1,]  0.01997201 -0.1761483
[2,] -0.01770984  0.1775137
[1] 150 150
[1] 117.3429 118.1182


null device 
          1 
# Zastosuj specc do danych crabs # Porownaj wynik klastrowania z prawdziwymi danymi o gatunku i plci library(MASS) head(crabs) 
       
  sp sex index   FL  RW   CL   CW  BD
1  B   M     1  8.1 6.7 16.1 19.0 7.0
2  B   M     2  8.8 7.7 18.1 20.8 7.4
3  B   M     3  9.2 7.8 19.0 22.4 7.7
4  B   M     4  9.6 7.9 20.1 23.1 8.2
5  B   M     5  9.8 8.0 20.3 23.0 8.2
6  B   M     6 10.8 9.0 23.0 26.5 9.8
?specc 
       
WARNING: Output truncated!  
full_output.txt



specc                 package:kernlab                  R
Documentation

_S_p_e_c_t_r_a_l _C_l_u_s_t_e_r_i_n_g

_D_e_s_c_r_i_p_t_i_o_n:

     A spectral clustering algorithm. Clustering is performed by
     embedding the data into the subspace of the eigenvectors of an
     affinity matrix.

_U_s_a_g_e:

     ## S4 method for signature 'formula'
     specc(x, data = NULL, na.action = na.omit, ...)
     
     ## S4 method for signature 'matrix'
     specc(x, centers, kernel = "rbfdot", kpar = "automatic", 
            nystrom.red = FALSE, nystrom.sample = dim(x)[1]/6,
iterations = 200,
            mod.sample = 0.75, na.action = na.omit, ...)
     
     ## S4 method for signature 'kernelMatrix'
     specc(x, centers, nystrom.red = FALSE, iterations = 200, ...)
     
     ## S4 method for signature 'list'
     specc(x, centers, kernel = "stringdot", kpar = list(length=4,
lambda=0.5),
                            nystrom.red = FALSE, nystrom.sample =
length(x)/6, iterations = 200,
                            mod.sample =  0.75, na.action = na.omit,
...)
     
_A_r_g_u_m_e_n_t_s:

       x: the matrix of data to be clustered, or a symbolic
description
          of the model to be fit, or a kernel Matrix of class
          ‘kernelMatrix’, or a list of character vectors.

    data: an optional data frame containing the variables in the
model.
          By default the variables are taken from the environment
which
          `specc' is called from.

 centers: Either the number of clusters or a set of initial cluster
          centers. If the first, a random set of rows in the
          eigenvectors matrix are chosen as the initial centers.

  kernel: the kernel function used in computing the affinity matrix.
          This parameter can be set to any function, of class
kernel,
          which computes a dot product between two vector arguments.
          kernlab provides the most popular kernel functions which
can
          be used by setting the kernel parameter to the following
          strings:

            • ‘rbfdot’ Radial Basis kernel function "Gaussian"

            • ‘polydot’ Polynomial kernel function

            • ‘vanilladot’ Linear kernel function

            • ‘tanhdot’ Hyperbolic tangent kernel function

            • ‘laplacedot’ Laplacian kernel function


...

     ...: additional parameters

_D_e_t_a_i_l_s:

     Spectral clustering works by embedding the data points of the
     partitioning problem into the subspace of the k largest
     eigenvectors of a normalized affinity/kernel matrix. Using a
     simple clustering method like ‘kmeans’ on the embedded points
     usually leads to good performance. It can be shown that
spectral
     clustering methods boil down to graph partitioning.
     The data can be passed to the ‘specc’ function in a ‘matrix’ or
a
     ‘data.frame’, in addition ‘specc’ also supports input in the
form
     of a kernel matrix of class ‘kernelMatrix’ or as a list of
     character vectors where a string kernel has to be used.

_V_a_l_u_e:

     An S4 object of class ‘specc’ which extends the class ‘vector’
     containing integers indicating the cluster to which each point
is
     allocated. The following slots contain useful information

 centers: A matrix of cluster centers.

    size: The number of point in each cluster

withinss: The within-cluster sum of squares for each cluster

 kernelf: The kernel function used

_A_u_t_h_o_r(_s):

     Alexandros Karatzoglou
     <email: alexandros.karatzoglou@ci.tuwien.ac.at>

_R_e_f_e_r_e_n_c_e_s:

     Andrew Y. Ng, Michael I. Jordan, Yair Weiss
     _On Spectral Clustering: Analysis and an Algorithm_
     Neural Information Processing Symposium 2001
     <URL: http://www.nips.cc/NIPS2001/papers/psgz/AA35.ps.gz>

_S_e_e _A_l_s_o:

     ‘kkmeans’, ‘kpca’, ‘kcca’

_E_x_a_m_p_l_e_s:

     ## Cluster the spirals data set.
     data(spirals)
     
     sc <- specc(spirals, centers=2)
     
     sc
     centers(sc)
     size(sc)
     withinss(sc)
     
     plot(spirals, col=sc)