Last active
August 29, 2015 14:05
-
-
Save masazdream/31a31d1dab16375ac9ea to your computer and use it in GitHub Desktop.
adaboost分類する昔つくったphpクラス
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* pjAdaBoost.class.php | |
* | |
* AdaBoost分類をするクラス | |
* | |
* @package goalstart | |
* @subpackage pjAdaBoost | |
* | |
*/ | |
class pjAdaBoost | |
{ | |
/** | |
* AdaBoost分類器 | |
* | |
* @param int $question_id 質問ID | |
* @param array<array<int>> $coordinates 座標点の組みの配列 | |
* @param array<int> $scores スコアの配列 | |
* @return int 正解に分類した添字番号 | |
* | |
*/ | |
public static function classifierAdaBoost($question_id, $coordinates, $scores){ | |
$positive_array = array(); | |
foreach($coordinates as $key=>$coordinate){ | |
// 特徴量の配列を取得 | |
$feature_array = pjFeature::getFeatureArray($coordinate, $scores[$key]); | |
// 特徴量を分類器にかける | |
$result = self::classify($feature_array); | |
if($result == 1){ | |
$positive_array[] = $key; | |
} | |
} | |
if(count($positive_array) < 1){ | |
return -1; | |
} | |
// 最良のscoreのkeyを返す | |
return $positive_array[0]; | |
} | |
/** | |
* 弱分類器を合わせて強分類器とする | |
* | |
* @param 特徴量配列 | |
* @return 1:positive -1:negative 0:can not classify | |
*/ | |
public static function classify($feature_array){ | |
// インポート | |
$type = sfConfig::get('app_api_type_value'); | |
$threshold = sfConfig::get('app_api_threshold_value'); | |
$linear = sfConfig::get('app_api_linear_coefficient_value'); | |
// 区切り文字で分割 | |
$type_array = explode(",", $type); | |
$threshold_array = explode(",", $threshold); | |
$linear_array = explode(",", $linear); | |
$result = 0; | |
foreach($type_array as $key=>$type_c){ | |
$adaptive = -1; | |
$threshold_c = $threshold_array[$key]; | |
$linear_c = $linear_array[$key]; | |
if($type_c == 'angleMin0'){ | |
$feature = $feature_array['angle0']; | |
if($feature > $threshold_c){ | |
$adaptive = 1; | |
} | |
}elseif($type_c == 'angleMin1'){ | |
$feature = $feature_array['angle1']; | |
if($feature > $threshold_c){ | |
$adaptive = 1; | |
} | |
}elseif($type_c == 'angleMin2'){ | |
$feature = $feature_array['angle2']; | |
if($feature > $threshold_c){ | |
$adaptive = 1; | |
} | |
}elseif($type_c == 'angleMin3'){ | |
$feature = $feature_array['angle3']; | |
if($feature > $threshold_c){ | |
$adaptive = 1; | |
} | |
}elseif($type_c == 'angleMax0'){ | |
$feature = $feature_array['angle0']; | |
if($feature < $threshold_c){ | |
$adaptive = 1; | |
} | |
}elseif($type_c == 'angleMax1'){ | |
$feature = $feature_array['angle1']; | |
if($feature < $threshold_c){ | |
$adaptive = 1; | |
} | |
}elseif($type_c == 'angleMax2'){ | |
$feature = $feature_array['angle2']; | |
if($feature < $threshold_c){ | |
$adaptive = 1; | |
} | |
}elseif($type_c == 'angleMax3'){ | |
$feature = $feature_array['angle3']; | |
if($feature < $threshold_c){ | |
$adaptive = 1; | |
} | |
}elseif($type_c == 'perimeterMin'){ | |
$feature = $feature_array['perimeter']; | |
if($feature > $threshold_c){ | |
$adaptive = 1; | |
} | |
}elseif($type_c == 'perimeterMax'){ | |
$feature = $feature_array['perimeter']; | |
if($feature < $threshold_c){ | |
$adaptive = 1; | |
} | |
}elseif($type_c == 'innerMin0'){ | |
$feature = $feature_array['inner0']; | |
if($feature > $threshold_c){ | |
$adaptive = 1; | |
} | |
}elseif($type_c == 'innerMin1'){ | |
$feature = $feature_array['inner1']; | |
if($feature > $threshold_c){ | |
$adaptive = 1; | |
} | |
}elseif($type_c == 'innerMax0'){ | |
$feature = $feature_array['inner0']; | |
if($feature < $threshold_c){ | |
$adaptive = 1; | |
} | |
}elseif($type_c == 'innerMax1'){ | |
$feature = $feature_array['inner1']; | |
if($feature < $threshold_c){ | |
$adaptive = 1; | |
} | |
}elseif($type_c == 'score'){ | |
$feature = $feature_array['score']; | |
if($feature > $threshold_c){ | |
$adaptive = 1; | |
} | |
}elseif($type_c == 'deltaxmax'){ | |
$feature = $feature_array['deltax']; | |
if($feature < $threshold_c){ | |
$adaptive = 1; | |
} | |
}elseif($type_c == 'deltaxmin'){ | |
$feature = $feature_array['deltax']; | |
if($feature > $threshold_c){ | |
$adaptive = 1; | |
} | |
}elseif($type_c == 'deltaymax'){ | |
$feature = $feature_array['deltay']; | |
if($feature < $threshold_c){ | |
$adaptive = 1; | |
} | |
}elseif($type_c == 'deltaymin'){ | |
$feature = $feature_array['deltay']; | |
if($feature > $threshold_c){ | |
$adaptive = 1; | |
} | |
}elseif($type_c == 'vevmax'){ | |
$feature = $feature_array['vev']; | |
if($feature < $threshold_c){ | |
$adaptive = 1; | |
} | |
}elseif($type_c == 'vevmin'){ | |
$feature = $feature_array['vev']; | |
if($feature > $threshold_c){ | |
$adaptive = 1; | |
} | |
}else{ | |
// error | |
} | |
$result += $linear_c * $adaptive; | |
} | |
if($result > 0){ | |
return 1; | |
}elseif($result == 0){ | |
return 0; | |
}elseif($result < 0){ | |
return -1; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment