HW4_2

pdf

School

Georgia Institute Of Technology *

*We aren’t endorsed by this school

Course

6501

Subject

Computer Science

Date

Dec 6, 2023

Type

pdf

Pages

2

Uploaded by DeanProtonRabbit35

Report
HW4_2 2023-09-07 Data = read.csv ( "iris.txt" , sep = "" ) str (Data) ## ’data.frame’: 150 obs. of 5 variables: ## $ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ... ## $ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ... ## $ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ... ## $ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ... ## $ Species : chr "setosa" "setosa" "setosa" "setosa" ... print ( summary (Data)) ## Sepal.Length Sepal.Width Petal.Length Petal.Width ## Min. :4.300 Min. :2.000 Min. :1.000 Min. :0.100 ## 1st Qu.:5.100 1st Qu.:2.800 1st Qu.:1.600 1st Qu.:0.300 ## Median :5.800 Median :3.000 Median :4.350 Median :1.300 ## Mean :5.843 Mean :3.057 Mean :3.758 Mean :1.199 ## 3rd Qu.:6.400 3rd Qu.:3.300 3rd Qu.:5.100 3rd Qu.:1.800 ## Max. :7.900 Max. :4.400 Max. :6.900 Max. :2.500 ## Species ## Length:150 ## Class :character ## Mode :character ## ## ## # Look for which k to use # use iter.max to ensure the algorithms converge # use nstart=25 to ensure at least 25 random sets are chosen set.seed ( 123 ) # k = 1 is bad, try to plot wss for k = 2 ...6 k.max <- 8 data <- as.matrix (Data[, 1 : 4 ]) wss = c () for (k in 1 : k.max){ current_cluster = kmeans (data,k, nstart= 5 , iter.max= 15 ) wss[k] = current_cluster $ tot.withinss } plot ( 1 : k.max, wss, type= "b" , pch = 19 , frame = FALSE , 1
xlab= "Number of clusters K" , ylab= "Total within-clusters sum of squares" ) 1 2 3 4 5 6 7 8 100 300 500 700 Number of clusters K Total within-clusters sum of squares # List the within-class sum of squares for different sets of predictors find_predictors = function (d){ kmeans (d, 3 , nstart= 5 , iter.max= 15 ) $ tot.withinss} find_predictors (Data[, 1 ]) #15.8 ## [1] 15.81662 2
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help