PHP foreach shopping cart values being overwritten

Question: I have one array_push to push values through these are fine.
The question is how can i make the array more reliable so that i can have more values that will not be overwritten when they are outputted to the screen.
I currently have -
array_push($cart, array("id" => $_GET["itemId"]));

This gets my item id's then i use select on the other page. Is it possible to add more values?
On my other page i have -
        $cart = $_SESSION["cart"];

        echo "Your Cart Contains:
";

        $counter = 0;

        foreach($cart as $key){
            foreach($cart [$counter] as $item => $value){
                echo '
<cartNum>' . $counter . '</cartNum>' . '<productNum>' . $value . '</productNum>' . '<productImg></productImg>' . '<productDesc>' . $desc . '</productDesc>' . '<productRemove><input width="200px" height="400px" type="button" value="remove" class="buttonCSS"/></productRemove>

';
            }
            $counter++;

        }

The problem is that $image, $desc are being overwritten to equal the same over each product being displayed in the basket. I believe this is because they aren't being set as separate values in array so that they can differ from each other.
Is there a way that i can put them into the same array or in fact a different one which can be run with the foreach loop to differ each product?
Comment: Should probably mention i am relatively new to php.
Comment: Is that $image and $desc stored in DB ?
Comment: they are stored in a SQL database yes, the values are being taken fine. it's just that they are being over written when they are displayed.
Comment: okay, so solution is when you have all your product ids you do select everything else from DB for every id. I don't see in code where they getting updated. You just show the same $image var every time in your loop
Comment: Where are `$image` and `$desc` being set?
Comment: your foreach is a little bit strange. please provide an example output of `print_r($cart)` to let us show you a better way to walk through the array.
Answer: When you render your basket, $image and $desc are, ever assigned, this can only go wrong.
Also, the fact that you have a $counter into a foreach loop that gets assigned to a <cartNum> tag shows that there is something really wrong here. The great benefit of foreach loop is that you don't need to keep track of your array index, you should use them properly.
You should have something like below, but I can't be sure without seeing your data structure.
$cart = $_SESSION["cart"];

echo "Your Cart Contains:
";

$counter = 0;

foreach($cart as $cardNum => $cartEntry){
    foreach($cartEntry as $itemNum => $item){
        $image = $item['image']; // retrieve the image
        $desc = $item['desc']; // retrieve the description

        echo '
<cartNum>' . $itemNum . '</cartNum>'
            . '<productNum>' . $itemNum . '</productNum>'
            . '<productImg></productImg>'
            . '<productDesc>' . $desc . '</productDesc>'
            . '<productRemove><input width="200px" height="400px" type="button" value="remove" class="buttonCSS"/></productRemove>

';
    }
}

Answer: // assuming your cart array looks like this:
$cart = array(
    array("itemid" => 56, "count"=> 2),
    array("itemid" => 57, "count"=> 1),
    array("itemid" => 58, "count"=> 1),
    array("itemid" => 59, "count"=> 5),
    array("itemid" => 60, "count"=> 2),
);

// you should walk through the array
foreach($cart as $v) {

    // get itemid from the current array-item
    $itemid = $v['itemid'];
    $count = $v['count'];

    // get details of your item from db
    $details = class_cartitem::getDetailsById($itemid);

    // display the current item
    echo "
"
        . "name: ".$details['name'].""
        . "desc: ".$details['desc'].""
        . "img: "
        . "count: ".$count.""
        . "price: ".$details['price'].""
        . "total: ". ($details['price']*$count) .""
        . "
";
}


pegando um JSON e transformando em um Objeto dentro do HTML

Question: estou tendo um problema no código abaixo, eu recebo de uma variavel( {{devices}} ) um JSON e tento converter ele num objeto para pegar as informações de quantos dispositivos eu tenho dentro dele e quantos alarmes críticos eu possuo para uma regra de negocio. Porém, o metodo Json.Parse não funciona ou eu não estou conseguindo implementar ele corretamente, segue o código abaixo:
<!DOCTYPE html>

 {{devices}}

Json que eu recebo desta variavel!
{
  "7097240": {
  "additionParents": {
    "references": [],
    "self": {}
  },
  "owner": "service_device-simulator",
  "childDevices": {
    "references": [],
    "self":
  },
  "com_cumulocity_model_Agent": {},
  "childAssets": {
    "references": [],
    "self": {}
  },
  "creationTime": "2019-10-01T16:44:15.111+02:00",
  "ts_home": {
    "lng": -47.873174,
    "lat": -22.064469
  },
  "c8y_SupportedOperations": [
    "c8y_Configuration",
    "c8y_Position",
    "c8y_Address",
    "c8y_Mobile",
    "c8y_LocationUpdate",
    "c8y_Firmware",
    "c8y_Restart",
    "c8y_Software"
  ],
  "c8y_Position": {
    "lng": -47.87335,
    "alt": 0,
    "accuracy": 10,
    "lat": -22.065523
  },
  "type": "c8y_MQTTDevice",
  "lastUpdated": "2019-10-18T15:39:31.664+02:00",
  "c8y_ActiveAlarmsStatus": {
    "critical": 0
  },
  "c8y_IsDevice": {},
  "childAdditions": {
    "references": [],
    "self": {}
  },
  "name": "GPS_VW-SC_2 #1",
  "assetParents": {
    "references": [],
    "self": {}
  },
  "deviceParents": {
    "references": [],
    "self":{}
  },
  "self":{},
  "id": "7097240"
}

Comment: Mas pq está fazendo parse para Json se isto já é um? Não ficou clara sua questão.
Comment: Entao, eu gostaria de transformar a string q eu estou recebendo em um objeto, para que eu possa trabalhar em cima disso, na verdade queria saber quantos eu estou recebendo e de que tipo.
Comment: Acho que o seu problema é que o Json está inválido.
Comment: Entao eu verifiquei e o erro que da é o seguinte: Uncaught SyntaxError: Unexpected token { in JSON at position 1
    at JSON.parse ()
    at :5:21....

Porém, quando joga num site que verifica e organiza esse json para mim, que está vindo como uma string, ele organiza certinho e reclama se tem algum { a mais ou menos!
Comment: Sim se reparar no 2º **self** de cima para baixo, está faltando os **{}**
Comment: Opá LeAndrade, na verdade esse Json creio que está correto, pq nessa parte eu tirei umas informações mesmo, porém eu descobri que o erro q estou tendo é que dentro da primeira tag existe uma variavel {{devices}} e quando tento manipular ele pega essa string e nãoo json que é jogado posteriormente, queria saber se tem como pegar de alguma maneira o conteudo dessa variavel para trabalhar dentro do meu script?
Comment: Mas qual sintaxe é essa ` {{devices}} `?? Usa Angular?
Comment: Na verdade existe uma plataforma em cloud da empresa que trabalho, e eles disponilizam essa variável, e dentro dela uma parte onde posso programar em HTML, e quando eu executo meu programa ele pega o nome da variável para trabalhar e não seu conteúdo, sendo assim, não sei se quando meu script roda ele não pega a informação correta porque a variável é grande de demora á chegar ou de certa maneira o valor dessa variável só é exibida após ser compilado meu programa!
Answer: Oi, Lukas.
Tenta substituir:
//var objJson = JSON.parse(document.getElementById("in").innerHTML); //motivo do erro, ele para o funcionamento aqui

por:
var objJson = JSON.parse(strJson);
Forte abraço!
Comment: Já realizei esse procedimento e ele simplesmente nao roda, gostaria de saber se tem algum outro meio de transformar essa string em um objeto do javascript\
Comment: Então acredito que o problema esteja no formato do json que vc ta recebendo. Pode ser alguma chave que não esteja fechando, conte todos os '{' se não der um número par é porque em algum lugar não esta fechando as chaves.
Comment: Ok vou verificar, muito obrigado pela ajuda!
Mas a logica estaria certa, sou novo com a linguagem de javascript!


Block visibility based on the content type

Question: I have created a block for showing author details for every individual author; the article's authors can be identified by the 'Author' field.
Now I want to match the author block with the particular author's content.
For example, I have an author called 'vividvilla', and I have created a block for 'vividvilla'. How do I show this block for articles where 'vividvilla' is the author?
I know it can be done with PHP code in blocks, but I am not sure about the code.
Answer: I would use the menu_get_object() function to determine whether or not the block visibility check is being executed on a full node page, then check the field value on the node from there.
EDIT
The other edits were getting a bit long so I've deleted them. I've just set up a site quickly to test this and the following steps have worked for me (this is Drupal 7):

Created a content type called Author Profile (machine name author_profile)
Added a node reference field from the Article content type to the Author Profile
Created an Author Profile node with a title of "vividvilla"
Created an Article node linked to the Author Profile node above
Enabled the core PHP Filter module
Created a block called Author Details with some dummy content in the body, choosing "Pages on which this PHP code returns TRUE (experts only)" for the block visibility settings.
Entered the PHP code below
<?php

$node = menu_get_object();

// Check we're on a 'full' node page.
if ($node && isset($node->nid)) {
  // Check the node is of the right type
  if ($node->type != 'article') {
    return FALSE;
  }

  // Load the author node based on the node reference field value.
  $author_node = node_load($node->field_blog_authors[$node->language][0]['nid']);

  // Check the title of that node
  if ($author_node->title == 'vividvilla') {
    return TRUE;
  }
}

// Default is to not show the block
return FALSE;

?>

The block only shows on an article page that is linked to an Author Profile with the title of "vividvilla" which I'm pretty sure is what you're trying to.
On the off-chance you're using Drupal 6 just change the $author_node = line to:
$author_node = node_load($node->field_blog_authors[0]['nid']);

Comment: my field machine name is 'field_blog_authors' and it is a Node reference field.
Comment: @vividvilla I've updated the answer with another example, let me know if I'm still not understanding what you need to do :)
Comment: You understood my question correctly and the updated answer is exactly what i needed but i have Content type called 'profile' from where the authors field is referenced.What should be changed now ?
Comment: @vividvilla In that case you should just be able to change `if ($node->type != 'article') {` to `if ($node->type != 'profile') {`, I'll update the answer :)
Comment: it is not working,the block is shown at all the articles.The original content type is Article only the author belongs to profile content type.To test the code i changed the author name to some invalid name but then also the block is showing up on articles.
Comment: @vividvilla Sorry I don't understand, how is your profile content type linked to the article content type? Or is it linked to the user through a user reference field or something?
Comment: I have created a Author Profile Content type for taking basic details about Author and In Articles content type I referenced the Profile content type Node.So the Profile content type Title is the Authors name.
Comment: Ok I get it, one last thing...are you using Drupal 6 or Drupal 7? Should really have asked that before!
Comment: @vividvilla I've updated the answer again with what is (hopefully) a solution for Drupal 6 or Drupal 7. I've tested it on a local environment and it works perfectly, hope you get it working
Comment: Thanks a lot @clive it worked like a charm but the error in that code was instead of 'field_blog_authors' it was 'field_blog_author' I just changed it and worked..
Answer: the most easy way to do it i think is using Views.
In views you can add conditions and get arguments from the url and you can easy set a condition to display the author when you are seeing a node who belongs this author
Answer: First of all, I would like to address

I know it can be done with PHP code in blocks, but I am not sure about the code.

Please avoid inputting code through the UI in this fashion. Use of the eval()
is a large source of problems, including security issues. Just see the big warning in php's own docs. I strongly suggest finding a different approach.
That said, this is easily done with Panels and the Page manager. Please note though, that using both Panels and the Page manager requires quite a bit of understanding for both of them. They are powerful tools, and as such a bit complex.

Install the Chaos tool suite (ctools) and Panels modules.
Enable the Page Manager and Panels.
Enable the node template
Create a variant of type "Panel", add to it a selection rule on the content type that has been authored.
Add a relationship of the type that corresponds to the link between the authored content, and it's author.
Place the authored content in one location, and the authors "content" in another.
Save

Page manager will now automatically derive the author from every displayed piece of content, and render what ever information you have on the authors where you place it.
Video series on using the two modules: Learn Page Manager!.
Answer: If each post has a Node Reference field pointing at the author, then create a View which displays the author for a particular post, by adding an argument to the view and selecting the author field as the argument.
Create a Block display for that View.  Blocks don't have URLs, so can't directly get arguments, but the way around this is to click the argument in the display editing pane and specify an 'Action to take if argument is not present'. One option is 'Provide Default Argument', and you can select 'Node ID from URL'.
Then just arrange for the block to be included on the relevant pages - I tend to do this simply by URL.
This question is actually very similar:
How to show a view of content types on another content type
And my response there has a lot more detail!
Answer: If you insist in having blocks created with admin/structure/block/add (this means that you are using "semi-static" blocks), I would recommend do it this way:

install the block reference module http://drupal.org/project/blockreference
replace your author field with a block reference field on your content type
edit your node and select the correct block in you block reference field

Now you have 2 options to display the block:

use the field UI to configure and display the block in the node body;
use the field UI to hide the field in the body and then edit your template file and add the following code where you want your block to be displayed:
print theme('blockreference_item_simple', array('item' => field_get_items('node', $node, 'field_your-field-name-here', $node->language)));

Answer: You can do this without any custom code just using Taxonomy, and the Context module.
Create a taxonomy vocabulary called Authors for instance, and add a term to the list for each author.
For your author's field on the articles, set that as a Term Reference field, and input the appropriate author taxonomy term into there.
With the Context module, you can create a context which has a specific block (select the author's block) to be displayed only when a specific Taxonomy term is present for the currently active node.
Answer: I will not answer directly to your question since there are plenty of responses with custom code and various modules that you can use in order to achieve the functionality that you want.
My answer will focus on the method that you chose to use. From what I've read, your approach can be summarized as follows:
1) You have x authors in your website
2) You created x number of blocks (one for each author)
3) You are trying to "associate" each block with nodes, based on the author of the node. So, for example, the first block will only appear if the author is "John Doe", the second block ... , the x-th block will only appear if the author is "Jane Doe".
(please correct me if I'm wrong and you are not using the methodology that I described above)
In my opinion, there are some facts about this specific approach that you should know:

Conceptually, the custom blocks that you are creating share a lot of common "properties". For example, all blocks describe an author and present information about the specific author.
You may have only 5 (or 2, or 3) authors right now, but in the future more authors may be added. How will you add the extra authors? You will have to create new custom blocks, copy and paste the PHP code and change the information in the block to match the new author (an old programming teacher of mine used to say: when you are copying and pasting large chunks of your code, there is something wrong with your code). Not to mention that your blocks administration page will become a nightmare to manage.
Even if you are absolutely sure that you will not have to add more authors in the future, are you sure that you will not want to change the information in each block? For example, if you want to remove the "Occupation" field from the block and add an "IM" field, how will you do it? Go to every block, for every author and remove and add new fields.
When you'll need to change anything related to the blocks, how sure can you be that you'll remember how the PHP code works in each block?
What if you want to give someone else permission to change the author's information? Perhaps, the authors would like to add their own information (e.g. add their own bio). Will you give them access to a block with PHP code?

- My approach would be significantly different:

New content type: Authors. You add fields that you want to display (like: picture, bio, email, website etc)
Create a new node for each author. Add the corresponding information for each author (or even give access to other users to do it).
Associate the "Author" field in your existing content type with the new content type ("Authors") using References if you are in Drupal 7 or Node Reference which is a part of the CCK if you are in Drupal 6.
Create a block via Views to display the information from the new content type's nodes.

P.S: I intentionally did not include the details on how to setup Views or the References module for 2 reasons: (a) I do not know which version of Drupal you are using and (b) I understand that the chances of changing your whole approach are slim. However, if you decide to follow my method, let me know and I'll edit my post to add some more details.
Comment: This is actually the best answer to the question overall because if you use Views to standardize each block then you can use Panels to display the same dynamic block on every node of content type X.


Max Function Isn't Working in SQL Server 2005

Question: I just want to find the max value from the Visitor table. Last is working fine but when digit come VI9999 after that it always returns max value same not VI10000. Please Suggest me what I will do for this...
Thanks in Advance..
Sql query:
select MAX(TSID)
FROM VISITOR_TABLE;

Answer: select Max(cast(substring(TSID, 3, 10) as int))
FROM VISITOR_TABLE;


How does it make sense to do OLS after LASSO variable selection?

Question: Recently I have found that in the applied econometrics literature, when dealing with feature selection problems, it is not uncommon to perform LASSO followed by an OLS regression using the selected variables.
I was wondering how can we qualify the validness of such a procedure. Will it cause troubles such as omitted variables? Any proofs showing that it is more efficient, or the results are more interpretable?
Here are some related discussions:
Variable selection with LASSO
Using trees after variable selection using Lasso/Random
If, as pointed out, such a procedure is not correct in general, then why are still there so many researches doing so? Can I say that it is just a rule of thumb, a compromise solution, due to some of the uneasy properties of LASSO estimator, and people's fondness towards OLS?
Comment: In [this](http://web.stanford.edu/~hastie/StatLearnSparsity/) recent LASSO book (free online), section 11.4 appears to address this issue. I have not read this in detail, but the introduction ends by saying "Given [a LASSO estimate] $\hat{\beta}$ that correctly recovers the support of $\beta^*$, we can estimate $\beta^*$ very well ... simply by performing an ordinary least-squares regression restricted to this subset."
Comment: There are a few recent working papers on the subject. Many seem to require the assumption that the set of valid variables is sparse. If that assumption doesn't hold, then yes omitted variables bias would be present. And people like ols because they want to interpret coefs as unbiased out of sample marginal effects. Econometrics is fairly stuck in that paradigm.
Comment: Could you explain what it means to do "OLS regression" after performing the LASSO?  What, specifically, is this OLS step attempting to estimate that the LASSO has not estimated?
Answer: There was a similar question a few days ago which had the relevant reference:

Belloni, A., Chernozhukov, V., and Hansen, C. (2014) "Inference on Treatment Effects after Selection among High-Dimensional Controls", Review of Economic Studies, 81(2), pp. 608-50 (link)

At least for me the paper is a pretty tough read because the proofs behind this relatively simple are fairly elaborate. When you are interested in estimating a model like
$$y_i = \alpha T_i + X_i'\beta + \epsilon_i$$
where $y_i$ is your outcome, $T_i$ is some treatment effect of interest, and $X_i$ is a vector of potential controls. The target parameter is $\alpha$. Assuming that most of the variation in your outcome is explained by the treatment and a sparse set of controls, Belloni et al. (2014) develop a double-robust selection method which provides correct point estimates and valid confidence intervals. This sparsity assumption is important though.
If $X_i$ includes a few important predictors of $y_i$ but you don't know which they are (either single variables, their higher order polynomials, or interactions with other variables), you can perform a three step selection procedure:

regress $y_i$ on $X_i$, their squares, and interactions, and select important predictors using LASSO
regress $T_i$ on $X_i$, their squares, and interactions, and select important predictors using LASSO
regress $y_i$ on $T_i$ and all the variables which were selected in either of the first two steps

They provide proofs as for why this works and why you get the correct confidence intervals etc out of this method. They also show that if you only perform a LASSO selection on the above regression and then regress the outcome on the treatment and the selected variables you get wrong point estimates and false confidence intervals, like Björn already said.
The purpose for doing this is twofold: comparing your initial model, where variable selection was guided by intuition or theory, to the double-robust selection model gives you an idea as to how good your first model was. Perhaps your first model forgot some important squared or interaction terms and thus suffers from misspecified functional form or omitted variables. Secondly, the Belloni et al. (2014) method can improve inference on your target parameter because redundant regressors were penalized away in their procedure.
Comment: "Correct" point estimates?
Answer: To perform a variable selection and then re-run an anslysis, as if no variable selection had happened and the selected model had be intended from the start, typically leads to exaggerated effect sizes, invalid p-values and confidence intervals with below nominal coverage. Perhaps if the sample size is very large and there are a few huge effects and a lot of null effects, LASSO+OLS might not be too badly affected by this, but other than that I cannot see any reasonable justification and in that case the LASSO estimates ought to be just fine, too.
Comment: But why the second model starts from scratch as if no variable selection happened? Does not LASSO selects explanatory variable with best predictive power? BTW I thought to do stuff LASSO sparse matrix variable into glm again. Now I have understood LASSO per se is a regression.
Answer: It may be an excellent idea to run an OLS regression after LASSO.  This is simply to double check that your LASSO variable selection made sense.  Very often when you rerun the model using OLS regression you uncover that many of the variables selected by LASSO are nowhere near being statistically significant and/or have the wrong sign.  And, that may invite you to use another variable selection method that given your data set may be much more robust than LASSO.
LASSO does not always work as intended.  This is due to its fitting algorithm that includes a penalty factor that penalizes the model against higher regression coefficients. It seems like a good idea, as people think it always reduces model overfitting, and improves predictions (on new data). In reality it very often does the opposite... increase model under-fitting and weakens prediction accuracy. You can see many examples of that by searching the Internet for Images and searching specifically for "LASSO MSE graph." Whenever such graphs show the lowest MSE at the beginning of the X-axis, it shows a LASSO that has failed (increase model under-fitting).
The above unintended consequences are due to the penalty algorithm. Because of it LASSO has no way of distinguishing between a strong causal variable with predictive information and an associated high regression coefficient and a weak variable with no explanatory or predictive information value that has a low regression coefficient. Often, LASSO will prefer the weak variable over the strong causal variable. Also, it may at times even cause to shift the directional signs of variables (shifting from one direction that makes sense to an opposite direction that does not). You can see many examples of that by searching the Internet for Images and searching specifically for "LASSO coefficient path".
Comment: I suspect the downvote might be in response to the duplication of this post at https://stats.stackexchange.com/a/445519/919.  Please choose which thread is the best fit and delete the superfluous copy.
Comment: That's a good suggestion.  And, I'll do that.  But, the downvote preceded my duplications.
Comment: Whoever has downvoted my answer has not digested the abundance of evidence supporting my arguments.  Such evidence has mentioned in my answer can be readily uncovered by doing search for Images and doing the following searches: "LASSO MSE graph" and "LASSO coefficient path."  After reviewing carefully this body of evidence in conjunction with my arguments, can you please remove your down vote.


Find the limit of a multivariable function $f(x,y)=\frac{\ln(1+x^2y^2)}{x^2}$

Question: The function is as follows:
$f(x,y)=\frac{\ln(1+x^2y^2)}{x^2}$
and I want to calculate the following limit:
$\lim_{(x,y)\to(0,y_0)}f(x,y)$
The reason I'm having trouble with this one is because the limit doesn't seem to be $0$ but $y_0^2$. Because of that, I need 2 functions to compare $f$ to, instead of one.
The greater one I found like this:
$\ln(1+x^2y^2)\leq x^2y^2$
$\frac{\ln(1+x^2y^2)}{x^2}\leq y^2$
so (if I'm correct) the limit is definitely lower or equal to $y_0^2$. But I can't find the function to be my upper bound that also converges to that value.
Comment: sorry i had not read question and problem exactly,i have updated
Answer: $$\ln(1+x) \sim x$$  if $x \to 0.$
So you have $$\frac{\ln(1+x^2y^2)}{x^2} \sim y^2$$
$$\lim_{x, y \to 0, y_0} y^2 = y_0^2$$
Comment: Are you saying that my upper bound is enough to prove the limit? Could you offer some more explanation?
Comment: @Darwin   i have updated my answer
Comment: @Darwin usually you need to find upper bounds to simplify the limit. But it is always true that you can replace a function in a limit with it's taylor approximation. Doing so we know that the limit is equal to $y_0^2$, so no need to do anything else ;-)
Answer: You can use the fact that
$$\ln(1+u)=u + o(u)$$
when $u$ converges to $0$.
Here you can apply this to $u=x^2y^2$, to get that
$$f(x,y)=\frac{x^2y^2+o(x^2y^2)}{x^2}=y^2+o(y^2)$$
The last $o(y^2)$ converges to $0$ when taking the limit, so you are done.
Comment: Why is $\frac{o(x^2y^2)}{x^2}=o(y^2)$?
Comment: $o(sth)$ means here "a function $h(x,y)$ such that $h(x,y)/sth$ converges to $0$ when $x^2y^2\to 0$". From this definition, it should be clear that you can do this.


Correlation with high weight to single features

Question: I am a statistics newbie, so not sure how to appropriately frame this question or if it is too naive.
Essentially, I have a matrix of 500 items and each item has 8 numerical properties associated with it. I want to calculate all-vs-all pairwise similarities between these items such that even if one of the properties is significantly different for Item A vs Item B, then their pairwise similarity should be substantially reduced, regardless of how close the other properties are. I know that traditional correlation metrics like Pearsons and Spearman won't be able to do that, and as far as I understand unsupervised clustering methods like PCA/Hierarchical clustering using all 8 properties also will not downgrade a single metric. So what would be the appropriate way to go about doing this?
Comment: You seem to be describing a version of the [sup norm](https://en.wikipedia.org/wiki/Uniform_norm). Although this is but one of many solutions, one advantage is that it truly is a metric. Another is that a weighted version is readily available.
Answer: In your case, you are looking for a nonparametric dependency measure between pairs of your 8 numerical properties (your feature variables). My proposal is that you bin each of your $8$ feature variables into meaningful histograms, each with $8-10$ equidistant intervals.
Now compute the mutual information among each $\frac{8\cdot 7}{2}=28$ possible pairs of feature variables. Define MI as
$
\begin{split}
MI(X;Y)=\sum_{y \in Y} \; \sum_{x \in X} \; p(x,y) \; log \left (\frac{p(x,y)}{p(x) \cdot p(y)} \right )
\end{split}
$
with $P(x,y)$ the bivariate histogram distribution for the pair of feature variables $X$ and $Y$, and $P(x),\; P(y)$ the histogram distributions for the individual variables $X$ and $Y$.
Sort your variable pairs based on $MI(X;Y)$, and you have a ranking for the inter-dependencies among your $8$ feature variables.
Comment: Why recommend binning--which loses information--and what's the basis for "8-10 equidistant intervals"?  This seems awfully *ad hoc.*
Comment: Thanks. If I understand this correctly, it will tell me which features are associated with each other across the 500 instances. Is that right? I'm not sure though how that leads to estimating distances between pairs of instances.
Comment: First, regarding 'binning' - the question begins with "correlation metrics like Pearsons and Spearman won't be able to do that..." indicating that a nonparametric analysis of probabilistic dependence is the logical next step. The numbers $8-10$ bins are not so arbitrarily chosen. I refer to Sturge's Rule and for 500 observations end up with this number of bins, that is 8 till 10 bins for each variable.
Comment: "...I'm not sure though how that leads to estimating distances between pairs of instances...". This sub-question does not appear as part of your main question text. As soon as you leave the metric space and transform into binning space, a Hamming distance measure can be used instead. Recall that your begin premise was that the real-valued scale is unsuited for your application. A Hamming distance measure is coarse.
Comment: I'm sorry if that wasn't clear. The goal is to say whether A and B are very similar to each other, based on the similarity between their features. However, I need a way to bias the final similarity index by the number of dissimilarities between features rather than similarities. Crudely speaking in percentages, for example, if 7 features are similar but 1 is not, the final index shouldnt be (100*7)/8 = 87.5, but something lower. Realistically, the desired method would also consider the actual feature values to estimate the similarity index. I hope I'm making sense.


Are there better ways to have different actions chosen in a function based on its inputs?

Question: I've made a generic function to generate a 2D grid of size x, y, make_grid.
def make_grid(x, y, mode="empty"):
    if mode == "rand":
        def filling():
            return randint(0, 1)
    else:
        def filling():
            return 0
    grid = []
    for r in range(x):
        row = []
        for c in range(y):
            row.append(filling())
        grid.append(row)
    return grid

I've set it up to define the filling function based on the input mode so that I don't end up running a bunch of ifs being checked unnecessarily over the for loops - the hope is that over a large grid this would save considerable time.
I'm not sure, however, that this would be the most Pythonic way of achieving this - it doesn't feel clean to define functions within functions in this way. Can anyone suggest better (clearer or more efficient) methods? I feel like lambdas might be the right way, but I haven't ever really got comfortable with them... I should, at the least, be able to define functions elsewhere, choose which one should be called before the for loop, and then use it during the for loops, right?
Edit: here's a more minimal example, as I was shamefully lazy at first and didn't bother making a proper one... (I'm leaving the original so that answers still make sense.)
def example(mode=0):
    if mode == 1:
        def action_to_take():
            print("mode1")
    else:
        def action_to_take():
            print("mode_other")
    for r in range(1000):
        for c in range(1000):
            action_to_take()

Just imagine that the conditionally defined action_to_take function is complicated enough to warrant this question, and the loop goes round enough times to make questioning the performance of the code reasonable.
Comment: It's pretty uncommon to conditionally define functions like this. Why not use `row.append(x)` where x is just conditionally defined in a preceding line?
Comment: [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example)
Comment: @duhaime Yes, I figured conditionally defining functions would be pretty abnormal. The issue with conditionally defining what I append in the preceding line would be that the conditional would be calculated x*y times, even though it has to come to the same conclusion every time
Comment: sorry @Peter, that was lazy of me. More minimal example added.
Answer: you can use a dict:
def fill_rand():
    return randint(0, 1)

def default_fill():
    return 0

# you can add how  many modes you want
filling_mode = {
    'rand': fill_rand,

}

def make_grid(x, y, mode="empty"):
    filling = filling_mode.get(mode) or default_fill

    grid = []
    for r in range(x):
        row = []
        for c in range(y):
            row.append(filling())
        grid.append(row)
    return grid

Comment: Thanks, a dict is a nice clean way of dealing with this
Answer: It's uncommon to conditionally define functions like this. You can simplify this and shorten it quite a bit with a more straightforward approach:
import numpy as np

def make_grid(x, y, mode='empty'):
  if mode == 'rand':
    return np.random.randint(0, high=2, size=(y, x)).tolist()
  return np.zeros((y, x)).tolist()

If you don't like numpy, I'd use list comprehensions because it keeps the code concise:
from random import randint

def grid(x=1, y=1, zeros=False):
  return [[0 if zeros else randint(0,1) for _ in range(y)] for _ in range(x)]

a = grid(x=7, y=10, zeros=False)

It's tough to generalize too much beyond this though. In general, it's quite uncommon to conditionally define a function. It's much more common to conditionally define a value inside a function (see the x if b else y syntax in my grid() function above for an example).
If you post your actual situation, or more info about the functions you conditionally want to instantiate, we can probably give more info. Either way, if you need to support both behaviors, it usually makes sense to define those functions outside of a conditional but call them inside the conditional as @kederrac has done...
Comment: Thanks @duhaime, I didn't know numpy's randint had a size parameter. Useful to know and definitely avoids unnecessary for looped conditionals in the case of my example! Would be great to have an answer for the general type of problem though. If numpy didn't exist in this case, I'd be stuck having to write my own `numpy.random.randint` and `numpy.zeros`, which would involve a fair bit of duplication of effort between the two. Would I be right to say that this delegates the issue I'm trying to find the cleanest solution for to other functions?
Comment: Thanks again, I wasn't aware of list comprehensions, and they look like another great solution (assuming that I understand the way they run - in your example, calculating the conditional once, and then calling the resultant choice [`0` or `randint(0,1)`] in each iteration of the loops). In response to your edit suggesting I post my actual situation: I don't have a specific situation in mind, rather I was interested in better methods of avoiding repeatedly calculating conditionals within loops in general. I'd say you and @kederrac's answers have provided excellent answers to that. Thanks!
Comment: @JRVeale I added a little to my answer to show that you could replicate the required behaviors of numpy pretty easily and concisely with list comprehensions. That style might be too terse for you, but as a codebase gets to be tens of thousands of lines it can be helpful to keep things as succinct as possible. In any event, there's no delegation so to speak in the second example, except to the `random` module I suppose...
Answer: In the case where the mode argument is only being used to determine which action to take at a certain point within the function, another option I just thought of would be to simply pass the function name as a parameter. As below:
def action_one():
    print("mode1")

def action_default():
    print("other")

def example(func=lambda *args: None):
    for r in range(1000):
        for c in range(1000):
            func()

You could then call example(action_default) or example(action_one), and with that lambda expression there as a default value even example() - which will do nothing at the point where func is called.


Would it make sense to use $\frac{df(x)}{dx}$ as a notation when differentiating a function?

Question: While I have never seen the above notation, it seems to make the most sense to me: you are considering the rate of change of $f(x)$ 'divided by' the rate of change of $x$. By contrast, writing $\frac{d}{dx}f(x)$ feels odd because $\frac{d}{dx}$ does not mean anything on its own. You need the $f(x)$ to make sense of what you are differentiating—the $d$ on top of the 'fraction' pertains to $f(x)$. Am I missing something?
Comment: Both notations can be used interchangeably. You should think of $\frac{\mathrm d}{\mathrm dx}$ as an operator meaning "differentiate the following expression with respect to $x$".
Comment: @Rahul I now understand the logic behind the $\frac{d}{dx}$ notation more, so thank you for this comment. Is one of these notations more common than the other?
Answer: I find it odd that you say "I have never seen the above notation", $\frac{df(x)}{dx}$, while I have seen in just about every Calculus book I have looked at!  You also say that $\frac{d}{dx}$ does not "mean anything on its own".  Of course it does- it is the differential operator and has as much meaning as talking about the "sine function" rather than "sin(x)".  Finally you say "you are considering the rate of change of (x) divided by' the rate of change of x". No, you are not.   $\frac{df}{dx}$ is the limit of the average "rate of change of f(x) divided by the rate of change of x".  The limit concept is crucial here.
Answer: Differentiation can be thought as applying linear operator to a function.
I like to think about notation that confuses you, as applying that operator to each function.
While differentiating single function it may seem odd to make such notation, it becomes very intuitive, when you have to differentiate not only one function, but many:
$$\frac{d}{dx}\biggr[f(x)+g(x)+h(x)\biggl]$$
It shows that you have to apply linear operator to each of the function, while notation like:
$$\frac{d\biggr(f(x)+g(x)+h(x)\biggl)}{dx}$$
may provide a little confusion, although it is still correct.


Computing Cohen's Kappa variance (and standard errors)

Question: The Kappa ($\kappa$) statistic was introduced in 1960 by Cohen [1] to measure agreement between two raters. Its variance, however, had been a source of contradictions for quite a some time.
My question is about which is the best variance calculation to be used with large samples.  I am inclined to believe the one tested and verified by Fleiss [2] would be the right choice, but this does not seem to be the only published one which seems to be correct (and used throughout fairly recent literature).
Right now I have two concrete ways to compute its asymptotic large sample variance:

The corrected method published by Fleiss, Cohen and Everitt [2];
The delta method which can be found in the book by Colgaton, 2009 [4] (page 106).

To illustrate some of this confusion, here is a quote by Fleiss, Cohen and Everitt [2], emphasis mine:

Many human endeavors have been cursed with repeated failures before
  final success is achieved. The scaling of Mount Everest is one
  example. The discovery of the Northwest Passage is a second. The
  derivation of a correct standard error for kappa is a third.

So, here is a small summary of what happened:

1960: Cohen publishes his paper "A coefficient of agreement for nominal scales" [1] introducing his chance-corrected measure of agreement between two raters called $\kappa$. However, he publishes incorrect formulas for the variance calculations.
1968: Everitt attempts to correct them, but his formulas were incorrect as well.
1969: Fleiss, Cohen and Everitt publish the correct formulas in the paper "Large Sample Standard Errors Of Kappa and Weighted Kappa" [2].
1971: Fleiss publishes another $\kappa$ statistic (but a different one) under the same name, with incorrect formulas for the variances.
1979: Fleiss Nee and Landis publish the corrected formulas for Fleiss' $\kappa$.

At first, consider the following notation. This notation implies the summation operator  should be applied to all elements in the dimension over which the dot is placed:
$\ \ \ p_{i.} = \displaystyle\sum_{j=1}^{k} p_{ij}$
$\ \ \ p_{.j} = \displaystyle\sum_{i=1}^{k} p_{ij}$
Now, one can compute Kappa as:
$\ \ \ \hat\kappa = \displaystyle\frac{p_o-p_c}{1-p_e}$
In which
$\ \ \ p_o = \displaystyle\sum_{i=1}^{k} p_{ii}$ is the observed agreement, and
$\ \ \ p_c = \displaystyle\sum_{i=1}^{k} p_{i.} p_{.i}$ is the chance agreement.
So far, the correct variance calculation for Cohen's $\kappa$ is given by:
$\ \ \ \newcommand{\var}{\mathrm{var}}\widehat{\var}(\hat{\kappa}) = \frac{1}{N(1-p_c)^4} \{ \displaystyle\sum_{i=1}^{k} p_{ii}[(1-p_o) - (p_{.i} + p_{i.})(1-p_o)]^2 \\
\ \ \ + (1-p_o)^2 \displaystyle\sum_{i=1}^{k} \displaystyle\sum_{j=1 \atop i\not=j}^{k} p_{ij} (p_{.i} + p_{j.})^2 - (p_op_c-2p_c+p_o)^2 \} $
and under the null hypothesis it is given by:
$\ \ \ \widehat{\var}(\hat{\kappa}) = \frac{1}{N(1-p_c)^2} \{ \displaystyle\sum_{i=1}^{k} p_{.i}p_{i.} [1- (p_{.i} + p_{i.})^2] + \displaystyle\sum_{i=1}^{k} \displaystyle\sum_{j=1, i\not=j}^{k} p_{.i}p_{j.}(p_{.i} + p_{j.})^2 - p_c^2 \} $
Congalton's method seems to be based on the delta method for obtaining variances (Agresti, 1990; Agresti, 2002); however I am not sure on what the delta method is or why it has to be used. The $\kappa$ variance, under this method, is given by:
$\ \ \ \widehat{\var}(\hat{\kappa}) = \frac{1}{n} \{ \frac{\theta_1 (1-\theta_1)}{(1-\theta_2)^2} + \frac{2(1-\theta_1)(2\theta_1\theta_2-\theta_3)}{(1-\theta_2)^3} + \frac{(1-\theta_1)^2(\theta_4-4\theta_2^2)}{(1-\theta_2)^4} \} $
in which
$\ \ \ \theta_1 = \frac{1}{n} \displaystyle\sum_{i=1}^{k} n_{ii}$
$\ \ \ \theta_2 = \frac{1}{n^2} \displaystyle\sum_{i=1}^{k} n_{i+}n_{+i}$
$\ \ \ \theta_3 = \frac{1}{n^2} \displaystyle\sum_{i=1}^{k} n_{ii}(n_{i+} + n_{+i})$
$\ \ \ \theta_4 = \frac{1}{n^3} \displaystyle\sum_{i=1}^{k} \displaystyle\sum_{j=1}^{k} n_{ij}(n_{j+} + n_{+i})^2$
(Congalton uses a $+$ subscript rather than a $.$, but it seems to mean the same thing. In addition, I am supposing that $n_{ij}$ should be a counting matrix, i.e. the confusion matrix before being divided by the number of samples as related by the formula $p_{ij} = \frac{n_{ij}}{\mathrm{samples}}$)
Another weird part is that Colgaton's book seems to refer the original paper by Cohen, but does not seems to cite the corrections to the Kappa variance published by Fleiss et al, not until he goes on to discuss weighted Kappa. Perhaps his first publication was written when the true formula for kappa was still lost in confusion?
Is somebody able to explain why those differences? Or why would someone use the delta method variance instead of the corrected version by Fleiss?
[1]: Fleiss, Joseph L.; Cohen, Jacob; Everitt, B. S.; Large sample standard errors of kappa and weighted kappa. Psychological Bulletin, Vol 72(5), Nov 1969, 323-327. doi: 10.1037/h0028106
[2]: Cohen, Jacob (1960). A coefficient of agreement for nominal scales. Educational and Psychological Measurement 20 (1): 37–46. DOI:10.1177/001316446002000104.
[3]: Alan Agresti, Categorical Data Analysis, 2nd edition. John Wiley and Sons, 2002.
[4]: Russell G. Congalton and Green, K.; Assessing the Accuracy of Remotely Sensed Data: Principles and Practices, 2nd edition. 2009.
Comment: I can at least give you a hand with this part: "I am not sure on what the delta method is" -- http://en.wikipedia.org/wiki/Delta_method and the variance there comes from [here](http://en.wikipedia.org/wiki/Taylor_expansions_for_the_moments_of_functions_of_random_variables#Second_moment)
Comment: By the way, I just noticed what seems to be a printing error on Colgaton's book: he defines $p_c = \sum_{i=1}^k p_{i+} p_{+j}$, but this $j$ comes from nowhere. I suppose it was meant to be $p_c = \sum_{i=1}^k p_{i+} p_{+i}$, otherwise I am not sure it makes much sense.
Comment: some of your parentheses are off, can you please fix them? Also, you might want to format the nested parentheses as { [ (x+y)^z + a]^ b - c } to make them more readable.
Comment: also, please give $\kappa$ itself, and alternative equivalent formulations if they exist. Depending on the specific alternative formulations, the variance expressions may be easier to get. (I am thinking of the Gini index, for which there are five or so formulations for i.i.d. data that imply totally different variance estimators for complex survey data.)
Comment: Thanks for the feedback. I have corrected the formulas and added how Kappa is computed. The Kappa formulation seems consistent across the literature, only its variance doesn't.
Comment: Thanks Glen. I had read the Wikipedia on the Delta, but I couldn't understand it very well, thus the "not sure" part. But I am very thankful for the second link; the variance really seems to come from the Taylor expansion. So I am supposing it is an approximation when the exact formula is not known or may be too difficult to evaluate directly.
Comment: Could someone explain the variance part conceptually? So far, kappa is a statistic computed from observed counts. In order to speak of kappa's variance we need to assume that counts are random and distributed in some way. This distribution is of course unknown. Hence one would use non-parametric bootstrap in place of the unknown distribution of the observed counts (i.e., one assumes multinomial distribution with parameters given by observed frequencies). This gives the estimate of the variance in principle. The rest is technical and may include approximations. Is all this conceptually correct?
Answer: I don't know which of the two ways to calculate the variance is to prefer but I can give you a third, practical and useful way to calculate confidence/credible intervals by using Bayesian estimation of Cohen's Kappa.
The R and JAGS code below generates MCMC samples from the posterior distribution of the credible values of Kappa given the data.
library(rjags)
library(coda)
library(psych)

# Creating some mock data
rater1 <- c(1, 2, 3, 1, 1, 2, 1, 1, 3, 1, 2, 3, 3, 2, 3)
rater2 <- c(1, 2, 2, 1, 2, 2, 3, 1, 3, 1, 2, 3, 2, 1, 1)
agreement <- rater1 == rater2
n_categories <- 3
n_ratings <- 15

# The JAGS model definition, should work in WinBugs with minimal modification
kohen_model_string <- "model {
  kappa <- (p_agreement - chance_agreement) / (1 - chance_agreement)
  chance_agreement <- sum(p1 * p2)

  for(i in 1:n_ratings) {
    rater1[i] ~ dcat(p1)
    rater2[i] ~ dcat(p2)
    agreement[i] ~ dbern(p_agreement)
  }

  # Uniform priors on all parameters
  p1 ~ ddirch(alpha)
  p2 ~ ddirch(alpha)
  p_agreement ~ dbeta(1, 1)
  for(cat_i in 1:n_categories) {
    alpha[cat_i] <- 1
  }
}"

# Running the model
kohen_model <- jags.model(file = textConnection(kohen_model_string),
                 data = list(rater1 = rater1, rater2 = rater2,
                   agreement = agreement, n_categories = n_categories,
                   n_ratings = n_ratings),
                 n.chains= 1, n.adapt= 1000)

update(kohen_model, 10000)
mcmc_samples <- coda.samples(kohen_model, variable.names="kappa", n.iter=20000)

The plot below shows a density plot of the MCMC samples from the posterior distribution of Kappa.

Using the MCMC samples we can now use the median value as an estimate of Kappa and use the 2.5% and 97.5% quantiles as a 95 % confidence/credible interval.
summary(mcmc_samples)$quantiles
##      2.5%        25%        50%        75%      97.5%
## 0.01688361 0.26103573 0.38753814 0.50757431 0.70288890

Compare this with the "classical" estimates calculated according to Fleiss, Cohen and Everitt:
cohen.kappa(cbind(rater1, rater2), alpha=0.05)
##                  lower estimate upper
## unweighted kappa  0.041     0.40  0.76

Personally I would prefer the Bayesian confidence interval over the classical confidence interval, especially since I believe the Bayesian confidence interval have better small sample properties. A common concern people tend to have with Bayesian analyses is that you have to specify prior beliefs regarding the distributions of the parameters. Fortunately, in this case, it is easy to construct "objective" priors by simply putting uniform distributions over all the parameters. This should make the outcome of the Bayesian model very similar to a "classical" calculation of the Kappa coefficient.
References
Sanjib Basu, Mousumi Banerjee and Ananda Sen (2000). Bayesian Inference for Kappa from Single and Multiple Studies. Biometrics, Vol. 56, No. 2 (Jun., 2000), pp. 577-582
Comment: Do you know if there's an extension of this for more than two raters?


homework: never ending loop 5**200000

Question: I had an homework assignment: to find the maximal sum of 50,000 consecutive digits in the number 5**200,000.
I know that if i make a for loop over this number it will not end in a feasible time.
I tried to put this number in a list and iterate over the list, and I don't understand how does it work that way? why doesn't it act like in the first case? what makes it faster?
that's my code:
def maxdigits(number,digits):
    s=str(number) #conversting the number to a string
    l = [int(char) for char in s]
    maximum = current = sum(l[:digits])
    for i in range(0,len(l)-digits):
        current = current-l[i]+l[i+digits]
        if current > maximum: maximum = current
    return maximum

Comment: Define "the maximal sum of 50,000 consecutive digits in the number 5**200,000". How is this different from the sum of the digits? Are you talking about any 50,000 consecutive digits? In any case, your problem here is about mathematical insight, rather than code.
Comment: Are you doing a [Maximum subarray problem](http://en.wikipedia.org/wiki/Maximum_subarray_problem)?
Comment: yes i converted 5**200000 to a string
Comment: The code runs fine on my machine.
Comment: It's not clear to me what you're asking. I think I understand the assignment and your code looks like it's probably either correct or very close. What are you saying this is faster than? Can you provide the code that you are saying is slower, because your description of it isn't clear enough for me.
Comment: it runs fine that wasnt my question..
Comment: why if i make a for loop on the number 5**200000 it stuck but this code doesnt?
Comment: What does that mean? A number isn't iterable, so I don't understand what a for loop on a number means. Do you mean `for i in range(5**200000)`?
Comment: That's because instead of going from 0 ... 5**200000, you are going from 0 ... 139 795 - 50000. 139 795 is the number of digits in that number.
Answer: I think you're asking why does a loop over the digits of the number 5**200000 complete quickly, while looping over all the numbers in the range 0 to (5**200000)-1 inclusive take a long time.
I think the only reason this isn't obvious to you is because these are large numbers that are difficult to write down. Think about it again. Would it be faster to loop over the seven digits of 1000000, or over the million numbers from 0 to 999999? There are 139795 digits in 5**200000. It's not a huge amount of work to do a simple loop 139795 times. It is a gargantuan amount of work to do a simple loop 5**200000 times. That's a ridiculous number. If every atom in the universe were a computer and each ran for the lifetime of the universe I doubt they would succeed in doing anything 5**200000 times.
Answer: The code runs faster because instead of iterating over the list
[ 0, 1, ..., 5 ** 200000 ]

you are iterating over
[ 0, 1, ..., 139 795 - 50000 ]

which is orders of magnitude smaller than the first list.
And the way you calculate the number of digits in that number is:
200000 * log5 + 1

homework: never ending loop 5**200000


Ubuntu: Execute shell script with fail2ban when user fails to log in ssh

Question: In my ubuntu server I use fail2ban to block repeated failed ssh logins, when fail2ban detects this I want to execute a shell script, is this possible to do?
My ssh jail:
[ssh]
findtime = 10m
maxretry = 3

My shell script is in this location:
/home/MyUser/scripts/send-mail

Also, how can I get the banned IP? I want to send that information too if possible
Answer: There are already predefined actions like /etc/fail2ban/action.d/sendmail.conf for sending email. You could simply enable one of them in your [ssd] jail with, e.g.,
sendmail[dest="[email]email@example.com[/email]", sender="[email]firstname.lastname@example.com[/email]", sendername="Fail2Ban",

Comment: the thing is I send mails with mailgun (I didnt configure postfix or similar) so I use curl to send mail with mailgun efficently, that's why I need to call sh script


How should graphs of True Positive Rate / False Positive Rate be interpreted?

Question: I have recently seen some graphs like this in the context of classificaiton:

Source: http://on-demand.gputechconf.com/gtc/2014/webinar/gtc-express-convolutional-networks-webinar.pdf, page 85
How should one interpret these graphs?
My try
In this case, it is about face recognition I guess there is some threshold parameter which leads to a decision if something is a face. The lower this parameter is, the more parts of images get "recognized" as a face. So both, the true positive value and the false positive value increase.
Now the two definitions
\begin{align}
    \text{true positive rate}  &:= \frac{TP}{TP+FN}\\
    \text{false positive rate} &:= \frac{FP}{FP+TN}
\end{align}
The true positive rate gives the proportion of actual positives which are correctly identified as such. (Higher is better)
The false positive rate gives the proportion of falsely identified positives amongst all actual negatives. (lower is better)
Obviously, the most right curve (combined Joint Baysian) is worst, because for a fixed true positive rate it has always the highest false positive rate.
But how would one decide if the red or the black curve is better? Isn't there the need of a "cost" of false positives / negatives to decide that? What information can I derive from such diagrams?
Comment: I am quite sure that the tag is wrong. However, I don't know what good tags would be. Please correct it if you know it. Also, English is not my mother tongue. Feel free to correct any sentence that seems to be a language mistake.
Answer: For decision problems, you do need a loss function for each type of error. However, this type of graph is useful because it identifies "dominated" and "dominating" solutions. Ill explain below:
The ideal form of the curve would be a vertical step function of height 1 at false-positive rate =  0. The worst possible curve would be the inverse of this.
Now, lets say there are two curves of true positive rate as a function of false positive rate $(\alpha)$, $f_1(\alpha)$, $f_2(\alpha)$. If $f_1\geq f_2\;\;\forall \alpha$, then $f_1$ will incur less loss than $f_2$ regardless of the loss functions, as long as the losses (L) for each type of error (False positive=FP, false negative=FN) are additive:
$L_{f_1}=(1-f_1)L_{FN}+\alpha L_{FP}$
$L_{f_2}=(1-f_2)L_{FN}+\alpha L_{FP}$
$L_{f_2}-L_{f_1}= L_{FN}[f_1-f_2]$
$f_1(\alpha)\geq f_2(\alpha) \implies L_{f_2}-L_{f_1}\geq 0$
Thus, the condition $f_1(\alpha)\geq f_2(\alpha)$ indicates that $f_1$ will lead to less loss than $f_2$ regardless of the loss functions.
Applying this to your actual data shows that Deep-Face Ensemble dominates all other approaches except human-cropped, which appears to be the best choice if very low fase positives are needed, but inferior for higher allowable false positives.


Remove email validation from Feed Import

Question: I'm trying to import about 15000 users from a CSV file. Most of the users have valid email addresses, but some (+/- 5000) don't, they're also not blank fields.
Is there a way that I can bypass the email validation? Or a module that can easily help with this?
I've tried re-importing the CSV file with "Do not replace existing users" and removing the email field from the mapping, and I still cannot import them and am getting the "User name missing or email not valid."
I have Optional Mail installed, and have tried importing after disabling the email field for registrations and edit pages and still get the same error.
Any ideas?
Answer: The validation logic for the email is embedded within the user feeds module in FeedsUserProcessor.inc which means the best way to go about this is to create you own, almost identical, feeds processor in your own custom module. Read up on how to create a custom processor.
After reading that documentation you'll need a processor class and you can use FeedsUserProcessor.inc within the feeds module as a starting point. Copy that file into your own module where necessary, rename it and also rename the class name defined within the file. In this file you should see this implementation of the entityValidate function:
<code>/**
 * Validates a user account.
 */
protected function entityValidate($account) {
  if (empty($account->name) || empty($account->mail) || !valid_email_address($account->mail)) {
  throw new FeedsValidationException(t('User name missing or email not valid.'));
  }
}
</code>
Remove the !valid_email_address($account->mail) bit in that if statement and configure your feeds importer to use the custom processor you just created.
FYI, I would not recommend importing users with invalid email addresses as this will cause major problems with your users trying to use your site properly.


coefficient of $x$ in a determinant

Question: What is the coefficient of $x$ in the expansion of the determinant$\begin{vmatrix}
(1+x)^2 & (1+x)^4 &    (1+x)^6  \\
(1+x)^3 & (1+x)^6 & (1+x)^9  \\
(1+x)^4 & (1+x)^8 & (1+x)^{12}  \\
\end{vmatrix}$.
I simplified the determinant to be $x^3(1+x)^{16}(x+2)$,how will i find coefficient of $x$?Someone help me in getting answer?
Comment: Are you sure you got the correct answer? In case this is true, $x$ has a zero coefficient.
Comment: @BolzWeir,Sir you can verify yourself.
Comment: You multiply $x^3$ by another polynom. There is cannot be terms with $x$.
Answer: $x^3(x+2)(x+1)^{16} = x^4(x+1)^{16}+2x^3(x+1)^{16}= a_1x^3+a_2x^4+\cdots + a_{20}x^{20}$. It appears that the term $bx$ does not exist, as suggested by @BolzWeir so we can take $b = 0$.

coefficient of $x$ in a determinant


fundamental group of torus minus a point

Question: I want  to calculate fundamental group of torus minus a point using Van-Kampen Theorem.I know that result is $\Bbb Z* \Bbb Z$.
I proved to consider like open $U$ the torus minus a disk $S$ that contain the point and like open $V$ the internal part of torus, then $U$ intersected $V$ is the internal part of torus minus disk $S$. Is this right? And then what are fundamental groups of $U$, $V$ and $U$ interseced $V$?
Answer: Think of this space as the closed unit square with sides properly identified, and the $(1/2,1/2)$ point removed.
Take $U$ to be an open ball around $(1/2,1/2)$. Consider a ball $B$ around $(1/2,1/2)$ that sits within $U$, and let $V$ be the complement of $B$.
So, this means that:

$U$ is an open annulus
$V$ is homotopic to a wedge of two circles
$U\cap V$ is also an open annulus.

Because the loops in $U\cap V$ are the same loops in $U$, this means that the fundamental group is
$$\underbrace{\mathbb{Z}}_{U}\ast_{\mathbb{Z}}\underbrace{(\mathbb{Z}\ast\mathbb{Z})}_{V} \cong \mathbb{Z}\ast\mathbb{Z}.$$
You should fill in all the details, here!

Why is $V$ homotopic to a wedge of two circles?
Why does $V$ have fundamental group $\mathbb{Z}\ast\mathbb{Z}$?
Why does the "$U$" part of the free product die in the amalgamation?


Counting when there are two inclusive conditions.

Question:
How many 8-bit sequences begin with $101$ OR (inclusive) have a $1$ as
  their fourth bit?

For the first condition, we need only to decide the values of the $5$ other bits, so there are
$$2^5$$
sequences starting with $101$.
For the second condition, we have to decide the values of the other $7$ bits, so there are
$$2^7$$
sequences with a $1$ as their fourth bit.

The final answer, however, surely cannot be
$$2^5+2^7$$
Because $2^5$ includes some scenarios with a $1$ as the fourth digit, whereas $2^7$ includes cases with a $101$ at the beginning, so I would be over-counting this. What do I do in this case?
Comment: @Omega subtract the number of "intersections". i.e, see how many 8-bits start with 101 **and** have 1 as their fourth bit
Comment: http://en.wikipedia.org/wiki/Inclusion%E2%80%93exclusion_principle
Comment: @vadim123: Ah, so the answer would be like $$2^7 + 2^4$$ I think?
Comment: This doesn't really help though, because he's derived this principle himself, but doesn't know how to calculate the intersection of the two sets.
Answer: The big problem here: if $A_1$ is the set of sequences which satisfy the first property and $A_2$ is the set of sequences which satisfy the second, then (exactly as you suggest), $\lvert A_1\rvert+\lvert A_2\rvert$ over-counts.
However, it over-counts in a very predictable way: namely, any sequence which is in exactly one of the sets is counted only once, while any sequence in $A_1\cap A_2$ is counted twice!  So, this tells us that
$$
\lvert A_1\rvert+\lvert A_2\rvert=\lvert A_1\cup A_2\rvert+\lvert A_1\cap A_2\rvert.
$$
But, we can rearrange this to get
$$
\lvert A_1\cup A_2\rvert=\lvert A_1\rvert+\lvert A_2\rvert-\lvert A_1\cap A_2\rvert.
$$
So, you need only compute $\lvert A_1\cap A_2\rvert$ -- that is, the number of sequences which satisfy BOTH properties -- and subtract it from your previous total.
(This is actually the most basic form of a more general process called the inclusion-exclusion principle.)
Answer: I think the easiest method here is to take the total of all 8-bit sequences and subtract those that don't meet the requirement: those that do not begin with $101$ AND do have a $0$ as their fourth bit.
There are $7$ choices for the first $3$ bits, $1$ choice for the fourth, and $2^4=16$ for the remaining bits.  This gives an answer of
$$2^8-7(2^4)=16(2^4)-7(2^4)=9(2^4)$$


Need help merging two seperate strings together

Question: It print the list how I want it but I want them next to each other on the same line instead of on different lines.
The inputs that I am using are abcdefghi and jklmnopqr
str1 = (input("please enter a string: "))
str2 = (input("please enter a string: "))

for i in range(0, len(str1)):
  e = str1[i] + str2[i]
  print(e)

I want it to print something like: ajbkclemdnfogphqir
but it prints it like this:
aj

bk

cl

em

dn

fo

gp

hq

ir

Comment: `print` will add a `newline` character unless you tell it to add something else: `print(e, end='')`
Comment: Thanks for the help!
Answer: str1 = (input("please enter a string: "))
str2 = (input("please enter a string: "))

e = ' '
for i in range(0, len(str1)):
  e = e + str1[i] + str2[i]
print(e)

Hope it will be helpfull


Index out of range [1] with length 0

Question: I'm working ona little MUD in Go, and I'm trying to read a list of rooms with exits from a file.
I expect the code to iterate through the lines of the file, filing every line with the index 1 to the room ID, every line with the index 2 to the room Description and every line wit the index 3 to be used to fill in the room's links. However when I run the code, i get a panic: runtime error: indext out of range[1] with length 0. I've been mulling over this for two days with no luck, any help would be mucho appreciado.
Edit: tried initializing r with r:= []*Room{} but am still getting the same error.
    r:= []*Room{}
    roomdoc, err := readLines("/usr/go/src/gopherit/GopherIT/roomdoc.txt")
    if err != nil {
        return r, err
    }
    i := 0
    index := 1
    for _, str := range roomdoc {
        if i == 0 {
            r[index].roomInitId(str)
            i++
        }
        if i == 1 {
            r[index].roomInitDesc(str)
            i++
        }
        if i == 2 {
            vri := strings.Split(str, ":")
            v, ri := vri[0], vri[1]
            r[index].addLink(v, ri)
            i++
        }
        if i == 3 {
            index++
            i = 0
        }

    }
    return r, err
}

Comment: You did not initialize `r`
Comment: What is `MUD`? .
Comment: Multi-User Dungeon, a type of text-based server RPG. Check out CoffeeMud for an example.
Comment: @BurakSerdar Tried initializing r with `r = []*Room{]` still getting the same error
Comment: @Hermeneut do you expect `r[index]` to *grow* the slice?
Comment: @mkopriva Okay. Just read up on slices, 'cause I thought I was working with arrays (doh). How would I append an empty `Room` type to the slice to then fill in the fields of? I tried `r.append(roomInitId(str))` but that is def not the right answer. Also tried `r.append(r, r[index])` but that didn't work.
Comment: @Hermeneut btw shouldn't your `if`s be chained with `else`s? As it stands right now, all of the `if` conditionals will be `true` in the first iteration. (each `if` block increment the `i` a by that the next `if` condition is satisfied)
Comment: @Hermeneut it's difficult to suggest a solution because the problem description doesn't make much sense. You say things like "every line with index 1" and "every line with index 2", but by definition an index will be unique per line, hence there is no "every line" there's only one line with index 1 and one with index 2. Can you please try rephrasing the question so that it makes more sense?
Comment: @mkopriva, @burak-serdar Thank you guys! fixed it! needed to initialize `r` and use `r=append(r, &Room{})` The if/else problem also cropped up, but I fixed that too! Thanks for the pointers!
Answer: What are you doing is "Hey go make a variable r that is a slice of pointers from the Room Struct" r:= []*Room{}" and then using on the For, so is basic trying to access a position in memory that doesn't exist in that context.
So you need to append into that slice, like  r = append(r,&Room{})

Index out of range [1] with length 0


How do I create a taxonomy tag from node title

Question: I have a "Tags" vocabulary, and I would like to automatically add the node title to this vocabulary as a tag on saving the node. I have tried to use the Rules module to do this, but I can't get it to work.
Can you help me?
Answer: I wouldn't recommend you doing that, you might get crazy node title names like "hello world I love peaches" and you dont want that tag.
anyway you can do a Hook_node_insert in your custom module to add your code after creating a node, then you should use taxanomy_term_save to insert your new term.
let me know if you need more on this.
Answer: Give Taxonomy_node a chance, in the issue queue are patches to add the term to the node itself.
Nodes and terms are created, changed and deleted synchroniosly, it sounds like youu are searching for this. This module is for D6 in the moment.
There is a similar module: Node Auto Term but I have not worked with it, it has a D7 - version.


Hypercube and dihedral group

Question: Let $G_n$ denote the subgroup of the orthogonal group $O_n$ of elements that send the hypercube to itself, the group of symmetries $C_n$, including the orientation-reversing symmetries.
It would like to show that $G_2$ is isomorphic to the dihedral group $D_4$ of order 8
I determined a formula for the order of $G_n$ which is $n!2^n$. Therefore, we already notice that both sets have the same order.
Comment: 1. It is not clear what $C_n$ is and how it is different from $G_n$. 2. Group theorists often denote by $C_n$ the cyclic group of order $n$, so this is confusing. 3. When you say $D_4$, please specify what you mean: the dihedral group of order $4$ or of order $8$, because different authors use different notation.
Comment: $C_n$ is the hypercube not the cyclic group.
Comment: Now the answer depends on the exact definition of the dihedral group $D_4$ that you use. Some people just define it as the group of symmetries of a square, in which case the problem is trivial.
Comment: I edited above based on your comment
Comment: I see, and my second comment is still relevant.
Comment: @DanShved I don't understand. Do you need another specification ?
Comment: Yes. Here are several ways to define $D_4$: (i) $D_4$ is the group of symmetries of a square. (ii) $D_4$ is the semidirect product of $\mathbb{Z}/4\mathbb{Z}$ with $\mathbb{Z}/2\mathbb{Z}$, where the latter acts on the former by taking inverses. (iii) $D_4$ is specified by generators and relations as $\langle a, b \mid a^4 = 1, b^2 = 1, b^{-1}ab = a^{-1} \rangle$. (iv) Your variant. The exact answer to your problem depends on which definition you want to use.
Comment: Definition 3 is the one I am using
Comment: Sorry, I edited my comment. OK, so, the one with generators and relations.
Answer: OK, we have established that your definition of $D_4$ is
$$
D_4 = \langle a, b \mid a^4 = b^2 = 1, b^{-1} a b = a^{-1} \rangle.
$$
Now, in the group $G_2$ choose $A$ to be a rotation by 90 degrees and $B$ to be a mirror reflection. Clearly, there are such $A$ and $B$ in $G_2$. It is also clear that $G_2$ is generated by $A$ and $B$.
To prove that $G_2$ is isomorphic to $D_4$, we will build homomorphisms $\varphi: D_4 \to G_2$ and $\psi: G_2 \to D_4$ that are inverses of each other.
To build $\varphi: D_4 \to G_2$, let us note that in group $G_2$ elements $A$ and $B$ satisfy these relations: $A^4 = 1$, $B^2 = 1$, $B^{-1} A B = A^{-1}$ (check this by hand). It follows that there exists a unique homomorphism $\varphi: D_4 \to G_2$ that sends $a$ to $A$ and $b$ to $B$.
Now let us build $\psi: G_2 \to D_4$. Note that every element $C \in G_2$ can be represented as $C = A^m B^n$. Define $\psi(C) = a^m b^n$.
First of all, we need to check that this definition is correct. If we represent $C$ in two different ways, $C = A^m B^n = A^{m'} B^{n'}$, we need to check that the same value is assigned to $\psi(C)$, i.e. $a^m b^n = a^{m'} b^{n'}$. Indeed, if $A^m B^n = A^{m'} B^{n'}$, then $A^{m-m'} = B^{n'-n}$. $A$ preserves orientation and $B$ doesn't. It follows that $n-n'$ is even, so $A^{m-m'}=1$, so $m-m'$ is a multiple of $4$. But then $$a^{m'}b^{n'} = a^m a^{m'-m} b^n b^{n'-n} = a^m b^n,$$ because $a^4 = b^2 = 1$ in $D_4$. So map $\psi$ is defined correctly.
We also need to check that $\psi$ is a homomorphism. This is quite mundane and involves a little case analysis, so I will omit the check.
Now that we have built $\psi$ and $\varphi$, we see that $\psi\varphi(a) = a$ and $\psi\varphi(b) = b$, so $\psi\varphi$ is the identity map of $D_4$. In a similar way $\varphi\psi$ is the identity map of $G_2$. It follows that $\psi$ and $\varphi$ are inverses of each other, so they are isomorphisms, qed.
Comment: Can you help me with this ? Nobody answered me for a couple of days

http://math.stackexchange.com/questions/542669/point-group-of-a-pattern
Comment: I wonder if one is permitted to simply observe that upon examining the sets of generators of $\mathcal{C}_2$ and $D_4$, it turns out that they are equivalent, hence the isomorphism between the two. It boils down to whether or not specifying set of generators (plus law of composition) as in the case of the definition of $D_4$ already determines the group unambiguously, anyway, what else can there be to make $\mathcal{C}_2$ different if we have already discovered the full structure of its generators?

Hypercube and dihedral group


A $4\times 4$ matrix counterexample.

Question: A question in Dummit & Foote is asking to prove that two $3\times 3$ matrices are similar iff they have the same characteristic and the same minimal polynomial. I was able to prove that. But then the question is asking me to give an explicit counterexample to this assertion for  $4\times 4$ matrices. And I do not know how to give this example.
Could anyone help me please?
Comment: Read the 3rd comment here https://math.stackexchange.com/questions/4687946/show-that-two-matrices-are-similar-in-m-p-mathbbf-p
Comment: Consider the case where the matrix has an eigenvalue of multiplicity 4.
Comment: From the duplicate: the matrices
$$\left(\begin{array}{cccc}
0 & 1 & 0 & 0\\
0 & 0 & 0 & 0\\
0 & 0 & 0 & 1\\
0 & 0 & 0 & 0
\end{array}\right)\qquad\text{and}\qquad
\left(\begin{array}{cccc}
0 & 1 & 0 & 0\\
0 & 0 & 0 & 0\\
0 & 0 & 0 & 0\\
0 & 0 & 0 & 0
\end{array}\right)$$
both have characteristic polynomial $x^4$ and minimal polynomial $x^2$, but since their Jordan canonical forms are different, they are not similar.
Answer: For example,choose
$A=\left( \begin{matrix}
 0&  1&  0&  0\\
 0&  0&  0&  0\\
 0&  0&  0&  1\\
 0&  0&  0&  0\\
\end{matrix} \right)
$, $B=
\left( \begin{matrix}
 0&  0&  0&  1\\
 0&  0&  0&  0\\
 0&  0&  0&  0\\
 0&  0&  0&  0\\
\end{matrix} \right) $. The characteristic and minimal polynomial is $\lambda^4$ and $\lambda^2$.
But $A$ and $B$ are not similar.

A $4\times 4$ matrix counterexample.


Generic types in an array for a tree with more than one child

Question: I am trying to implement a tree with more than one child and I need to store these children somehow. I decided I could use a LinkedList but I want to try and use an array first.
(Please I do not want to use any Imports.)
class Node<T extends Comparable<? super T>>
{
    Node<T> arrChildren[] = new Node<T>[size];
}

This does not work.
class Node<T extends Comparable<? super T>>
{
    Comparable<Node<T>> arrChildren[] = new Comparable[size];
    T element;
}

This works but I cannot compare the arrChildren[0] with a normal Node<T> and if I make all my Node<T>s Comparable Nodes I cannot reach the elements inside.
This is my first post on Stack overflow I hope too get a good response, I dont mind criticism.
Thanks.
Answer: Generics and arrays simply do not mix well in Java. It will be much easier to just use a List<T> implementation like you were considering:
List<Node<T>> arrChildren = new LinkedList<>();

Longer explanation:
Arrays:

Keep track of their component type at runtime.
Are covariant (an Integer[] is a Number[] is an Object[]).

Generic types:

Have their type arguments erased by the compiler such that they aren't available at runtime. Calls to a Node<String> become calls to a Node with appropriate casts to String.
Aren't covariant (a List<Integer> is not a List<Number>).

The reason new Node<T>[size] isn't allowed is because the array needs to know its component type at runtime, at which point there's no longer a concept of T. Only something like new Node<?>[size] is permissible.
Ignoring the restriction on creating an array of Node<T>, simply having a reference to one isn't safe because the following scenario is possible:
Node<Integer>[] intNodeArray = ...;
Object[] objArray = intNodeArray; //legal because arrays are covariant
objArray[0] = new Node<String>("asfd"); //should fail but doesn't

...

//sometime later
Node<Integer> intNode = intNodeArray[0]; //still doesn't fail because of erasure
Integer i = intNode.getValue(); //only now do we get a runtime exception

Adding a Node<String> to the array should throw a runtime exception, but it doesn't because the array only knows that its component type is Node, not Node<Integer>.
For more information, see Angelika Langer's Generics FAQ: Can I create an array whose component type is a concrete parameterized type?
Comment: Hi, Thanks for the response. I ended up using this "Node [] arrChildren = new Node[size];" combined with the comparable in the class declaration and it actually worked perfectly. I could access arrChildren[0].elements;. Is it just bad practice? I think mine worked because in this case I actually wanted a reference to the Nodes and programmed my Tree in such a way that I would never lose contact with the memory referenced. Does this make sense? I didn't want to use List from the start because if I wanted to use a Linked List I would have had to make it myself.
Comment: @Enfodo That should be fine as long as you hide it as an implementation detail and stay cognizant of the scenario I demonstrated - I didn't realize that you literally couldn't import anything.
Answer: Node<T>[] arrChildren = (Node<T>[])new Node<?>[size];

Comment: Hi, thanks I ended up using this. except I excluded the (Node[]). From what I gather it is a conversion I would use it to make a float a integer, but what does it do in this case? Thanks.
Comment: @Enfodo You're using `new Node[size]`, where plain `Node` is a raw type. It's implicitly doing the same thing as this answer.


Object doesn't support property or method 'prepend'

Question: Estou tentando tornar minha aplicação WEB compatível com o Navegador Edge. Porém eu recebo a mensagem "Object doesn't support property or method 'prepend'" no console quanto eu tento executar o seguinte código:
    var li = document.createElement("li");
 ...
    var list = document.getElementById("history");
    if (this.stackHistory.size() === 0) {
        list.innerHTML = "";
        list.appendChild(li);
    } else {
        if (document.getElementById("historyorder").value === "last")
           list.prepend(li); //ERROR AQUI
        else
           list.appendChild(li);
}

Tal código funcionou no Google Chrome e Firefox
Comment: `prepend` não é suportado nem no IE nem no Edge. [Confirme na documentação](https://developer.mozilla.org/en-US/docs/Web/API/ParentNode/prepend)
Comment: Explique mais @dvd
Answer: O método prepend que indicou não é suportado no navegador Edge nem no IE.
Isso é algo que consegue confirmar na parte de compatibilidade da documentação
No entanto pode utilizar o polyfill que a própria documentação indica:
(function (arr) {
  arr.forEach(function (item) {
    if (item.hasOwnProperty('prepend')) {
      return;
    }
    Object.defineProperty(item, 'prepend', {
      configurable: true,
      enumerable: true,
      writable: true,
      value: function prepend() {
        var argArr = Array.prototype.slice.call(arguments),
          docFrag = document.createDocumentFragment();

        argArr.forEach(function (argItem) {
          var isNode = argItem instanceof Node;
          docFrag.appendChild(isNode ? argItem : document.createTextNode(String(argItem)));
        });

        this.insertBefore(docFrag, this.firstChild);
      }
    });
  });
})([Element.prototype, Document.prototype, DocumentFragment.prototype]);

Link para este polyfill no github
O polyfill acima indicado é o código que irá adicionar "ao navegador" ao função prepend caso ele não a suporte. Por este motivo terá de ser executado antes do código que utiliza o prepend.
Comment: @dvd O *polyfill* não é para adicionar um elemento é para adicionar a função `prepend` "ao navegador" fazendo com que `prepend` passe a funcionar na mesma no IE e no Edge. Note que eu não "inventei" este código. Ele está no fim da pagina de documentação no MDN linkada na minha resposta. Simplesmente coloquei aqui para acrescentar mais conteúdo relevante à resposta.
Comment: @dvd A vantagem de usar polyfill é que neste caso pode utilizar código JS puro e funcionar em todos os browsers, mesmo sendo uma função recente como o `prepend`. A única particularidade é que tem de chamar este código de polyfill uma vez no inicio de carregamento da página, o que pode até ser num arquivo incluído só com polyfills por exemplo. Logo parece na verdade pior do que é


How to classify the opinions to get the best prediction?

Question: Assume I have four friends, each of them will predict the result of each football match (based on history data, weather data, etc). They will tell me their decisions.
I also have the set of their data history data and weather data. I can also do the similar prediction. However, I want to classify which of my four friend make the right decision instead of predict the football match. (The reason I said that because I do NOT want to put all the data including my friends' decisions into a classify to make the decision. It is because their decisions will be the same weight of the history and weather data. My friends' decisions should weight much more  )
How I can maximize the correct prediction?
Comment: what if none of your friends prediction is correct?
Comment: So there should have 5 possible output, the last one is do not trust any of those four prediction
Answer: Your model is highly inefficient. Each of the players prediction has no influence on the other players predictions' truthfulness. So instead of inputting them all at once, you should input them sequentially. So:
<code>friend1 + other data = output
friend2 + other data = output
friend3 + other data = output
friend4 + other data = output
</code>
This makes your network smaller, making the training happen in less iterations and in a less amount of time.
But still. You shouldn't input your friends prediction at all. The only thing that your network is doing is comparing it's own prediction with your friends prediction internally.
In what way should your friends' predictions add anything useful to the (each others) output?
Comment: The way you did will make the friends' decisions as the same weight of the data. Moreover, each friend gives a decision. The way you did is not getting result from their decision.
Comment: Are you familiar with neural networks? What makes you think that the friends decision have the same weight on the output just because i divided them? There is NO way you can control the weight of a certain input - this is what the neural network will figure out for you (a neural network is a black box). Your proposed model simply doesnt make sense.
Comment: Did I say that we need to limit ourselves to use neural network? I read more paper this week. I found that I can use genetic algorithm to do a weight optimaization for a few preset situation and I can use other classifier to predetermine the situation.In other words, the 'other' classifier is to choose the opinion weight set from GA. Weight x opinion will be the final result.


Should I use Realm for React Native?

Question: can I have your feedback on @realm/react? I used SQLite in my previous projects. Now I want to use Realm because of its simplicity and context feature. However, I have concerns about the future. The number of downloads per week is 3000. I'm really not sure if I should use Realm. Is it worth using or should I go with SQLite?
Comment: While the question is clear, it's asking for our opinion when we have zero knowledge of your use case. How could we tell you if it's a good option or not when we don't know what you're doing with it. Realm may be perfect solution for some applications and be not be for others.
Answer: SQLite is complex that is why the majority use Realm in their first projects but when the projects get more complex and large scale, SQLite coupled with Room is the better option. Realm can be a good choice if you have an application with a very simple database. But in reality, When the application starts to expand and manipulations in the database become more serious than just putting and getting data from the table, you will understand the complexity of NoSQL databases.
Realm combined with App Services is a modern and robust mobile database solution, wherein it is superior to SQLite with Core Data and/or Room in many aspects. It allows users to build cross-platform, offline-first applications backed by the entire Realm and MongoDB Atlas cloud platform.
Comment: Can you clarify how a *majority* was determined that move from Realm to the platform you're suggesting? Was that from a research study that's documented? Can you also clarify - *putting and getting data from the table* - from my understanding, NoSQL document databases like Realm/Atlas store data in [non-tabular](https://www.mongodb.com/nosql-explained) documents (e.g. there are no tables). Just trying to gain an understanding of your recommendation when we don't what the use case is and why Realm cannot be used for complex queries and manipulations. Thanks!
Comment: Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer).

Should I use Realm for React Native?


Can you share a file and it's history between two git repositories?

Question: I'm not sure if this is possible but I'd like to be able to have a single file and use it in two projects saved in git repostories. Such that, if I make a change in one project the other one would pick up that change when the file was pulled. Ideally the history of that particular file would show all the changes from both projects.
Everything else would remain separate.
Answer: git submodule command can give you part of the functionality but it requires additional steps to keep the file in sync.
Answer: Along with git submodules, subtree merging is another approach to doing this.  You can read a nice write-up in the Pro Git book (free online); there's also an official howto.
Avery Pennarun has written a specialized Git tool called git-subtree to handle git subtree merging more gracefully.  You can read about it in his blog and you can find the code on github.  From his post:

Note that, unlike git submodule, git subtree doesn't change the way people using your project need to work. As far as they're concerned, it's just one big project; nobody has to run (or install) git subtree unless they want to. It can just be the responsibility of a single person to extract the subproject history and upload it to the subproject repository, if you want.


How to handle / cleanup obsolete aggregated css / js?

Question: I'm only using Drupal 7 built in CSS / JS aggregation, however the files folder where the css.gz and js.gz files live are filling up at a fairly rapid pace, and while I'm sure it'll be a while before it starts to totally fill up the drive, nows as good a time as any to get a handle on the situation.

Current file count in /js is 335
Current file count in /css is 451

Is there any standard method I should be employing to deal with this situation? I'd prefer a solution that keeps the drupal in the loop.
Additionally, I see that many of the gz files have non-gz counterparts. Is there some reason both .css and .css.gz files are kept? Degredation maybe?
Thanks
Comment: Can you confirm that cron is running?
Comment: It certainly is
Answer: This is actually by design so that cached pages with older versions of files don't get broken. See this closed issue.
TL;DR: They will be deleted automatically 30 days (or whatever your drupal_stale_file_threshold variable is set to) after they were created via drupal_clear_css_cache() and drupal_clear_js_cache(). So the solution is to modify the drupal_stale_file_threshold value to something lower than the default 30 days.

When are old files deleted

Old cache files are not deleted immediately when the lookup variable is emptied, but are deleted after a set period by drupal_delete_file_if_stale(). This ensures that files referenced by a cached page will still be available.

drupal_delete_file_if_stale() defaults to 30 days - so if a) Cron is running properly and b) you see aggregated files older than 30 days, you have a different problem.

variable_get('drupal_stale_file_threshold', 2592000) is the 30 day check.
variable_set('drupal_stale_file_threshold', 172800) would change the timeout to two days. On a site where cache handling is strictly controlled, the time could be even shorter.
Source: http://api.drupal.org/api/drupal/includes!common.inc/function/drupal_build_css_cache/7
See drupal_delete_file_if_stale() for more information.

Is there some reason both .css and .css.gz files are kept?

If CSS gzip compression is enabled, clean URLs are enabled (which means that rewrite rules are working) and the zlib extension is available then create a gzipped version of this file. This file is served conditionally to browsers that accept gzip using .htaccess rules.

  Source: http://api.drupal.org/api/drupal/includes!common.inc/function/drupal_build_css_cache/7 (In the function comments)

Also see drupal_build_js_cache() which is nearly identical to drupal_build_css_cache().
Comment: Thanks, I feel a bit lazy not having looked this up myself now ;)
Answer: After 4 years, I have to disagree with the first answer, where the author states :

"This ensures that files referenced by a cached page will still be available.".

Maybe certains things have been changed/optimized in the aggregation provisioning of older files, but if I delete an older file manually on the server at files/advagg_js (which I apparently still use in one of my browsers), the subsequent page reload is regenerating exactly the same file again with recently added javascript source code, as if drupal_build_js_cache() was executed on that aggregated file name.
eg. js__22qMV1d_G25luSFBkuR7bIuKD5FE80eKuXx6ldibEixg__yjA2JTeF2f1LUJ3PMdjMr8k9nOPZQJIcvVw-c5Gz_yc__FY0NTHFBVMd9MIGE5srDXTejEZGP-ccSH7UX2zImN-0.js
So I am concluding setting a significant lower drupal_stale_file_threshold would not cause any problems, and even deleting all aggregated files followed by a cache clear will force a regeneration of aggregates (tested and confirmed working on page reloads)
Answer:
Get the Rules module
Add a new rule that will be executed when the cron is running
As an action select execute php code.
Write the php code

Might be something like this:
$dir = 'your/directory/';
  foreach(glob($dir.'*.*') as $v){
  unlink($v);
}

These php functions might help you to modify the code as you like.

Glob http://php.net/manual/en/function.glob.php
unlink http://uk.php.net/manual/en/function.unlink.php

Be very careful before executing this! If not used right you may delete your site! Before trying this code test it on a localhost


SKPhysicsbody error on collision swift

Question: i have to following code that makes a custom physicsBody for my enemy.
see the code below:
let offsetX = CGFloat(Enemy.frame.size.width * Enemy.anchorPoint.x)
let offsetY = CGFloat(Enemy.frame.size.height * Enemy.anchorPoint.y)

let path = CGPathCreateMutable()

CGPathMoveToPoint(path, nil, 25 - offsetX, 73 - offsetY)
CGPathAddLineToPoint(path, nil, 32 - offsetX, 72 - offsetY)
CGPathAddLineToPoint(path, nil, 45 - offsetX, 66 - offsetY)
CGPathAddLineToPoint(path, nil, 47 - offsetX, 63 - offsetY)
CGPathAddLineToPoint(path, nil, 43 - offsetX, 11 - offsetY)
CGPathAddLineToPoint(path, nil, 25 - offsetX, 2 - offsetY)
CGPathAddLineToPoint(path, nil, 11 - offsetX, 15 - offsetY)
CGPathAddLineToPoint(path, nil, 5 - offsetX, 29 - offsetY)
CGPathAddLineToPoint(path, nil, 5 - offsetX, 30 - offsetY)
CGPathAddLineToPoint(path, nil, 3 - offsetX, 44 - offsetY)
CGPathAddLineToPoint(path, nil, 3 - offsetX, 51 - offsetY)
CGPathAddLineToPoint(path, nil, 7 - offsetX, 65 - offsetY)
CGPathAddLineToPoint(path, nil, 9 - offsetX, 67 - offsetY)
CGPathAddLineToPoint(path, nil, 15 - offsetX, 70 - offsetY)

CGPathCloseSubpath(path)

Enemy.physicsBody = SKPhysicsBody(polygonFromPath: path)

when i run this code my physicsBody is half the size of my picture. and sits at the bottom left of the picture. how do i fix this?
Answer: i figured it out if you multiply all your values it should work!
see the next line of code:
CGPathAddLineToPoint(path, nil, 11*2 - offsetX, 15*2 - offsetY)

SKPhysicsbody error on collision swift