EDA365电子论坛网
标题:
函数或变量 'knnclassify' 无法识别
[打印本页]
作者:
TEL15286598
时间:
2022-10-19 10:50
标题:
函数或变量 'knnclassify' 无法识别
之前是matlab2016b,写的这个KNN,跑的没什么问题,中午重装matlab换成2019b了,同样的程序上午还在用,下午就不行了。
: `0 }! b( f* y/ e% l6 O) D4 P; @
%数据集划分
. K x! _% P! J8 [) M% G' T% Y
flag=fix(0.8*n);
9 X3 t+ J3 V0 A! X; N
x_train=data(idx(1:flag),: );
0 E0 |+ J3 n! s ]! P% o
y_train=label(idx(1:flag),: );
9 u" P1 K O. {8 Z6 ^6 }
x_test=data(idx(flag+1:end),: );
8 d! f' \8 h3 a u
y_test=label(idx(flag+1:end),: );
! ]6 ?7 u" r6 T( v: [
%knn分类
' b4 ?& [! H, W, f7 U$ _
class = knnclassify(x_test, x_train, y_train,2);%k取2
, n2 K# Q6 K! s+ C/ X. L1 E4 V) Q R
acc=sum(class==y_test)/size(class,1);
& R* |2 ~5 Z* u+ o8 }
plot(class,'k*');
0 ~% d- R: x7 v
hold on;
* h9 ?" _/ t- M6 |, n
plot(y_test,'ro');
6 e4 B9 ^6 V$ z* h5 o
legend('预测值','实际值')
# p* A* Y8 D% M2 b2 d* {( ~( ~
6 l2 t9 t2 M/ c6 P1 \0 c" o
作者:
Nain
时间:
2022-10-19 13:12
2019b已经没有knnclassify这个函数了,换成了fitcknn。
/ \2 P8 }; g6 L9 w: _9 @; ^8 z
把class = knnclassify(x_test, x_train, y_train,2);%k取2
& B i6 [" C* M- \
换成下面的代码即可:
- y o; e" x$ P& o1 k+ D
mdl = fitcknn(x_train, y_train,'NumNeighbors',2);
9 `' q2 g% G, {$ n5 t7 S' p' Y+ B
class = predict(mdl,x_test);
作者:
hfiwioq
时间:
2022-10-19 13:20
之前的matlab2016b可能安装了包含knnclassify的工具箱,而2019b没有,安装对应工具箱或将相关放在同一文件下运行
欢迎光临 EDA365电子论坛网 (https://bbs.eda365.com/)
Powered by Discuz! X3.2