R-将KNN从类别转换为数值 [英] R - convert from categorical to numeric for KNN
本文介绍了R-将KNN从类别转换为数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用R的Caret包来使用UCI Machine Learning中应用于鲍鱼"数据库的KNN(链接到)分别转换为数字值,例如1,2,3
?
I'm trying to use the Caret package of R to use the KNN applied to the "abalone" database from UCI Machine Learning (link to the data). But it doesn't allow to use KNN when there's categorical values.
How do I convert the categorical values (in this database: "M","F","I"
) to numeric values, such as 1,2,3
, respectively?
推荐答案
通过read.table
读取数据时,第一列中的数据是因素.然后
When data are read in via read.table
, the data in the first column are factors. Then
data$iGender = as.integer(data$Gender)
会工作.如果它们是字符,则绕过因子最容易:
would work. If they are character, a detour via factor is easiest:
data$iGender= as.integer(as.factor(data$Gender))
这篇关于R-将KNN从类别转换为数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文