moved grouplens-algorithm to own class

highcharts
agp8x 2014-08-27 00:38:42 +02:00
parent df16d71f35
commit 4eebfe79ff
4 changed files with 76 additions and 84 deletions

1
.gitignore vendored
View File

@ -4,4 +4,5 @@ data/
*.svg
charts/
config.php
newData

71
lib/Grouplens.php Normal file
View File

@ -0,0 +1,71 @@
<?php
class Grouplens{
public function r($p,$i,$data){
return $this->average($p,$p) + $this->c($p,$i,$data);
}
public function wrapR($subject, $item, $data){
return $this->r($data[$subject], $item, $data);
}
private function sim($a, $b){
$averageA=$this->average($a,$b);
$averageB=$this->average($b,$a);
$div=0.0;
$sumA=0;
$sumB=0;
foreach($a as $i=>$value){
if(!empty($value) && !empty($b[$i])){
$div+=($value - $averageA)*($b[$i] - $averageB);
$sumA+=pow(($value-$averageA), 2);
$sumB+=pow(($b[$i]-$averageB), 2);
}
}
return ($div / (sqrt($sumA) * sqrt($sumB)));
}
private function c($p,$i,$data){
$divident=$divisor=0.0;
foreach($data as $q){
if (empty($q[$i]) || $p===$q){
continue;
}
$simPQ=$this->sim($p,$q);
$averageQ=$this->average($q,$p);
$divisor+=abs($simPQ);
$divident+=(($q[$i]-$averageQ)*$simPQ);
}
return ($divident / $divisor);
}
private function average($set, $controlSet){
$average=0.0;
$count=0;
foreach($set as $i=>$value){
if (!empty($value) && !empty($controlSet[$i])){
$average+=$value;
$count+=1;
}
}
return ($average / $count);
}
}
#$ratings=array(
# "alice"=>array(5, 1, 0, 3, 2),
# "bob"=>array(3, 1, 5, 4, 2),
# "carol"=>array(4, 0, 5, 0, 3),
# "chuck"=>array(1, 4, 0, 0, 2),
# "dave"=>array(0, 4, 3, 0, 1),
# "eve"=>array(5, 4, 5, 4, 3),
# "fran"=>array(4, 0, 0, 0,2),
# "gordon"=>array(3, 4, 0, 5, 1),
# "isaac"=>array(5, 0, 4, 3, 0),
# "ivan"=>array(3, 1, 1, 0, 1)
#);
#echo sim($ratings['alice'],$ratings['ivan']);
#echo "<br>";
#echo "<br>";
#echo wrapR('alice', 2, $ratings);
#

View File

@ -1,78 +1,11 @@
<?php
include('lib/Grouplens.php');
$start=microtime(true);
function sim($a, $b){
$averageA=average($a,$b);
$averageB=average($b,$a);
$div=0.0;
$sumA=0;
$sumB=0;
foreach($a as $i=>$value){
if(!empty($value) && !empty($b[$i])){
$div+=($value - $averageA)*($b[$i] - $averageB);
$sumA+=pow(($value-$averageA), 2);
$sumB+=pow(($b[$i]-$averageB), 2);
}
}
return ($div / (sqrt($sumA) * sqrt($sumB)));
}
function r($p,$i,$data){
return average($p,$p) + c($p,$i,$data);
}
function c($p,$i,$data){
$divident=$divisor=0.0;
foreach($data as $q){
if (empty($q[$i]) || $p===$q){
continue;
}
$simPQ=sim($p,$q);
$averageQ=average($q,$p);
$divisor+=abs($simPQ);
$divident+=(($q[$i]-$averageQ)*$simPQ);
}
return ($divident / $divisor);
}
function wrapR($subject, $item, $data){
return r($data[$subject], $item, $data);
}
function average($set, $controlSet){
$average=0.0;
$count=0;
foreach($set as $i=>$value){
if (!empty($value) && !empty($controlSet[$i])){
$average+=$value;
$count+=1;
}
}
return ($average / $count);
}
#$ratings=array(
# "alice"=>array(5, 1, 0, 3, 2),
# "bob"=>array(3, 1, 5, 4, 2),
# "carol"=>array(4, 0, 5, 0, 3),
# "chuck"=>array(1, 4, 0, 0, 2),
# "dave"=>array(0, 4, 3, 0, 1),
# "eve"=>array(5, 4, 5, 4, 3),
# "fran"=>array(4, 0, 0, 0,2),
# "gordon"=>array(3, 4, 0, 5, 1),
# "isaac"=>array(5, 0, 4, 3, 0),
# "ivan"=>array(3, 1, 1, 0, 1)
#);
#echo sim($ratings['alice'],$ratings['ivan']);
#echo "<br>";
#echo "<br>";
#echo wrapR('alice', 2, $ratings);
#
#die();
echo "<title>[BETA] prediction</title>";
$date=array(date("Y"),date("n"),date("j"));
#$date=array(2014, 8, 26);
$diffuse=0;
$selectionStart=mktime(0,0,0,$date[1],$date[2],$date[0]);
@ -84,8 +17,6 @@ foreach($data as $record){
}
$data=null;
#var_dump($tupel);
#echo date("H:i:s", $tupel['time']);
$month=date('n', $tupel['time']);
$year=date('Y', $tupel['time']);
$day=date('j', $tupel['time']);
@ -100,19 +31,13 @@ for($i=1; $i<=$day;$i++){
$sumary[]=prepareData($data,$div,$selectionStart,$selectionEnd);
}
#foreach($sumary as $key=>$daily){
# echo "<tr><td>Day: ".($key+1)."</td>";
# foreach ($daily as $key=>$hour){
# echo "<td>".$key."-".$hour." ";
# }
# echo "</tr>";
#}
$gl=new Grouplens();
$div=getdiv('temp');
echo "prediction for ".date("d.m.Y");
echo "<table border='border-collapse'>\n";
echo "<tr><th>hour</th> <th>prediction</th> <th>difference</th><th>actual measurement</th></tr>\n";
for ($i=0;$i<25;$i++){
$r=wrapR($day-1,$i,$sumary);
$r=$gl->wrapR($day-1,$i,$sumary);
$r=$r/$div;
$r=round($r,2);
$a=$sumary[$day-1][$i+1]/$div;
@ -122,11 +47,6 @@ for ($i=0;$i<25;$i++){
echo "</table>\n";
#for($i=0;$i<=24;$i++){
# $hours[]=$i*60*60;
#}
#var_dump($hours);
#var_dump(array($month,$year,$day));
$runtime=microtime(true)-$start;
echo "<div style='position:fixed;bottom:20px;right:50px;' >Runtime: ".$runtime." s</div>";
$db->close();

2
recent
View File

@ -1 +1 @@
Temperatur: 14.75 C<br>Luftfeuchtigkeit: 84.9 %<br>Helligkeit: 0.01 Lux<br>Luftdruck: 981.881mbar
Temperatur: 13.43 C<br>Luftfeuchtigkeit: 89.5 %<br>Helligkeit: 0.01 Lux<br>Luftdruck: 982.473mbar