knn train 和 class 有不同的长度
knn train and class have different lengths
NN 模型预测新数据,但错误提示:
“'train' 和 'class' 的长度不同”
有人可以复制并解决这个错误吗?
weather <- c(1, 1, 1, 0, 0, 0)
temperature <- c(1, 0, 0, 1, 0, 0)
golf <- c(1, 0, 1, 0, 1, 0)
df <- data.frame(weather, temperature, golf)
df_new <- data.frame(weather = c(1,1,1,1,1,1,1,1,1), temp = c(0,0,0,0,0,0,0,0,0), sunnday= c(1,1,1,0,1,1,1,0,0))
pred_knn <- knn(train=df[, c(1,2)], test=df_new, cl=df$golf, k=1)
非常感谢!
R中的knn函数要求训练数据只包含自变量,因变量在"cl"参数中单独调用。更改下面的行将修复此特定错误。
pred_knn <- knn(train=df[,c(1,2)], test=df_new, cl=df$golf, k=1)
但是,请注意 运行 上面的行会抛出另一个错误。由于 knn 计算观测值之间的欧氏距离,因此它要求所有自变量都是数值型的。这些页面包含有用的相关信息。我建议为这个特定的数据集使用不同的分类器。
https://towardsdatascience.com/k-nearest-neighbors-algorithm-with-examples-in-r-simply-explained-knn-1f2c88da405c
https://discuss.analyticsvidhya.com/t/how-to-resolve-error-na-nan-inf-in-foreign-function-call-arg-6-in-knn/7280/4
希望对您有所帮助。
NN 模型预测新数据,但错误提示: “'train' 和 'class' 的长度不同”
有人可以复制并解决这个错误吗?
weather <- c(1, 1, 1, 0, 0, 0)
temperature <- c(1, 0, 0, 1, 0, 0)
golf <- c(1, 0, 1, 0, 1, 0)
df <- data.frame(weather, temperature, golf)
df_new <- data.frame(weather = c(1,1,1,1,1,1,1,1,1), temp = c(0,0,0,0,0,0,0,0,0), sunnday= c(1,1,1,0,1,1,1,0,0))
pred_knn <- knn(train=df[, c(1,2)], test=df_new, cl=df$golf, k=1)
非常感谢!
R中的knn函数要求训练数据只包含自变量,因变量在"cl"参数中单独调用。更改下面的行将修复此特定错误。
pred_knn <- knn(train=df[,c(1,2)], test=df_new, cl=df$golf, k=1)
但是,请注意 运行 上面的行会抛出另一个错误。由于 knn 计算观测值之间的欧氏距离,因此它要求所有自变量都是数值型的。这些页面包含有用的相关信息。我建议为这个特定的数据集使用不同的分类器。
https://towardsdatascience.com/k-nearest-neighbors-algorithm-with-examples-in-r-simply-explained-knn-1f2c88da405c https://discuss.analyticsvidhya.com/t/how-to-resolve-error-na-nan-inf-in-foreign-function-call-arg-6-in-knn/7280/4
希望对您有所帮助。