166 lines
4.8 KiB
PHP
166 lines
4.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\User;
|
|
use App\Models\Atribut;
|
|
use App\Models\Probability;
|
|
use App\Models\TestingData;
|
|
use Illuminate\Support\Str;
|
|
use App\Models\NilaiAtribut;
|
|
use App\Models\TrainingData;
|
|
use Illuminate\Http\Request;
|
|
use App\Exports\TestingExport;
|
|
use App\Imports\TestingImport;
|
|
use App\Models\Classification;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Hash;
|
|
use Maatwebsite\Excel\Facades\Excel;
|
|
use Illuminate\Database\QueryException;
|
|
use Yajra\DataTables\Facades\DataTables;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
|
|
|
class HomeController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
$atribut = Atribut::all();
|
|
return view('user.index', compact('atribut'));
|
|
}
|
|
public function user_testing()
|
|
{
|
|
$atribut = Atribut::get();
|
|
if (count($atribut) === 0) {
|
|
return to_route('atribut.index')
|
|
->withWarning('Tambahkan Atribut dulu sebelum menginput Dataset');
|
|
}
|
|
$nilai = NilaiAtribut::get();
|
|
$calculated = Probability::count();
|
|
$hasil = ProbabLabel::$label;
|
|
return view(
|
|
'user.dataset.testing',
|
|
compact('atribut', 'nilai', 'calculated', 'hasil')
|
|
);
|
|
}
|
|
public function user_training()
|
|
{
|
|
$atribut = Atribut::get();
|
|
if (count($atribut) === 0) {
|
|
return to_route('atribut.index')
|
|
->withWarning('Tambahkan Atribut dulu sebelum menginput Dataset');
|
|
}
|
|
$nilai = NilaiAtribut::get();
|
|
$hasil = ProbabLabel::$label;
|
|
return view('user.dataset.training', compact('atribut', 'nilai', 'hasil'));
|
|
}
|
|
|
|
public function user_probab()
|
|
{
|
|
$atribut = Atribut::get();
|
|
if (count($atribut) === 0) {
|
|
return to_route('atribut.index')
|
|
->withWarning('Atribut dan Nilai Atribut Kosong');
|
|
}
|
|
$nilaiattr = NilaiAtribut::get();
|
|
$data = Probability::get();
|
|
$kelas = Probability::probabKelas();
|
|
$training = TrainingData::get();
|
|
$attribs = ['atribut' => $atribut, 'nilai' => $nilaiattr];
|
|
$hasil = ProbabLabel::$label;
|
|
return view(
|
|
'user.naivebayes.probab',
|
|
compact('attribs', 'data', 'kelas', 'training', 'hasil')
|
|
);
|
|
}
|
|
public function user_class()
|
|
{
|
|
return view('user.naivebayes.classify', ['hasil' => ProbabLabel::$label]);
|
|
}
|
|
|
|
public function profile()
|
|
{
|
|
return view('user.profil');
|
|
}
|
|
|
|
public function update(Request $request)
|
|
{
|
|
$id = auth()->user()->id;
|
|
$validator = Validator::make($request->all(), [
|
|
'name' => 'required',
|
|
'email' => 'required',
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
return redirect()->back()->with('error', 'terdapat data yang belum diisi')->withInput();
|
|
}
|
|
|
|
$input = $request->all();
|
|
$findUser = User::find($id);
|
|
|
|
if($findUser) {
|
|
// update data
|
|
$findUser->name = $input['name'];
|
|
$findUser->email = $input['email'];
|
|
if(!empty($input['password'])) {
|
|
$findUser->password = bcrypt($input['password']);
|
|
}
|
|
|
|
// save data
|
|
$findUser->save();
|
|
return redirect()->back()->with('success', 'Data berhasil disimpan');
|
|
} else {
|
|
return redirect()->back()->with('error', 'Tidak ditemukan data!');
|
|
}
|
|
}
|
|
|
|
public function performa()
|
|
{
|
|
if (Classification::count() === 0) {
|
|
return to_route('class.index')
|
|
->withWarning('Lakukan klasifikasi dulu sebelum melihat performa klasifikasi');
|
|
}
|
|
$data['train'] = $this->cm('train');
|
|
$data['test'] = $this->cm('test');
|
|
$performa['train'] = $this->performas($data['train']);
|
|
$performa['test'] = $this->performas($data['test']);
|
|
$stat = ProbabLabel::$label;
|
|
return view('user.performa', compact('data', 'performa', 'stat'));
|
|
}
|
|
|
|
private static function cm(string $type)
|
|
{
|
|
$tp = Classification::where('type', $type)->where('predicted', true)
|
|
->where('real', true)->count(); //True Positive
|
|
$fp = Classification::where('type', $type)->where('predicted', true)
|
|
->where('real', false)->count(); //False Positive
|
|
$fn = Classification::where('type', $type)->where('predicted', false)
|
|
->where('real', true)->count(); //False Negative
|
|
$tn = Classification::where('type', $type)->where('predicted', false)
|
|
->where('real', false)->count(); //True Negative
|
|
$total = $tp + $fp + $fn + $tn;
|
|
return [
|
|
'tp' => $tp,
|
|
'fp' => $fp,
|
|
'fn' => $fn,
|
|
'tn' => $tn,
|
|
'total' => $total
|
|
];
|
|
}
|
|
private static function performas(array $data)
|
|
{
|
|
if ($data['total'] === 0) $accu = $prec = $rec = $f1 = 0;
|
|
else {
|
|
$accu = (($data['tp'] + $data['tn']) / $data['total']) * 100;
|
|
$prec = ($data['tp'] / ($data['tp'] + $data['fp'])) * 100;
|
|
$rec = ($data['tp'] / ($data['tp'] + $data['fn'])) * 100;
|
|
$f1 = 2 * ($prec * $rec) / ($prec + $rec);
|
|
}
|
|
return [
|
|
'accuracy' => $accu, 'precision' => $prec, 'recall' => $rec, 'f1' => $f1
|
|
];
|
|
}
|
|
}
|