Jump to content

Recommended Posts

my php array knowledge is still basic and i can't seem to get this code to yield an answer in the form i want.

 

i would like to be able to get at $m and $b separately so I can manipulate them.

 

how, for example do i just print $m?

 

<?php


var_dump( linear_regression(array(1, 2, 3, 4), array(1.5, 1.6, 2.1, 3.0)) );


/**
* linear regression function
* @param $x array x-coords
* @param $y array y-coords
* @returns array() m=>slope, b=>intercept
*/
function linear_regression($x, $y) {

  // calculate number points
  $n = count($x);

  // ensure both arrays of points are the same size
  if ($n != count($y)) {

    trigger_error("linear_regression(): Number of elements in coordinate arrays do not match.", E_USER_ERROR);

  }

  // calculate sums
  $x_sum = array_sum($x);
  $y_sum = array_sum($y);

  $xx_sum = 0;
  $xy_sum = 0;

  for($i = 0; $i < $n; $i++) {

    $xy_sum+=($x[$i]*$y[$i]);
    $xx_sum+=($x[$i]*$x[$i]);

  }

  // calculate slope
  $m = (($n * $xy_sum) - ($x_sum * $y_sum)) / (($n * $xx_sum) - ($x_sum * $x_sum));

  // calculate intercept
  $b = ($y_sum - ($m * $x_sum)) / $n;

  return array("m"=>$m, "b"=>$b);
}

?>

 

Link to comment
https://forums.phpfreaks.com/topic/257772-simple-array-question/
Share on other sites

If you're talking about after the function call, you get them by saving the return value and accessing them through there.

$result = linear_regression(array(1, 2, 3, 4), array(1.5, 1.6, 2.1, 3.0)) );
echo $result['m'];
echo '<br>';
echo $result['b'];

i tried

 

<?php

var_dump( linear_regression(array(1, 2, 3, 4), array(1.5, 1.6, 2.1, 3.0)) );


/**
* linear regression function
* @param $x array x-coords
* @param $y array y-coords
* @returns array() m=>slope, b=>intercept
*/
function linear_regression($x, $y) {

  // calculate number points
  $n = count($x);

  // ensure both arrays of points are the same size
  if ($n != count($y)) {

    trigger_error("linear_regression(): Number of elements in coordinate arrays do not match.", E_USER_ERROR);

  }

  // calculate sums
  $x_sum = array_sum($x);
  $y_sum = array_sum($y);

  $xx_sum = 0;
  $xy_sum = 0;

  for($i = 0; $i < $n; $i++) {

    $xy_sum+=($x[$i]*$y[$i]);
    $xx_sum+=($x[$i]*$x[$i]);

  }

  // calculate slope
  $m = (($n * $xy_sum) - ($x_sum * $y_sum)) / (($n * $xx_sum) - ($x_sum * $x_sum));

  // calculate intercept
  $b = ($y_sum - ($m * $x_sum)) / $n;

return array("m"=>$m, "b"=>$b);



}
$result = linear_regression(array(1, 2, 3, 4), array(1.5, 1.6, 2.1, 3.0) );
echo $result['m'];
echo '<br>';
echo $result['b'];


?>

 

which gave

 

array(2) { ["m"]=> float(0.5) ["b"]=> float(0.8) } 0.5

0.8

 

and i just want

 

0.5 0.8

basically where would your code fit into my script?

 

if it were the end, then the data array appears at the start and end - how do i get around having it twice?

 

You replace this:

var_dump( linear_regression(array(1, 2, 3, 4), array(1.5, 1.6, 2.1, 3.0)) );

 

with my code. 

 

var_dump() is just something you use for debugging and testing.  Once you verify whatever you need to know (in this case, the return value of linear_regression) then you just replace it with whatever you actually want the code to do.

 

ah, i see. that works really well, than you very much.

 

my final question is how can i automatically input the data from another php file?

 

the file name is Februarycetchange.txt and the content reads

 

 Date,cet,predict,16d
Wed 02/15 @ 12Z,0.7,3.4,x
Wed 02/15 @ 18Z,0.7,3.3,x
Thu 02/16 @ 00Z,0.7,3.3,x
Thu 02/16 @ 06Z,1.1,3.7,x
Thu 02/16 @ 12Z,1.1,3.7,x

 

i'd like the date to be replaced by numbers 1,2,3 and to be the x value, and the 3rd value in each row to be the y value.

 

so these values would be auto inputted into the line

 

$result = linear_regression(array(1, 2, 3, 4), array(1.5, 1.6, 2.1, 3.0)) );

 

is this possible?

 

so the values would read (1,2,3,4,5) for x and (0.7,0.7,0.7,1.1,1.1) for y.

 

is this possible please?

ok, found this code from another file i have.

 

can you please help me change it to suit my need?

 

$namefile = date("F")."cetchange.txt";
define('DATAFILE', $namefile); // External data file
require_once 'phplot.php';
function read_prices_text_data($filename)
{
    $f = fopen($filename, 'r');
    if (!$f) {
        fwrite(STDERR, "Failed to open data file: $filename\n");
        return FALSE;
    }
    // Read and check the file header.
    $row = fgetcsv($f);
    if ($row === FALSE || $row[0] != 'Date' || $row[1] != 'cet'
            || $row[2] != 'predict'  || $row[3] != '16d') {
        fwrite(STDERR, "Incorrect header in: $filename\n");
        return FALSE;
    }
    // Read the rest of the file into array keyed by date for sorting.
    while ($r = fgetcsv($f)) {
        $d[$r[0]] = array( $r[1], $r[2], $r[3]);
    }
    fclose($f);
//   ksort($d);
    // Convert to a PHPlot data array with label and 3 values per row.
    foreach ($d as $date => $r) {
        $data[] = array($date, $r[0],$r[1], $r[2]);
    }
    return $data;
}

 

i only want the first value as 1,2,3,4 and the second. ignore the rest.

 

thanks

no wusing this to add the second value in each row to try to build an array

 

nearly there i think ...but not quite..

 

// get data from the file
$data = file_get_contents('GFS/Februarycetchange.txt');

// use the newline as a delimiter to get different rows
$rows = explode("\n", $data);

// go through all the rows starting at the second row
// remember that the first row contains the headings
for($i = 1; $i < count($rows); $i++)
{
$temp = explode(',', $rows[$i]);
$date = $temp[0];
$cet = $temp[1];


$stack = array($stack);
array_push($stack, $cet);

}
print_r ($stack);

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.