使用KNN

时间:2019-05-16 20:17:08

标签: matlab knn

我为组织中的员工提供了一个数据集,我需要将其分为三类:

  • 常规数据:薪水低于50000 $且职务中包含“助手”的员工
  • 重要数据:薪水在“ 50000”到“ 100000”之间且职务包含“董事”的员工。
  • 重要数据:薪水超过100000美元且职务包含“代理”的员工。

我在Matlab中编写了以下代码,并使用了fitcknn,但收到了以下错误消息:

Error using classreg.learning.FullClassificationRegressionModel.prepareDataCR (line 192)
X must be a numeric matrix.

Error in classreg.learning.classif.FullClassificationModel.prepareData (line 487)
                classreg.learning.FullClassificationRegressionModel.prepareDataCR(...

Error in ClassificationKNN.prepareData (line 878)
                prepareData@classreg.learning.classif.FullClassificationModel(X,Y,varargin{:},'OrdinalIsCategorical',true);

Error in classreg.learning.FitTemplate/fit (line 213)
                    this.PrepareData(X,Y,this.BaseFitObjectArgs{:});

Error in ClassificationKNN.fit (line 863)
            this = fit(temp,X,Y);

Error in fitcknn (line 261)
    this = ClassificationKNN.fit(X,Y,RemainingArgs{:});

Error in WhiteHouse (line 11)
MDL = fitcknn (B,G,'NumNeighbors',5,'standardize',1);

请帮助我看看代码中缺少什么。

    %Training Data
    B= [ "" "Employee" 50000 "Per Annum" "Assistant" ; "" "Employee" 100000 "Per Annum" "Director" ; "" "" 150000 "Per Annum" "Deputy" ] ;

    % Labels
    G = [ "Normal" ; "Important"  ; "Critical" ] ;
    %SampleData
    A = ["Brundage" "Employee" 103000 "Per Annum" "SPECIAL" ; "Buffa Nicole" "Employee" 80000  "Per Annum" "DEPUTY DIRECTOR OF CABINET AFFAIRS" ] ;

    MDL = fitcknn (B,G,'NumNeighbors',5,'standardize',1);
    class = predict (MDL,A)
    disp ('Result:') ;
    disp (class) ;

1 个答案:

答案 0 :(得分:0)

如错误消息中所述,该参数无效。 您的B变量是一个包含数字和文本数据的单元格。 应该只能是数字,不能是单元格数组。

请参考文档中的示例。 https://www.mathworks.com/help/stats/fitcknn.html#bt6d3vt-2