猿问

ErrorException(E_NOTICE)未定义变量:actualLabels

帮助,我的代码中出现错误“ErrorException (E_NOTICE) Undefined variable: actualLabels”


$title = "Data Confusion Matrix";

$testing_data = DataTesting::count();

$klasifikasi = Klasifikasi::with('sentimen')->get();

foreach($klasifikasi as $kelas){

    $predictedLabels[] = $kelas->sentimen->kategori;

    $testing = DataTesting::where('id_testing',$kelas->id_testing)->first();

    $twitter = TwitterStream::with('sentimen')->where('id_crawling',$testing->id_crawling)->first();

    $actualLabels[] = $twitter->sentimen->kategori;

 }

 $getPrecision = new ControllerConfusionMatrix($actualLabels, $predictedLabels);

 $accuracy = ControllerConfusionMatrix::score($actualLabels, $predictedLabels);

 $recall = $getPrecision->getRecall();

 $precision = $getPrecision->getPrecision();


弑天下
浏览 172回答 2
2回答

胡子哥哥

将此行添加到代码的开头:$actualLabels = []; 您收到错误,因为当$klasifikasi为空时,则不会执行循环内的语句。所以$actualLabels没有创建变量。在这种情况下,您会得到(E_NOTICE) Undefined variable: actualLabels. 希望你能理解。

互换的青春

定义Array()$predictedLabels = array();$actualLabels = array();            foreach($klasifikasi as $kelas){                $predictedLabels[] = $kelas->sentimen->kategori;                $testing = DataTesting::where('id_testing',$kelas->id_testing)->first();                $twitter = TwitterStream::with('sentimen')->where('id_crawling',$testing->id_crawling)->first();                $actualLabels[] = $twitter->sentimen->kategori;            }
随时随地看视频慕课网APP
我要回答