1、 显示数据集的内部结构 str(iris)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 .$ Spe
2、cies : Factor w/ 3 levels setosa,versicolor,.: 1 1 1 1 1 1 1 1 1 1 .显示数据集的属性 attributes(iris)$names -就是数据集的列名 $row.names -个人理解就是每行数据的标号 1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 2021 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 4041 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
3、 57 58 59 6061 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 8081 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100101 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120121 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
4、140141 141 142 143 144 145 146 147 148 149 150$class -表示类别data.frame查看数据集的前五项数据情况 iris1:5, Sepal.Length Sepal.Width Petal.Length Petal.Width Species1 5.1 3.5 1.4 0.2 setosa2 4.9 3.0 1.4 0.2 setosa3 4.7 3.2 1.3 0.2 setosa4 4.6 3.1 1.5 0.2 setosa5 5.0 3.6 1.4 0.2 setosa查看数据集中属性Sepal.Length前10行数据10, 1
5、5.1 4.9 4.7 4.6 5.0 5.4 4.6 5.0 4.4 4.9同上 iris$Sepal.Length1:101 5.1 4.9 4.7 4.6 5.0 5.4 4.6 5.0 4.4 4.9显示数据集中每个变量的分布情况 summary(iris) Sepal.Length Sepal.Width Petal.Length Petal.Width Species Min. :4.300 Min. :2.000 Min. :1.000 Min. :0.100 setosa :50 1st Qu.:5.100 1st Qu.:2.800 1st Qu.:1.600 1st Qu.
6、:0.300 versicolor:Median :5.800 Median :3.000 Median :4.350 Median :1.300 virginica :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 显示iris数据集列Species中各个值出现频次 table(iris$Species) setosa versicolor
7、 virginica 50 50 50 根据列Species画出饼图 pie(table(iris$Species)算出列Sepal.Length的所有值的方差 var(iris$Sepal.Length)1 0.6856935算出列iris$Sepal.Length和iris$Petal.Length的协方差 cov(iris$Sepal.Length, iris$Petal.Length)1 1.274315算出列iris$Sepal.Length和iris$Petal.Length的相关系数, 从结果看这两个值是强相关。 cor(iris$Sepal.Length, iris$Petal
8、.Length)1 0.8717538画出列iris$Sepal.Length分布柱状图 hist(iris$Sepal.Length)画出列iris$Sepal.Length的密度函数图 plot(density(iris$Sepal.Length)画出列iris$Sepal.Length和iris$Sepal.Width的散点图 plot(iris$Sepal.Length, iris$Sepal.Width)绘出矩阵各列的散布图 plot(iris)or pairs(iris)第二步:使用knn包进行Kmean聚类分析将数据集进行备份,将列newiris$Species置为空,将此数据集
9、作为测试数据集 newiris <- iris newiris$Species &- NULL在数据集newiris上运行Kmean聚类分析, 将聚类结果保存在kc中。在kmean函数中,将需要生成聚类数设置为3 (kc &- kmeans(newiris, 3) K-means clustering with 3 clusters of sizes 38, 50, 62: K-means算法产生了3个聚类,大小分别为38,50,62. Cluster means: 每个聚类中各个列值生成的最终平均值 Sepal.Length Sepal.Width Petal.Length Petal
10、.Width1 5.006000 3.428000 1.462000 0.2460002 5.901613 2.748387 4.393548 1.4338713 6.850000 3.073684 5.742105 2.071053Clustering vector: 每行记录所属的聚类(2代表属于第二个聚类,1代表属于第一个聚类,3代表属于第三个聚类) 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 137 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 3 2 2 2 2 2
11、2 2 2 2 2 2 2 2 2 2 2 2 2 273 2 2 2 2 2 3 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 2 3 3 3 3 2 3109 3 3 3 3 3 2 2 3 3 3 3 2 3 2 3 2 3 3 2 2 3 3 3 3 3 2 3 3 3 3 2 3 3 3 2 3145 3 3 2 3 3 2Within cluster sum of squares by cluster: 每个聚类内部的距离平方和 1 15.15100 39.82097 23.87947(between_SS / total_SS = 8
12、8.4 %) 组间的距离平方和占了整体距离平方和的的88.4%,也就是说各个聚类间的距离做到了最大Available components: 运行kmeans函数返回的对象所包含的各个组成部分clustercenterstotsswithinss 5 tot.withinssbetweensssize(是一个整数向量,用于表示记录所属的聚类 是一个矩阵,表示每聚类中各个变量的中心点表示所生成聚类的总体距离平方和表示各个聚类组内的距离平方和表示聚类组内的距离平方和总量表示聚类组间的聚类平方和总量表示每个聚类组中成员的数量)创建一个连续表,在三个聚类中分别统计各种花出现的次数 table(iris$Species, kc$cluster) 1 2 3 setosa 0 50 0 versicolor 2 0 48 virginica 36 0 14根据最后的聚类结果画出散点图,数据为结果集中的列和,颜色为用1,2,3表示的缺省颜色 plot(newirisc(, ), col = kc$cluster)在图上标出每个聚类的中心点points(kc$centers,c(), col = 1:3, pch = 8, cex=2)
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1