This image is the example of the dataset
So i've asked this to ChatGPT and another AI helper, but the result is 0.
This image is table to show what i want
So from the image about table to show what i want, column "Baik" means how many number 1 is from column "kondisi" in the dataset, same as "sedang": 2 ; "buruk": 3;
And this below is the code that i try to fix because the result is 0 :
<h4>Jumlah Cluster</h4>
<table class="table table-border">
<thead>
<th>Cluster</th>
<th>Jumlah</th>
<th>Baik</th>
<th>Sedang</th>
<th>Buruk</th>
</thead>
<tbody>
<?php
if ($this->session->userdata("kmeans_result") !== NULL) {
$res = array();
$kondisiCounts = array();
foreach ($this->session->userdata("kmeans_result") as $key) {
if (!isset($res[$key[1]])) {
$res[$key[1]] = 1;
} else {
$res[$key[1]]++;
}
// Check if 'kondisi' key exists and access it safely
$kondisi = $key['kondisi'] ?? 0;
// Count Kondisi values
$clusterId = $key[1];
if (!isset($kondisiCounts[$clusterId])) {
$kondisiCounts[$clusterId] = array('Baik' => 0, 'Sedang' => 0, 'Buruk' => 0);
}
// Increment the count based on the Kondisi value
if ($kondisi === 1) {
$kondisiCounts[$clusterId]['Baik']++;
} elseif ($kondisi === 2) {
$kondisiCounts[$clusterId]['Sedang']++;
} elseif ($kondisi === 3) {
$kondisiCounts[$clusterId]['Buruk']++;
}
}
foreach ($res as $key => $val) {
?>
<tr>
<td><?=$key?></td>
<td><?=$val?></td>
<td><?=$kondisiCounts[$key]['Baik'] ?? 0?></td>
<td><?=$kondisiCounts[$key]['Sedang'] ?? 0?></td>
<td><?=$kondisiCounts[$key]['Buruk'] ?? 0?></td>
</tr>
<?php
}
}
?>
</tbody>
</table>
The dataset is from session : $this->session->userdata("kmeans_result")
And this below is code to show the result and become the dataset in the first image, and i use this code for reference when i ask to AI, but their solution still give result 0 in each column :
<h4>Hasil Cluster K-Means</h4>
<div class="table-responsive" id="export">
<table class="table table-border">
<thead>
<?php
foreach ($this->session->userdata("process_datasetindex") as $n => $v) {
if($n==0){
$obj = $v;
}
?>
<th><?=$v?></th>
<?php
}
?>
<th>Cluster</th>
</thead>
<?php
if($this->session->userdata("kmeans_result")!==NULL){
$resk = $this->session->userdata("kmeans_result");
aasort($resk,1);
foreach ($resk as $key) {
?>
<tr>
<td><?=$key[0]?></td>
<?php
foreach ($this->session->userdata("process_datasetindex") as $n => $v) {
if($n>0){
$attr = array_column($this->session->userdata("process_dataset"),$v,$obj);
?>
<td><?=$attr[$key[0]]?></td>
<?php
}
}
?>
<td><?=$key[1]?></td>
</tr>
<?php
}
}
?>
</table>
Nvm i know the answer now, turns out the data is in different array in different sessions, so the data to count which data is in this cluster is inside session : $this->session->userdata("kmeans_result")
.
But the other data including "kondisi" is inside $this->session->userdata("process_dataset")
So, first i merge the array and count how many data 1,2,3 from column "kondisi" in each clusters of 1,2,3. complete code :
<table class="table table-border">
<thead>
<th>Cluster</th>
<th>Kondisi Baik</th>
<th>Kondisi Sedang</th>
<th>Kondisi Buruk</th>
</thead>
<?php
if ($this->session->userdata("kmeans_result") !== NULL && $this->session->userdata("process_dataset") !== NULL) {
// Get the data from both session variables
$kmeansResult = $this->session->userdata("kmeans_result");
$processDataset = $this->session->userdata("process_dataset");
// Create an associative array to store cluster assignments based on the timestamp
$clusterAssignments = array();
foreach ($kmeansResult as $result) {
$timestamp = $result[0];
$cluster = $result[1];
$clusterAssignments[$timestamp] = $cluster;
}
// Initialize arrays to keep track of 'kondisi' counts for each cluster
$clusterKondisiCounts = array();
// Iterate through the dataset to count 'kondisi' values for each cluster
foreach ($processDataset as $data) {
$timestamp = $data['tanggaljam'];
$kondisi = $data['kondisi'];
// Get the cluster assignment for the current data point
$cluster = $clusterAssignments[$timestamp];
// Initialize the count for the cluster if it doesn't exist
if (!isset($clusterKondisiCounts[$cluster])) {
$clusterKondisiCounts[$cluster] = array(0, 0, 0);
}
// Increment the count for the corresponding 'kondisi' value
$clusterKondisiCounts[$cluster][$kondisi - 1]++;
}
// Display the counts in the table
foreach ($clusterKondisiCounts as $cluster => $counts) {
?>
<tr>
<td><?= $cluster ?></td>
<td><?= $counts[0] ?></td>
<td><?= $counts[1] ?></td>
<td><?= $counts[2] ?></td>
</tr>
<?php
}
}
?>
</table>