# 智能主题检测与无监督机器学习：识别颜色教程

## 一堆漂亮的颜色

```rgb(255, 0, 0) // Red
rgb(0, 255, 0) // Green
rgb(0, 0, 255) // Blue```

## 我们到底在分类什么?

```rgb(200, 0, 150) // Purple Plum => Red
rgb(50, 199, 135) // Sea Green => Green
rgb(100, 180, 255) // Sky Blue => Blue```

## 生成随机颜色

`rgb(255, 100, 175, maxColorValue = 255)`

## 将RGB转换为数值

```(Red * 256 * 256) +
(Green * 256) +
(Blue)```

```rgb(200, 0, 150)
// 13107350
rgb(50, 199, 135)
// 3327879
rgb(100, 180, 255)
// 6599935```

## 在图表上绘制颜色

```rgb2Num <- function(data) {
# Maps RGB colors to a single value.
result <- sapply(1:nrow(data), function(row) {
color <- data[row,]
(color\$red * 256 * 256) + (color\$green * 256) + color\$blue
})

result
}
generateColors <- function(n) {
# Generate a set of random colors.
colors <- as.data.frame(t(sapply(1:n, function(i) {
parts <- sample(0:255, 3)
c(red = parts[1], green = parts[2], blue = parts[3], hex = rgb(parts[1], parts[2], parts[3], maxColorValue = 255))
})), stringsAsFactors = F)
# Convert to numeric values.
colors\$red <- as.numeric(colors\$red)
colors\$green <- as.numeric(colors\$green)
colors\$blue <- as.numeric(colors\$blue)

# Map each color to an x/y-coordinate for easy plotting.
colors\$x <- 1:nrow(colors)
colors\$y <- rgb2Num(colors)
colors
}```

```> generateColors(10)
red green blue     hex  x        y
1  186   160  122 #BAA07A  1 12230778
2   40     2  114 #280272  2  2622066
3  126    99  118 #7E6376  3  8282998
4  182    14   90 #B60E5A  4 11931226
5  205   213   60 #CDD53C  5 13489468
7   75    37  178 #4B25B2  7  4924850
8   26     8  253 #1A08FD  8  1706237
9    8    86  232 #0856E8  9   546536
10 158    73  187 #9E49BB 10 10373563```

## K-Means聚类

`K = ~~(Math.sqrt(points.length * 0.5));`

## 计算质心

```# Run kmeans clustering on the data.
fit <- kmeans(train[,1:3], 3, nstart = 20)
train\$group <- fit\$cluster```

```K-means clustering with 3 clusters of sizes 24, 33, 43
Cluster means:
red     green      blue
1  98.16667  64.66667 189.20833
2 164.18182  65.03030  64.72727
3 140.67442 196.58140 132.20930
Clustering vector:
[1] 3 3 3 3 2 2 3 3 3 2 3 2 2 1 2 1 3 3 2 3 2 3 3 3 2 1 2 1 1 3 3 2 2 3 3 3 1 1 2 3 2 1 2 3 3
[46] 3 2 2 2 1 2 1 2 3 2 3 1 3 3 3 1 3 1 2 3 1 3 3 3 1 2 1 1 1 2 1 3 2 2 1 3 1 3 2 2 2 3 3 3 1
[91] 2 3 2 3 2 2 3 1 3 1
Within cluster sum of squares by cluster:
[1] 182410.6 201096.4 443809.0
(between_SS / total_SS =  46.1 %)```

###### 集群中心标识在它们各自的位置显示在图中颜色。

```Cluster 1 - 'Blue Group'
Cluster 2 - 'Red Group'
Cluster 3 - 'Green Group'```

## 在新数据上预测

`test <- generateColors(3)`

```  red green blue     hex x        y
1 232    17  252 #E811FC 1 15208956
2  86   109  216 #566DD8 2  5664216
3  67   219  216 #43DBD8 3  4447192```

```# Cast the k-means model to be of type kcaa, so we can use the predict method.
fit2 <- as.kcca(fit, data=train[,1:3])
# Predict the assigned color by mapping the color to a cluster.
group <- predict(model, newdata=data[,1:3])
# Assign the label of the cluster.
data\$label <- sapply(1:nrow(data), function(row) {
centroids[centroids\$group == data[row, 'group'], ]\$label
})```

```  red green blue     hex x        y group label
1 241    52   11 #F1340B 1 15807499     2   red
2  80   187  139 #50BB8B 2  5290891     3 green
3  34    15  194 #220FC2 3  2232258     1  blue```

1853 篇文章92 人订阅

0 条评论

## 相关文章

### Ensemble Learners

Udacity Ensemble Learners ---- Boosting Algorithm 不需要绞尽脑汁去想很复杂的 Rules，只需要一些简单的 ...

37270

37920

16760

### 实用 | 分享一个决策树可视化工具

【磐创AI导读】：这篇文章希望跟大家分享一个可视化决策树或者随机森林的工具。这可以帮助我们更好的去理解或解释我们的模型。想要获取更多的机器学习、深度学习资源。欢...

27210

34060

27940

19600

51540

83620

23330