I am building a Logistic Regression platform in PHP. The following code snippet works fine when there is only one feature inside the data frame. For example a CSV file like this:
"sample","language"
"Hello, how are you?","english",
"Je voudrais une boƮte de chocolats.","french"
...
However, when I try to train the AI with 2 features based on the titanic survival rate (hypothesis: Does the amount of siblings and spouses effect the survival rate) with a data frame like this:
"SibSp","Parch","Survived",
"1", "1", "1",
"3", "3", "1",
"4", "1", "0"
...
I am getting this error:
Phpml\Exception\InvalidArgumentException Size of given arrays does not match
My code snippet looks like this, $request->features holds the amount of features this data frame has since features +1 will hold the actual outcome (1 = survived, 0 = died):
$dataset = new CsvDataset($file, (int) $request->features);
$vectorizer = new TokenCountVectorizer(new WordTokenizer());
$tfIdfTransformer = new TfIdfTransformer();
$samples = [];
for($i = 0; $i <= $request->features -1; $i++):
foreach ($dataset->getSamples() as $sample):
$samples[$i][] = $sample[$i];
endforeach;
endfor;
for($i = 0; $i <= count($samples) -1; $i++):
$vectorizer->fit($samples[$i]);
$vectorizer->transform($samples[$i]);
$tfIdfTransformer->fit($samples[$i]);
$tfIdfTransformer->transform($samples[$i]);
endfor;
$dataset = new ArrayDataset($samples, $dataset->getTargets()); # This throws the error
I am using PHP-AI/PHP-ML and here is an example of how the AI works with the data frame with only 1 feature provided by the framework.
I understand the error, $dataset->getTargets() only holds 1 array, where as $samples holds 2 arrays. However, this has got me stumped since that is how it should be (in theory).
I am storing the classifier (or trained AI) as a serialised object inside my database once it has been trained to remember its trained state. Everything works fine when I only use a data frame with one feature. Does anyone have experience using PHP-AI within the PHP-ML library that can help?
How can I increase the amount of features inside PHP-AI?
Update to show what values my arrays hold:
$samples looks like this (array of siblings, array of spouses):
array ( 0 => array ( 0 => array ( ), 1 => array ( ), 2 => array ( ), 3 => array ( ), 4 => array ( ), 5 => array ( ), 6 => array ( ), 7 => array ( ), ), 1 => array ( 0 => array ( ), 1 => array ( ), 2 => array ( ), 3 => array ( ), 4 => array ( ), 5 => array ( ), 6 => array ( ), 7 => array ( ), ), )
$dataset->getTargets() looks like this (survived or died):
array ( 0 => '1', 1 => '1', 2 => '0', 3 => '1', 4 => '0', 5 => '0', 6 => '1', 7 => '1', )
I believe that the $samples array should be 1 array holding child arrays of [SibSp, Spous]. I cannot think how to re-organise the array to be like this.
After fiddling around with the code and researching the error and how to get around it - I realised that the $samples data should be expressed as
Array [ 0 => [SibSp, Spous], 1 => [SibSp, Spous], ... ]
So by re-fiddling the data like so:
$result = [];
foreach($samples as $arr) {
foreach($arr as $k => $v) {
$result[$k][] = $v;
}
}
I can achieve this desired outcome. I still had to push the samples into the vectorizer as $sample but the final Dataset had to be re-fiddled:
for($i = 0; $i <= count($samples) -1; $i++):
$vectorizer->fit($samples[$i]);
$vectorizer->transform($samples[$i]);
$tfIdfTransformer->fit($samples[$i]);
$tfIdfTransformer->transform($samples[$i]);
endfor;
$dataset = new ArrayDataset($result, $dataset->getTargets());
Related
I have a a array like below and i want min value and it's index for searching tax class id
Array
(
[tax_class_id] => Array
(
[0] => 12
[1] => 13
[2] => 13
)
[price] => Array
(
[0] => 6233
[1] => 3195
[2] => 19192
)
)
and i am searching least price and respective key in tax_class_id. In this Senario, i require lowest in price i.e 3195 and tax_id - 13 i.e key [1]
My Code is
$prod_total = array();
for($i = 1;$i <= $chunk;$i++){
if($i == 1) {
$min_product_amt = min($product_amt['price']);
$index = array_search($min_product_amt, $product_amt);
$product_total = $min_product_amt;
//ceil Round numbers up to the nearest integer
$prod_total['price'] = ceil($product_total * $discount/100);
$prod_total['tax_id'] = $product_amt['tax_class_id'];
//Remove the first element from an array
array_shift($product_amt['price']);
array_shift($product_amt['tax_class_id']);
} else {
$second_min_product_amt = min($product_amt['price']);
$index = array_search($min_product_amt, $product_amt);
$product_total = $second_min_product_amt;
$prod_total['price'] = ceil($product_total * $discount/100);
$prod_total['tax_id'] = $product_amt['tax_class_id'];
array_shift($product_amt['price']);
array_shift($product_amt['tax_class_id']);
}
}
print_r($prod_total);
die;
$array=Array
(
'tax_class_id' => Array
(
0 => 12,
1 => 13,
2 => 13
),
'price' => Array
(
0 => 6233,
1 => 3195,
2 => 19192
)
);
$minValue= min($array['price']);
$minKey=array_keys($array['price'], $minValue);
$tax_id=$array['tax_class_id'][$minKey[0]];
echo $tax_id;
This code will work for your issue. First i get the minimum value of nested array price and then it's associated key. After that i just access the nested array tax_class_id and get the value of the field i need like accessing every array.
$data = [
"tax_class_id" => [
12,
13,
13
],
"price" => [
6233,
3195,
19192
]
];
$lowestFound;
foreach($data["price"] as $i => $price){
if(!$lowestFound || $lowestFound[1] > $price)
$lowestFound = [$i,$price];
}
echo $data["tax_class_id"][$lowestFound[0]];
This code get tax_class_id of lowest price key in one cycle.
I think array_column gives you a nice output.
$array=Array
(
'tax_class_id' => Array(
0 => 12,
1 => 13,
2 => 13
),
'price' => Array(
0 => 6233,
1 => 3195,
2 => 19192
)
);
// Find minimum value
$min= min($array['price']);
// Find key of min value
$Key=array_search($min, $array['price']);
// Extract all values with key[min value]
$new = array_column($array, $Key);
Var_dump($new);
The output in $new will now be
array(2) {
[0]=> int(13)
[1]=> int(3195)
}
Basically both of the values you are looking for.
https://3v4l.org/NsdiS
Below is the array output which shows user data, month wise. I need to get the sum of ACTUAL_HOURS for different resources.
For below, sum of ACTUAL_HOURS for User 1 and User 2 for the month JUL-2015, should be 10 + 20 = 30.
Same goes for AUG-2015 which is 80 + 20 = 100
$user_array output
Array
(
[User 1] => Array
(
[JUL-2015] => Array
(
[SITE_STATUS] => Offshore
[ACTUAL_HOURS] => 10
[PROJECTED_HOURS] => 20
)
[AUG-2015] => Array
(
[SITE_STATUS] => Offshore
[ACTUAL_HOURS] => 80
[PROJECTED_HOURS] => 88
)
)
[User 2] => Array
(
[JUL-2015] => Array
(
[SITE_STATUS] => Offshore
[ACTUAL_HOURS] => 20
[PROJECTED_HOURS] => 0
)
[AUG-2015] => Array
(
[SITE_STATUS] => Offshore
[ACTUAL_HOURS] => 20
[PROJECTED_HOURS] => 0
)
)
)
$project_months output
Array
(
[0] => JUL-2015
[1] => AUG-2015
)
Looping the data like below gives summation but not for User 1
foreach ($user_array as $user_name => $user_data) {
foreach ($project_months as $month) {
}
}
How do I show summation of ACTUAL_HOURS or PROJECTED_HOURS for different resource month wise as shown above ?
You can simply use two foreach loops.
$hoursInMonth = array();
foreach ($users as $user)
{
foreach ($user as $month => $userData) {
$hoursInMonth[$month] += $userData['ACTUAL_HOURS'];
}
}
Considering the $users variable is the array from the question, this yields the following result:
Array
(
[JUL-2015] => 30
[AUG-2015] => 100
)
You could wrap it in a function, so you can easily switch between the desired index.
/**
* #param array $inputArray The input array
* #param string $typeOfHours The desired index
* #return array
*/
function calculateHoursInMonths(array $inputArray, $typeOfHours)
{
$hoursInMonth = array();
foreach ($inputArray as $user)
{
foreach ($user as $month => $userData) {
$hoursInMonth[$month] += $userData[$typeOfHours];
}
}
return $hoursInMonth;
}
Note, this does not take in consideration the Offshore/Onshore state. You can add that by adding a simple if.
You are doing it wrong. There is no need for two nested foreach loops. You just need a for loop to loop through project months and a foreach loop to loop through users array.
You did not shared the array in php code, but I assume you are array is as below:
$array = array(
'User 1' => array(
'JUL-2015' => array(
'SITES_STATUS' => 'Offshore',
'ACTUAL_HOURS' => 10,
'PROJECTED_HOURS' => 20
),
'AUG-2015' => array(
'SITES_STATUS' => 'Offshore',
'ACTUAL_HOURS' => 80,
'PROJECTED_HOURS' => 88
),
),
'User 2' => array(
'JUL-2015' => array(
'SITES_STATUS' => 'Offshore',
'ACTUAL_HOURS' => 20,
'PROJECTED_HOURS' => 0
),
'AUG-2015' => array(
'SITES_STATUS' => 'Offshore',
'ACTUAL_HOURS' => 20,
'PROJECTED_HOURS' => 0
),
),
);
And months are below:
$project_months = array('JUL-2015', 'AUG-2015');
Now to calculate total actual hours for all users for each month, you will need loops like below:
for ($i=0; $i < count($project_months); $i++) {
$totalActualHours = 0;
foreach($array AS $ar) {
$totalActualHours += $ar[$project_months[$i]]['ACTUAL_HOURS'];
}
echo $project_months[$i].': '.$totalActualHours.'<br>';
}
The output of this code when run for ACTUAL_HOURS:
JUL-2015: 30
AUG-2015: 100
Hope this will help.
I have an array:
Array
(
[product1] => Array
(
[id] => 1
[title] => 'p1'
[extra] => Array(
[date] => '1990-02-04 16:40:26'
)
)
[product2] => Array
(
[id] => 2
[title] => 'p2'
[extra] => Array(
[date] => '1980-01-04 16:40:26'
)
)
[product3] => Array
(
[id] => 3
[title] => 'p3'
[extra] => Array(
[date] => '2000-01-04 16:40:26'
)
)
[product4] => Array
(
[id] => 4
[title] => 'p4'
[extra] => Array(
[date] => '1995-01-04 16:40:26'
)
)
[product5] => Array
(
[id] => 5
[title] => 'p5'
[extra] => Array(
[date] => '1960-01-04 16:40:26'
)
)
...
I need to get 2 products with the latest date and move them to the start of the array.
I've looked into the multisort function, and I could sort the array like this, but then the entire array would be arranged by date, I want to maintain the order of the array but just bump up the latest 2 rows.
I need to pick out the 2 latest (order by date) from the array, then move these to the start of the array. So the order of the ids should be:
3,4,1,2,5
The latest 2 have been moved to the front of the array, the remainder are still ordered by id.
Not the most optimal implementation, but the most straight forward:
$array = /* your data */;
$latest = $array;
uasort($latest, function (array $a, array $b) {
return strtotime($a['extra']['date']) - strtotime($b['extra']['date']);
});
array_splice($latest, 2);
$latestOnTop = array_merge($latest, array_diff_key($array, $latest));
The array_splice operation requires that your array keys are actually product1 or similar; won't work with numeric indices, as they'll be renumbered. Use another truncation mechanism if that's the case.
If your array is really big, a complete sort will be unnecessarily slow. In that case, you should rather loop over the array once, keeping track of the two latest items (and their keys) you could find, then array_diff_key and array_merge on that. That's a bit more difficult to implement (left as exercise for the reader), but much more efficient.
// Making array with only dates
$dates = array();
foreach ($arr as $key => $item)
$dates[$key] = $item['extra']['date'];
// Sort it by date saving keys
uasort($dates, function($i1, $i2) { return strtotime($i1) - strtotime($i2); });
// Take keys
$dates = array_keys($dates);
// Create array with two needed items
$newarray = array( $dates[0] => $arr[$dates[0]], $dates[1] => $arr[$dates[1]]);
// remove these items
unset($arr[$dates[0]]); unset($arr[$dates[1]]);
// put them in array start
$arr = array_merge($newarray, $arr);
var_dump($arr);
// copy current array for new array
$temp = $input;
// sort temp array by latest date
uasort($temp, function($a,$b) {
return (strtotime($a['extra']['date']) < strtotime($b['extra']['date']));
});
// for 2 key value pairs to get on top
$sorted_keys = array_keys($temp);
// initialize your required array
$final = [];
// two keys to move on top
$final [ $sorted_keys[0] ] = $temp [ $sorted_keys[0] ];
$final [ $sorted_keys[1] ] = $temp [ $sorted_keys[1] ];
foreach ($input as $k => $v)
{
// insert your other array values except two latest
if(!array_key_exists($k, $final))
{
$final[$k]=$v;
}
}
unset($temp); // free up resource
$final is your required array
Struggling with concept of an associative array that maps userIDs to partIDs and re-order quantity for the part.
We have bunch of parts that I need to re-order, but I must keep track of which user needs what parts re-purchased for them. The list of UserIDs comes from one table, then the inventory_used comes from another table.
Suppose a list like this:
Example One:
UserID PartID Qty_Used
1 3 2
1 4 7
2 1 4
2 4 3
3 3 5
After creating an array with the above information, I must create a re-order form (table) for the parts. Therefore, ignoring the userID, group them by the partID, and sum up the total Qty (per part). The re-order table should look something like this:
Example Two:
PartID Qty_to_Reorder
1 4
3 7
4 10
I know I'm going to take a ton of downvotes for failing to show code, but I can't wrap my mind around this seemingly simple problem. (This is for my office, not a school project).
How do I:
(1) Structure the first array (what would the loop to create it look like?), and then
(2) Loop through that array to summarize/group partIDs => Qty for re-order report, as per 2nd example above?
For the first loop, i was thinking of something like this:
Loop through UserIDs {
Loop through PartIDs {
$arrReorder[UserID][PartID] = Qty_Used;
}
}
Is that correct? How would I loop through $arrReorder to sum-up the qty used for each partID, and get the re-order report (example 2)?
SELECT SUM(Qty_Used) AS total FROM mytable WHERE PartID=3
PS: Using PHP
<?php
$data = array();
$data[] = array("UserID" => 1, "PartID" => 3, "Qty_Used" => 2);
$data[] = array("UserID" => 1, "PartID" => 4, "Qty_Used" => 7);
$data[] = array("UserID" => 2, "PartID" => 1, "Qty_Used" => 4);
$data[] = array("UserID" => 2, "PartID" => 4, "Qty_Used" => 3);
$data[] = array("UserID" => 3, "PartID" => 3, "Qty_Used" => 5);
$PartID = 3;
$sum = 0;
foreach ($data as $arr) {
if ($arr['PartID'] == $PartID)
$sum += $arr['Qty_Used'];
}
echo $PartID."\t".$sum."\r\n";
?>
Arrays have a key and value where the value can be another array. Determine what is the key value.
I am assuming you have users consuming parts and you have to re-order the parts from your supplier. No where the user is a customer and the user has a auto re-order policy.
What triggers a reorder? If re-order quantity is 10 and user uses 1, there should be nine in stock.
Create the partsUsed array elements, This is a bit tricky:
$partsUsed[$part][] = array($qtyUsed,$user);
The reason the empty brackets [] is there is to allow duplicate part numbers in the parts used, and still key part as the key.
The value is an array to key the association between user and parts.
what you end up with is a sequentially numbered secondary key where the value is just a throw away, but allows duplicate part numbers.
$partsUsed[3][] = array(2,1);
$partsUsed[4][] = array(7,1);
$partsUsed[1][] = array(4,2);
$partsUsed[4][] = array(3,2);
$partsUsed[3][] = array(5,5);
ksort($partsUsed); // sorts key on part number
var_export($partsUsed);
Result array (var_export):
array (
1 =>
array (
0 =>
array (
0 => 4,
1 => 2,
),
),
3 =>
array (
0 =>
array (
0 => 2,
1 => 1,
),
1 =>
array (
0 => 5,
1 => 5,
),
),
4 =>
array (
0 =>
array (
0 => 7,
1 => 1,
),
1 =>
array (
0 => 3,
1 => 2,
),
),
)
Notice part 3 and 4 have two arrays.
$reorder[1] = 4 ;
$reorder[2] = 7 ;
$reorder[4] = 10 ;
var_export($reorder);
Result array:
array (
1 => 4,
2 => 7,
4 => 10,
)
Now not sure how to determine what gets reordered and how many.
I'll show how to get the values:
foreach($partsUsed as $part => $value){
foreach($value as $k => $v){
echo "Part $part: Qty:$v[0] User:$v[1]\n";
$qtyUsed[$part] += $v[1];
}
}
var_export($qtyUsed);
Outputs:
Part 1: Qty:4 User:2
Part 3: Qty:2 User:1
Part 3: Qty:5 User:3
Part 4: Qty:7 User:1
Part 4: Qty:3 User:2
$qtyUsed
array (
1 => 2,
3 => 4,
4 => 3,
)
foreach ($qtyUsed as $part => $qty){
$order[$part] = $reorder[$part];
}
var_export($order);
Result $order:
array (
1 => 4,
3 => 7,
4 => 10,
)
I i'm developing php application. I have used Google Chart API for display charts.
I have select and returned necessary data for chart.
I got following array as my output.
print_r($output);
//Out put
Array
(
[0] => Array
(
[month] => April
[sec_id] => 2
[sec_name] => Commerce
[count] => 1
)
[1] => Array
(
[month] => June
[sec_id] => 2
[sec_name] => Commerce
[count] => 3
)
[2] => Array
(
[month] => July
[sec_id] => 2
[sec_name] => Commerce
[count] => 1
)
[3] => Array
(
[month] => August
[sec_id] => 4
[sec_name] => Science
[count] => 3
)
[4] => Array
(
[month] => August
[sec_id] => 3
[sec_name] => Maths
[count] => 2
)
[5] => Array
(
[month] => August
[sec_id] => 1
[sec_name] => Art
[count] => 2
)
[6] => Array
(
[month] => August
[sec_id] => 2
[sec_name] => Commerce
[count] => 2
)
)
print_r(json_encode($output)); // return above array as output
I request above data using ajax ( data type is JSON)
I want to return data as bellow to generate google chart.
[
['Month', 'Art', 'Commerce', 'Maths', 'Sience'],
['April', '', 2, '', ''],
['June', '', 3, '', ''],
['July', '', 1, '', ''],
['August', 2, 2, 3, 3]
]
I tried this this code
$output = array();
$output[0] = array('Month', 'Art', 'Commerce', 'Maths', 'Science');
foreach($records as $key=> $record){
$art =''; $commerce =''; $maths=''; $science='';
if($record['sec_id'] == 1){
$art = $record['count'];
}else if($record['sec_id'] == 2){
$commerce = $record['count'];
}else if($record['sec_id'] == 3){
$maths = $record['count'];
}else if($record['sec_id'] == 4){
$science = $record['count'];
}
$output[++$key] = array(0 => $record['month'], 1 => $art, 2 => $commerce, 3 => $maths, 4 => $science);
}
function super_unique($array){
$result = array_map("unserialize", array_unique(array_map("serialize", $array)));
foreach ($result as $key => $value){
if ( is_array($value)){
$result[$key] = super_unique($value);
}
}
return $result;
}
$output = super_unique($output);
Out put was
[["Month","Art","Commerce","Maths","Science"],["April","","1"],["June","","3"],["July","","1"],{"0":"August","1":"","4":"3"},{"0":"August","1":"","3":"2"},["August","2",""],["August","","2"]]
This is pretty straightforward to loop through and reorganize, particularly since your sec_ids match up nicely with the array indices.
Example:
$temp = array();
$output = array(
array('Month', 'Art', 'Commerce', 'Maths', 'Science')
);
foreach ($records as $record) {
$month = $record["month"];
$sec_id = $record["sec_id"];
$count = $record["count"];
if (!isset($temp[$month])) {
$temp[$month] = array_fill(0, 5, '');
}
$temp[$month][0] = $month;
$temp[$month][$sec_id] += $count;
}
$output = array_merge($output, array_values($temp));
echo json_encode($output);
Output:
[["Month","Art","Commerce","Maths","Science"],["April","",2,"",""],["June","",3,"",""],["July","",1,"",""],["August",2,2,2,3]]
Let's rethink your algorithm. As you go through each element in records, we need to ask two things: what data are we pulling out and what do we want to do with it?
The first question is simple: we're just pulling out the value 'count'.
The second question is going to determine our algorithm. What we want to do is take that 'count' value, and stick it a particular spot in our ourpur array.
Looking at the output array as a table, we can see that the desired position of 'count' is determined by the 'month' field (which determines the row) and by the 'sec_id'/'sec_name' fields (which determine the column). So what you want your loop to look like is this...
foreach($records as $record)
{
$output[$record['month']][$record['sec_id']] = $record['count']
}
The first caveat to this is that for each unique month, you do still need to create and initialize the sub-array, and you must do it only once. So, the loop becomes.
foreach($records as $record)
{
if(!is_array($output[$record['month']]))
$output[$record['month']] = array(0 => $record['month'], 1 => '', 2 => '', 3 => '', 4 => '');
$output[$record['month']][$record['sec_id']] = $record['count']
}
Finally, we used the actual month name as the keys in the top-level array. To comply with the numeric-only keys specified in your desired output, we can ditch those keys with the following piece of code.
$output = array_values($output)
If I'm right in thinking you were trying to use super_unique() to combine rows with the same month, that's not what it was doing at all. Firstly, array_unique() doesn't combine rows, it eliminates duplicates. Since you were comparing serialized rows, rather than just looking at the month field, none of your rows were duplicates, so that function was doing nothing. Furthermore, since several of your array fields were set to '', the serialize/unserialize process was actually causing those fields to get dropped, which is why you were ending up with sub-arrays of less than five elements, and with associative keys.