I'm currently doing a profiling quiz, I've decided to use jQuery and php.
The issue is that each answer has different values.
For example for question n°1 :
When you look in the mirror :
a - You smile = profile handsom -> 3
= profile cute -> 2
b - You run = profile shy -> 3
= profile uglly -> 1
c - Who's that = profile yolo -> 2
= profile zen -> 3
And so on. The result of the game gives you the profile with more points.
Here's my idea. I create an array of values [0, 0, 0, 0, 0, 0], each one is a profile.
Each answer of the game is an array, for the example above :
a => [3, 2, 0, 0, 0, 0]
b => [0, 0, 3, 1, 0, 0]
c => [0, 0, 0, 0, 2, 3]
My question is: how I update this same array summing the values?
My array, in the end, must like :
array(32, 17, 0, 5, 10, 2)
Then I'll sort it and obtain my profile.
I think this can be done only with jQuery but I can use some php. Thank you very much, the problem is tricky so I hope I've explain it properly :)
Related
I want to calculate and store the dense rank and gapped rank for all entries in an array using PHP.
I want to do this in PHP (not MySQL because I am dealing with dynamic combinations 100,000 to 900 combinations per week, that’s why I cannot use MySQL to make that many tables.
My code to find the dense ranks is working, but the gapped ranks are not correct.
PHP code
$members = [
['num' => 2, 'rank' => 0, 'dense_rank' => 0],
['num' => 2, 'rank' => 0, 'dense_rank' => 0],
['num' => 3, 'rank' => 0, 'dense_rank' => 0],
['num' => 3, 'rank' => 0, 'dense_rank' => 0],
['num' => 3, 'rank' => 0, 'dense_rank' => 0],
['num' => 3, 'rank' => 0, 'dense_rank' => 0],
['num' => 3, 'rank' => 0, 'dense_rank' => 0],
['num' => 5, 'rank' => 0, 'dense_rank' => 0],
['num' => 9, 'rank' => 0, 'dense_rank' => 0],
['num' => 9, 'rank' => 0, 'dense_rank' => 0],
['num' => 9, 'rank' => 0, 'dense_rank' => 0]
];
$rank=0;
$previous_rank=0;
$dense_rank=0;
$previous_dense_rank=0;
foreach($members as &$var){
//star of rank
if($var['num']==$previous_rank){
$var['rank']=$rank;
}else{
$var['rank']=++$rank;
$previous_rank=$var['num'];
}//end of rank
//star of rank_dense
if($var['num']===$previous_dense_rank){
$var['dense_rank']=$dense_rank;
++$dense_rank;
}else{
$var['dense_rank']=++$dense_rank;
$previous_dense_rank=$var['num'];
}
//end of rank_dense
echo $var['num'].' - '.$var['rank'].' - '.$var['dense_rank'].'<br>';
}
?>
My flawed output is:
num
rank
dynamic rank
2
1
1
2
1
1
3
2
3
3
2
3
3
2
4
3
2
5
3
2
6
5
3
8
9
4
9
9
4
9
9
4
10
Notice when the error happens and there is a higher number in the number column it corrects the error in that row. See that when the number goes from 3 to 5.
Given that your results are already sorted in an ascending fashion...
For dense ranking, you need to only increment your counter when a new score is encountered.
For gapped ranking, you need to unconditionally increment your counter and use the counter value for all members with the same score.
??= is the "null coalescing assignment" operator (a breed of "combined operator"). It only allows the right side operand to be executed/used if the left side operand is not declared or is null. This is a technique of performing conditional assignments without needing to write a classic if condition.
Code: (Demo)
$denseRank = 0;
$gappedRank = 0;
foreach ($members as &$row) {
$denseRanks[$row['num']] ??= ++$denseRank;
$row['dense_rank'] = $denseRanks[$row['num']];
++$gappedRank;
$gappedRanks[$row['num']] ??= $gappedRank;
$row['rank'] = $gappedRanks[$row['num']];
// for better presentation:
echo json_encode($row) . "\n";
}
Output:
{"num":2,"rank":1,"dense_rank":1}
{"num":2,"rank":1,"dense_rank":1}
{"num":3,"rank":3,"dense_rank":2}
{"num":3,"rank":3,"dense_rank":2}
{"num":3,"rank":3,"dense_rank":2}
{"num":3,"rank":3,"dense_rank":2}
{"num":3,"rank":3,"dense_rank":2}
{"num":5,"rank":8,"dense_rank":3}
{"num":9,"rank":9,"dense_rank":4}
{"num":9,"rank":9,"dense_rank":4}
{"num":9,"rank":9,"dense_rank":4}
For the record, if you are dealing with huge volumes of data, I would be using SQL instead of PHP for this task.
It seems like you want the dynamic rank to be sequential?
Your sample data appears to be sorted, if this remains true for your real data then you can remove the conditional and just increment the variable as you assign it:
//start of rank_dense
$var['dense_rank']=++$dense_rank;
//end of rank_dense
It sounds like you're saying you won't be implementing a database.
Databases like MySQL can easily handle the workload numbers you outlined and they can sort your data as well. You may want to reconsider.
I am new to machine learning and neural network. I have a situation where, for house providing, we study the cases of a candidate and of a housing in order to predict the answer of the candidate. I have been given an exemple looking like this :
Input1:
['male', '+60yo', 'health reasons', 'domestic abuse reasons','asked with elevator', 'already denied without elevator', ...]
Input2:
['60m²', 'city_name', 'without elevator']
Hidden layers:
['+60yo', 'health reasons'], ['asked with elevator'], ['already denied without elevator'], [etc.]
Output:
'Denied without elevator'
So, I'd like to use ANN to simulate this situation, but the tools I'm using works only with numbers data, not key words.
I'm actually doing it on php with the PHP-ML library and think of using the Multilayer Perceptron Classifier method.
Does somebody know something that could help me?
PS:
Since I don't know yet how to do such a thing, I've been training only with numerical data leading to a label, and for this here is how it looks:
// $labels is an array with all the unique possible output
$labels = ['a','b','c'];
$mlp = new MLPClassifier(4, [2], $labels);
$mlp->train(
$samples = [[1, 0, 0, 0], [0, 1, 1, 0], [1, 1, 1, 1], [0, 0, 0, 0]],
$targets = ['a', 'a', 'b', 'c']
);
$mlp->predict([[1, 1, 1, 1], [0, 0, 0, 0]]);
// return ['b', 'c'];
I'm using TCPDF in my php application, but I'm not finding a way to draw a circle with black border and filled with another color;
here my code:
$style_bollino = array('width' => 0.25, 'dash' => 0, 'color' => array(0, 0, 0));
$this->SetAlpha(1);
$this->Circle(35, 100, 4, 0, 360, 'C', $style_bollino, array(210, 0, 0));
I tried also to change 'C' parameter to 'F' or null but I didn't get the result.
I'm not able to figure out what I'm missing
kind regards,
Matt
According to one of their examples, there's a one-liner way to do it, just pass 'DF' to $style parameter:
$this->Circle(35, 100, 4, 0, 360, 'DF', $style_bollino, array(210, 0, 0));
For a list of options for this parameter, check the PHPDoc in TCPDF_STATIC::getPathPaintOperator() function.
This seems like a fairly straightforward requirement but I can't work out how to do it in one line either.
However, it's pretty simple to fill the circle and then draw another stroked circle on top of it.
foreach (array("F", "S") as $fill_style) {
$this->Circle(35, 100, 4, 0, 360, $fill_style, $style_bollino, array(210, 0, 0));
}
Can someone teach me how to create a table side by side like this with fpdf:
I'm trying to combine the multiple column with the creating table tutorial but failed. Any help would be appreciated. Thank you.
If you got struggled with fpdf, you can try to do it with Debenu Quick PDF.
This C# code returns the exact result which is on your picture. It is done with merged cells, but also you can use zero border width - it depends on your needs. Of course you can change the width and the color of the borders.
DPL.LoadFromFile("blank.pdf", "");
DPL.SetOrigin(1); //the top left page corner will be used for the origin
DPL.SetMeasurementUnits(0); //the units are approximately the same as a "point"
string content1, content2, content3;
content1 = "1<br>2<br>3<br>4<br>5<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>20";
content2 = "21<br>22<br>23<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>30";
content3 = "31<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>40";
int tableID1 = DPL.CreateTable(20, 3); //20 rows, 3 columns
DPL.SetTableColumnWidth(tableID1, 1, 1, 20); //width of the first column
DPL.SetTableColumnWidth(tableID1, 2, 3, 40); //width of the second and third column
DPL.SetTableRowHeight(tableID1, 1, 20, 10); //height of all rows
DPL.MergeTableCells(tableID1, 2, 1, 20, 1); //merge the cells from second row to 20. row and from 1. column to 1. column - merge the cells of the first column
DPL.MergeTableCells(tableID1, 2, 2, 20, 2); //merge the cells of the second column
DPL.MergeTableCells(tableID1, 2, 3, 20, 3); //merge the cells of the third column
DPL.SetTableCellContent(tableID1, 2, 1, content1); //add the content
DPL.DrawTableRows(tableID1, 50, 50, 700, 1, 0); //draw the table to the page
int tableID2 = DPL.CreateTable(20, 3); // second table
DPL.SetTableColumnWidth(tableID2, 1, 1, 20);
DPL.SetTableColumnWidth(tableID2, 2, 3, 40);
DPL.SetTableRowHeight(tableID2, 1, 20, 10);
DPL.MergeTableCells(tableID2, 2, 2, 20, 2);
DPL.MergeTableCells(tableID2, 2, 3, 20, 3);
DPL.MergeTableCells(tableID2, 2, 1, 20, 1);
DPL.SetTableCellContent(tableID2, 2, 1, content2);
DPL.DrawTableRows(tableID2, 160, 50, 700, 1, 0);
int tableID3 = DPL.CreateTable(20, 3); //third table
DPL.SetTableColumnWidth(tableID3, 1, 1, 20);
DPL.SetTableColumnWidth(tableID3, 2, 3, 40);
DPL.SetTableRowHeight(tableID3, 1, 20, 10);
DPL.MergeTableCells(tableID3, 2, 2, 20, 2);
DPL.MergeTableCells(tableID3, 2, 3, 20, 3);
DPL.MergeTableCells(tableID3, 2, 1, 20, 1);
DPL.SetTableCellContent(tableID3, 2, 1, content3);
DPL.DrawTableRows(tableID3, 270, 50, 700, 1, 0);
DPL.SaveToFile("tables.pdf");
You can find the description of the functions on this site:
http://www.debenu.com/docs/pdf_library_reference/CreateTable.php
I have an array like this:
$a = [2, 1, 1, 2, 3, 1, 3, 2];
I need to sort it, by using an external variable. But I need the following output:
$output = [
[0, s], // Move $a[0] to storage
[5, 0], // Move $a[5] to $[0]
[s, 5], // Move storage to $a[5]
[4, s], // Move $a[4] to storage
[7, 4], // Move $a[7] to $a[4]
[s, 7] // Move storage to $[7]
];
I need an algorithm to make an array, a delimitered string, or any kind of output, containing the steps to sort the array.
Mainly in PHP but I can implement it from any lang.
A fun if perhaps not that efficient idea:
Determine the count and sorted offset of each element:
$a = [2, 1, 1, 2, 3, 1, 3, 2];
$count_and_offset = [1 => [3,0], 2 => [3,3], 3 => [2,6]]
Determine the permutation,
$a_permutation = [5, 2, 3, 4, 8, 1, 7, 6];
Enumerate the cycles (http://en.wikipedia.org/wiki/Cyclic_permutation),
(1586)(2)(3)(4)(7)
Make a list (example is not zero-based),
[[6, s]
,[8, 6]
,[5, 8]
,[1, 5]
,[s, 1]]
You can use insertion sort (or do similar thing for other sorting algorithm like bubble sort or quicksort)
Pseudo code (which I took from wiki)
for i ← 1 to length(A)
j ← i
while j > 0 and A[j-1] > A[j]
swap A[j] and A[j-1]
j ← j - 1
So for the each swap step, you just need to print
[j , s]
[j - 1,j]
[s , j - 1]
You are looking for array_map : http://php.net/manual/fr/function.array-map.php (old answer : usort : http://php.net/manual/fr/function.usort.php) : create a custom function which make what you are looking for, and call it with array_map.