/    Sign up×
Community /Pin to ProfileBookmark

Help with testing a source code in PHP

Hello! I found this source code from github. its a NK-NN algorithm and is there a way to test it if its “working”? like put some data in an array?. I have very minimal experience with PHP. Thank you so much!

“`
**functions.php**
<?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)/count($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_euclidian_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);
}
?>

**knn.php**

<?php

require_once dirname(__FILE__) . “/../accessory/functions.php”;

// returns the predictions (sorted array) in an array (“bestprediction”=>count, “pred2″=>count2…)
function ll_nn_predict($xs, $ys, $row, $k)
{
$distances = ll_nearestNeighbors($xs, $row);
$distances = array_slice($distances, 0, $k); // get top k.

$predictions = array();
foreach($distances as $neighbor=>$distance)
{
$predictions[$ys[$neighbor]]++;
}
asort($predictions);

return $predictions;
}

// returns the nearest neighbors for the nth row of $xs (sorted euclidian distances).
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_euclidian_distance($xi, $x);
}
asort($distances);
array_shift($distances); // has “self” as the smallest distance.
return $distances;
}

?>

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumJan 25.2021 — Single backtics don't work reliably when posting code. You better use code tags: `your code here` or triple backtics.

I edited your posting accordingly.
Copy linkTweet thisAlerts:
@frncsknauthorJan 25.2021 — @Sempervivum#1627122 Thank you for the heads up ulrich! 😀
Copy linkTweet thisAlerts:
@NogDogJan 25.2021 — Make a separate script something like:
<i>
</i>&lt;?php
require_once '/path/to/above/file.php';

$rows = array(/* enter test values here */);
$result = ll_transpose($rows);
echo "RESULT:n";
var_export($rows);

Then run that from the command line in a terminal window:
<i>
</i>php whatever_you_called_it.php
Copy linkTweet thisAlerts:
@johncarryJan 28.2021 — I want more experience. thank you for sharing.
×

Success!

Help @frncskn spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 12.1,
social: @webDeveloperHQ,
});

legal: ({
terms: of use,
privacy: policy
analytics: Fullres
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: Anonymous,
tipped: article
amount: 1000 SATS,

tipper: @aldoushuxley,
tipped: article
amount: 1000 SATS,

tipper: Anonymous,
tipped: article
amount: 1000 SATS,
)...