R performs hierarchical clustering on the term for a complete instance (TM package)

Source: Internet
Author: User
# It is basically the same as the previous article, but the clustering object is different.
# Connect to the database and read the files in the database # load the package Library (rmysql) # establish a connection conn <-dbconnect (dbdriver ("MySQL"), dbname = "eswp ", user = "root", password = "root") # read Table 2008 yearnewtext = dbreadtable (Conn, "2008 yearnew ")[, 2: 2] # Read Only the column of the mesh word and modify the number of rows read using the first subscript # load the TM package Library (TM) # Build a corpus = corpus (vectorsource (text) # create a word-document matrix from the corpus and use TF-IDF for representation. stopwords = stopwords ("mesh ") indicates that the mesh disabled Word Table is used. The disabled Word Table is placed in the stopwords folder of the TM package. TDM = termdocumentmatrix (corpus, Co Ntrol = List (stopwords = stopwords ("mesh"), weighting = weighttfidf) # Calculate Word Similarity using angle cosine. In R, it seems that cosine between words cannot be calculated, only the angle between documents can be calculated. Therefore, you need to use other methods to calculate distance rather than similarity # dist_tdm <-dissimilarity (TDM, method = 'cosine ') # The distance calculation methods in R include Euclidean, maximum, Manhattan, Canberra, minkoski, and binary. Here, Euclidean distance is used, that is, the Euclidean distance dist_tdm <-dist (TDM, method = 'euclidean ') # perform hierarchical clustering based on similarity. The distance is the same as the average class distance. Other available distances include single, complete, median, mcquitty, and aver. Age, centroid, Ward, and other HC <-hclust (dist_tdm, method = 'ave ') # cluster graph plot (HC) # if the number of classes is large, the number of classes will not be clear, use the following method to draw a large pixel image PNG ("test.png", width = 3000, Height = 3000) # change the output device to PNG, with the pixel as big as possible, however, problems may occur if the change is too large. # Cex indicates the label size. You can also use cex. axis attribute to change the number size in the coordinate system, using cex. lab changes the size of the matrix name below # Use cex. main to change the title size, use cex. sub changes the size of the cluster method name below. LWD is the width of the midline graph. In this case, the graph will see plot (HC, cex = 2, cex. axis = 3, cex. lab = 3, cex. main = 3, cex. sub = 3, LWD = 1.5) Dev. off () # Use the cutree function to view the clustering result. k = 3 indicates dividing the result into three types. This method is always divided into many classes. Unless you have many classes, my 600 words are divided into 400 categories to see something. Ct = cutree (HC, K = 3) # Use the h Parameter for classification. In this way, you can view the image for classification. Set the CT1 parameter to cutree (HC, H = 0.5) # continue testing # count the number of clustering results to obtain the number of terms in each class table (CT) # view termct1 in the first category <-CT [Ct = 1]; CT1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.