Subspace is convex and closed set

Question: Let $V$ be a vector space. Would you help me to prove that if $A$ is a subspace of $V$ then $A$ is convex and closed set.
I can prove that $A$ is convex (it's easy) and try to prove that $A$ is closed by showing that any sequences   {$x_n$} in $A$ that converge to $x$ implies $x\in A$ but don't have any idea.
Thanks.
Comment: Subspace of infinite dimensional normed linear spaces may not be closed.
Comment: $l_\infty(\mathbb{R})$ with supremum norm. Let $M$ be the subspace of sequences with finitely many nonzero terms. Then the closure contains $(1, \frac{1}{2}, \frac{1}{3}, ...)$, which is not in $M$.
Comment: Why? Could you give a counter example?
Answer: I expanded my comments into an answer.

In infinite dimensional normed linear spaces, subspaces are convex but not necessarily closed.
Consider $l_\infty(\mathbb{R})$ which is the set of bounded sequences in $\mathbb{R}$ with the norm $|(a_n)_{n \in \omega}| = \sup a_n$. Note that the vector space structure is given by term by term addition and term scalar multiplication.
Then let $M$ be the subspace of sequences that have only finitely many nonzero terms. You can verify that $M$ is a subspace.
The sequence $\alpha = (1, \frac{1}{2}, \frac{1}{3}, ..., \frac{1}{n}, ...)$ is a bounded sequence, hence is in $l_\infty(\mathbb{R})$. $\alpha \in \overline{M}$, the closure, since
$|\alpha - (1, \frac{1}{2}, ..., \frac{1}{n}, 0, 0, 0, ...)|_\infty = \frac{1}{n + 1}$.
So $\alpha$ is the limit of a sequence of elements from $M$. $\alpha$ is in the closure of $M$ but not an element of $M$. Hence $M$ is not closed.
Comment: In fact, in every infinite dimensional normed linear space that is complete (a.k.a Banach space), one can find a linear subspace that is not closed.
Answer: You mean $V$ is finite dimensional, no? We can then regard $V$ as the standard $\mathbb R^n$.
Consider a supplementary base $v_1,\ldots,v_n$ orthogonal to $A$. Then
$$A=\{x \mid \forall i:\langle x,v_i \rangle =0\} = \bigcap_i {v_i}^\perp$$
 and, as $\langle\cdot,v_i\rangle$ is continuous, we have that ${v_i}^\perp$ is closed.
For infinite dimension, this is not true. Neither the topology is not uniquely determined, can be of many kind..
Answer: I suppose $V$ is a finite dimensional vector space over $\mathbb{R}$.
We can identify $V$ as $\mathbb{R}^n$.
Let $A$ be an $n - r$ dimensional subspace of $V$.
There is a bijective linear transformation $f\colon V \rightarrow V$ such that $f(A) = \{(x_1\dots,x_n) \in \mathbb{R}^n\colon x_1 =\cdots=x_r = 0\}$.
Hence $A$ is closed.

Subspace is convex and closed set


Why javascript is needed for preloading images?

Question: On this site I found some explanation of preloading images stuff.

Could someone explain, what is the difference between the above code and the following:

In both cases images are cached and ready to use somewhere on page.
Why we need any special preloading js code ?
Comment: because js can load from the  before anything is visible, and it can pre-load on many pages without bloating the html.
Comment: You only really benefit from caching if you want to change an image on the screen - last time I saw code like this was when we did button roll-overs by changing the image being shown.
Comment: I'd say another advantage is that your markup is cleaner using JS. You're not stuff things into the DOM simply for the purpose of pre-loading it.
Answer: Some advantages I can think of:

Loops, you only need to specify the image-folder once.
Better control over when you want to pre-load
Cleaner DOM
Comment: ok, but all that is not enough as reason to create so many special plugins for preload images.


Check if a Toplevel window is open from outer function in python tkinter

Question: What I want to to do is to avoid opening the same window if it's already open.
So, I have a window and a button, and a button that opens another window, I want to click the button again and if the second window it's already open, change the focus to the second window or if it's not open, open it.
I tried with secondwindow.winfo_exists() but due that the function of the button that launches the second window is out the secondwindow function, it returns me that my secondwindow is not defined.
Way apart of this, I added a second button to check if the second window is created instead of checking calling the same function again.
Any way to do this? This is part of the code I have used:
def startwind1():
    wind1 = tk.Tk()
    wind1.title('Window 1')
    w1button1 = ttk.Button(wind1,text='Launch Window 2',command=startwind2).pack()
    w1button2 = ttk.Button(wind1,text='Check if Window 2 exists',command=checkwind2).pack()
    wind1.mainloop()

def startwind2():
    wind2 = tk.Toplevel()
    wind2.title('Window 2')

def checkwind2():
    if wind2.winfo_exists() == 1:
        print('Window 2 exists')
    else:
        print('Window 2 not exists )

Hope you can help me!
Comment: you have to use `global wind2` in `startwind2` to make it global. I would also create at start `wind2 = None` (outside functions) and check also `if wind2 is not None`
Answer: Inside startwind2 you have to use global wind2 to assign window to global variable and then it will be available in other functions.
It also need to create wind2 = None so variable will be available even before you create window.
import tkinter as tk
from tkinter import ttk

wind2 = None

def startwind1():
    #global wind2
    #wind2 = None

    wind1 = tk.Tk()
    wind1.title('Window 1')

    w1button1 = ttk.Button(wind1,text='Launch Window 2', command=startwind2)
    w1button1.pack()

    w1button2 = ttk.Button(wind1,text='Check if Window 2 exists',command=checkwind2)
    w1button2.pack()

    wind1.mainloop()

def startwind2():
    global wind2

    wind2 = tk.Toplevel()
    wind2.title('Window 2')

def checkwind2():
    if (wind2 is not None) and wind2.winfo_exists():
        print('Window 2 exists')
    else:
        print('Window 2 not exists')

startwind1()

Answer: You can also do this with an if statement. It's a different approach but works well, and is very simple.
win1 = None

def startwind1():
    if wind1 is not None and win1.winfo_exists():
        pass

    else:
        global wind1
        wind1 = tk.Tk()
        wind1.title('Window 1')
        wind1.mainloop()

Comment: Instead of "pass" you could put wind1.lift() to raise the window to the front in  case it is hidden behind.


Add user's role as a class to all user links

Question: My goal is to attach a users role as a class to their user name anywhere it is displayed so I can color the link based on role.
I can get roles of every displayed user link using MYTHEME_username_alter:
function MYTHEME_username_alter(&$name, $account) {
  $user = user_load($account->uid);
  // I can get the roles of each user link through $user
}

Is this the most efficient way to get the role for each user link?
What is the best way to go about adding the role as a class to every user link?
Am I way off track and overlooking an easier way to complete my goal?
Answer: I don't think you can do it with username_alter. You can only modify the user name string in there. Its probably better to use preprocess. In your theme template.php. Do something like this:
function YOUR_THEME_NAME_preprocess_username(&$variables) {
  // Username is linked, use the link_attributes to inject the class or any other attribute
  if (isset($variables['link_path'])) {
    $variables['link_attributes']['class'][] = 'SOME_CLASS';
  }
  // Username is NOT linked, maybe do something else or nothing
  else {
    $variables['attributes_array']['class'][] = 'SOME_CLASS';
  }
}

Comment: I kept running into preprocess_user when looking for a solution, thanks for pointing me at the correct preprocess. I figured out a solution using this function, I'll post it since it may help others but I'll accept this as the answer
Answer: To add roles as a class to all user links I used the following:
function MYTHEME_preprocess_username(&$variables) {
  $user = user_load($variables['account']->uid);

  foreach ($user->roles as $role) {
    if (isset($variables['name'])) {
      $variables['attributes_array']['class'][] = 'role-' . strtolower(str_replace(' ', '-', $role));
    }
  }
}

Comment: You need not do user_load. Use `global $user` and you will have the $user object.
Comment: @arsenelupin That would give every user link the class of the currently logged in user
Comment: Global user and user_load are identical. You are doing an extra load which hurts perfirmance . every time you do a load on the theme layer you nay be doing something wrong.
Comment: Pretend I have a block with 10 different users listed, the goal was to color each user based on their role. `global $user` gives you the currently logged in user's info.  The code above is loading the role for each different user name as it is loaded.  This can't be done with global $user.
Comment: I see well every case is a little different. In any case,I would avoid doing user_load esp if you have listing of user names/ids. You should hook into whatever mechanism you use to generate this list, and add the role to its theme variables and then use the vars in your preprocess. Anyway, this is out of the scope of the question.
Comment: I think it's in scope, I'd still like to find the most efficient way to do this.  If I were just doing it in one block that would work, but I need to add the class to EVERY name that is listed on the site ie "whos online, node authors, forums, member list, several different blocks and many other places that shows a user's name.  Would it be more efficient to hook all 10+ modules individually or use the above code?  Is there a way to do it without either `user_load` or hooking every module that outputs an user name?


Problem Deriving Expected Sufficient Statistic

Question: Any exponential family distribution can be expressed as $p(x|\theta) = g(\theta) f(x)  e^{\phi(\theta)^T T(x)} = f(x) e^{\phi(\theta)^T T(x) - A(\theta)}$ where $A(\theta) = -\log{g(\theta)}$.
We know that this integrates to 1 since it's a valid pdf:
$\int f(x) e^{\phi(\theta)^T T(x) - A(\theta)} dx = 1$
The expected value of the sufficient statistic is obtained by differentiating both sides with respect to $\theta$. Assuming this is a parameter vector, we will need a vector derivative:
$\begin{align} \frac{\partial}{\partial \theta_i} \int f(x) e^{\phi(\theta)^T T(x) - A(\theta)} dx &= \frac{\partial}{\partial \theta_i} (1) \\ \int \frac{\partial}{\partial \theta_i} [ f(x) e^{\phi(\theta)^T T(x) - A(\theta)}] dx &= 0 \\ \int p(x|\theta) \left[ \frac{\partial \phi(\theta)^T}{\partial \theta_i} T(x) - \frac{\partial A(\theta)}{\partial \theta_i } \right] dx &= 0 \\ \nabla_i \phi(\theta)^T \int p(x|\theta) T(x) dx - \nabla_i A(\theta) \int p(x|\theta) dx &= 0 \\ \nabla_i \phi(\theta)^T E \left[ T(x) \right] - \nabla_i A(\theta) &= 0\end{align}$
But this is not quite correct since the first term is a scalar (dot product between two vectors?) and the second term is a vector.
The expectation should be the vector i.e. I should have something like $E[T(x)]_i$ with the vector index $i$ on the expected value. What has gone wrong?
I think (but not sure) that the answer should be $E[T(x)]_i = \frac{1}{\phi'(\theta)} \nabla_iA(\theta)$
Answer: Let $\theta \in \Theta \subset \mathbb R^m$ and $\phi : \Theta \to \mathbb R^d$ so $T(x) \in \mathbb R^d$ too. I'll use without proof the fact that differentiation and integration can be exchanged for exponential families.
First I'm going to treat this as a natural exponential family and see what happens (this is like if $\phi$ is just the identity function). I'll use $\theta$ as the parameter so right now I have $p(x|\theta) = \exp(\theta^TT(x) - A(\theta))f(x)$.
In this case the differentiation is easy and we have
$$
0 = \int p(x|\theta) \left[T_j(x) - \nabla _jA(\theta)\right]\,\text dx
$$
so
$$
\nabla A(\theta) = \text E(T(X)).
$$
So if you're content to work with $\phi := \phi(\theta)$ as the parameter, then this is a tidy result.

Now I'll consider what happens if $m = d = 1$ to build our intuition for the general case. We'll have
$$
0 = \int p(x|\theta)\left[T(x) \cdot \phi'(\theta) - A'(\theta) \right]\,\text dx \\
\implies \text E(T(X)) \cdot \phi'(\theta) =  A'(\theta)
$$
so
$$
\text E(T(X)) = \frac{A'(\theta)}{\phi'(\theta)}
$$
as you thought.

But the situation is more complicated if $m,d > 1$. The derivates in particular are more complicated because now $\phi$ is vector-valued so we'll get a matrix of first derivatives.
We need
$$
\frac{\partial}{\partial \theta_j} \phi(\theta)^TT(x).
$$
Writing this as a sum, we have
$$
\frac{\partial}{\partial \theta_j} \phi(\theta)^TT(x) = \sum_{i=1}^d T_i(x) \frac{\partial}{\partial \theta_j}\phi_i(\theta).
$$
$\phi : \Theta\to\mathbb R^d$ with $\Theta\subset\mathbb R^m$ so for each component we have $\phi_i : \Theta\to\mathbb R$ with $i=1,\dots,d$. Each $\phi_i$ is just a typical scalar-valued function so $\frac{\partial}{\partial \theta_j} \phi_i = (\nabla \phi_i)_j$.
I'll collect these gradients into a matrix $\Phi(\theta) \in \mathbb R^{m\times d}$ where
$$
\Phi(\theta) = \left[\begin{array}{c|c|c|c}\nabla \phi_1(\theta) & \nabla \phi_2(\theta) & \cdots & \nabla \phi_d(\theta)\end{array} \right].
$$
This means
$$
\frac{\partial}{\partial \theta_j}\phi_i(\theta) = \Phi(\theta)_{ji}
$$
so
$$
\frac{\partial}{\partial \theta_j} \phi(\theta)^TT(x) = \left(\Phi(\theta)T(x)\right)_j.
$$
All together this means
$$
\mathbf 0 = \int p(x|\theta) \left[ \Phi(\theta)T(x) - \nabla A(\theta) \right] \,\text dx \\
\implies \Phi(\theta)\text E [T(X)] = \nabla A(\theta)
$$
which is a linear system that we're trying to solve for $\text E [T(X)]$. The dimensions in question and the specific properties of $\phi$ now will determine how  many solutions this system has.

Here's one example. Suppose $X \sim \mathcal N(\mu, \sigma^2)$ so
$$
p(x|\mu,\sigma^2) = \frac{1}{\sqrt{2\pi\sigma^2}}\exp\left(-\frac{(x-\mu)^2}{2\sigma^2}\right).
$$
Rearranging and expanding the quadratic we have
$$
p(x|\mu,\sigma^2) = \frac{1}{\sqrt{2\pi}}\exp\left(\frac{\mu}{\sigma^2}x - \frac{1}{2\sigma^2} x^2 - \left[\frac{\mu^2}{2\sigma^2} + \frac 12 \log \sigma^2\right]\right)
$$
which means we'll have $\theta = (\mu,\sigma^2)$, $\Theta = \mathbb R \times (0,\infty)$, $T(x) = {x \choose x^2}$,
$$
\phi(\theta) = \left(\begin{array}{c}\frac{\theta_1}{\theta_2} \\ \frac{-1}{2\theta_2}\end{array}\right),
$$
and
$$
A(\theta) = \frac 12 \theta_1^2\theta_2^{-1} + \frac 12 \log\theta_2.
$$
This means
$$
\Phi(\theta)= \left(\begin{array}{cc}\theta_2^{-1} & 0 \\ -\theta_1\theta_2^{-2} & \frac 12 \theta_2^{-2}\end{array}\right).
$$
Noting that
$$
\det \Phi(\theta) = \frac 12 \theta_2^{-3} > 0
$$
we know this matrix is invertible, and it's just $2\times 2$ so the inverse is easy enough to compute:
$$
\Phi^{-1}(\theta) = 2\theta_2^3 \left(\begin{array}{cc}\frac 12 \theta_2^{-2} & 0 \\ \theta_1\theta_2^{-2} & \theta_2^{-1}\end{array}\right) = \left(\begin{array}{cc}\theta_2 & 0 \\ 2\theta_1\theta_2 & 2\theta_2^2\end{array}\right).
$$
We also have
$$
\nabla A(\theta) = \left(\begin{array}{cc}\theta_1\theta_2^{-1} \\ -\frac 12\theta_1^2\theta_2^{-2} + \frac 12 \theta_2^{-1}\end{array}\right)
$$
so
$$
\text E(T(X)) = \Phi^{-1}(\theta)\nabla A(\theta) \\
= \left(\begin{array}{c}\theta_1 \\ \theta_1^2 + \theta_2\end{array}\right) = {\mu \choose \mu^2 + \sigma^2}
$$
which we know to be the first two raw moments of $X$. This was a lot of work to get there but it shows the unavoidable role of the matrix $\Phi$.

Update: here's the multivariate normal case. Let $X \sim \mathcal N_n(\mu, \Omega^{-1})$ where $\Omega$ is the precision matrix. I'll use $\Omega_{(i)}$ for the $i$th row of $\Omega$, and for a vector $v$ and $1\leq i\leq j$ I'll use $v_{i:j}$ to be $(v_i,v_{i+1},\dots,v_j)$.
The density is
$$
p(x \mid \mu, \Omega) = \exp\left[-\frac 12 x^T\Omega x + x^T \Omega \mu - \frac 12 \left(\mu^T\Omega\mu - \log\det \Omega\right)\right] \cdot(2\pi)^{-n/2}.
$$
The parameters are $\mu$ and $\Omega$ but I want to deal with these as a flat vector so I'll combine $\mu$ with $\Omega$ "unrolled" as
$$
\theta = (\mu, \Omega_{11}, \Omega_{12}, \dots, \Omega_{1n}, \Omega_{22}, \dots, \dots, \Omega_{n-1,n}, \Omega_{nn}).
$$
I'm only taking the diagonal and one triangle of $\Omega$ since it's symmetric. This means $\Theta\subset \mathbb R^m$ where $m = n + {n+1\choose 2} = 2n + {n\choose 2}$.
I now need to get this as $\phi(\theta)^TT(x) - A(\theta)$. For $\phi$ and $T$ I'll use the fact that
$$
x^T\Omega x = 2\sum_{i < j} x_ix_j \Omega_{ij} + \sum_{i=1}^n x_i^2\Omega_{ii}
$$
so I'll take
$$
\phi(\theta) = \left(\Omega\mu, -\frac 12 \Omega_{11}, -\Omega_{12}, \dots, -\Omega_{1n}, -\frac 12 \Omega_{22}, -\Omega_{23}, \dots, \dots, -\Omega_{n-1,n}, -\frac 12 \Omega_{nn}\right)
$$
(I'm writing this in terms of $\mu$ and $\Omega$ instead of $\theta$ just because it's way clearer, but these can all be written in terms of $\theta$ if desired) and
$$
T(x) = (x, x_1^2, x_1x_2, \dots, x_1x_n, x_2^2, x_2x_3, \dots,\dots, x_n^2).
$$
We can now construct $\Phi$. I'll build it as a $2\times 2$ block matrix where
$$
\Phi(\theta) = \left(\begin{array}{c|c}A&B\\\hline C&D\end{array}\right)
$$
with $A$ being $n\times n$ and $D$ being ${n+1c\choose 2}\times{n+1\choose 2}$. $A$, $B$, and $D$ are easy to work out.
For $A$, I need $\frac{\partial \phi_i}{\partial \theta_j}$ where $i,j=1\dots,n$. Since $\phi_i(\theta) = \Omega_{(i)}^T\mu$ for this range of $i$ values, and $\theta_j = \mu_j$, we'll get
$$
(\nabla \phi_i)_{1:n} = \Omega_{(i)}
$$
(using the symmetry of $\Omega$ for the fact that $\Omega_{k\ell} = \Omega_{\ell k}$). This means that $A = \Omega$.
Now for $i > n$, $\phi_i(\theta)$ is proportional to some $\Omega_{j\ell}$ so derivatives w.r.t. $\mu$ are zero. This means $B = \mathbf O$, the appropriately sized zero matrix.
Next, for $i,j>n$ if $i\neq j$ then $\frac{\partial \phi_i}{\partial \theta_j} = 0$ so $D$ is diagonal. If $i$ corresponds to a diagonal entry of $\Omega$ then we have a derivative of $-\frac 12$, otherwise it'll be a derivative of $-1$. The first element of the diagonal of $D$ corresponds to $\Omega_{11}$ so it begins with $-\frac 12$. The next value of $-\frac 12$ is for $\Omega_{22}$ which happens at element $1+n$ since all of the first row of $\Omega$ has been counted and $\Omega_{21}$ is skipped (since it's accounted for by $\Omega_{12}$). There are $n-2$ remaining elements of $\Omega_{(2)}$ to account for, so the 3rd value of $-\frac 12$ in $D$ is at position $1+n+(n-1)$. The next $-\frac 12$ is at $1+n+(n-1)+(n-2)$ and so on, so the locations of $-\frac12$s are given by the sequence
$$
1+\sum_{j=0}^{i-2}(n-j)
$$
with $i=1,\dots,n$. As an example, if $n=5$ then the $-\frac 12$s will be at locations $1,6,10,13,15$. As a sanity check, $D$ is supposed to have a diagonal of length ${n+1\choose 2}$, and the final $-\frac 12$ is for $\Omega_{nn}$. We have
$$
1+\sum_{j=0}^{n-2}(n-j) = \sum_{j=0}^{n-1}(n-j) \\
= \sum_{j=1}^n j = {n+1\choose 2}
$$
so this agrees with that.
Finally, we need $C$. I'm going to skip $C$ for now until the final step.
For $\Phi^{-1}$, $\Omega$ and $D$ are always invertible so $\Phi$ is too, and we can use the formula for the inverse of a $2\times 2$ block matrix to find
$$
\Phi^{-1}(\theta) = \left(\begin{array}{c|c}\Omega^{-1} & \mathbf O \\ \hline
-D^{-1}C\Omega^{-1} & D^{-1}\end{array}\right).
$$

Now we can get $\nabla A$ to finish this off. I'll also partition it into the first $n$ elements, which lines up the upper two blocks of $\Phi^{-1}$, and the remaining ${n+1\choose 2}$ which I'll call $V$. We have
$$
A(\theta) = \frac 12 \mu^T\Omega\mu - \frac 12 \log\det \Omega
$$
so
$$
\frac{\partial A}{\partial \theta_{1:n}} = \frac{\partial A}{\partial \mu} = \Omega\mu.
$$
This means
$$
\nabla A(\theta) = {\Omega\mu \choose V}
$$
so
$$
\Phi^{-1}(\theta)\nabla A(\theta) = {\mu \choose -D^{-1}(C\mu - V)}.
$$
This shows that $\text E(T_{1:n}(X)) = \text E(X) = \mu$ which is nice to see.
For the rest, I won't work it out in general but I'll just show what we get for the elements corresponding to diagonal terms of $\Omega$ and off-diagonal terms.
For $V$, I'll need derivatives of $\log\det \Omega$ w.r.t. $\Omega_{ij}$ which I can get via this result:
$$
\frac{\partial}{\partial \Omega_{ij}} \log\det \Omega = (2 \Sigma - \text{diag}(\Sigma))
$$
due to symmetry. This means that for the elements of $V$ corresponding to a diagonal element of $\Omega$ we'll have $\frac 12 \mu_i^2 - \frac 12 \Sigma_{ii}$ while the off-diagonal will be $\mu_i\mu_j - \Sigma_{ij}$.
For $C\mu$, the diagonal elements contribute $\mu_i^2$ because for that row the only non-zero element is $\mu_i$ in the $i$th column. $-D^{-1}$ will multiply the result by $2$, and then $V_i$ is
$$
2\left(\mu_i^2 - \frac 12 \mu_i^2 + \frac 12 \Sigma_{ii} \right) \\
= \mu_i^2 + \Sigma_{ii}.
$$
For the off-diagonal, you can show that $C\mu$ will contribute $2\mu_i\mu_j$ so we get
$$
2\mu_i\mu_j - \mu_i\mu_j + \Sigma_{ij} = \mu_i\mu_j + \Sigma_{ij}.
$$
Thus after all this work we have confirmed that $\text E(X_i) = \mu_i$ and $\text E(X_iX_j) = \mu_i\mu_j + \Sigma_{ij}$.
Comment: Wow. Thanks for an awesome reply! But what about if I want to consider e.g. a multivariate normal distribution in D dimensions. In this case the matrix of derivatives $\Phi(\theta) $ will be DxD meaning I won't be able to invert it explicitly. And yet we should still be able to obtain algebraic solution for E[T(X)] in this same way?
Comment: @user11128 I think in general $\Phi(\theta)^{-1}\nabla A(\theta)$ might be as explicit as you can get if you take this approach unless $\Phi$ happens to have some special structure. I'll go through the multivariate normal example and update with that. I'm also looking more into general conditions for $\Phi$ to be invertible.
Comment: @user11128 I just updated with a multivariate normal example, $\Phi$ had enough structure that the inverse was easy to do


Classifier feature importance

Question: If I train a GNB/LDA/kNN/other classifier I would like to know, in the model built, how important are features to classify or which feature(s) drives the classifier.
For example in SVM models the importance of the feature is sometimes evaluated looking at weights magnitude, but for non-linear and generative models is more difficult to extract weights.
Do you know a book/paper that could be useful to solve my question?
Answer: I know of no book, but you can "probe" the behaviour for single variates: test what happens with the class scores if a certain input feature is varied, but the remaining are kept constant. For the nonlinear classifiers, make sure that your constant part and "probe pulse" are meaningful.
If you need a paper where feature influence on LDA was interpreted (not only coefficients, but also taking into account magnitude of input and irrelevant differences between models), we did this here: C. Beleites, K. Geiger, M. Kirsch, S. B. Sobottka, G. Schackert and R. Salzer: Raman spectroscopic grading of astrocytoma tissues: using soft reference information, Anal. Bioanal. Chem., 400 (2011), 2801 - 2816.  The linked page has both links to the official web page and my manuscript.
Answer: One famous paper is Wrappers for feature subset selection by Kohavi and John (1997). It basically deals with the problem of removing the irrelevant features, which should provide an answer to your question. The approach is agnostic wrt the classifier used.
It's old, and it has almost 4k citations, so there is probably a wealth of follow-up work.
Comment: They define two different approaches: a filter, which selects features independent of the classifier used (which wouldn't help you) and the wrapper approach, which basically runs the classifier (learner) with different feature subsets and evaluates the results to give the best subset.

It doesn't literally tell you which features were actually dominant in the classification with the full set, but I would say that the subset that gives the best performance, are in some sense the most important.
Comment: I'll take a look, but (looking at the title!) my question is after feature selection. So the scenario is basically after feature selection and cross-validation, I've the model and I would like to know which features are more important to the classification.
Answer: The wrapper model popularized by Kohavi as mentioned @Peter would help in finding optimal features which are not necessarily relevant to your target labels. In the same paper, Kohavi states that "relevance does not imply optimal" and vice versa. In addition, the generally defined wrapper model does not rank the importance of your features.
You may follow one of the following solutions to rank the features selected by the wrapper model:
1- Rank the featuers using some filtering method as mRMR. Then, using forward selection you optimize your classifier and once the performance degrades you stop.
2- Select the featuers by the wrapper model and then, rank the selected ones by mRMR (opposite of 1).
3- Run the wrapper model over different folds of your data and then, you rank selected featuers based on frequency of being selected in different folds. An ensemble idea can follow this framework for featuer selection, also.

Classifier feature importance


flyway/springboot - configure to run on production/test but not on dev

Question: How to check through spring-boot that if the database schema is already present do not run but run if no matching schema. Can I do the following through spring-boot flyway integration.

DEV - already has the database tables matching the sql scripts (e.g V1_Baseline.sql has both table creation and reference data) so should not run this but will need to create the internal SCHEMA_VERSION table and make this script as already run so that V2_....sql will run next)
TEST - has tables that should not be dropped but the scripts need to be run and internal SCHEMA_VERSION table created.
PROD - this is empty. So no issues should be there (same as test i guess).

From flyway site  I saw this:
flyway clean  (cannot run - nothing should be dropped)
flyway baseline (i think this is the one I need? check if current db is matching then just make current scripts as baseline and dont run if not run the scripts)
Nothing concrete here on here spring io too and on baeldung too.
I think answer is here with baseline and doing some checks but not sure what works.
Comment: I don't have my laptop in front of me but the easiest way I can think of achieving this is via spring.flyway.locations=classpath:db/migration,filesystem:/opt/migration - have a dev profile which uses an extra separate location with your internal schema version script in there. Hope that helps.
Comment: @RobScully your approach is most likely to work. thx
Answer: Baseline is indeed what you need here as it serves this exact purpose.
Comment: How Can I implement a conditional Baseline - ? check if baseline needs to be executed or Can I Baseline be implemented even in prod and test?


Why is the remote Sensu Client not visible in Uchiwa?

Question: I have setup sensu on a Centos 6.5 box and installed/configured the client on the sensu box so it monitors itself like so
/etc/sensu/conf.d/client.json
{
  "client": {
    "name": "sensu",
    "address": "10.100.1.200",
    "subscriptions": [
      "ALL"
    ]
  }
}

This works fine and it shows up in Uchiwa, but now I have another host I want to register with Sensu/Uchiwa and it does not show up in clients under Uchiwa/Sensu
Here's the configuration on the client "coffee"
/etc/sensu/conf.d/rabbitmq.json
{
 "rabbitmq":{
  "host": "10.100.1.200",
  "port": "5672",
  "vhost": "/sensu",
  "user": "user",
  "pass": "pass"
 }
}

/etc/sensu/conf.d/client.json
{
  "client": {
    "name": "coffee",
    "address": "10.100.1.19",
    "subscriptions": [
      "ALL"
    ]
  }
}

When I tail the /var/log/sensu/sensu-client.log on coffee I see the following:
{"timestamp":"2015-07-13T07:15:52.856009-0500","level":"warn","message":"loading config file","file":"/etc/sensu/conf.d/client.json"}
{"timestamp":"2015-07-13T07:15:52.856157-0500","level":"warn","message":"config file applied changes","file":"/etc/sensu/conf.d/client.json","changes":{"client":{"subscriptions":[["ALL","ALL"],["ALL"]]}}}
{"timestamp":"2015-07-13T07:15:52.856235-0500","level":"warn","message":"loading config file","file":"/etc/sensu/conf.d/rabbitmq.json"}
{"timestamp":"2015-07-13T07:15:52.856330-0500","level":"warn","message":"config file applied changes","file":"/etc/sensu/conf.d/rabbitmq.json","changes":{"rabbitmq":{"port":[5672,"5672"],"pass":"REDACTED"}}}
{"timestamp":"2015-07-13T07:15:52.859696-0500","level":"warn","message":"loading extension files from directory","directory":"/etc/sensu/extensions"}
{"timestamp":"2015-07-13T07:15:53.051176-0500","level":"warn","message":"reconnecting to transport"}

It looks like it's talking to RabbitMQ but for some reason the client does not appear in Uchiwa.  How can I dive into this further and figure out what's going on and get the coffee server to register?
Comment: You should try to verify the logs for sensu-server (`/var/log/sensu/sensu-server.log`) and sensu-api (`/var/log/sensu/sensu-api.log`). Since the Uchiwa dashboard uses the sensu-api to retrieve information about your clients, you could start by making sure they appear in the API.
Comment: So I was getting a Plain login refused error in the rabbitmq logs.  I changed the password to something like pAssW0Rd (not my actual password fyi) and rabbitmq allowed the sensu client to connect.  But now it's still not showing in my Uchiwa dashboard.
Comment: Here's the log showing the connection =INFO REPORT==== 13-Jul-2015::12:22:22 ===
accepting AMQP connection <0.435.0> (10.100.1.19:43998 -> 10.100.1.200:5672)
Comment: They do show up in the sensu-api.log actually.
Answer: The issue was caused by a significant time difference between the sensu-client and sensu-server.
user@sensu-server:~# date
Mon Aug 15 06:17:37 UTC 2016

vs.
user@sensu-client:~$ date
ma aug 15 08:17:37 CEST 2016

Solution
The issue was solved by synchronizing the clocks using timedatectl:
user@sensu-server:~# sudo timedatectl list-timezones
Africa/Abidjan
Africa/Accra
Africa/Addis_Ababa
Africa/Algiers
...

and changing the timezone by issuing:
user@sensu-server:~# sudo timedatectl set-timezone Europe/Amsterdam

synchronized the time on the server with the client:
user@sensu-server:~# date
Mon Aug 15 08:25:14 UTC 2016

After restarting the sensu-services and rabbitmq the client was found in the sensu-api and registered in Uchiwa.
Comment: I'm shocked this is a thing, but thanks for your answer.  Spent hours on this.  `UTC` for everything I guess...


Does having both first and second violin on the same staff replace the use of div. in a symphony score?

Question: With consideration to the general rule that when div. is presented upper notes in the music are played by the violinist sitting on the outside, and lower notes are played by those sitting on the inside - when the music presents both first and second violin on the same staff, would this divide upper and lower notes between 1st and 2nd violin, taking place of div.?
And therefore here we are seeing these played as double stops?
Attached example
Comment: Would you mind to provide the name of the piece? Note, that the full score does not necessarily indicate, what has to happen within the violin register, space saving here is top priority.
Comment: Looks like the 3rd movement of Tchaikovsky's "Pathetique" Symphony.
Comment: hello, @dekkadeci is correct! That was my initial thought - there is the possibility as this is an excerpt from a text book there may be omitted instruction.
Answer: In a situation like this, if the stem goes up it's V1, down for V2. E.g. bar 102 beat 2 V1 plays e-b double-stop.


REPLACE na cláusula IN - MySQL

Question: Eu tenho a seguinte query abaixo, sendo que a idéia é pegar o valor da variável @subcategorias e colocar no IN da consulta:
set @subcategorias = replace('10, 11, 29, 30, 31', '''', '');

SELECT
car.NomeCaracteristica,
MAX(catcar.Valor) AS Valor,
catcar.Condicional AS Condicional
FROM
caracteristica car
    inner join categoriacaracteristica catcar on car.IdCaracteristica =     catcar.IdCaracteristica
WHERE
catcar.IdCategoria IN (@subcategorias) AND
catcar.IdCaracteristica = 19 AND
catcar.Valor is not null
GROUP BY
car.NomeCaracteristica,
catcar.Condicional;

Fazendo dessa forma, não me retorna a quantidade de registros que eu preciso. Deveria retornar dois ao invés de um.
Eu sei disso porque seu eu colocar o valor no IN na mão (catcar.IdCategoria IN ('10, 11, 29, 30, 31')) ele funciona.
Álguem sabe me dizer o que pode estar acontecendo?
Answer: Basicamente é problema de sintaxe.
Você disse que se fizer isso "na mão" funciona:
catcar.IdCategoria IN ('10, 11, 29, 30, 31')

Funciona só se idcategoria for exatamente a string 10, 11, 29, 30, 31, afinal o IN espera uma lista de ítens separada por vírgula.
Do jeito que fez, '10, 11, 29, 30, 31' é um ítem só (o fato de ter vírgulas dentro da string não tem relação nenhuma com a lista de ítens que o IN espera).
Estes dois exemplos são completamente diferente da linha acima:
catcar.IdCategoria IN ( 10, 11, 29, 30, 31 )

e
catcar.IdCategoria IN ( '10', '11', '29', '30', '31' )

Neste caso, são 5 ítens diferentes, e não uma string como descrito na pergunta.
Se quer achar dados em uma string separada por vírgulas, a função é outra:
WHERE FIND_IN_SET( catcar.IdCategoria, '10,11,29,30,31' )

Manual:

https://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_find-in-set

Nota: o que poderia funcionar, no seu caso, é aplicar o seu raciocínio a uma linguagem extra que esteja gerando a query, aí se trata de uma situação completamente diferente.
Exemplo em PHP:
$lista = '10, 11, 29, 30, 31';
$sql = ' ... resto da query... WHERE catcar.IdCategoria IN ('.$lista.') AND ...';
mysqli_query( sql );

Isto já funciona, pois está acontecendo fora da camada SQL. Quando o SQL processar a string, as aspas já nem estariam lá. Aí sim, um replace da respectiva linguagem ainda teria eficácia.
Comment: Sim, isso eu li na pergunta. E não tem o mínimo fundamento achar que o replace tenha qualquer relação com o número de ítens. Uma _string_ é uma _string_.
Comment: Mas eu cheguei a fazer um replace na variável (set subcategorias = replace('10, 11, 29, 30, 31', '''', '');) que vai no IN e tirando elas, ficaria os 5 itens, mas mesmo assim, não funcionou.
Até dei um SELECT subcategorias pra saber se estava sem as aspas e estava.
Comment: Blza... compreendido!
Usei o FIND_IN_SET e funcionou.

Obrigado pela ajuda!
Comment: @Cmcampos86 Uma observação: se estiver usando alguma linguagem paga gerar/usar as queries, pode usar o IN, mas precisa remover as aspas antes do SQL. Publiquei uma solução para MySQL puro, pq é o que tinha na pergunta. Mas numa linguagem que possa manipular a query (em vez dos valores da query), dá pra fazer o que você tentou (mas fora da camada SQL). O `FIND_IN_SET` faz busca na string, mas não é tão eficiente quanto IN + lista.
Comment: Haaaaa entendi @Bacco!
Seguirei esta dica!

Obrigado!

REPLACE na cláusula IN - MySQL


Kappa2 zero value despite almost identical ratings

Question: I have a dataset with self and informant ratings of a pathological outcome in 47 people. Despite the fact that the self and informant ratings are almost identical, kappa2 (irr) gives me a kappa value of 0 (meaning chance performance) with NaN z and p-values. I have removed missing values from the data
I used the following code:

library(irr)
  kappa2(df, weight = "squared")

The results are as follows:
<code>Cohen's Kappa for 2 Raters (Weights: squared)

 Subjects = 46
   Raters = 2
    Kappa = 0

   z = NaN
  p-value = NaN
</code>
The data is literally all 'normal' values from one rater, and all but two 'normal' values from the other rater, ie extremely high concordance at face value.
Why am I getting kappa=0?
Answer: With highly skewed classes, kappa will estimate chance agreement ($p_c$) as very high. This, in turn, will result in a very low value of $\kappa$ despite a high degree of observed agreement ($p_o$):
$$
\kappa = \frac{p_o - p_c}{1 - p_c}
$$
This property has been dubbed one of the "paradoxes" of the kappa coefficient. To understand why this is, you need to dig into how kappa estimates chance agreement. Basically, with high skew, one of the following two products will be very high and the other will be very low.
$$
p_c = \Bigg(\frac{f_1}{n}\Bigg)\Bigg(\frac{g_1}{n}\Bigg) + \Bigg(\frac{f_2}{n}\Bigg)\Bigg(\frac{g_2}{n}\Bigg)
$$
Here is a contingency table for two raters (i.e., $A$ and $B$) assigning items to two mutually exclusive response categories (i.e., $1$ or $2$). The marginal totals are sums; for example, $f_1=a+c$.
$$
\begin{array}{|c|c|c|c|}
\hline
~&A=1&A=2&\text{Total}\\ \hline
B=1&a&b&g_1\\ \hline
B=2&c&d&g_2\\ \hline
\text{Total}&f_1&f_2&n\\ \hline
\end{array}
$$
References
Feinstein, A. R., & Cicchetti, D. V. (1990). High agreement but low kappa: I. The problems of two paradoxes. Journal of Clinical Epidemiology, 43(6), 543–549.
Comment: Thank you Jeffrey for a clear and referenced answer, that makes a lot of sense.


Limpar lista de activity

Question: Preciso que quando eu chegue na ultima tela de perguntas do meu aplicativo eu so consiga voltar pra tela de menu que e a tela apos a main e somente nessa tela final de perguntas que posso zerar a lista . Obrigado !
Answer: Você já pensou em usar Flag? Por exemplo:
Intent telaMenu = new Intent(RegistroActivity.this, MenuActivity.class);  telaMenu.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);startActivity(telaMenu);
Comment: Eu vi sobre porem nao funcionou continua voltando pra tela interior
Comment: tenta adicionando esse código que eu te passei

Limpar lista de activity


Calculating Value State matrix for a finite MDP without limit condition

Question: In the book "Reinforcement Learning" From Andrew Sutton and Barto there is an example given for the Bellman equations:

Figure 3.2 (left) shows a rectangular gridworld representation of a simple finite MDP. The cells of the grid correspond to the states of the environment. At each cell, four actions are possible: north, south, east, and west, which deterministically cause the agent to move one cell in the respective direction on the grid. Actions that would take the agent o↵ the grid leave its location unchanged, but also result in a reward of .1. Other actions result in a reward of 0, except those that move the agent out of the special states A and B. From state A, all four actions yield a reward of +10 and take the
  agent to A0. From state B, all actions yield a reward of +5 and take the agent to B0

Then it continues:

Suppose the agent selects all four actions with equal probability in all states. Figure 3.2
  (right) shows the value function, v⇡, for this policy, for the discounted reward case with
  gamma = 0.9. This value function was computed by solving the system of linear equations
  (3.14). Notice the negative values near the lower edge; these are the result of the high
  probability of hitting the edge of the grid there under the random policy.

3.14 equation being (so the state-value bellman equation):

I just wonder, without knowing when the task ends, how can we calculate the given matrix on figure 3.2?
And with that gamma, it will need a lot of iterations until we can ignore it.
Just trying to wrap my head around how to calculate each cell or if there is something I am ignoring.
Answer: The task doesn't end.
In RL terms, "value" at state $s$ means the weighted sum of all future rewards when starting from state $s$ and following policy $\pi$. In this example the states will transit infinitely times.
Of course it's impossible to iterate infinitely times to calculate the value, that's why the Bellman equation $(3.14)$ is so important, because Bellman discovers that the values (the wieghted sum of all future rewords) of different states satisfy a relation stated by $(3.14)$, which is basically a system of linear equations of the values.
So to calculate the values of the states, instead of iterating infinite times and accumulating the rewords for each state, you can simply solve the linear system and the result is there.
Comment: Yes, I understand that s why Bellman equation is relevant, but i guess i cant wrap my head around the math. Could you solve one cell of the value matrix ?
Comment: In this example you don't need any matrix algebra. The authors use matrix to represent the game board, each cell in the board corresponds to a state, there are 25 states in total. Bellman equation $(3.14)$ provides a linear system of 25 equations with 25 unknown values. Solve this linear system will get you the result.
Comment: ah, i got it know, it was just not productive to show that it just applied 3.14 for each s and a within the MDP process. Thanks


Associativity of a binary operator

Question: I am reading the book "A first course in abstract algebra", and i am reading the chapter called subgroups. It says that, for general situations we do not use "*" to denote the binary operation on a group, but we simply use addition symbol "+" or multiplication symbol ".". Then it says, we denote the product a.a.a.a.a...a for n factors by an. But i am confused here: is that always associative? For example, for 3 factors, a.a.a is a3 according to what book says, but are we sure that a.(a.a)=(a.a).a=a3 is always true? Thank you.
Comment: Note that not all binary operations need be associative. For example, take the integers under subtraction: $-4=(1-2)-3\neq 1-(2-3)=0$.
Comment: Do you know what "group" means?
Comment: Yes, i do. Why?
Comment: Then you know the answer to this question.
Comment: This is why i am asking this, i must be missing something
Answer: Recall: One of the key properties satisfied by all groups is that its binary operation is associative on the set defined by the group.
If you are referring to Fraleigh's text, see

Definition 4.1: "A group $\langle G, *\rangle$ is a set $G$, closed under a binary operation $*$, such that the following axioms of satisfied:

$\mathcal G_1$: For all $a, b, c \in G,$ we have $$(a*b)*c = a*(b*c)\tag{associativity of $*$}$$

Of course, $\mathcal G_2,\;\text{and}\;\mathcal G_3\;$ are crucial, as well: the existence of an identity element $e\in G$, and for each $g\in G$, its inverse, $g^{-1}$ is also in $G$, including the definitions of the identity and how we define the inverse of a group element.
Comment: Oh thanks, that's what i have been missing.
Answer: Not only, As @amWhy noted, the binary operation is associative but also depend on how you show the binary operation, the figures of composition of elements varies: $$\cdot\longrightarrow a*b=a\cdot b$$ so $$\underbrace{a\cdot a\cdot a\cdot ...\cdot a}_{\large n\;times}=a^n~$$ And $$+\longrightarrow a*b=a+ b$$ so $$\underbrace{a+ a+ a+ ...+ a}_{\large n\;times}=na$$
Comment: Don't know why the downvote? +1
Comment: Thank you! I did not downvote, just to let you know
Comment: @amWhy: Thanks Amy for the edit. :-)

Associativity of a binary operator


google sheets - list (or numbering) depending on content in Columns and rows

Question: in the following sheet i try to number rows depending on the content:
https://docs.google.com/spreadsheets/d/1Nu8il1f-5sR32hEPGbdHUSRODRbGhFRIV2sMzPv1GvY/edit?usp=sharing
I got 2 sheets. One with the content and one with a query sorted by column A(Kategory), B(SUB Kategory), C(SUB SUB Kategory).
This is the content sheet:
<code>**Kategory  SUB Kategory    SUB SUB Kategory**
Fruits      Orange          Italia
Fruits      Apple           New Zealand
Fruits      Cherry          Australia
Vegetables  Tomato          France
Fish        Salmon          Canada
Meat        Pork            Ireland
Fruits      Orange          Israel
Fruits      Apple           Germany
Fish        Salmon          New Zealand
Fish        Makrele         Germany
Vegetables  Tomato          Spain
Vegetables  Cucumber        Germany
</code>
The goal is to number in the sheet with the query depending on the content as follows:
<code>**Kategory  SUB Kategory    SUB SUB Kategory    Numbering #**
Fish        Makrele         Germany             1.1.1
Fish        Salmon          Canada              1.2.1
Fish        Salmon          New Zealand         1.2.2
Fruits      Apple           Germany             2.1.1
Fruits      Apple           New Zealand         2.1.2
Fruits      Cherry          Australia           2.2.1
Fruits      Orange          Israel              2.3.1
Fruits      Orange          Italia              2.3.2
Meat        Pork            Ireland             3.1.1
Vegetables  Cucumber        Germany             4.1.1
Vegetables  Tomato          France              4.2.1
Vegetables  Tomato          Spain               4.2.2
</code>
The content will grow and the query updates it every time in the numbering sheet. So the numbering  should update it aswell. Similar to a table of contents.
Maybe there is a way to achive this.
thanks jenar
Comment: Hi there @jenargun! This operation could be easily done by [Apps Script](https://developers.google.com/apps-script). Would an Apps Script be a valid solution for you?
Comment: Do you want the numbering to appear on your first (unsorted) sheet, or on your second (sorted) sheet? Are all rows unique (as they appear to be from your sample data)?
Comment: @jacques i have no experience with apps script, but if there is no other way, i would try this for sure. It is a big sheet with quite some tabs (sheets). So if there are no Problems in the other tabs, what do i have to do?
Comment: @Tom the numbering should appear in the second sorted sheet. All rows are unique. No Row will appear twice in the sheet, only once with its Kategory, SUB Kategory and SUB SUB Kategorie possibilities.
Answer: You can do it by repeated use of match, first on unique values in column A, then on unique values in A&B combined, then on values in A&B&C combined (because we know that these are already unique):
<code>ArrayFormula(if (A2:A="",,match(A2:A,UNIQUE(A2:A))&
"."&match(A2:A&B2:B,unique(A2:A&B2:B),0)-match(A2:A,index(unique({A2:A,B2:B}),0,1),0)+1&
"."&match(A2:A&B2:B&C2:C,A2:A&B2:B&C2:C,0)-match(A2:A&B2:B,A2:A&B2:B,0)+1))
</code>

or slightly shorter:
<code>=ArrayFormula(if (A2:A="",,match(A2:A,UNIQUE(A2:A))&
"."&match(A2:A&B2:B,unique(A2:A&B2:B),0)-match(A2:A&"*",unique(A2:A&B2:B),0)+1&
"."&match(A2:A&B2:B&C2:C,A2:A&B2:B&C2:C,0)-match(A2:A&B2:B,A2:A&B2:B,0)+1))
</code>
Comment: ...wow! Thank you Tom. That was the solution! Works like a charme! Now i can bring my learning flash cards in an order and keep track. You are great! Thank you! All the best! jenar


Juntar informações corrrespondentes que estão em arrays diferentes no mesmo array

Question: Quando o usuário da um submit no form, eu recebo os seguintes arrays (No caso tinha 3 produtos no form, se tiver apenas 1, nos 4 arrays teria apenas o indice 0, se tivesse 5 produtos, nos 4 arrays teriam indices 0, 1, 2, 3 e 4)
'tempero' =>
    array (size=3)
      0 => string 'Ketchup' (length=7)
      1 => string 'Mustard' (length=7)
      2 => string 'Barbecue' (length=8)
  'quantidade' =>
    array (size=3)
      0 => string '1' (length=1)
      1 => string '2' (length=1)
      2 => string '3' (length=1)
  'frequencia' =>
    array (size=3)
      0 => string 'FKetchup' (length=2)
      1 => string 'FMustard' (length=2)
      2 => string 'FBarbecue' (length=2)
  'combo' =>
    array (size=3)
      0 => string 'CKetchup' (length=2)
      1 => string 'CMustard' (length=2)
      2 => string 'CBarbecue' (length=2)

Meu problema é seguinte, eu precisaria juntar os itens correspondentes em arrays, no caso, precisaria de algo assim:
 'tempero1' =>
    array (size=3)
      0 => string 'Ketchup' (length=7)
      1 => string '1' (length=7)
      2 => string 'FKetchup' (length=8)
      3 => string 'CKetchup' (length=8)
  'tempero2' =>
    array (size=3)
      0 => string 'Mustard' (length=7)
      1 => string '2' (length=7)
      2 => string 'FMustard' (length=8)
      3 => string 'CMustard' (length=8)
  'tempero3' =>
    array (size=3)
      0 => string 'Barbecue' (length=7)
      1 => string '3' (length=7)
      2 => string 'FBarbecue' (length=8)
      3 => string 'CBarbecue' (length=8)

Como eu poderia fazer para juntar os itens correspondentes em um único array, talvez utilizando um foreach?
Answer: Para criar um array da forma que explicou, basta utilizar a função array_map passando o primeiro parâmetro como NULL, assim será retornado um array com as combinações dos arrays de entrada. Por exemplo:
$tempero = ["Ketchup", "Mustard", "Barbecue"];
$quantidade = ["1", "2", "3"];
$frequencia = ["FKetchup", "FMustard", "FBarbecue"];
$combo = ["CKetchup", "CMustard", "CBarbecue"];

$saida = array_map(NULL, $tempero, $quantidade, $frequencia, $combo);

Gerará um array da forma:
Array
(
    [0] => Array
        (
            [0] => Ketchup
            [1] => 1
            [2] => FKetchup
            [3] => CKetchup
        )

    [1] => Array
        (
            [0] => Mustard
            [1] => 2
            [2] => FMustard
            [3] => CMustard
        )

    [2] => Array
        (
            [0] => Barbecue
            [1] => 3
            [2] => FBarbecue
            [3] => CBarbecue
        )

)

Veja funcionando no Ideone.

Se estes arrays vem do POST, basta fazer algo como:
$saida = array_map(NULL, $_POST["temperos"], $_POST["quantidade"], ...);

Comment: Show de bola, era exatamente isso que eu estava buscando. Tinha tentado com diversas formas de `foreach`, mas no final das contas não precisava usar.
Answer: Tente utilizar este código:
$temperos = array();
foreach($itens['tempero'] as $index => $item){
    $indice = 'tempero'.$index;
    $temperos[$indice] = array($item, $itens['quantidade'][$index], $itens['frequencia'][$index], $itens['combo'][$index]);
}


Revoked certificate still valid

Question: I've recently setup a new domain joined Standalone CA on a Windows 2012 R2 server which is publicly accessible and authenticating fine, however, revoked certificates still appear to be authenticating. The actual server-side revoke process works fine as in the revoked certificate gets added to the CRL after publishing but  the certificate still authenticates the client end.
I've added externally accessible CDP and AIA locations and have cleared the local CRL cache the client end using the following commands:
certutil -urlcache CRL delete

&
certutil -setreg chain\ChainCacheResyncFiletime @now

I'm aware of the minimum OS requirements for the last command. Clients are Windows 7 upwards.
I'm using a simple test app on the client that is configured to use the one certificate so if revoked would simply stop working but isn't the case in this instance. If I remove the certificate from local certificate stores then it does stop working so confident that it's dependant on this one certificate.
All web searches so far have led to the above commands and making sure the CDP is externally resolvable but I've covered those issues already.
I appreciate and advice
Comment: when I try to run `certutil -setreg chain\ChainCacheResyncFiletime @now`, windows indicates that i'm missing an argument, as if I have to add time on to @now. I've done this, but it still doesn't invalidate the cache.
Answer: Thought it was worth reporting my findings.
Turns out that Windows was revoking the certificates but there was some local caching of the CRL taking place as is intended by design. It seems that even when the certificate is showing as revoked, a cached copy is still used regardless until a specific period has passed, however, from my understanding the two commands I ran in my initial post should have expired this and requested an up-to-date one from the server but doesn't appear to be the case.
I've introduced delta CRLs which have made troubleshooting must quicker and all seems fine now so false alarm ultimately.
Thanks for your advice Greg
Answer: I'm using a simple test app on the client that is configured to use the one certificate so if revoked would simply stop working.
You need to verify the client is checking the CRL with a packet capture to your CRL on tcp/80. That would take five minutes.
If you want to validate that Windows CRL checking is working, you can use the following command on the client:
certutil -f –urlfetch -verify ExportedCertificate.cer

You also need to enable the CAPI2 event log, any CRL check failures will be logged there.
But the way an application uses and validates a certificate is not necessarily the same as how Windows would use and validate a certificate.
Comment: Thanks Greg. Once I'm back in the office I'll check this out and let you know my findings
Comment: In regards to your last paragraph, this is true and would be easier to tell if that was case if the certificate got marked that it's been revoked or now invalid, which I gather it does once updated CRL is used? The fact that it isn't leads me to address that it's simply a case of the certificate still thinking it's valid.
Comment: Ok, so ran the validate to see if Windows CRL checking is working and got the following result:

>Load(CRL) returned The system cannot find the file specified. x80070002 (WIN32: 2)
CertUtil: The system cannot find the file specified.
Comment: Errors seen were due to not running this on an exported crt file. The above command threw me by providing a .cer extension. Anyway, looks like this may be down to a bug in the app as have verified that the certificate is revoked with certutil yet the application still works so investigating this aspect. Thanks for the help.
Comment: I realize that my last update is rubbish as there's nothing wrong with the above command as an exported DER encoded certificate is in .cer format not crt. Apologies for the causing confusion.

Revoked certificate still valid


Why does the D# work in Pink Floyd's Mother guitar solo?

Question: I'm curious why the D# sounds good in the guitar solo of Pink Floyd's Mother.  You can check for example the tab here - the 14p13 is a pull-off from E to D#, but D# isn't in the G major scale even though Mother is in the key of G major.
Comment: Are you sure there isn't a key change in the solo?  Also, D# is the leading tone to E minor (E harmonic minor) and that is the relative minor to G major.  In that context it does work.  I think he is clearly soloing in E minor.
Comment: I believe the solo resolves on a G and that all the rest of the notes other than the D# are in the G major scale for what it's worth.  Not sure that he's soloing in E minor but I don't know...
Answer: Basically, D♯ doesn't mean he's playing the ♯5 in this case. He's playing the ♮7 in E minor.
The solo is played in the key of E minor (which is G major's relative minor), and a common thing to do when in minor is to raise the 7th degree, often for harmonic purposes (it creates a leading tone, because D♯ resolves better to E than D does). You'd never guess that they call it harmonic minor. Another common thing to do is to raise both the 7th and the 6th, making melodic minor.
So, one is soloing in E minor, the relative minor of G major, and one uses the harmonic minor scale to get that D♯ in the solo.
Comment: Indeed. If OP wants a seasonal comparison, here's the sheet music for [What Child Is This?](https://www.8notes.com/scores/1577.asp) in E minor with a lovely D# near the end of each phrase.
Comment: Isn't the nat.7th of Em D nat? (1st para). Did you mean D#? It's probably the way it's phrased - the *normal seventh* note in Em is D# - apart from the Dnat found in E natural minor. That's just dinged an alarm bell. Imagine the scale 'A flat natural minor' Sounds like a contradiction in terms!
Comment: @Tim I see the potential for confusion, so I'd just say *naturalised seventh* or *raised seventh*. The natural seventh would be D♯ (the 7th is D, naturalised up to D♯ even though it has a sharp). I and many others call it that way because major scales include a natural seventh, not a sharp one. Also, the sharp 7th would just be enharmonic to the octave. "A flat natural minor" is weird, but completely standard
Comment: @Tim also, this makes a lot more sense in keys like D♭minor, where C♮ is the natural 7th. I guess another way to look at it is that in natural minor, the 7th degree is flattened, and undoing that takes a natural, not a sharp.
Comment: It's just, maybe only to me, but C maj and several of the 'flat' keys (F, Bb, Eb etc)all have a leading note that is in fact a natural, whereas all the sharp keys (G, D, A etc) have a sharp (not a natural) for their leading note: major and two minors. I understand the leading note is natural (normal) for any key, it's just that some readers may suffer confusion! Merely trying to make things as clear as poss.
Comment: @Tim I see your point. It's much clearer from a strictly intervallic perspective instead of within any particular key. Point is, the D♯ is part of the **E harmonic minor scale**.
Comment: @user45266 - in reality, the natural minor is just that. The 7th note is as it should be. In the harmonic and melodic minors, that 7th note is raised, rather that the other way round. D# is also (your last comment) part of the **E melodic minor scale**.
Comment: @Tim We're on the same page. I believe we may have confused each other :)


How can I search in another language?

Question: I have 2 languages in my site and a search view for property search
and I am using the Search API module.
When I try to search in English (default language), the search is working.
But when I try to search in the another language the search is not working.
I am searching by tittle.
Any suggestions?
Answer: If you are using entity translation for translating the content, you will have to install the Search API Entity Translation module.

Search API Entity Translation module provides multilingual support for the Search API module through integration with Entity Translation.

This module provides a new index type : "multilingual node index", similar to the "default node index" which is provided by default by Search API.
You can use this to create the views and they will honor the language selected from the language switcher.
Comment: thanks a lot for your help @Ajits .
but i want to ask you some thing ,after creating the "multilingual node index" i changed the language to the second language (arabic) and i searched then the result that i got its the same that i want but its in the English not in Arabic its back to the default language !
So ... what should i edit in the search view to make the result shown in Current user's language ?
Comment: @WaseeemAlfarra Are you sure you are creating a new view with the "default multilingual node index" and not "multilingual node index"?
Comment: mmmmm...@Ajits . i created a view with "multilingual node index" not the  "default multilingual node index" !!
Comment: @WaseeemAlfarra I'm sorry! Cancel my previous comment. It should have been - Are you sure you are creating a new view with the "default multilingual node index" and not "default node index"?
Comment: don't worry my friend . am now creating a view with "default multilingual node index" ...
Comment: @WaseeemAlfarra Cool. That should resolve the issue then.
Comment: dude nothing happened still not working with arabic version :(

How can I search in another language?


Verify User Permission on Action Filter or Authroize Filter?

Question: I am developing a website in MVC4. I developed user roles and permissions.
I want to ask where I should check user permission access: in the Custom Action filter, or the Custom Authorization filter?
If user does not have access to the module, then I must show a toaster error message. How do I show this message in an action filter?
Comment: Use Authorization Attribute - http://stackoverflow.com/questions/2504923/how-to-redirect-authorize-to-loginurl-only-when-roles-are-not-used. Let me know if it helps.
Answer: I use to write custom action filter attribute so that on the action call this method is called and i check in it if user role allows him to call this action or not.
You have to write custom action filter attribute same way but you have to write your own business logic in CheckAccessRight method:
<code>public class AuthorizationAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        string actionName = filterContext.ActionDescriptor.ActionName;
        string controllerName = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName;

        if (!CheckAccessRight(actionName, controllerName))
        {
            string redirectUrl = string.Format("?returnUrl={0}", filterContext.HttpContext.Request.Url.PathAndQuery);

            filterContext.HttpContext.Response.Redirect(FormsAuthentication.LoginUrl + redirectUrl, true);
        }
        else
        {
            base.OnActionExecuting(filterContext);
        }
    }

    private bool CheckAccessRight(string Action, string Controller)
    {
        if (HttpContext.Current.Session["userId"] != null)
        {
            string userID = HttpContext.Current.Session["userId"].ToString();
            using (var db = new cloud_clinicEntities())
            {
                assignment objAss = null;
                if (HttpContext.Current.Session["AccountType"].ToString() == "lab")
                {
                    objAss = db.assignments.SingleOrDefault(model => model.userid == userID);
                }
                else
                {
                    objAss = db.assignments.SingleOrDefault(model => model.employeeId == userID);
                }

                String UserRole = objAss.itemname;

                itemchildren objChild = db.itemchildrens.SingleOrDefault(model => model.parent == UserRole && model.child == Controller + " " + Action);

                if (objChild != null)
                {
                    return true;
                }
                else
                {
                    return false;
                }

            }
        }
        else
        {
            return false;
        }
    }
}
</code>
And then use this attribute on the actions like this:
<code>[AuthorizationAttribute]
        public ActionResult MyAction()
        {
        }
</code>
Answer: Here's a good article. You can set your own attributes for several roles like admin.


Cannot start docker after installation on Windows 10 Home edition

Question: I am brand new to docker with windows. I have installed the Docker toolbox version as my laptop cannot run hyper-v. After the installation of Docker Toolbox (OS is Windows 10 Home) I run Docker Quickstart Terminal and in the console, I see this:

Running pre-create checks...
  Error with pre-create check: "This computer doesn't have VT-X/AMD-v enabled. Enabling it in the BIOS is mandatory"
  Looks like something went wrong in step ´Checking if machine default exists´... Press any key to continue...

I have opened my BIOS menu to try to change this but I am not presented with an option to do so as if it's hidden. I've no idea how to get around this and other StackOverflow solutions are not fixing my issue.
My Laptop model is an Acer Aspire E1-531.
Any help on enabling the VT-X/AMD-v would be brilliant thanks
Comment: This is off-topic for Stack Overflow. Anyway that laptop has a six year old Intel Pentium Dual Core B960 processor which doesn't support virtualization: https://ark.intel.com/products/59836/Intel-Pentium-Processor-B960-2M-Cache-2_20-GHz
Comment: Right okay so, in a nutshell, get a new laptop? Should I be able to run a VirtualBox if the processor can't handle virtualization?
Answer: You really don't need a new laptop. If the virtualization is disabled, you may enable it while turning on your system.
From: https://amiduos.com/support/knowledge-base/article/enabling-virtualization-technology-in-hp-systems

Turn ON the System
Repeatedly press Esc key at startup.
Press the F10 key for BIOS Setup.
Press the right arrow key to System Configuration tab, Select Virtualization
Technology and then press the Enter key.
Select Enabled and press the Enter key.
Press the F10 key and select Yes and press the Enter key to save changes and Reboot.
Comment: what about the OS. I have widows 10 home edition. Should this definitely be professional edition?
Comment: Mine is windows 10 home edition as well. I run docker using virtuabox. https://golb.hplar.ch/2019/01/docker-on-windows10-home-scratch.html
Answer: Follow this link to see your laptop support VT-X/AMD-v. If not then you have no other choice other than changing your laptop to run hypervisors.
Comment: Thank you. Unfortunately, a new laptop is on the cards.


Can you perform stochastic learning followed by batch learning in neural networks?

Question: I'm trying to teach myself about neural networks. I've been reading through the "Efficient BackProp" paper that's highly sited and it's brought me this question;
Since stochastic learning converges significantly faster than batch learning, but has a higher level of noise (doesn't converge to the global minimum) should one perform Stochastic learning until the network reaches a platue/minimum then use batch learning to finish the process?
When I say batch learning I mean treat the entire training set as a single batch (compute the sum of the derivatives of the weights for all samples and then update the network).
Answer: Yes you can perform stochastic learning followed by batch learning in neural networks. In fact the very same paper discusses it:

Another method to remove noise is to use "mini-batches", that is, start with
  a small batch size and increase the size as training proceeds. Møller discusses
  one method for doing this [25] and Orr [31] discusses this for linear problems.
  However, deciding the rate at which to increase the batch size and which inputs
  to include in the small batches is as difficult as determining the proper learning
  rate. Effectively the size of the learning rate in stochastic learning corresponds
  to the respective size of the mini batch.

But keep in mind that the paper was written in 1998, when GPUs were not commonly used to train neural networks. With GPUs, it is much cheaper to use mini-batch training than it is on CPUs. (See {1} for one of the first papers underlying the use of GPUs for neural networks.)
FYI: Tradeoff batch size vs. number of iterations to train a neural network

{1} Dave Steinkraus; Patrice Simard; Ian Buck (2005). "Using GPUs for Machine Learning Algorithms". 12th International Conference on Document Analysis and Recognition (ICDAR 2005). pp. 1115–1119. http://doi.ieeecomputersociety.org/10.1109/ICDAR.2005.251


How do you get the Default Users folder (e.g. c:\users\Default)

Question: I've looked at the Environment.GetFolderPath method and the System.Environment.SpecialFolder enum but I couldn't see anything that returns the path of the Default Users folder.
Can someone please tell me how to get the Default Users folder (or even better the Default Users AppData Local folder path e.g. c:\users\Default\AppData\Local) programmatically as I need to copy some files into this folder?
Thank you
Answer: There are lots of articles on the web that describe how to change the Default User Profile path:
http://support.microsoft.com/kb/214636
http://www.nextofwindows.com/how-to-change-user-profile-default-location-in-windows-7/
They all say the current Default Profile Path is stored in the following registry location:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList

e.g.
%SystemDrive%\Users\Default
And I found this page to get the System Drive:
How to get current windows directory e.g. C:\ in C#
Path.GetPathRoot(Environment.SystemDirectory)

So I'm going to use that. Thanks for your help.
UPDATE
I've just tried the following code and it returns C:\Users\Default. So there is no need to replace the %SystemDrive% text stored in the registry key. It replaces it automatically.
using (RegistryKey profileListKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList"))
{
    string defaultPath = profileListKey.GetValue("Default").ToString();
}

Answer: Snippet from LINQPad (Language: C# Program) that outputs 'C:\Users\Default\Desktop':
void Main()
{
    GetFolderPath(Environment.SpecialFolder.Desktop).Dump();
}

// Define other methods and classes here
[DllImport("shfolder.dll", CharSet=CharSet.Auto)]
internal static extern int SHGetFolderPath(IntPtr hwndOwner, int nFolder, int hToken, int dwFlags, StringBuilder lpszPath);

public static string GetFolderPath(Environment.SpecialFolder folder)
{
    if (!Enum.IsDefined(typeof(Environment.SpecialFolder), folder))
    {
        throw new Exception("Crap");
    }
    StringBuilder lpszPath = new StringBuilder(260);

    SHGetFolderPath(IntPtr.Zero, (int) folder, -1, 0, lpszPath);
    string path = lpszPath.ToString();
    new FileIOPermission(FileIOPermissionAccess.PathDiscovery, path).Demand();
    return path;
}

Edit: I had the following imports in LINQPad
System.Runtime.InteropServices
System.Globalization
System.Security.Permissions

I used reflector to look at Environment.GetFolderPath and then took a look at SHGetFolderPath that specifies by passing -1 as hToken you get Default User instead.
Comment: SHGetFolderPath -- "Assigning the hToken parameter a value of -1 indicates the Default User. This enables clients of SHGetFolderPath to find folder locations (such as the Desktop folder) for the Default User. The Default User user profile is duplicated when any new user account is created, and includes special folders such as My Documents and Desktop. Any items added to the Default User folder also appear in any new user account." -- http://msdn.microsoft.com/en-us/library/windows/desktop/bb762181(v=vs.85).aspx
Comment: I wouldn't recommend using p/Invoke nowadays.
Comment: @JoanComasFdz so how would you recommend getting it instead?
Comment: @JoanComasFdz, there's nothign wrong with using pinvoke, especially if you're accessing functionality not exposed by the .NET framework (it's unnecessary for functionality that is exposed).
Answer: You can't because the access to that folder is denied, this folder is used only by the Microsoft. I sure Environment or any other class will not provide you such functionality. This can only be done with some sort of hacking maybe?
Comment: Permissions are based on the context the application is running in. An admin can certainly access the folder.


Anomaly detection vs Outlier detection vs Extreme event detection

Question: Are anomaly detection, outlier detection and extreme event detection same or different?
If different, what are the differences among them?
Answer: This is a bit of a philosophical question. I did write a blogpost on a similar theme a while ago. I will reproduce here much of what I wrote:
Outlier detection is when we find unusual records in datasets, and those datasets could be multivariate or time series. So, what is anomaly detection, then?
From a language point of view, an outlier and an anomaly are the same thing, it's just one word is derived from old English and the other is Latin and Greek. And although we are based in North America, we still have a taste for the simpler old English words.
From an academic point of view, an outlier is a genuine data point that is just far from the norm (think: Dionne quintuplets) whereas an anomaly is a data point generated by a different process (think: multiple births resulting from fertility treatments). But even academics use the terms interchangeably, because in practice the impact of an anomaly vs. an outlier is often the same.
But what is anomaly detection for business?
If academics use the terms interchangeably, does the commercial world also? Then we get into common business usage. Anomaly detection software typically refers to software that analyzes streams of time series data in near real time. The idea is to find events that require action. A classic application is the monitoring of IT logs. Let's say your company has several machines and processes running continuously. If one of your machines goes offline or one of the processes fails, that's when you want an alert to go out. In fact, if one of your machines slows right down or the process is taking much longer than usual, that's also when you want an alert to go out. The need for alerts has always been there but machine learning models can now increase the sophistication of the alerting, so that you are not waking up your support people in the middle of the night for a false alarm and you are only showing them the most important issues.
Finally, I would say an extreme event is really the same thing as a time series anomaly. So, an example of this is when I wrote another blogpost about finding extreme weather events in 80 years of Canadian weather data.
Comment: I don't understand why the answer got into business topics when that was not the question at hand.


How can I install a library in script as well as importing it?

Question: I want to test in script whether all the necessary libraries are installed or not, if not, install it. Here is my code:
libs = ['lib1','lib2','lib3']
def import_m(name):
    try:
        import name
    except:
        pip.main(['install',name])
        import name                    #look at this line
for i in libs:
        import_m(i)
print("Done importing %s." % i)

But when running it raise an exection:
ImportError: No module named name

The line mentioned with this exection are indicated by the comment.
How can I fix it?
Comment: start with checking the return value of pip.main
Comment: @tkhurana96 What do you mean by **return value** ?
Comment: have a look at [this link](https://stackoverflow.com/questions/45799042/installing-packages-from-a-list-using-pip)
Comment: @tkhurana96 It does return 0.
Comment: Possible duplicate of [Installing python module within code](https://stackoverflow.com/questions/12332975/installing-python-module-within-code).
The error says `No module named name`, even though you're actually installing `lib1`, `lib2` and `lib3`. `import name` literally imports `name`, not the value of the `name` variable.
Answer: you are going to need

importlib

for this functionality.
A similar question is answered here
For your requirement you can first try to import a library using

globals()['module_name'] = importlib.import_module('module_name')

if it throws an exception you just need to install it and run the above code again. Don't try to run import module_name again. If module_name is successfully stored in globals you are all good to go.


What legal gotchas should a sysadmin be familiar with?

Question: What legal issues should you research as a sysadmin to avoid you, or your employer, being accused of negligence, or of violating privacy, etc?
While laws vary from country to country and state to state, it could still be enlightening if you have an example of a law which you, or someone you know, has broken without realizing it.
Comment: btw, wish i had a good synonym for "gotcha" - i hate that word.
Answer: It largely varies on a few things like what industry you're in (the following applies to the USA only)...

Health Care: HIPAA
Education:  FERPA
If your company is traded with the SEC: Sarbanes Oxley
If your company does credit card
transactions - PCI DSS

A lot of the smaller jobs I've worked have been pretty bad about PCI DSS storing CC info in plaintext, publicly accessible database server... basics that were just neglected.
Comment: One of the challenges, particularly for small businesses, is that some of the regulations mentioned (HIPAA for instance) can be confusing or ambiguous. The theory is that the regs are written not to force companies to lock-in to particular solutions, but taking that wiggle room for granted could be problematic.
Comment: Can we add federal rules of discovery (http://www.law.com/jsp/legaltechnology/pubArticleLT.jsp?id=1202429657841&Guidelines_to_Craft_EMail_Retention_Policies) in the US (retention requirements).
Also, breach notification laws exist in many states and a federal healthcare information breach notice standard was passed as part of ARRA and is currently being drafted (http://www.dwt.com/LearningCenter/Advisories?find=79311)
Comment: +1 @Milner, attempts in these regulations to be 'technology agnostic' leave little clear direction, which is both good and bad.  The best most of us can do is have a clear Policy + Procedures that explains how you address the gray areas -- and then sticking to it (or revising, then sticking to it).  Having to explain why you didn't follow your own SOP is a bad situation.
Answer: The following applies to the USA only;
CIPA: Children's Internet Protection Act
Specially if you're employed by an state or federal educational entity:
http://www.fcc.gov/cgb/consumerfacts/cipa.html
FOIA: Freedom of Information Act
Again if you're employed by a government entity: http://www.fcc.gov/foia/
FERPA: Family Educational Rights and Privacy Act
Education: http://www.ed.gov/policy/gen/guid/fpco/ferpa/index.html
Comment: Child Porn - your business can go under because your servers were in a datacenter NEXT TO some servers that had child porn on them (in the US).  You really can't be too paranoid about this.
Answer: Be aware of the legal side of network analysis and intrusion detection. Some places, an unauthorized use of nmap can be considered a crime, as can trying to break into systems for security (non-malicious) purposes.
Be aware of software licensing issues, both for end users (if you deal with them) and for your servers and other sysadmins. Know the possible ramifications if you choose to run pirated software on a business server.
Be aware of privacy laws for your place of business, on the local, state, and federal law. Know what info you are and aren't allowed to store. Also know what info you are and aren't allowed to look at, both in legal terms and as laid down by your company guidelines.
On the flip side, be aware of information retention laws for your place of business. Know what info you're required to keep, how long you need to keep it, and who you have to divulge it to when requested. Be able to draw the line between privacy and complying with regulations (and know when to stand up for one or the other).
Comment: There's more to licensing than just piracy. Lots of software we take for granted at home that has liberal personal-use licenses are much more restrictively licensed for commercial use. It's not safe to assume it's freeware everywhere.
Answer: I'm in the UK, and I'd say the most important laws to an average ecommerce business would be:

The Data Protection Act
Distance Selling Regulations and Trade Descriptions Act
Certain parts of The Companies Act - for example you have to have your registered company number and address on a business website even if you don't sell anything. I've seen that one broken many times.
PCI Compliance (ok, not a law but important)
Answer: This question can actually only be answered if you tell us where you are located.
Personally I consider the SysAdmin to be the person that is in charge of each and every bit of data, thus carries the largest risk when data is lost/exposed/abused (Even if you won't face legal consequences your boss will come to you and you will have to explain why on earth the data could get out of your company).
I personally make sure that:

In case it's needed I can access each piece of information (everything, after all I'm the standing on the wrong side of the fan when shit hits it)
I tell this to my boss
I tell my boss that I won't access anything without permission
I tell my boss that I will ask for another party to watch me and the requestor if I don't feel comfortable with the data access request
I want all of the above signed and sealed in a written manner

Other things I make sure:

Hear everything
See everything
Talk about nothing

These points aren't about snooping around in files or anything like that, it's just about the regular chatting with colleagues and co-workers and trying to fit together the different pieces.
Talk about nothing nothing means to not participate in the chatting from a certain point, people come to me regularly with requests about lost passwords, files to be restored or other stuff. That could lead back to ceertain opinions about otherwise hard working people, I don't want that.

Tell everyone about the stuff my boss and I agreed over

This can be in terms of talking from person to person, company mails or posters with friendly reminders that there's a party in the company that could access all data.
These aren't exactly examples of laws colleagues or I stumbled over. But that is the part where "Talk about nothing" comes to play. Sorry to disappoint you with examples.
Answer: Your Data Protection legislation.
Your employer's AUP - know it inside out - it applies to you too!
Answer: There are various state legislationss concerning PII (Personally Identifiable Information) in the event of a data breach.  California's 1386 requires that everybody who as affected by the data breach (compromise of their information) must be notified.  Many other states have similar provisions.
Also as a clarification on PCI-DSS, that is not a strictly legal requirement, the card brands (MasterCard, Visa, Discover, AmEx) require their merchant banks to require the vendors to adhere to PCI-DSS.  If you violate PCI, you won't prosecuted legally, however you can be fined thousands of dollars a day (or more) by your merchant bank while you are in violation.  If you don't come into compliance, you will eventually lose your ability to do credit card transactions, which would be a kiss of death for most online retailers.
Answer: PCI DSS for customers who take credit cards, and the chance that every time you enable logging that you might be required to produce those logs in the future. Sometimes it is better not to have recorded anything.
Answer: E-discovery is a big "gotcha."  These are the requirements in the US to preserve electronic information in the event of a lawsuit, and to make it available to the other party.
The sysadmin should spend some time with the company's lawyers BEFORE the first time the company is sued so that you have a plan in place to comply with these requirements if you ever have to.  Failure to preserve all the necessary electronic records (and in the right way) immediately upon a lawsuit coming up and hurt the company tremendously (including losing a lawsuit that might not otherwise have been lost).
Answer: In a policing or crown council environment, you need to be careful when handling digital evidence. The last thing you want is to be required to testify in court when all you did was help convert some sort of media from one format to another.


Python: requests.get("https://pt.stackoverflow.com/") nunca retorna nada

Question: Ao tentar usar um requests.get(url) não obtenho nenhuma resposta do servidor, porém, adicionando o kwarg
 timeout=1 por exemplo, obtenho a resposta depois de 1 segundo...
exemplo:\
import requests

url = "https://google.com/"
r = requests.get(url, timeout=1)
print(r.elapsed)

obtenho
0:00:01.211611.
se usar
r = requests.get(url, timeout=5)

obtenho:
0:00:05.223328.
Pelo que percebi a função só me retorna algo quando chega no timeout.
Talvez esteja fazendo algo muito errado, mas acho que eu deveria receber uma resposta do get assim que o servidor responder e não após o timeout...
Obti o Seguinte resultado imprimindo o r.dict (retirei o content)
_content_consumed True
_next None
status_code 200
headers {'Date': 'Mon, 29 Jun 2020 20:40:42 GMT', 'Expires': '-1', 'Cache-Control': 'private, max-age=0', 'Content-Type': 'text/html; charset=ISO-8859-1', 'P3P': 'CP="This is not a P3P policy! See g.co/p3phelp for more info."', 'Content-Encoding': 'gzip', 'Server': 'gws', 'Content-Length': '5387', 'X-XSS-Protection': '0', 'X-Frame-Options': 'SAMEORIGIN', 'Set-Cookie': '1P_JAR=2020-06-29-20; expires=Wed, 29-Jul-2020 20:40:42 GMT; path=/; domain=.google.com; Secure, NID=204=AuH0fx2X4m3kT6AeVtg0YMDEGr6uehL7Kt8WyzO7cmIlNDq_qnh4QXcUybI9aPOMAuC8_PuHsidpBN--vMfU1jJRreb2lM340XOSv2-CZAkK1qfXbrSSii9cRG-uX1caNB3HlnL4QDjErvgcYPtedlatyvLEaLALJ4Lj0aigT7c; expires=Tue, 29-Dec-2020 20:40:42 GMT; path=/; domain=.google.com; HttpOnly'}
raw <urllib3.response.HTTPResponse object at 0x7f1cb7b5f0b8>
url http://www.google.com/
encoding ISO-8859-1
history []
reason OK
cookies <RequestsCookieJar[<Cookie 1P_JAR=2020-06-29-20 for .google.com/>, <Cookie NID=204=AuH0fx2X4m3kT6AeVtg0YMDEGr6uehL7Kt8WyzO7cmIlNDq_qnh4QXcUybI9aPOMAuC8_PuHsidpBN--vMfU1jJRreb2lM340XOSv2-CZAkK1qfXbrSSii9cRG-uX1caNB3HlnL4QDjErvgcYPtedlatyvLEaLALJ4Lj0aigT7c for .google.com/>]>
elapsed 0:00:05.147145
request <PreparedRequest [GET]>
connection <requests.adapters.HTTPAdapter object at 0x7f1cb6d8e1d0>
[Finished in 6.0s]

Percebi também que em páginas de html mais simples como:
http://www.brainjar.com/java/host/test.html
o problema não acontece e eu recebo a resposta quase imediatamente.
0:00:00.212320.
Comment: Veja esse se [exemplo](https://repl.it/@RonaldoVasques/RequestTimeout) o ajuda.
Answer: O facto de retornar apenas no fim do timeout leva-me a crer que o request esteja a falhar.
Corri o código praticamente como está e funcionou.
É possível que a tua firewall esteja a bloquear o executável python? Experimenta imprimir o status code.
url = "https://google.com/"
r = requests.get(url, timeout=10)
print(r.elapsed)
print(r.status_code)

0:00:00.180191
200

Comment: Adicionei novas informações no problema
Comment: E sim, eu recebo a response 200
Answer: Acabei de descobrir que o problema ocorre quando faço o request usando o ipv6.
Ao usar o ipv4, o problema não ocorre.
Esse código retirado de: https://stackoverflow.com/questions/33046733/force-requests-to-use-ipv4-ipv6 Resposta do usuario:Jeff Kaufman, resolveu meu problema.
# Monkey patch to force IPv4, since FB seems to hang on IPv6
import socket
old_getaddrinfo = socket.getaddrinfo
def new_getaddrinfo(*args, **kwargs):
    responses = old_getaddrinfo(*args, **kwargs)
    return [response
            for response in responses
            if response[0] == socket.AF_INET]
socket.getaddrinfo = new_getaddrinfo

Isso força o request com ipv4


iPad UIWebview showing all contents in top-left corner (1 fourth of screen)?

Question: In my universal app when i click button am opening a new view controller with 2 xib (one is for iPad and another is for iPhone  .Auto layout enabled for both xib's).
Am trying to load a html string as url in iPhone and iPad. In iPhone its coming correctly (Full screen) and in iPad its coming only top-left corner .
This is my code
- (void)viewDidLoad
{
    [super viewDidLoad];
    NSString *str=@"#aphrsindia Tweets";

       [_webview loadHTMLString:str baseURL:nil];

       [self initializeNavBarItems];

    // Do any additional setup after loading the view from its nib.
}

iPad screenshot link
Please help me ..
Comment: May sound stupid, but in your xib the webview is spanned over the whole UI? Otherwise try to disable auto-layout and set the height&width manually, to see if something changes.

Edit: maybe provide a screenshot of your iPad's xib
Comment: i have done that before no change at all
Answer: Problem: This twitter url adding a iframe with width:520px and thats the reason .
Solution:
I have created a html file with style

and  in body i have added rest of the url string .
Comment: I guess Twitter sees the browser as an iPhone and giving out iPhone layout. Try to set a custom User Agent suitable for iPad and see if that works out.
Answer: Make sure your content in html is correctly formated. And when you navigate on this view controller, check that you load correct .xib. Like :
YourViewController *yourController;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
    yourController = [[YourViewController alloc] initWithNibName:@"YourViewController_iPad" bundle:nil];
else
    yourController = [[YourViewController alloc] initWithNibName:@"YourViewController_iPhone" bundle:nil];
[self.navigationController pushViewController:yourController animated:TRUE];

Comment: i figure it out the problem, its like twitter is setting width 520px for iframe.but i dont know the solution !!! .ryt now am trying dynamic css injection
Comment: Great... @johnmikemike


Excel VBA: Moving Columns between Workbooks

Question: Currently I have spreadsheets coming in that are formatted incorrectly.  Our client sent out to his suppliers an old spreadsheet where columns are laid out differently than what they are currently setup as.  Normally we would tell them to correct it, but some of these spreadsheets have over 220k rows and 33 columns.  They're updating it for the future, but asking them to have their clients redo their tables is a no-go.  I've written a script that will copy a column, and place it into the corresponding static column in another workbook.  This works okay but I feel there is more that could be done.
Name of open workbook copying from varies.
Name of workbook copied to: C:\User\(Name)\UCOR\Catalogs\PSX-Toolset v1.503-EN.xls
What I would like is help writing a macro that will do the following from open workbook:
1.) Select an entire column minus Row 1 to the first blank row. - This goes from B to AH
2.) Paste that column into PSX-Toolset workbook, worksheet name "Item Data" - Static Assigned Columns
3.) Perform a Save As on PSX-Toolset as (Catalog-PSX-<Workbook Copied From>.xls)
Lastly, I'd like to know if it's possible to do the above, but mapping heading cells.  Unfortunately the cell names are not identical.
Comment: Yes, it's possible. Have you tried anything yourself? For example, recording a macro and sanitizing the resulting code?
Answer: How I learned most of my vba is through 'record macro'. You start recording, do what you want to do yourself, stop recording and then look at the generated code.
Usually you can improve the code by eliminating a lot of redundant lines, but it should at least expose all the commands you need to complete your goal.
Answer: Untested:
Sub MapAndCopyColumns()

Dim i As Integer, rng As Range
Dim shtSrc As Worksheet, wbDest As Workbook
Dim shtDest As Worksheet
Dim iNew

    Set shtSrc = ActiveSheet
    Set wbDest = Workbooks.Open("C:\User\(Name)\UCOR\Catalogs\PSX-Toolset v1.503-EN.xls")
    Set shtDest = wbDest.Sheets("Item Data")

    For i = 2 To 34
        Set rng = shtSrc.Cells(2, i)
        If rng.Value <> "" Then
            If rng.Offset(1, 0).Value <> "" Then
                Set rng = Range(rng, rng.End(xlDown))
            End If
            'map old position >> new position
            '  mapping table has 2 columns of numbers: "old" and "new"
            iNew = Application.VLookup(i, _
                ThisWorkbook.Sheets("Mapping").Range("A2:B40"), 2, False)
            If Not IsError(iNew) Then
                'copy if the column has an entry in the mapping table
                rng.Copy shtDest.Cells(2, iNew)
            End If
        End If
    Next i

    wbDest.SaveAs "C:\wheretosaveto\Catalog-PSX-" & shtSrc.Parent.Name

End Sub


Dynamically inject class in spring

Question: I want to inject a class dynamically based on the argument of my method call
I`ve try use AspectJ and different annotations of Spring
I need a solution like that:
<code>@Component
class MyUseCase(private val spi: MySpiPort) {

    fun myAction() {
        spi.doSomething("myId")
    }
}

interface Injectable

interface MySpiPort : Injectable {
    fun doSomething(id: String)
}

class MyProxyClass {

    //will intercept all Injectable
    fun resolver(id: String): MySpiPort {
        if(id == "myId"){
            //inject MyFirstImpl
        }else{
            //inject MySecondImpl
        }
        TODO("not implemented")
    }

}

@Component
class MyFirstImpl : MySpiPort {
    override fun doSomething(id: String) {
        TODO("not implemented")
    }
}

@Component
class MySecondImpl : MySpiPort {
    override fun doSomething(id: String) {
        TODO("not implemented")
    }
}

</code>
I expect inject just a common interface of the implementations, I don't want inject in MyUseCase Class a FactoryBean Class or something like that.
Comment: I think you need to implement strategy pattern. This link will help you [Strategy pattern with spring beans](https://stackoverflow.com/questions/17629761/strategy-pattern-with-spring-beans)
Answer: You can leverage Spring's Conditional injection. Please refer https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#beans-java-conditional
Comment: how do i get argument passed in runtime in my method to use in @Conditional
Comment: If you have a finite set of values, you can very well use the Conditionals. Otherwise not

Dynamically inject class in spring


Absolute convergence of $\sum_{n\geq 0} (f_n(x)+g_n(x))$ when $\sum_{n\geq 0} g_n(x)$ or $\sum_{n\geq 0} f_n(x)$ or both converge only conditionally

Question: Considering two function series i know that
$$\sum_{n\geq 0} f_n(x) \mathrm{\,\, converges \,\,\, absolutely \,\,\, and } \sum_{n\geq 0} g_n(x) \mathrm{\,\, converges \,\,\, absolutely} \implies \sum_{n\geq 0} (f_n(x)+g_n(x)) \mathrm{\,\, converges \,\,\, absolutely}$$
But can anything else be said about the absolute convergence of $\sum_{n\geq 0} (f_n(x)+g_n(x))$ if $\sum_{n\geq 0} g_n(x)$ or $\sum_{n\geq 0} f_n(x)$ or both converge only conditionally (that is, converge but not absolutely)?
Can $\sum_{n\geq 0} (f_n(x)+g_n(x))$ converge absolutely in that case?
Comment: It may if both converge conditionally since e.g. you could take $f_n=-g_n$. If one converges abs and the other cond (and not abs) then the sum converges cond (and not abs).
Answer: It is possible to analyze possible behaviors using constants (instead of functions).
So given $f_n$ and $g_n$ such that both $\sum f_n$ and $\sum g_n$ converge conditionally, you are asking if it is possible that $\sum (f_n + g_n)$ converges absolutely.
The answer is yes. For instance, consider
$$ \sum_{n \geq 1} \frac{(-1)^n}{n} + \sum_{n \geq 1} \frac{(-1)^{n+1}}{n}.$$
Each series converges conditionally, but their termwise sum converges (trivially) absolutely to $0$.
More generally, if $f_n = -g_n$, then this will occur. If $f_n = -g_n + h_n$, where $\sum g_n$ is conditionally convergent and $\sum h_n$ is absolutely convergent, then $\sum f_n$ is conditionally convergent and you will also have $\sum (f_n + g_n)$ is absolutely convergent.


A question on notation related to covariant derivative

Question: Let us denote a 1 form on manifold M with $\eta$ which in a chart looks like $\eta=\eta_{\mu}dx^{\mu}$ where $\eta_{\mu}$ are smooth functions on M. Now given the coordinate vector fields $\frac{\partial}{\partial x^{\mu}}$, $$\nabla_{\nu}\eta\equiv\nabla_{\frac{\partial}{\partial x^{\nu}}}\eta$$ is a (0,1) tensor field, i.e, another 1 form. so, it makes sense to talk about $(\nabla_{\nu}\eta)_{\mu}$ and I can see that (after acting $\nabla_{\nu}\eta$ on a coordinate vector field) the following holds
$$(\nabla_{\nu}\eta)_{\mu}=\nabla_{\nu}\eta_{\mu}-\Gamma^{\rho}_{\mu\nu}\eta_{\rho}.$$ Note that since $\eta_{\mu}$ are smooth functions on M, $\nabla_{\nu}\eta_{\mu}=\eta_{\mu,\nu}$ are ordinary partial derivatives of $\eta_{\mu}$. The above equation can be rewritten as: $$\eta_{\mu;\nu}=\eta_{\mu,\nu}-\Gamma^{\rho}_{\mu\nu}\eta_{\rho}.$$ My question is why in the physics/general relativity literature then  $\nabla_{\nu}\eta_{\mu}$ denotes the covariant derivative of the 1 form $\eta$ along the coordinate vector field $\frac{\partial}{\partial x^{\nu}}$? The covariant derivative of $\eta$ along $\frac{\partial}{\partial x^{\nu}}$, denoted by $\nabla_{\nu}\eta$ is a (0,1) tensor field whose components are denoted by $(\nabla_{\nu}\eta)_{\mu}$ (the left hand side of the second equation above) where as $\nabla_{\nu}\eta_{\mu}$ are mere partial derivatives of the component functions $\eta_{\mu}$.
Answer: This use of notation is not limited to physics - it's very popular amongst differential geometers too. The reason it's useful to interpret things this way is because it makes it easier to write down coordinate-invariant expressions (which are those that are geometrically or physically meaningful) while retaining the power of index notation to clearly express complicated tensor products and contractions. We're much more likely to care about $(\nabla_\nu \eta)_\mu$ than we are $\partial_\nu \eta_\mu$, so why not make the former easier to write down?
If you don't adopt this convention, then you either have to clutter your calculations with extra parentheses as in your $(\nabla_\nu \eta)_\mu$ (becomes extremely unwieldy once you start taking iterated derivatives), or forget about covariance entirely and work with partial derivatives and Christoffel symbols (ugly!).
Comment: So, is $(\nabla_{\nu}(\nabla\eta))_{\rho\mu}$  denoted by $\nabla_{\nu}\nabla_{\mu}\eta_{\rho}$? The problem, as you can see, is   that it's hard to realize what your final object is from this notation. From $(\nabla_{\nu}(\nabla\eta))_{\rho\mu}$ one can easily detect that the final object is a (0,2) tensor field while the later is less informative.
Comment: @user78032: In abstract index notation, $\nabla_\nu \nabla_\mu \eta_\rho$ denotes the 3-tensor $(\nabla \nabla \eta)_{\nu \mu \rho}.$ If you're interpreting $\nabla_\nu(\nabla \eta)_{\rho \mu}$ as a $2$-tensor (so $\nu$ is fixed) then this is not a coordinate-invariant expression (since $x^\nu$ is some distinguished coordinate), so it's not going to be well-adapted to abstract index notation.
Comment: The idiomatic way to do this would be to make the coordinate vector field $X = \partial_\nu$ an object in its own right, and write $X^\alpha \nabla_\alpha \nabla_\mu \eta_\rho$, which is clearly a 2-tensor.
Comment: OK, thanks, I get it, so $\nabla_{\nu}\nabla_{\mu}\eta_{\rho}$ is the (0,3) tensor field whose components are $(\nabla\nabla\eta)_{\nu\mu\rho}$.
Comment: Yeah, that's right.
Comment: Sorry for the last question, I guess $\nabla_{\nu}\eta_{\mu}$ is not the same as $(\nabla\eta)_{\nu\mu}$ (I'm trying to match the indices as in the iterated case). Because, as we have seen already that $\nabla_{\nu}\eta_{\mu}$ denotes $(\nabla_{\nu}\eta)_{\mu}$!
Comment: @user78032: These are the same, since by definition we have  $(\nabla \eta)(\partial_\nu) = \nabla_{\partial_\nu} \eta$. Indeed, $(\nabla \eta)_{\nu\mu}$ is the form that is obviously invariant.


Greek characters in text file on Isolated storage

Question: The problem is coming when i read a file from the Isolated storage using StreamReader.
The Greek characters are appears as ??????
Any ideas?
Thanks
Comment: Can you edit and add the code you are using to read/write this text to isolated storage?
Comment: The font you're displaying the characters with doesn't have any greek character glyphs.  [Edit] and add info about how you are displaying the characters (textblock?  what font?  etc)
Comment: its a TextBlock and the Greek its OK on it
Comment: :| you mean if you don't serialize/deserialize it shows the greek text correctly?  Can you [edit] and add the code you are using to read/write this text to isolated storage?
Comment: I mean when i'm writing something to the textblock it shows correct the Greek. When i'm reading the file, the greek letters are ???????
Answer: I saved the file in Unicode format and its ok now