不会出现有关“矩阵”,“召回”,“精度”,“准确性”的数据

时间:2019-12-23 08:09:45

标签: php laravel sentiment-analysis

矩阵','回想','精度','精度'上的数据未出现

这是我的代码控制器:

public function confusion_matrix()
{

    $title = "Data Confusion Matrix";
    $testing_data = DataTesting::count();

    $klasifikasi = Klasifikasi::with('sentimen')->get();
    $predictedLabels=[];
    $actualLabels = [];
    foreach($klasifikasi as $kelas){
        $testing = DataTesting::where('id_testing',$kelas->id_testing)->first();
        $twitter = TwitterStream::with('sentimen')->where('id_crawling',$testing->id_crawling)->first();
        $predictedLabels[] = $kelas->sentimen->kategori;
        $actualLabels[] = optional(optional($twitter)->sentimen)->kategori;
    }

    $getPrecision = new ControllerConfusionMatrix($actualLabels, $predictedLabels);
    $accuracy = ControllerConfusionMatrix::score($actualLabels, $predictedLabels);
    $recall = $getPrecision->getRecall();
    $precision = $getPrecision->getPrecision();


    foreach($precision as $index_pc => $value_pc){
        $th[] = $index_pc;
    }

    sort($th);
    $confusionMatrix = ControllerConfusionMatrix::compute($actualLabels, $predictedLabels,$th);


    $matrix = array();

    foreach($th as $index_th => $value){
        $matrix[$value] = $confusionMatrix[$index_th];
        if(!array_key_exists($value,$recall)){
            $recall[$value] = 0;
        }
        if(!array_key_exists($value,$precision)){
            $precision[$value] = 0;
        }
    }
    ksort($precision);


    return view('visualisasi.confusion_matrix', compact(['title','testing_data','confusionMatrix','th','matrix','recall','precision','accuracy']));

}

     public function column_drilldown()
        {
            try {
                $testing_data = DataTesting::count();
                $klasifikasi = Klasifikasi::with('sentimen')->get();
                $predictedLabels=[];
                $actualLabels = [];
                foreach($klasifikasi as $kelas){
                    $predictedLabels[] = $kelas->sentimen->kategori;
                    $testing = DataTesting::where('id_testing',$kelas->id_testing)->first();
                    $twitter = TwitterStream::where('id_crawling',$testing->id_crawling)->first();
                    $actualLabels[] = $twitter->sentimen->kategori;
                }

                $getPrecision = new ControllerConfusionMatrix($actualLabels, $predictedLabels);
                $accuracy = ControllerConfusionMatrix::score($actualLabels, $predictedLabels);
                $error_rate = ControllerConfusionMatrix::error_rate($actualLabels, $predictedLabels);
                $recall = $getPrecision->getRecall();
                $precision = $getPrecision->getPrecision();
                $devide_recall = $getPrecision->getRecall();
                $devide_precision = $getPrecision->getPrecision();

                foreach ($devide_recall as $array_key1 => $array_item1) {
                    if ($devide_recall[$array_key1] == 0) {
                      unset($devide_recall[$array_key1]);
                    }
                }

                foreach ($devide_precision as $array_key2 => $array_item2) {
                    if ($devide_precision[$array_key2] == 0) {
                      unset($devide_precision[$array_key2]);
                    }
                }

                $sum_precision = array_sum($precision);
                $count_precision = count($devide_precision);
                $sum_recall = array_sum($recall);
                $count_recall = count($devide_recall);

                if($sum_precision == 0 && $count_precision == 0){
                    $total_precision = 0;
                } else {
                    $total_precision = $sum_precision/$count_precision;
                }

                if($sum_recall == 0 && $count_recall == 0){
                    $total_recall = 0;
                } else {
                    $total_recall = $sum_recall/$count_recall;
                }        

                $data[] = [
                    'accuracy' => round($accuracy*100,2),
                    'precision' => $precision,
                    'recall' => $recall,
                    'error_rate' => round($error_rate*100,2),
                    'total_precision' => round($total_precision,2),
                    'total_recall' => round($total_recall,2),
                ]; 


                return response()->json($data);

            }
            catch (\Exception $e) {
                return response()->json(error);
            }
        }

当我在出现如下所示的返回数据之前添加代码dd(compact(['title','testing_data','confusionMatrix','th','matrix','recall','precision','accuracy']))

 array:8 [▼
      "title" => "Data Confusion Matrix"
      "testing_data" => 100
      "confusionMatrix" => array:4 [▼
        0 => array:4 [▼
          0 => 0
          1 => 18
          2 => 38
          3 => 44
        ]
        1 => array:4 [▼
          0 => 0
          1 => 0
          2 => 0
          3 => 0
        ]
        2 => array:4 [▼
          0 => 0
          1 => 0
          2 => 0
          3 => 0
        ]
        3 => array:4 [▼
          0 => 0
          1 => 0
          2 => 0
          3 => 0
        ]
      ]
      "th" => array:4 [▼
        0 => ""
        1 => "negatif"
        2 => "netral"
        3 => "positif"
      ]
      "matrix" => array:4 [▼
        "" => array:4 [▶]
        "negatif" => array:4 [▼
          0 => 0
          1 => 0
          2 => 0
          3 => 0
        ]
        "netral" => array:4 [▼
          0 => 0
          1 => 0
          2 => 0
          3 => 0
        ]
        "positif" => array:4 [▼
          0 => 0
          1 => 0
          2 => 0
          3 => 0
        ]
      ]
      "recall" => array:4 [▼
        "" => 0.0
        "negatif" => 0.0
        "netral" => 0.0
        "positif" => 0.0
      ]
      "precision" => array:4 [▼
        "" => 0.0
        "negatif" => 0.0
        "netral" => 0.0
        "positif" => 0.0
      ]
      "accuracy" => 0
    ]

我的查看代码:

<table style="table-layout:fixed" id="table-negatif"
       class="table table-striped table-bordered table-hover">
    <thead>
    <tr>
        <th style="background-color:#F2F1EF"></th>
        @foreach($th as $index_th => $head)
        <th>
            <center>Pred. {{$head}}<center>
        </th>
        @endforeach
        <th>
            <center>CLASS RECALL</center>
        </th>
    </tr>
    </thead>
    <tbody>
    <?php $no=1; ?>
    @foreach($matrix as $index_matrix => $value_m)
        <tr>
            <td align="center"><b>True. {{$index_matrix}}</b></td>
            @foreach($value_m as $index_value => $value_v)
                <td align="center">{{$value_v}}</td>
            @endforeach
            <td align="center">{{round($recall[$index_matrix],2)}}%</td>
        </tr>
    @endforeach
    <tr>
        <td align="center"><b>CLASS PRECISION</b></td>
        @foreach($precision as $index_p => $value_p)
            <td align="center">{{round($value_p,2)}}%</td>
        @endforeach
        <td style="background-color:#F2F1EF"></td>
    </tr>
    </tbody>
</table>
<hr>
{{-- <h4> Accuracy : <span id="accuracy">{{round($accuracy*100,2)}}</span>%</h4> --}}
</div>

<script type="text/javascript">
    $(document).ready(function () {
        $.ajaxSetup({
            beforeSend: function (xhr, type) {
                if (!type.crossDomain) {
                    xhr.setRequestHeader('X-CSRF-Token', $('meta[name="csrf-token"]').attr(
                        'content'));
                }
            },
        });

        var url = $('#url_root').val();
        $.ajax({
            type: "GET",
            url: url + '/column-drilldown',
            success: function (data) {
                let precision = Object.entries(data[0].precision)
                let recall = Object.entries(data[0].recall)
                // Create the chart
                Highcharts.chart('container', {
                    chart: {
                        type: 'column'
                    },
                    title: {
                        text: 'Persentase Confusion Matrix'
                    },
                    subtitle: {
                        text: ''
                    },
                    xAxis: {
                        type: 'category'
                    },
                    yAxis: {
                        title: {
                            text: ''
                        }

                    },
                    legend: {
                        enabled: false
                    },
                    plotOptions: {
                        series: {
                            borderWidth: 0,
                            dataLabels: {
                                enabled: true,
                                format: '{point.y:.2f}%'
                            }
                        }
                    },

                    tooltip: {
                        headerFormat: '<span style="font-size:11px">{series.name}</span><br>',
                        pointFormat: '<span style="color:{point.color}">{point.name}</span>: <b>{point.y:.2f}%</b> of total<br/>'
                    },

                    "series": [
                        {
                            "name": "Confusion Matrix",
                            "colorByPoint": true,
                            "data": [
                                {
                                    "name": "Accuracy",
                                    "y": data[0].accuracy,
                                },
                                {
                                    "name": "Precision",
                                    "y": data[0].total_precision,
                                    "drilldown": "Precision"
                                },
                                {
                                    "name": "Recall",
                                    "y": data[0].total_recall,
                                    "drilldown": "Recall"
                                },
                                {
                                    "name": "Error Rate",
                                    "y": data[0].error_rate,
                                }
                            ]
                        }
                    ],
                    "drilldown": {
                        "series": [
                            {
                                "name": "Precision",
                                "id": "Precision",
                                "data": precision,
                            },
                            {
                                "name": "Recall",
                                "id": "Recall",
                                "data": recall,
                            }
                        ]
                    }
                });

            },
            error: function (data) {
                console.log('Error:', data);
                new PNotify({
                    title: 'Error !',
                    text: 'Tidak Ada Yang Diproses',
                    type: 'error'
                });
            }
        });
    });
</script>

我无法从矩阵','回想','精度','精度'中获取值。

0 个答案:

没有答案