Jump to content

Adding Variables to an existing PHP Script(s)


-franciskane-

Recommended Posts

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

@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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.