-franciskane- Posted March 1, 2021 Share Posted March 1, 2021 Hello! I found this code from github. https://github.com/gburtini/Learning-Library-for-PHP and I want to know if its possible to add variables to it and what changes needed to be done to the whole script. My first guess is the variables are $xs and $ys I want replace them and add two more. The whole code is here: <?php require_once 'function.php'; function ll_nn_predict($xs, $ys, $row, $k) { $distances = ll_nearestNeighbors($xs, $row); $distances = array_slice($distances, 0, $k); $predictions = array(); foreach($distances as $neighbor=>$distance) { $predictions[$ys[$neighbor]]++; } asort($predictions); return $predictions; } function ll_nearestNeighbors($xs, $row) { $testPoint = $xs[$row]; $distances = _ll_distances_to_point($xs, $testPoint); return $distances; } function _ll_distances_to_point($xs, $x) { $distances = array(); foreach($xs as $index=>$xi) { $distances[$index] = ll_euclidean_distance($xi, $x); } asort($distances); array_shift($distances); return $distances; } and the function.php that is required is this: <?php function ll_transpose($rows) { $columns = array(); for($i=0;$i<count($rows);$i++) { for($k = 0; $k<count($rows[$i]); $k++) { $columns[$k][$i] = $rows[$i][$k]; } } return $columns; } function ll_mean($array) { return array_sum($array)/($array); } function ll_variance($array) { $mean = ll_mean($array); $sum_difference = 0; $n = count($array); for($i=0; $i<$n; $i++) { $sum_difference += pow(($array[$i] - $mean),2); } $variance = $sum_difference / $n; return $variance; } function ll_euclidean_distance($a, $b) { if(count($a) != count($b)) return false; $distance = 0; for($i=0;$i<count($a);$i++) { $distance += pow($a[$i] - $b[$i], 2); } return sqrt($distance); } I just want to know where is the variables in those 2 scripts and add more as I'm not well versed in using PHP. Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/312228-adding-variables-to-an-existing-php-scripts/ Share on other sites More sharing options...
gw1500se Posted March 1, 2021 Share Posted March 1, 2021 You need to explain what those variables are and what you intend to do with them. Once you understand that, you should be able to figure out how to add them to the script and where on your own. If your attempt fails then come back and post what you tried and any error messages or what you got that is different from what you expected. Quote Link to comment https://forums.phpfreaks.com/topic/312228-adding-variables-to-an-existing-php-scripts/#findComment-1584818 Share on other sites More sharing options...
-franciskane- Posted March 1, 2021 Author Share Posted March 1, 2021 @gw1500sehello! Those 4 variables' data will be coming from a database but will be split into pairs. The first pair of variables will be coming from a mobile app. then the second pair will be already saved in the database and will be used as the data(s) to be compared to the first pair of variables. Or at least its what we intended to happen. Quote Link to comment https://forums.phpfreaks.com/topic/312228-adding-variables-to-an-existing-php-scripts/#findComment-1584826 Share on other sites More sharing options...
gw1500se Posted March 2, 2021 Share Posted March 2, 2021 So what did you try and what didn't work? Quote Link to comment https://forums.phpfreaks.com/topic/312228-adding-variables-to-an-existing-php-scripts/#findComment-1584838 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.