-4

Help, i got an error "ErrorException (E_NOTICE) Undefined variable: actualLabels" in my code

$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();
Has QUIT--Anony-Mousse
  • 76,138
  • 12
  • 138
  • 194
Listyawan
  • 11
  • 5
  • 1
    Put this line before foreach loop, `$actualLabels = [];` – Prashant Deshmukh..... Dec 17 '19 at 10:16
  • 1
    @AksenP - about all we can do is just vote to close and move on then; doubtless there'll be some people answering to pick the "low hanging fruit" (unless the question is closed) - decreasing the overall quality/usefulness of SO going forward, but such is life. – CD001 Dec 17 '19 at 10:22
  • @AksenP If you don't want to please don't insult, I really don't know how to fix the error – Listyawan Dec 17 '19 at 10:26
  • 1
    @Listyawan - to be fair, you've asked 4 questions in the last 2 days, all of which amount to the same thing - very basic undefined index/variable errors, for which there are already canonical QAs you should refer to. Having the same questions repeatedly asked on SO just increases the noise and thus reduces the quality of the site, as a resource, overall. Please refer to the [Help Centre](https://stackoverflow.com/help) for more info. – CD001 Dec 17 '19 at 10:40

2 Answers2

2

Add this line to beginning of your code : $actualLabels = []; You are getting error because when $klasifikasi is empty, then the statement inside loop is not executed. So $actualLabels variable is not created. In this case you get errro of (E_NOTICE) Undefined variable: actualLabels. Hope you understand.

Ankur Mishra
  • 1,284
  • 1
  • 6
  • 15
0

define 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;
            }
VIKAS KATARIYA
  • 5,867
  • 3
  • 17
  • 34