How do I multiply a Pandas dataframe by a multiplier from a dict?

Question: Starting from a dataframe like the below (simplified example of my real case):
import pandas as pd
df = pd.DataFrame({
    'a': [1.0, 1.1, 1.0, 4.2, 5.1],
    'b': [5.0, 4.2, 3.1, 3.2, 4.1],
    'c': [3.9, 2.0, 4.2, 3.8, 6.7],
    'd': [3.1, 2.1, 1.2, 1.0, 1.0]
})

And then taking a dictionary containing some multipliers I want to multiply certain columns in the dataframe by:
dict = {
  "b": 0.01,
  "d": 0.001
}

i.e. I want to check if each column in the dataframe is in my dictionary, and if it does exist as a key, then multiply that column of the dataframe by the value in the dictionary. In this example, I would want to multiply column 'b' by 0.01 and column 'd' by 0.001. I would end up with:
    'a': [1.0, 1.1, 1.0, 4.2, 5.1],
    'b': [0.05, 0.042, 0.031, 0.032, 0.041],
    'c': [3.9, 2.0, 4.2, 3.8, 6.7],
    'd': [0.0031, 0.0021, 0.0012, 0.001, 0.001]

In my real example, the dataframe is a cleaned-up set of data read in from Excel, and the dictionary of multipliers is read in from a config file, to allow users to specify which columns need converting from whatever is in Excel to the desired/expected units of measure (e.g. converting 'g/h' in the raw data to 'kg/h' in the dataframe).
What are some good, clear ways of achieving this intent, even if I have to restructure the implementation a bit?
Answer: Try:
df[list(dct)] *= dct.values()

print(df)

Prints:
     a      b    c       d
0  1.0  0.050  3.9  0.0031
1  1.1  0.042  2.0  0.0021
2  1.0  0.031  4.2  0.0012
3  4.2  0.032  3.8  0.0010
4  5.1  0.041  6.7  0.0010

If in dct are keys not in dataframe:
tmp = {k: dct[k] for k in dct.keys() & df.columns}

df[list(tmp)] *= tmp.values()

Comment: This is much cleaner than my answer - use this!
Comment: @RobBaker `a *= b` is shorthand for the `a = a * b`
Answer: Use this:
import pandas as pd
df = pd.DataFrame({
    'a': [1.0, 1.1, 1.0, 4.2, 5.1],
    'b': [5.0, 4.2, 3.1, 3.2, 4.1],
    'c': [3.9, 2.0, 4.2, 3.8, 6.7],
    'd': [3.1, 2.1, 1.2, 1.0, 1.0]
})

dic = {
    'b': 0.01,
    'd': 0.001
}

dfKeyList = [key for key in df.keys()]

for key, value in dic.items():
    if key in dfKeyList:
        df[key] = df[key] * value

print(df)

Answer: cols = list(dict.keys())
vals = list(dict.values())
df[cols] = df[cols] * vals

Answer: Iterate over each element in your dataframe, then multiply when the critia is met.
for x in df:
    if x in dict:
        df[x] *= dict[x]

Answer: Let's create a new dictionary with default multiplier of all columns as 1 and update the created dictionary with your existing multiplier dictionary
d = dict({col: 1 for col in df.columns},
         **{k:v for k, v in d.items() if k in df.columns})
# If you are sure keys in d exist in df.columns, use following instead
d = dict({col: 1 for col in df.columns}, **d)

out = df.mul(pd.DataFrame([d]).values, axis=1)

print(out)

     a      b    c       d
0  1.0  0.050  3.9  0.0031
1  1.1  0.042  2.0  0.0021
2  1.0  0.031  4.2  0.0012
3  4.2  0.032  3.8  0.0010
4  5.1  0.041  6.7  0.0010

Answer: What is about a classic for loop?
for col_name, value in dct.items():
    if col_name in df.columns:
        df[col_name] *= value

Don't name your variables using python object names like dict!
Comment: This works, thanks - but the higher-rated answer is even more succinct.
Obviously my actual code does not have 'dict' as a variable name - but it seems the done thing on StackExchange is to use 'dct' for a generic dictionary, so I'll remember that for next time.


Study design: equal/unequal sample sizes

Question: I have recently sent a paper on aflatoxin levels in nuts for publishing. The aim of the study was to analyze a total amount of 106 nuts. The nuts were divided into four categories: walnuts (n=33), hazelnuts (n=19), pistacchio (n=29) and almonds (n=25).
The reviewer seems not to have a broad knowledge on study design and statistical analysis, which can be seen in his critique: "In my opinion, every sample should be equal in size, in order to improve the quality of the research."
I totally disagree with his opinion, mostly because following statistical analysis did not require the same sample size. This was a cohort epidemiological study, not a experimental one.
Can somebody back me up with references or explain in details why do i don't need equal sample sizes to do a high quality research?
Or in other words:
Would the research be better if i compared 4 categories with the same sample size (eg. walnuts=23, hazelnuts=23, almonds=23, pistacchio=23), compared to my sample sizes?
Comment: You don't need equal sample sizes but having equal sample sizes can increase the power of your tests.
Comment: Was the result 'significant'? Or are you trying to assert the null here?
Comment: I've posted a more detailed answer down there with the results :)
Answer: I feel that your observational study design argument is strong. There is no way to trim all the group down to 19; that would be silly.
Even or uneven group assignment is not the main issue, but rather your samples are large enough to detect a meaningful difference in a group as small as 19. This is also known as the statistical "power" of the sample. If you attached a power calculation, and support with an argument that even your smallest group can help detecting a meaningful difference, then there shouldn't be a point to criticize the uneven distribution.
The reviewer is half-correct. This is perhaps due to some rules of thumb gone wrong. Given all criteria being the same, even groups have higher power than uneven groups. If 15-15 have 80% power to detect a difference of, say, 5 mmol/dl of toxin. Then a 10-20 splitting will become underpowered (e.g. lower than 80%), becoming less likely to detect the same difference if such difference does exist.
Comment: My thoughts exactly, @Penguin_Knight.
Comment: To be more precise: The maximum level of aflatoxins in a nut is  4ug/kg. I am interested how many nuts have aflatoxins above this critical value. Luckily only 9 nuts have had >4ug/kg aflatoxins (see the attachment Table 1: Almonds=5, Hazelnuts=3, Pistacchio=1, Walnuts=0).
A chi square test was performed to see if there is a statistically significant difference between the nuts (p=0,0225).
Due to the 0 walnuts and small sample size of the >4 ug/kg nuts (5,3,1,0), I chose not do a pairwise comparison, but just to list the mean, s.d., etc.
Here's the data:
http://www.filedropper.com/nutdata


Simulated annealing and k-means

Question: One of my problems https://stackoverflow.com/questions/7783933/clustering-data-outputs-irregular-plot-graph  suffers from the curse of dimensionality, which also makes it infeasible for exhaustive search or analytical methods, metaheuristics for combinatorial problems like simulated annealing might help in my endeavour how would I combine simulated annealing with k-means in my example problem?
Sorry for the very short question im still trying to get my head around simulated annealing and how it could possibly help.
Other questions that could help is the comparison of simulated annealing to pca, vds or how it can be used in conjunction. (sorry abit late for me) Edits are welcome if you undertsand my lack of knowledge and what I might be trying to get at.
Answer: It sounds like you're carrying out a cluster analysis on a dataset that has a pretty large number of variables; having difficulty obtaining good results because of the large number of variables (the curse of dimensionality, as you mention); and you're considering using an optimization technique such as simulated annealing to carry out a search through your variables to discover whether you might be able to use just a subset - is that right?
If so, that activity is typically called feature selection (sometimes feature extraction), and there's plenty of literature out there that describes how you might approach it. Feature selection involves selecting a subset of the original variables, and is not quite the same as dimensionality reduction, which typically involves creating a small number of linear combinations of the original variables that summarise them (this is what a technique such as PCA or SVD does).
A suggestion I might give is to note that you're trying to search through what is a discrete space (the power set of your variables). Simulated annealing, as an optimization technique, is in my experience more easily applied to searching through continuous spaces. This is particularly true of the implementation in MATLAB Global Optimization Toolbox (since I note you added the MATLAB tag). If you're using MATLAB for this, I'd suggest that a genetic algorithm might be easier to adapt to searching through discrete spaces.
I wrote an article for MATLAB Digest a while ago that applies genetic algorithms to a related problem (classification rather than cluster analysis), which comes with example code. You might find that it's possible to adapt that code to your needs. The article carries out feature selection on a classification problem though, so it's maximizing classification accuracy - you'd need to provide a clustering metric for the algorithm to optimize, such as separation, heterogeneity, or a gap statistic.
Hope that helps!
Comment: Sam thank you very much! Very informative and will help along way!
Answer: Simulated Annealing is heuristic search optimization algorithm.
You need to define a loss function.
SA will optimize this and will converge to local minumum.
K-means clustering by definition also includes optimization.
It minimizes instances' distance to cluster centers.
This distance can be eucledean or manhattan distance.
Look to http://www.iro.umontreal.ca/~lisa/pointeurs/kmeans-nips7.pdf
for its convergence properties and similarities to gradient descent.
From my limited understanding only way to use to SA ve K-Means together is
to define K-Means loss differently than distance minimization.
For example you may define a loss function :
using different distance metrics or different feature subsets.
Loss Function = sum of metric distance of features and different combinations of features
SA will try to minimize this Loss Function.

Simulated annealing and k-means


Custom module Pager does not work with Ajax filter

Question: My custom module uses an Ajax enabled drop down to allow users to filter images. There is also a pager attached to the query. The pager works fine when all the data is displayed i.e. no filter applied but after applying a filter, clicking a pager button just gives a white screen.
Here is my code.
function view_colouring_sheets($form, $form_state){
$query = db_select('field_data_field_image','fi')->extend('PagerDefault');
$query->join('node','n','n.nid = fi.entity_id');
$query->fields('fi');
$query->fields('n',array('title'));
$query->limit(20);
if($form_state['input']['tid']){
    $query->condition('entity_id',  colouring_sheets_filtered_tid($form_state['input']['tid']),'in');
}
$images = $query->execute()->fetchAll();

$output = array(
    '#theme'=>'image_matrix',
    '#type'=>'markup',
    '#images'=>$images,
    '#title'=>'Colouring Sheets',
    '#pager'=>theme('pager'),
    '#filter_form'=>  drupal_render(drupal_get_form('image_filter_form')),
);

return $output;
}

function colouring_sheets_filtered_tid($tid){
    $query = db_select('field_data_field_image_category','ic');
    $query->condition('field_image_category_tid',$tid);
    $query->fields('ic');
    $result = $query->execute()->fetchAll();
    $term_array = array();
    foreach($result as $row){
        $term_array[] = $row->entity_id;
    }
    return $term_array;
}

function image_filter_form($form, $form_state){
    $terms = taxonomy_get_tree(18);
    $terms_array = array();
    foreach($terms as $term){
        $terms_array[$term->tid] = $term->name;
    }
    $form['tid'] = array(
        '#type'=>'select',
        '#options'=>$terms_array,
        '#required'=>TRUE,
        '#title'=>'Fitler Images',
        '#ajax'=>array(
            'method'=>'replace',
            'wrapper'=>'image_matrix',
            'callback'=>'view_colouring_sheets',
            'progress' => array('type' => 'throbber', 'message' => 'Images Loading'),
            'effect'=>'fade',
        ),
    );
    return $form;
}

Comment: Where is the image_matrix div located?  not seeing it in your form html anywhere
Comment: Also is this a separate module and showing us functions?  Drupal conventions would have all your functions named modulename_function.  Your functions are named differently.  not that it will cause any issues.
Comment: After further digging around different forums, I found the answer. This is a bug in Drupal and which still exists in Drupal 8. They are going to try a fix in 8 and if successful will port it to Drupal 7. The issue is that after refreshing a page via ajax which contains a Pager all the links are built around the current url which in this case would be system/ajax which is why the pager then stops working.
Answer: I have discovered a sort of workaround.  It requires storing the result of current_path() in a JS setting variable, and rewriting the path in JS when the pager is regenerated.
Sample code:
php
// building an ajax form...
global $base_path;
$settings = array(
    'my_module' => array(
    'my_path' => $base_path . current_path(),
  ),
);
drupal_add_js($settings, 'setting');

That will make the current page's path available in your JavaScript file to manipulate.  There are still kinks though, especially with AJAX filtering and AJAX paging all at once.  I will update this once I resolve all those kinks.


Anonymouse users can't see Openlayers Data Overlay

Question: I have a map view displaying the Openlayers Data Overlay. It's displaying a list of GPS tracks from a "Map Feature" content type I made.
Logged in with the Administrator, I can see all the tracks on the map fine. The preview for the Data Overlay shows all the data coming through great.
When I few the map as an Anonymous user, not logged in, none of the GPS tracks show up. The map displays on the page but none of the data from my Data Overlay.
I've looked all over the place trying to find content permissions that are wrong that are screwing things up but I can't find anything.
Why is the data on the map showing for the administrator and not Anonymous users?
Comment: your other question might be related/ result of the same problem: http://drupal.stackexchange.com/a/102874/13366
Answer: The pages were cached for anonymous users. I went into Performance -> Caching  and cleared the caches and it worked. I had cleared my own cache but not on the site.


PHP: display 90 min as 1.5 hours

Question: Minutes=90
Need to display in hours
echo intdiv($ct['time'], 60).'.'. ($ct['time'] % 60)." Hours";

The output is 1:30 hours.
But i need to display as 1.5 hours.
How to convert  the minute into hours? As 1.5, 1.6, 1.7, 1.8, 1.9, 2 hours and so on .
Comment: 90 / 60 = 1.5…‽
Comment: What have you tried so far? Where are you stuck? How is the given code even related to Laravel or CodeIgniter?
Comment: I think `1:20` is going to be more useful to people than `1.3333333333`.
Comment: 30 / 60 is .5, isn't it?
Comment: If I can add a note about terminology, `1:30` is the human readable form. `1.9` looks more like a machine representation (unless such format happens to be used in some culture I'm not aware of).
Comment: _IMHO_ 1.50 is less "human readable". I would assume it's 1 hour 50 mins. Like what time is `1.957`?
Comment: Is there a way to display as required
Comment: It's hard to find dupes for super-basic questions sometimes.  This is as close as I can get at the moment: https://stackoverflow.com/q/27474671/2943403 and https://stackoverflow.com/q/53554315/2943403
Comment: Does this answer your question? [Convert Time String to Decimal Hours PHP](https://stackoverflow.com/questions/13595935/convert-time-string-to-decimal-hours-php)
Answer: I agree with all the comments saying that it will be clearer to display 1h20 than 1.33 hours. You may could round it and display 1 h and ½ or 1 hour and ¼ but still, it's not easy to read.
If you want to stick to your hours then you could do this:
$min = $cat['prepration_time'];
if ($min < 60) {
    echo $min == 1 ? "$min minute" : "$min minutes";
}
else {
    $hours = round($min / 60, 1);
    echo $hours == 1 ? "$hours hour" : "$hours hours";
}

Examples of output:
1 minute
20 minutes
50 minutes
1 hour
1.2 hours
1.5 hours
1.7 hours

Answer: It appears that you have one variable that stores an amount of minutes, $cat['prepration_time'] in your case. To output the minutes as decimal hours all you really need to do is to divide the number of minutes by 60, that's the number of minutes in one hour. In your case that would be
echo $cat['preparation_time'] / 60 . ' hours';

To avoid awkward results like 1.33333333333 hours, you can additionally wrap the calculation in round, where the second parameter of round is the precision, i.e. the number of digits behind the dot.
$minutes = 77;
echo round($minutes / 60, 2) . ' hours'; // 1.28 hours

Be aware however that this format is badly readable and prone to misunderstanding. Does 1.30 hours mean 1 hour and 30 minutes or rather 1 hour and 18 minutes? And while you might be able to convert 1.30 hours in your head, you will surely need a calculator for 1.283 hours. I would recommend to use a commonly understood and unambiguous format, like for example 1 hour and 32 minutes, or 1:32 hours.
Answer: Add this code.
Firstly add the hour and minutes after that I have added round function so it will show proper output upto 2 decimal.
$hours =1; $minutes=30 ; $CalculateHours = $hours + round($minutes / 60, 2); echo $CalculateHours  . " hours";
Result: 1.5 Hours
Comment: Why? Please add some explanation to your answer such that others can learn from it
Comment: I have already mention the brief code with proper steps.
Comment: So, how would your code work with the given sample input of `$minutes = 90` from the question?
Comment: @NicoHaase for that $hours =0; $minutes= 90 ; $CalculateHours = $hours + round($minutes / 60, 2);  echo $CalculateHours;

PHP: display 90 min as 1.5 hours


How do I pass a URL to a route?

Question: I need to send the URL of a Drupal 8 route to a template. I read some documentation on Drupal API, but when I implement this in my module, nothing happens.
My route in my routing file looks like the following.
movies.addMovie:
    path: /movies/add/{system}/{id}
    defaults:
        _controller: 'Drupal\movies\Controller\InsertController:addMovie'
        _title: 'Add Movie'
        system: ''
        id: ''
    requirements:
        _permission: 'create movies_movie'

This is the code I am using to get the URL from the route.
use Drupal\Core\Url;
Url::fromRoute('movies.addMovie')

When I put the URL in a variable, the code stops when calling Url::fromRoute(). Can someone tell me what I'm doing wrong?
Comment: Looks like you found the twig wrapp function ```url()``` -- you should use that inside twig. If you're set on using ```Url::fromRoute()```, then you probably want to call [Url::toString()](https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Url.php/function/Url%3A%3AtoString/8.2.x) on the returned [Url](https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Url.php/class/Url/8.2.x) object. Like: ```$url_string = Url::fromRoute('add.movie')->toString();```
Comment: Can you paste the entire error?
Comment: No errors are thrown. The line is just skipped.
Comment: So you assign a breakpoint and set a variable to the result of that call and nothing happens?
Comment: Indeed. When I place a breakpoint on the call and one after the call, the one after the call is not be hit.
Comment: What is the context of your usage?
Comment: I need to place a url of a custom Drupal page to my twig template.
Found already a way to get the url object. Now how can I generate the url of that object. Everything in that object is empty exepct the routename?
Comment: See also [Link::createFromRoute](https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Link.php/function/Link%3A%3AcreateFromRoute/8.2.x) if you're creating a link element.
Answer: should be:
InsertController::addMovie
Needs a double-colon.
Yours is single.
Answer: Found a way to generate the url of my route using twig template.
I'm now using the url function of twig to generate the full url to my page:
{{ url('movies.addMovie')}}

How do I pass a URL to a route?


How do I get a dropdown in yogiben:meteor-admin to populate with all values from my collection?

Question: I have a collection called Academics. Each academic can have up to two advisors, who are also in the Academics collection.
I'm using the yogiben:admin plugin for my admin back-end, but the advisors field (which I call 'advisor1.who', see schema below) is not working how I want/expect - if I edit an academic, the only option in the dropdown for their advisor is the academic I am currently editing.
I'm also using Mongol, and when I go into the admin side, if I list all Academics, then only the current 10 listed are viewable in Mongol, and when I edit an individual Academic, only that academic is available in Mongol. How can I make all documents in the Academics collection available when I'm editing an individual in meteor-admin?
Here is my list of plugins, the ones that are of consequence to this problem are all up to date:
accounts-password           1.1.1  Password support for accounts
accounts-ui                 1.1.5  Simple templates to add login widgets to an app
alanning:roles              1.2.13  Role-based authorization
aldeed:autoform             5.3.0  Easily create forms with automatic insert and update, and auto...
aldeed:autoform-select2     1.0.5* Custom select2 input type for AutoForm
aldeed:collection2          2.3.3  Automatic validation of insert and update operations on the cl...
coffeescript                1.0.6  Javascript dialect with fewer braces and semicolons
dburles:collection-helpers  1.0.3  Transform your collections with helpers that you define
email                       1.0.6  Send email messages
fortawesome:fontawesome     4.3.0  Font Awesome (official): 500+ scalable vector icons, customiza...
iron:router                 1.0.9  Routing specifically designed for Meteor
joshowens:accounts-entry    1.0.3  Make signin and signout their own pages with routes.
meteor-platform             1.2.2  Include a standard set of Meteor packages in your app
meteorhacks:fast-render     2.4.0* Render you app even before the DDP connection comes live. - ma...
meteorhacks:subs-manager    1.3.0* Subscriptions Manager for Meteor
mizzao:autocomplete         0.5.1  Client/server autocompletion designed for Meteor's collections...
msavin:mongol               0.6.5* The insanely handy development package for Meteor.
twbs:bootstrap              3.3.4* The most popular front-end framework for developing responsive...
underscore                  1.0.3  Collection of small helpers: _.map, _.each, ...
yogiben:admin               1.2.0  A complete admin dashboard solution
yogiben:autoform-modals     0.3.5* Create, update and delete collections with modals

and the relevant part of the schema (in coffeescript):
Schemas.Academics = new SimpleSchema

    // other fields that work just fine are here...

    advisor1:
        type: Object
        optional: true

    'advisor1.who':
        type: String
        regEx: SimpleSchema.RegEx.Id
        optional: true
        autoform:
            options: ->
                _.map(Academics.find({}).fetch(), (academics)->
                    label: academics.name
                    value: academics._id
                )

This seems like it should work, since it is very close to the code from the plugin readme (which returns a list of meteor users as potential owners):
owner: {
    type: String,
    regEx: SimpleSchema.RegEx.Id,
    autoValue: function () {
      if (this.isInsert) {
        return Meteor.userId();
      }
    },
    autoform: {
      options: function () {
        _.map(Meteor.users.find().fetch(), function (user) {
          return {
            label: user.emails[0].address,
            value: user._id
          };
        });
      }
    }

and the admin config:
@AdminConfig = {
    collections: {
        Academics: {
            tableColumns: [
                {label: 'Name', name: 'name'},
                {label: 'Verified', name: 'advisor1.adminConfirmedAccurate'}
            ]
        }
    }
}

Anything else I can add that would be helpful?
Comment: It looks like your code in the autoform.options object doesn't do anything because it doesn't return. If you return the _.map line it should work.
Answer: My problem ended up being that I was using meteorhacks:subs-manager to add subscriptions in my routes. Because of that, the admin package couldn't pick up the subscription for all the other academics I needed. Once I added a subscription in a top-level client folder, everything was fine. jamgold over at the meteor forums pointed me in this direction - so thanks jamgold!


Apache - Return 404 for all execept selected

Question: I am seeking for method to prevent access to Wordpress admin folder "wp-admin", but also i need apache to generate 404 error (Not found) for all, but not selected IPs.
I found this post Returning 404 code for unauthorized attempts and tried Rewrite solution, but does not work for me.
So i keeping all websites in /usr/local/www/apache22/data
and tried to use such config in httpd.conf (Apache2.2 FreeBSD 9.1)
<Directory "/usr/local/www/apache22/data">
RewriteEngine on
RewriteCond %{REMOTE_ADDR} !=126.96.36.199 [OR]
RewriteCond %{REMOTE_ADDR} !=188.8.131.52
RewriteRule ^wp-admin($|/) - [L,R=404]
</Directory>

For example, location of wp-admin for website is: /usr/local/www/apache22/data/doman.com/wp-admin
Comment: Why do you want to do this? Why isn't simply denying access (HTTP/403) for unauthorized IP addresses (which you can do with simple `Allow`/`Deny` rules) adequate?
Comment: I have been awaiting for such reply. The reason is to obfuscate bad bots trying to guess presence of Wordpress installed. So if they get 404 error then they won't try to explore for wordpress vulnerabilities.
Comment: is it not possible to actually MOVE the wp-admin stuff? If you can just move the file/folder to a new location you get your 404, your obfuscation, and you aren't doing absolutely silly (breaking internet standards) things to do it...
Comment: To answer my own questions: Can't move wp-admin...easily... with that said, lets move on... WHERE have you added the above code? httpd.conf? .htaccess? is mod_rewrite reloaded and enabled in apache? have you restarted apache since you made the changes? edit the question with your responses and we will go from there.
Comment: I mentioned in question already where i have added rules. Sure restarted apache, mod_rewrite loaded http://pastebin.com/b65GhsVZ and working fine as plenty of my website using it. Regarding moving wp-admin, sure i can use bash script to rename all in one shot, but i'm curious (and actually going to use) how to accomplish this without touching core files.
Answer: OK, First let me say that your entire premise is flawed: There are many other ways besides the presence of /wp-admin to determine that you're using Wordpress. You're attempting Security through Obscurity, and since you're not obscuring everything you aren't doing a particularly elegant job of it.

Those inherent problems aside, at a quick glance your RewriteRule seems to be wrong.
Per the Apache documentation:

What is matched?
In VirtualHost context, The Pattern will initially be matched against the part of the URL after the hostname and port, and before the query string (e.g. "/app1/index.html").
In Directory and htaccess context, the Pattern will initially be matched against the filesystem path, after removing the prefix that led the server to the current RewriteRule (e.g. "app1/index.html" or "index.html" depending on where the directives are defined).

Simply put that means you need more of the path to wp-admin in your rule if you want to do this in the Directory context (like ^domain.com/wp-admin($|/)). The simple/naive fix would be to change your regex to ^.*wp-admin($|/) and just match anything that contains wp-admin (which is also a good way to verify that the rule itself is being parsed).
If you insist on pursuing this avenue of "security" though I would advise making this change in the VirtualHost context (or /wp-admin/.htaccess file) instead of in the Directory context for your whole server. It is both simpler to write and more robust. (Your current solution breaks if someone on your server DOESN'T want their wordpress admin page locked down (and there are probably a lot of people who won't -- anyone with a dynamic IP for example). Doing this in each VirtualHost or a .htaccess file allows you to keep certain sites "unlocked".)
Comment: Hope I found what i need. This works for me if below block inserted in httpd.conf context

    RewriteCond %{REMOTE_ADDR} !^77\.120\.9\.3$
    RewriteRule . http://google.com/404.php [R=404,L]

I saw this in Rewrite redirects examples and adapted to serve 404 error, but initially it was intended to do redirection.


Ajuda com várias divs em Selenium em Python

Question: Amigos, podem me ajudar? Quero fazer web scrap de uma página e retornar todos os 8 textos de um span.text-ellipsis. Eu fiz o seguinte código:
    self.otherPlayers = self.driver.find_element_by_css_selector('span.text-ellipsis').text

mas como eu transformo em string, eu só consigo retornar um valor. como retornar os outros?
Answer: Utilizando elements ao invés de element:
self.otherPlayers = self.driver.find_elements_by_css_selector('span.text-ellipsis').text


GRE 8767 - 34 - Graphical explanation

Question:
Let the bottom edge of a rectangular mirror on a vertical wall be parallel to and h feet above the level floor. If a person with eyes t feet above the floor is standing erect at a distance d feet from the mirror, what is the relationship among h, d and t if the person can just see his own feet in the mirror?

I don't quite understand the solutions here and here.
There's an explanation here:

pretend the mirror is a hole into which you are looking at another object.
1) If we represent this the X,Y axis, with the line x = 0 representing the wall, and x = d representing the person. Then, the reflection of the person is the line x = -d.
2) Then, because he can just see his feet in the mirror: draw a line from the top of the person's head to the feet of his reflection. Imagine if another person is actually standing there and you can only see down to just his feet through the "hole".
3) This creates two congruent triangles. Giving the result t = 2h, regardless of d.

I don't quite understand the last step. Please help me visualise this:
Let $d > 0$. The person stands from $(d,0)$ to $(d,t)$. The reflection stands from $(-d,0)$ to $(-d,t)$. The bottom edge of the mirror is at $(0,h)$ where the top edge is at $(0,h_1)$ where $h_1 > h$.
So the feet are in $(d,h)$ to $(d,h_1)$?
Then we're supposed to arrive at
$$\frac{t}{h} = \frac{2d}{d} \to t = 2h$$
I'm so confused. Please help.
Answer: Here's a picture:

Here, the person is standing at $B$, with his/her head at $D$. The mirror's bottom is at $C$, which is $h$ feet above the floor. Finally, the "feet of the reflection" are at point $A$.
Triangles $\triangle ABD$ and $\triangle AEC$ are similar, so that $\frac{t}{h} = \frac{2d}{d}$.

BCLC edit: $(0,h)$ is on the line $AD$ which is given by $$y=\frac{t}{2d}(x+d)$$ hence $$h=\frac{t}{2d}(0+d) \to h=\frac{t}{2}$$
Comment: The feet are at point $B$. The bottom of the mirror is at $C$. Your head is at $D$. And $A$ are the feet of the reflection.
Comment: $E$ is the floor of the room. $C$ is the bottom of the mirror, $h$ feet above the floor.
Comment: But the question only asks about your feet. Yes, the top of the mirror is higher that $C$, and you can see more than just the feet. But that is irrelevant to the question, isn't it? (Think about the physical situation, or go stand in front of a mirror).
Comment: Oh, I see the issue. I interpreted the words "just see his own feet" as "just barely see his own feet" - that is, if he moved further away, he would not be able to see his feet. You interpreted it to mean "see only his feet". I think my interpretation is what was meant.
Comment: Yes, sorry. Closer to is correct.
Comment: Approximately where are the feet?
Comment: C is the bottom edge? Or E?
Comment: I don't follow. If C is the bottom of the mirror then the top is somewhere $(0,h1)$ and I can see only my feet in the mirror? I think this would make sense if C is the top edge
Comment: Oh thanks rogerl! I I think I'm getting it. You mean further toward?
Comment: thanks rogerl (or rogerI ?) ^-^

GRE 8767 - 34 - Graphical explanation


Determining Why X is a Subgroup of Y

Question: I'm currently taking a course on Intro. to Abstract Algebra and I am a bit confused on what subgroups and subsets are. During class today, for the set $\mathbb{Z}_6$ = {0, 1, 2, 3, 4, 5}, the professor stated that the subgroups are {0, 2, 4}, {0, 3}, and {o}. He wasn't very clear on the explanation, so could anyone explain to me why and how to determine subgroups? Thanks.
Comment: first, the operation must stay internal. I suppose that they are cyclic groups with modulo plus. In this case , for example adding 5 to {0,2,4} will produce a set where the operation is not internal for 5+4=3 mod 6, 3 not in {0,2,4,5}
Comment: A sub-group must have elements that are a subset of the elements of the larger group, yet maintain all of the properties of a group. (closure, identity, inverse)
Answer: You have to specify what binary operation you are working with here. You must first understand the definition of a subgroup.
A subgroup $H \subset G$ of a group $G$ is a set that is closed under the associative binary operator $\cdot_G$ and contains the identity element $e$ of $G$ such that $a\cdot_G e = e\cdot_G a = a$. Further, for each $a \in H$ there exists a unique $a'\in H$ such that $a\cdot_G a' = a'\cdot_G a = e$.
Notice that in each example your professor gave, the identity element is present and that the set containing the identity element is the smallest subgroup of any group.
Answer: A subgroup $H$ of a group $(G,*)$ is a subset of $G$ that is also a group with respect to the binary operation $*$.
In your case the group is $\mathbb{Z}_6$ and the binary operation is addition $+$.
You can check that the subsets $\{ 0,2,4 \}$, $\{ 0,3\}$ and $\{ 0\}$ are indeed subgroups. In the first case note in particular that $2+4 = 0$ since we are adding residue classes mod $6$ (it should really be written $[2]_6 + [4]_6 = [6]_6 = [0]_6$).
To see that these are the only subgroups the quickest way would be to note that $\mathbb{Z}_6$ is a cyclic group and so any subgroup of it must be cyclic. Now go through each element in $\mathbb{Z}_6$ and repeatedly add it with itself to generate all subgroups.
Answer: A set is any collection of objects.  There doesn't have to be any group operation or group structure to them.
The elements of a subgroup must be a subset of the elements of the group, BUT these elements must form a group when using the group operater.
So the group, $\mathbb Z_6$ is the set {0,1,2,3,4,5} combined with the group operator "+".
So say {1, 3,4} is a subset of  {0,1,2,3,4,5} but it is not a subgroup because {1,3,4} combined with "+" is not a group.  1 + 1 = 2 is not an element so the group isn't closed.  0, the identity element, is not in set at all and to be a group, 0, the identity element has to be in the set.  The inverses of 1 (which is 5) and the inverse of 4 (which is 2) are not in the set at all.
So {1,3,4:+} is simply not a group at all.
So to be a subgroup:  The identity element must be included--  So must an inverse for every element that is included--  And the set must be closed.
There are 64 subsets of {0,1,2,3,4,5} and most of them do not form a group.
A group must contain 0.  {0:+} is a subgroup because it contains -0=0 and for every 0, 0+0 = 0 is in the group.
If 1, is in the subgroup, then -1 =5 must be in the subgroup. So must 1+1=2, 2+1 =3, 3+1 =4.  So if 1 is in a subgroup the subgroup is the entire group $\mathbb Z_6$.  There is no other subgroup containing 1.
If 2 is in the subgroup so is -2 = 4.  So is 2+2 = 4.  {0,2,4} has all the elements necessary to be a group.  So {0,2,4:+} is a subgroup.
If 2 is in the subgroup and so is 3 then 3 -2 = 1 must also be in the group and we discussed that above. If 5 is in the subgroup than so is -5 =1.
So {0,2,4:+} is the only subgroup with 2 but not 1.
If 3 is in the subgroup then so is -3 = 3 and 3+3 = 0.  So {0,3: +} is a subgroup.  It's the only subgroup with 3 that doesn't also have 1. (If it has 2 or -2 =4 or -1 = 5, then it will have 3-2 or 3+4 or -5 = 1.)
If 4 is in the group then -4 = 2 is in the group and we discussed that above.
If 5 is in the group then -5 = 1 is in the group and we discussed that.
So the only subgroups are {0:+},{0,2,4:+}, {0,3:+} and $\mathbb Z_6$.
Of all the 64 subsets these are the only 4 that form groups when combined with the group operator.
Answer: In the present case, the law is addition modulo $6$, and the unit element is $0$.
Now, if $H$ is a subgroup of $\mathbf Z_6$, let $a$ the smallest non-zero element of $H$, if any. If $a\in H$, by closedness of a subgroup, $H$ contains $a+a=2a$, $2a+a=3a$, &c. Thus

if $a=1$, then $H$ also contains $2,3, 4,5$ and $6\cdot 1=0$. So $H=\mathbf Z_6$.
if $a=2$, $H$ also contains $2\cdot 2=4$, $3\cdot 2=4+2=0$, $4\cdot 2=0+2=2$. So $H=\{0, 2,4\}$.
if $a=3$, $H$ also contains $2\cdot 3=3+3=0$, $3\cdot 3=2\cdot 3+3=0+3=3$, so $H=\{0,3\}$
if $a=4$, $H$ contains $4+4=2<4$, so $a$ can't be equal to $4$.
Similarly, $a$ can't be equal to $5$, since $5+5=4<5$, $3\cdot 5=3$, $4\cdot5=2$, $5\cdot 5=1$.

We see the subgroup generated by $5$ is the same as the whole group, generated by $1$. This is a general fact: the generators of the group  $\mathbf Z_n$ are the numbers $a$ $\;(1\le a\le  n-1)$ such that $a$ is coprime to $n$.
.

Determining Why X is a Subgroup of Y


Remove fieldset from date field

Question: I am trying to remove the <fieldset> surrounding a date popup field, but I find that the label is also removed. This is my code:
function mymodule_profile_form_alter(&$form, $form_state, $form_id) {

  global $user;
  //print_r($form['field_last_name']);

  $form['field_birthdate']['und'][0]['#theme_wrappers']=$form['field_birthdate']['und'][0]['#theme_wrappers'][0];
}

Can anyone help me to solve this problem?
Comment: If you dont want the fieldset at all what about removing it from the theme altogether i.e. so it doesnt get generated?
Comment: could you please tell me the code to remove the field set and tell me where can i type it and thanks
Comment: Are you sure your using date popup because this should theme as a form_element?
Comment: There is a patch which brings option to render date field as normal textfield:
[http://drupal.org/node/1467712#comment-6137556](http://drupal.org/node/1467712#comment-6137556) But in some cases fieldset is required and redner it as texfield may cause some problems.
Answer: Typically there is a theme function associated with the different sort of output you can give data fields or indeed any fields If you want to change this you do so on the theme level by overriding the default theme function in your themes template.php file.
Date popup
function YOURTHEME_date_popup($vars) {
  $element = $vars['element'];
  $attributes = !empty($element['#wrapper_attributes']) ? $element['#wrapper_attributes'] : array('class' => array());
  $attributes['class'][] = 'container-inline-date';
  // If there is no description, the floating date elements need some extra padding below them.
  $wrapper_attributes = array('class' => array('date-padding'));
  if (empty($element['date']['#description'])) {
    $wrapper_attributes['class'][] = 'clearfix';
  }
  // Add an wrapper to mimic the way a single value field works, for ease in using #states.
  if (isset($element['#children'])) {
    $element['#children'] = '
' . $element['#children'] . '
';
  }
  return '
' . theme('form_element', $element) . '
';
}

Although it must be said this is being themed as a form_element so there is no fieldset.
Date combo is themed as a fieldset and again can be overriden...
function YOURTHEME_date_combo($variables) {
    $element = $variables['element'];
    $field = field_info_field($element['#field_name']);
    $instance = field_info_instance($element['#entity_type'], $element['#field_name'], $element['#bundle']);

    // Group start/end items together in fieldset.
    $fieldset = array(
            '#title' => t($element['#title']) . ' ' . ($element['#delta'] > 0 ? intval($element['#delta'] + 1) : ''),
            '#value' => '',
            '#description' => !empty($element['#fieldset_description']) ? $element['#fieldset_description'] : '',
            '#attributes' => array(),
            '#children' => $element['#children'],
    );

    // Fix required marker.
    if ($element['#required']) {
        $fieldset['#title'] .= " " . theme('form_required_marker');
    }
    return theme('fieldset', array('element' => $fieldset));
}

If you replaced 'fieldset' with 'form_element' this would work.

Remove fieldset from date field


Databricks delta file to SQL table (Azure)

Question: In Databricks I want to upload a Delta File to a table that already exists in SQL.  But I need that only the records that doesn`t exist are upload.
I am using this code:
%scala
val df = spark.read.format("delta").load("dbfs:/user/hive/warehouse/hourly_tables_JMA/")

df.write
.format("jdbc")
.option("url", "jdbc:sqlserver://qdenaz.database.windows.net:1433;databaseName=database")
.option("dbtable", "[DWH].HOURLY_TABLES_JMA")
.mode("append")
.option("user", "Admin")
.option("password", "****")
.save()

In mode option:

If I use Append, I obtain duplicate records
If I use Ignore, it doesn't do anything because the table already exists
If I use Overwrite, it truncates the table and upload the records of the Delta File.

What other option can I use?   or what can I do to upload only the records of the Delta File that are not on the table.
Thanks.
Answer: To upsert non existing records in SQL table from data bricks it does not provide any specific writing mode only available writing modes are Append, Overwrite, Ignore, errorifexists .
To achieve your goal, follow below steps:

First read the delta table in Dataframe.

val df1 = spark.read.format("delta").load("<table_path>")

My delta table DF:

Read table where you want to write the data from databricks in a dataframe.

val df2 = spark.read.format("jdbc")
    .option("url","<JDBC_URL>")
    .option("dbtable","<Tablename>")
    .option("user", "<username>")
    .option("password", "<password>")
    .load()

My SQL table DF:

Now with left anti join on primary key join the table to get non existing records in SQL table from delta table in dataframe.

val  df3 = df1.join(df2,Seq("<primarykey>"),"left_anti")

My DF with non-existing records:

Now write this new dataframe with Non existing records in SQL table.

df3.write.format("jdbc")
    .option("url", "<JDBC_URL>")
    .option("dbtable","<Tablename>")
    .option("user", "<username>")
    .option("password", "<password>")
    .mode("append")
    .save()

Output:


Given iid random variables, expected number of times that a random variable is greater than all previous variables.

Question: I'm given $X_1, X_2, \dots$ iid with continuous cdfs, and let $A_1 = \Omega, A_n=\{\omega:X_n
(\omega) > X_i(\omega) \textrm{ for } i < n\}$.
I'd like to show that $\lim_{n\rightarrow \infty}{\frac{1}{\log(n)}\sum_{j=1}^{n}{1_{A_j}}} = 1$ almost surely.
This is a pretty interesting result, but I'm pretty unsure of how I might go about showing this.
Answer: Let me briefly sketch a proof. The first step should be to note that the $A_n$'s are independent and  $\mathsf{P}(A_n)=1/n$ for all $n$ (do you see why?). From Kolmogorov's inequality, for any $\epsilon>0$
$$\mathsf{P}\left(\max_{2^{n^2}\le k\le 2^{(n+1)^2}} \left|\sum_{j=1}^k \left(1_{A_j}-1/j\right)\right|> \epsilon n^2\right)\lesssim \frac{1}{\epsilon^2 n^2}.$$
Now we use Borel-Cantelli lemma to argue that almost surely, for $n$ large enough
$$\left|\sum_{j=1}^n \left(1_{A_j}-1/j\right)\right|\le \epsilon\log n.$$
What we just proved is
$$\forall \epsilon>0, \mathsf{P}\left(\exists n_0(\epsilon), \forall n>n_0(\epsilon), \left|\frac{1}{\log n}\sum_{j=1}^n 1_{A_j}-\frac{1}{\log n}\sum_{j=1}^n 1/j\right|\le \epsilon\right)=1.$$
As the intersection of a countable number of almost sure events is almost sure,
$$\mathsf{P}\left(\forall \epsilon\in\mathbb{Q}_+^*, \exists n_0(\epsilon), \forall n>n_0(\epsilon), \left|\frac{1}{\log n}\sum_{j=1}^n 1_{A_j}-\frac{1}{\log n}\sum_{j=1}^n 1/j\right|\le \epsilon\right)=1.$$
Now notice that if we take $n_0(\epsilon)$ large enough, from the asymptotics of the harmonic sum, it holds that for all $n>n_0(\epsilon)$, $\left|\frac{1}{\log n}\sum_{j=1}^n 1/j-1\right|\le \epsilon$. Hence:
$$\mathsf{P}\left(\forall \epsilon\in\mathbb{Q}_+^*, \exists n_0(\epsilon), \forall n>n_0(\epsilon), \left|\frac{1}{\log n}\sum_{j=1}^n 1_{A_j}-1\right|\le 2\epsilon\right)=1.$$
That's exactly the definition of almost sure convergence of $\sum_{j=1}^n 1_{A_j}/\log n$ to $1$.
Comment: Sorry, could you clarify why the second inequality suffices to show that $\sum_{j=1}^{n}{\frac{1_{A_j}}{\log{n}}} \rightarrow 1$ almost surely? @md5
Comment: @asdf654l: I added more details. Is that clearer now?


Filtering and then reassigning navigation properties

Question: I'm writing a piece of this website that handles list filtering.  User selects some options, and the data is filtered server-side and set back to the user through an AJAX request.
For this example, say I have a table of car manufacturers (Ford, BMW, Fiat, etc) and it has a foreign key to a list of all their vehicles, which in turn has a foreign key to a table of vehicle types.
So it'd be like this:
Manufacturer

ManufacturerID
Name

Type

TypeID
Name

Vehicle

VehicleID
ManufacturerID
TypeID
Name

This would come back as a nice entity and some convenient navigation properties.  However, this is the problem I'm facing: Say I want to filter a manufacturer's vehicles based on their type, inline.
foreach (var manufacturer in manufacturers)
{
    manufacturer.Vehicles = manufacturer.Vehicles.Any(v => v.Type.Name == "Car"));
}

This seems pretty straightforward but I get the error:

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'System.Data.Objects.DataClasses.EntityCollection'. An explicit conversion exists (are you missing a cast?)

Casting to EntityCollection doesn't help, and throws an exception at run time.  Is it even possible to assign the result of a Linq query to a navigation property?  Is there another way of doing this?  I'm using a ViewModel but I'd like to re-use the navigation properties if I can, instead of, perhaps, writing in a dictionary and handling it all manually.
Answer: The problems is that the manufacturer.Vehicles's type is EntityCollection and manufacturer.Vehicles.Any returns  an objet of type IEnumerable, so you want to try as follows:
foreach ( manufacturer in manufacturers) {
   manufacturer.Vehicles = (from vehicle in manufacturer.Vehicleswhere vehicle.Type.Name == "Car");
}

Comment: Doesn't that effectively do the same thing, just a different way?
Comment: No because the returned type implements IEntityWithRelationships not IEnumerable.
Comment: Hm, it still comes back as IEnumerable for me when I try it this way.
Comment: Model first, technically it's just ADO.NET, no EntityFramework.
Comment: So then try adding .ToList at the end of the query:

manufacturer.Vehicles = (from vehicle in manufacturer.Vehicleswhere vehicle.Type.Name == "Car").ToList

and tell me what happend


(Is same case) loop return not working properly

Question: https://codewars.com/kata/5dd462a573ee6d0014ce715b

Write a function that will check if two given characters are the same case.

If either of the characters is not a letter, return -1
If both characters are the same case, return 1
If both characters are letters, but not the same case, return 0

Examples
'a' and 'g' returns 1
'A' and 'C' returns 1
'b' and 'G' returns 0
'B' and 'g' returns 0
'0' and '?' returns -1

char1 = 'G'
char2 = 'a'

def is_same_case(char1,char2):
    if char1.isupper() and char2.isupper or char1.islower() and char2.islower:
        return 1
    elif char1.isupper() and char2.islower or char1.islower() and char2.isupper:
        return 0
    else:
        return -1

print(is_same_case(char1,char2))
print(char1.isupper())
print(char2.isupper())

It returns 1 instead of 0.
Comment: what does `if char2.isupper` return? What is that trying to evaluate?
Comment: You're missing `()` on the `char2.isupper` and `char2.islower` function calls.
Comment: For what its worth, the case comparison can be simplified to `a.isupper() == b.isupper()`
Comment: @Sayse Generally yes, but that won't cover the `?` → `-1` case…
Comment: Sometimes you call the method `isupper()` sometimes you don't `isupper`. Always call it.
Answer: You can utilize the isalpha() method to test cases that aren't letters, and then perform a comparison of the isupper() method, and convert the boolean to an integer
def is_same_case(char1, char2):
    if not (char1.isalpha() and char2.isalpha()):
        return -1
    return int(char1.isupper() == char2.isupper())


How to translate the results from lm() to an equation?

Question: We can use lm() to predict a value, but we still need the equation of the result formula in some cases. For example, add the equation to plots.
Comment: Can you please rephrase your question or add some details? I'm quite familiar with R, `lm` and linear models more generally, but it's not at all clear what, exactly, you want. Can you give an example or something to clarify? Is this for some subject?
Comment: I guess you want the coefficients of the linear regression formula. Try calling `coef()` on the fitted `lm` object, as in: `mod <- lm(y ~ x); coef(mod)`
Comment: Related: [How to apply coefficient term for factors and interactive terms in a linear equation?](http://stats.stackexchange.com/q/24242/930)
Comment: Worth reading http://stackoverflow.com/questions/7549694/ggplot2-adding-regression-line-equation-and-r2-on-graph
Comment: This thread had been closed by five respected users and the votes to reopen it were evenly split.  Although there is an emphasis on the output of a particular software program, questions about (1) how to interpret such output -- which is standard across most statistical software -- and (2) how to translate it into the model equation are frequently asked here on CV.  That makes this a useful thread that we should curate well and maintain not just for historical interest.
Comment: If you type `lm(y~x)$call` it tells you the formula is `y ~ x`. If you mean something different from that, you need to be more specific.
Answer: Consider this example:
set.seed(5)            # this line will allow you to run these commands on your
                       # own computer & get *exactly* the same output
x = rnorm(50)
y = rnorm(50)

fit = lm(y~x)
summary(fit)
# Call:
# lm(formula = y ~ x)
#
# Residuals:
#      Min       1Q   Median       3Q      Max
# -2.04003 -0.43414 -0.04609  0.50807  2.48728
#
# Coefficients:
#             Estimate Std. Error t value Pr(>|t|)
# (Intercept) -0.00761    0.11554  -0.066    0.948
# x            0.09156    0.10901   0.840    0.405
#
# Residual standard error: 0.8155 on 48 degrees of freedom
# Multiple R-squared: 0.01449,  Adjusted R-squared: -0.006046
# F-statistic: 0.7055 on 1 and 48 DF,  p-value: 0.4051

The question, I'm guessing, is how to figure out the regression equation from R's summary output.  Algebraically, the equation for a simple regression model is:
$$
\hat y_i = \hat\beta_0 + \hat\beta_1 x_i + \hat\varepsilon_i  \\
\text{where } \varepsilon\sim\mathcal N(0,~\hat\sigma^2)
$$
We just need to map the summary.lm() output to these terms.  To wit:

$\hat\beta_0$ is the Estimate value in the (Intercept) row (specifically, -0.00761)
$\hat\beta_1$ is the Estimate value in the x row (specifically, 0.09156)
$\hat\sigma$ is the Residual standard error (specifically, 0.8155)

Plugging these in above yields:
$$
\hat y_i = -0.00761~+~0.09156 x_i~+~\hat\varepsilon_i  \\
\text{where } \varepsilon\sim\mathcal N(0,~0.8155^2)
$$
For a more thorough overview, you may want to read this thread: Interpretation of R's lm() output.
Comment: Given the OP's mention of a wish to put equations on graphs, I've been pondering whether they actually want a function to take the output of `lm` and produce a character expression like "$\hat y=−0.00761 + 0.09156x$" suitable for such a plotting task (hence my repeated call to clarify what they wanted - which hasn't been done, unfortunately).
Answer: If what you want is to predict scores using your resulting regression equation, you can construct the equation by hand by typing summary(fit) (if your regression analysis is stored in a variable called fit, for example), and looking at the estimates for each coefficient included in your model.
For example, if you have a simple regression of the type $y=\beta_0+\beta_1x+\epsilon$, and you get an estimate of the intercept ($\beta_0$) of +0.5 and an estimate of the effect of x on y ($\beta_1$) of +1.6, you would predict an individual's y score from their x score by computing: $\hat{y}=0.5+1.6x$.
However, this is the hard route. R has a built-in function, predict(), which you can use to automatically compute predicted values given a model for any dataset. For example: predict(fit, newdata=data), if the x scores you want to use to predict y scores are stored in the variable data. (Note that in order to see the predicted scores for the sample on which your regression was performed, you can simply type fit$fitted or fitted(fit); these will give you the predicted, a.k.a. fitted, values.)
Answer: Building on @keithpjolley's answer, this replaces the '+' signs used in the separator with the actual sign of the co-efficient and replaces the 'y' with whatever the model's dependent variable actually is.
The function accepts arguments to 'format', such as 'digits' and 'trim'.
library(dplyr)

model_equation <- function(model, ...) {
  format_args <- list(...)

  model_coeff <- model$coefficients
  format_args$x <- abs(model$coefficients)
  model_coeff_sign <- sign(model_coeff)
  model_coeff_prefix <- case_when(model_coeff_sign == -1 ~ " - ",
                                  model_coeff_sign == 1 ~ " + ",
                                  model_coeff_sign == 0 ~ " + ")
  model_eqn <- paste(strsplit(as.character(model$call$formula), "~")[[2]], # 'y'
                     "=",
                     paste(if_else(model_coeff[1]<0, "- ", ""),
                           do.call(format, format_args)[1],
                           paste(model_coeff_prefix[-1],
                                 do.call(format, format_args)[-1],
                                 " * ",
                                 names(model_coeff[-1]),
                                 sep = "", collapse = ""),
                           sep = ""))
  return(model_eqn)
}

library(MASS)

modelcrime <- lm(y ~ ., data = UScrime)
model_equation(modelcrime, digits = 3, trim = TRUE)

produces the result
[1] "y = - 5984.288 + 8.783 * M - 3.803 * So + 18.832 * Ed + 19.280 * Po1 - 10.942 * Po2 - 0.664 * LF + 1.741 * M.F - 0.733 * Pop + 0.420 * NW - 5.827 * U1 + 16.780 * U2 + 0.962 * GDP + 7.067 * Ineq - 4855.266 * Prob - 3.479 * Time"

and
library(car)
state.x77=as.data.frame(state.x77)
model.x77 <- lm(Murder ~ ., data = state.x77)
model_equation(model.x77, digits = 2)

produces
[1] "Murder = 1.2e+02 + 1.9e-04 * Population - 1.6e-04 * Income + 1.4e+00 * Illiteracy - 1.7e+00 * Life.Exp + 3.2e-02 * HS.Grad - 1.3e-02 * Frost + 6.0e-06 * Area"

*** Edit

made explicit the requirement for 'dplyr'
functionalized the code
incorporated improvements found in rvezy's answer - 'flexible' y argument, use of 'format' arguments
Answer: If you want to show the equation, like to cut/paste into a doc, but don't want to fuss with putting the entire equation together:
R> library(MASS)
R> crime.lm <- lm(y~., UScrime)
R> cc <- crime.lm$coefficients
R> (eqn <- paste("Y =", paste(round(cc[1],2), paste(round(cc[-1],2), names(cc[-1]), sep=" * ", collapse=" + "), sep=" + "), "+ e"))
[1] "Y = -5984.29 + 8.78 * M + -3.8 * So + 18.83 * Ed + 19.28 * Po1 + -10.94 * Po2 + -0.66 * LF + 1.74 * M.F + -0.73 * Pop + 0.42 * NW + -5.83 * U1 + 16.78 * U2 + 0.96 * GDP + 7.07 * Ineq + -4855.27 * Prob + -3.48 * Time + e"

Edit for if the spaces and signs bother:
R> (eqn <- gsub('\\+ -', '- ', gsub(' \\* ', '*', eqn)))
[1] "Y = -5984.29 + 8.78*M - 3.8*So + 18.83*Ed + 19.28*Po1 - 10.94*Po2 - 0.66*LF + 1.74*M.F - 0.73*Pop + 0.42*NW - 5.83*U1 + 16.78*U2 + 0.96*GDP + 7.07*Ineq - 4855.27*Prob - 3.48*Time + e"

Answer: You can use the equatiomatic package to solve many challenges with extracting and reporting equations. https://github.com/datalorax/equatiomatic
Basic Example

Inserting Model Coefficients
You can also include the coefficients.


What does this group theory statement ask for?

Question: This is a question that was asked in my group theory examination today:
Let $G$ be a finite cyclic group generated by an element $x$ of $G$. If $y(\ne x)\in G$ is also a generator of $G$, find the relation between the elements $x$ and $y$.
I do not think that given an arbitrary finite cyclic group one can give a nice relation between any two of its generators. For example if $\mathbb{Z}/50\mathbb{Z}$ what is the relation between 7 and 49 or 23 and 31 or say 3 and 43? I have not been able to understand clearly what kind of a relation the question asks for.
I know that  $x=y^m$ for some $m$, and  $m$ is then coprime to the order of the group but I do not know how could this give a relation involving only $x$ and $y$.
So what is the question asking for and what in general is a relation between $x$ and $y$?
Answer: Well, there are some very basic relations between $\,x\,,\,y\,$, for example say $\,|G|=n\,$ , then:
$$\begin{align*}*&\;\;\;|x|=|y|\\**&\;\;\;y=x^k\,\,,\,\,x=y^s\;\;,\;\text{for some}\;\;k,s\in\Bbb Z\;\;\text{with}\;\;(n,k)=(n,s)=1\end{align*}$$
Comment: The first relation you give could be a correct answer I suppose since that is a relation between $x$ and $y$(and that is the only relation that I can think of). But I wrote the second one as an answer. Thanks for it anyways.
Comment: @A.Gupta: Was this a take-home examination?
Comment: @ user1729 -No, this was not a take home examination. Take home examinations are not as common(not in my college atleast) in India as in other places like US or UK(as far as I know).  But, this was an 'open-notes' exam although I didn't carry my notes to the exam hall. Have a nice day!
Comment: @A.Gupta: Sorry, it was just the wasy you said "...I wrote the second one as an answer" which made me wonder if you had the exam in front of you and were just copying the answer! (Which would be...wrong?)
Answer: Your answer sounds fairly reasonable. If $y$ is also a generator then the order of $y$ is the same as the order of $x$. Since $G$ is cyclic $y=x^k$ for some $k<|G|$ but $y^m=e$ since it generates $G$. This will indicate $k$ is relatively prime to $m$. Towards a contradiction suppose otherwise, suppose $m=kl$ then $y^l=(x^{k})^l=x^{kl}=x^m=e$ which says $|y|=l \neq m$. Hence $m$ and $k$ are relatively prime.
As an example, if $G = <c>$ for $|G|=10$ then $c,c^3,c^7,c^9$ are the possible generators. I would wager your professor was fishing for something like what you say in your post.
Comment: Thanks for the explanation. I also think something similar was asked for.


How do you simplify this expression?

Question: $$\lim_{h\to0}(\frac{x}{h(x+h+1)} + \frac{1}{x+h+1} - \frac{x}{h(x+1)})$$
I know the answer is $$\frac{1}{(1+x)^2}$$
But I can't get there
Answer: Take the lcm of the denominators and simplify: You will get
\begin{align}
\dfrac{x}{h(x+h+1)} + \dfrac{1}{x+h+1} - \dfrac{x}{h(x+1)} & = \dfrac{x(x+1) + h(x+1) - x(x+h+1)}{h(x+1)(x+h+1)}\\
& = \dfrac{x^2+x + hx + h - x^2 - hx - x}{h(x+1)(x+h+1)}\\
& = \dfrac{h}{h(x+1)(x+h+1)}\\
& = \dfrac1{(x+1)(x+h+1)}
\end{align}
Now let $h \to 0$.
Answer: Using fraction addition rules, we have
$$\lim_{h\to0}\left(\frac{x}{h(x+h+1)} + \frac{1}{x+h+1} - \frac{x}{h(x+1)}\right)\\
=\lim_{h\to0}\left(\frac{x(x+1)}{h(x+h+1)(x+1)} + \frac{h(x+1)}{h(x+h+1)(x+1)} - \frac{x(x+h+1)}{h(x+1)(x+h+1)}\right)\\
=\lim_{h\to0}\frac{(x+h)(x+1)-x(x+h+1)}{h(x+1)(x+h+1)}\\
=\lim_{h\to0}\frac{x^2+x+hx+h-x^2-xh-x}{h(x+1)(x+h+1)}\\
=\lim_{h\to0}\frac h{h(x+1)(x+h+1)}=\frac 1{(x+1)^2}$$

How do you simplify this expression?


Automated snapshots of EBS-backed EC2 instance running Ubuntu

Question: I'm running an EBS-backed instance which acts as a software development team's build server (running Jenkins and host of other services). The server is running Linux (latest Ubuntu from the official AMIs).
I'd like to take regular, automated snapshots of the instance's associated EBS volume. I only need to keep one latest backup (i.e. old snapshots should be pruned), and a good frequency would be once a day.
It seems that Amazon does not provide such backup service out of the box, so you have to either go with 3rd party scripts or roll your own solution.
My question is, what is the simplest way to achieve this? I'd like a minimal amount of hassle, configuration, and external dependencies. Setting this up as some kind of timed script on the Linux box itself is, to my knowledge, a valid option.
Comment: I have created this script and good explanation how to setup. Read here http://serverfault.com/questions/237246/automate-amazon-ebs-snapshot-creation
Answer: Based on Jonik's concept, I created a python script using boto. You provide it a list of volumes to snapshot, and how many trailing snapshots to keep for each volume:
# Define the snapshots manage. We'll snapshot the specified volume ID, and only keep the X newest ones.
snapshots = [("vol-XXXXXXXX", 30), ("vol-YYYYYYYY", 180)]

import boto.ec2
auth = {"aws_access_key_id": "YOURACCESSKEY", "aws_secret_access_key": "YOURSECRETKEY"}
ec2 = boto.ec2.connect_to_region("YOURREGIONNAME", **auth)
description = "automated backup"
for volume, num_trailing in snapshots:
  snaps = ec2.get_all_snapshots(filters={"volume-id": volume, "description": description})
  print "%s: Creating new snapshot. %s automated snapshots currently exist." % (volume, len(snaps))
  ec2.create_snapshot(volume, description)
  purgeable = sorted(snaps, key=lambda x: x.start_time)[:-num_trailing]
  print "Deleting snapshots for %s > %s: %s" % (volume, num_trailing, purgeable)
  for snap in purgeable:
    ec2.delete_snapshot(snap.id)

I set this up as Jenkins job (via the Python plugin), configured to run daily. If you are using IAM to manage credentials, note that this will require in ec2 policies: DescribeRegions, DescribeVolumes, CreateSnapshot, DeleteSnapshot, DescribeSnapshots, CreateTags (because of boto's implementation).
Comment: Leeeeeerooooooooyyyyyyy
Answer: Okay, for what it's worth, here's what I did. I hope my feeble scripts encourage people to post better solutions!
I wrote two simple bash scripts and automated them using cron. (For now I run these on a local server, as I think (?) it's not recommended to put AWS's certificates in the instances/AMIs/EBSs themselves.)
To create a new snapshot:
# ESB volume associated with the instance we want to back up:
EBS_VOL_ID=vol-xxxxyyyy

ec2-create-snapshot --region eu-west-1 -K pk.pem -C cert.pem -d "Automated backup" $EBS_VOL_ID

To prune all except latest snapshot:
EBS_VOL_ID=vol-xxxxyyyy

ec2-describe-snapshots --region eu-west-1 -K pk.pem -C cert.pem  | grep "Automated backup" | grep "$EBS_VOL_ID" | awk '{ print $5 "\t" $2 }' | sort > .snapshots

latest_id=$(tail -n1 .snapshots | awk '{ print $2 }')

cat .snapshots | awk '{ print $2 }' > .snapshot_ids
for i in $(cat .snapshot_ids)
do
    if [ "$i" != "$latest_id" ]
    then
        echo "Deleting snapshot $i"
        ec2-delete-snapshot --region eu-west-1 -K pk.pem -C cert.pem $i
    fi
done

(This parses appropriate snapshot information from ec2-describe-snapshots output and creates a temp file with [timestamp tab snapshot-id] entries (e.g.
2011-06-01T10:24:36+0000   snap-60507609) where the newest snapshot is on the last line.)
Notes:

Put your X509 certificate and private key in some place where the scripts can find them.
You must explicitly specify --region with all commands. Otherwise e.g. ec2-create-snapshot would fail with volume ID being unknown. (YMMV if you use the default region "us-east-1".)
I used a snapshot description ("Automated backup") as a marker to avoid the prune script deleting other snapshots of the volume in question (e.g. snapshots related to AMIs).

Disclaimer: This became partly an exercise in Bash/Unix programming for me, especially the prune script. I readily admit you'd most likely get a much clearer result with e.g. Python, when you need logic like "do something for all but the last item in a list". And even with Bash you could probably do this more elegantly (for instance, you don't really need temp files). So, please feel free to post other solutions!
Comment: Of course, there are probably scenarios where the approach of keeping only **one** backup would fail miserably...
Answer: If you're open to external utilities, check out Skeddly.
Disclosure: I'm the CEO of Eleven41 Software, the company behind Skeddly.
Comment: Please take care when promoting your product, see the respective FAQ [May I promote products or websites I am affiliated with here?](http://serverfault.com/faq#promotion) for details.
Answer: I expanded on the idea of Jonik's script to allow multiple snapshots to be retained. The code is too long to fit in a comment so I'm adding a new answer. This code assumes all the right environment variables have been set up for the CLI tools. Also, this defaults to taking a snapshot of the current instance.
# Look up our instance ID using the magic URL
INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)

# The number of previous backups we want to keep
N=3

# get list of locally attached volumes via EC2 API:
VOLUME_LIST=$(ec2-describe-volumes | grep ${INSTANCE_ID} | awk '{ print $2 }')
DATE=$(date '+%Y-%m-%d-%H%M%S')

sync

# actually creating the snapshots
for VOLUME in $(echo $VOLUME_LIST); do
    echo "Processing volume $VOLUME"
    SNAPSHOT_LIST=$(ec2-describe-snapshots | grep completed | grep "Automatic snapshot" | grep $VOLUME | awk '{print $5 "\t" $2}' | sort | head "--lines=-$N" | awk '{print $2}')
    ec2-create-snapshot $VOLUME -d "Automatic snapshot on $DATE"
    for SNAPSHOT in $(echo $SNAPSHOT_LIST); do
        ec2-delete-snapshot $SNAPSHOT
    done
done

Answer: I have written a script in PHP that will automate EBS Snapshots and delete old ones.  It will even email the results of the snapshots to you.  You must configure the AWS PHP SDK and PHPMailer for email functionality, but both of these steps are pretty easy.  Then you just run the script every night with CRON or Windows Scheduled Tasks.  Detailed instructions and code can be found on my blog:
http://www.caleblloyd.com/software/automatically-take-ebs-snapshots-and-delete-old-ones-with-php-script/
Comment: Whilst this may theoretically answer the question, [it would be preferable](http://meta.stackexchange.com/q/8259) to include the essential parts of the answer here, and provide the link for reference.


What is the probability of getting any two numbers (the order is not important) in 4 rolls?

Question: A random number generator will generate a number randomly from 1 to 18 and remove the number generated every time from the pool of availble numbers to generate $(1-18)$. So, for example, if it rolls 1 on the first run then it will generate numbers from $(2-18)$, if it gets a 5 on the second run it will generate numbers from $(2-18)-(5)$, etc.
What is the probability of getting any two  numbers we choose (order wont matter) in 4 rolls ?
What is the probability of getting any three numbers we choose (order wont matter) in 4 rolls ?
From lurking around this forum i found this might be the probability  of getting any one number i choose in 4 rolls: $1-((17/18)^4)+4*((17/18)^3)*1/18=0,3915$.
I don't know if this is correct and im a bit at a loss on how to continue for the "any two numbers", maybe multiply it by the probability of getting any other number in another roll: $(1-((17/18)^4)+4*((17/18)^3)*1/18)*(1-((16/17)^4)+4*((16/17)^3)*1/17)=0,1611$ the number feels fine.
But i just don't know.
Thanks.
PS: But really, i arrived here because i was just wondering what is the probability of getting any set of 1, 2, 3 or 4 civilizations in your team when playing age of empires 2, 4vs4 random civs, it's not a matter of life or death, i guess i must have aspergers.
Comment: Do you have any thoughts on this yourself?
Answer: There are $\binom{18}{2}$ pairs we could choose. Each set of four contains $\binom{4}{2}$ pairs. The probability we get a particular pair is $\dfrac{\binom{4}{2}}{\binom{18}{2}}$.
The same would apply to whatever set sizes and pool sizes you're using.


Porting old CodeWarrior Project to Xcode

Question: I have a very old command line utility in need of updating. Its project file is a CodeWarrior <code>.mcp</code>. Xcode used to be able to import CodeWarrior projects but that ability seems to have been removed in more recent versions.
What is the best way to import old CodeWarrior projects in Xcode?
Answer: I'm not sure when Xcode quit supporting CodeWarrior imports, but CodeWarrior quit shipping for the Mac around 2005 or 2006. So if you've got some old PowerPC equipment and you're a registered Mac developer, you could...

Get copies of Mac OS X and Xcode from that era from developer.apple.com.
Load them up on your PowerPC Mac, import the CodeWarrior project, and fix any problems.
Get newer and newer versions of Mac OS X and Xcode and bring the code forward, fixing any problems.

However, having done things like this in the past, I would not recommend it unless you absolutely must - for instance, if you have to do regression testing or bug fixes against the original code.
Instead, I would just create a new Xcode project using the command-line utility template, add the source files and forget about the .mcp project, and fix any problems. It will be far less fooling around.


Symbolic form of "Only boys can swim."?

Question: I considered–
p = The person is a boy.
q = The person can swim.
...But confused with following statement–

If person can swim, he is boy. (p$\rightarrow$q)
Comment: The logical form of the statement is better expressed with [predicate calculus](https://en.wikipedia.org/wiki/Monadic_predicate_calculus): $\forall x \ (\text {Swim}(x) \to \text {Boy}(x))$.
Comment: What is it that confused you? How the logic statement is translated into "If a person can swim, he is a boy"? How " If a person is a boy" is equivalent to "Only boys can swim"? Something else?
Comment: Do you mean $q \implies p$? $p \implies q$ means "if he is a boy, he can swim".
Comment: "Only boys can swim" entails that the set of all people of can swim is entirely contained within the set of boys: everyone who can swim is a boy. Thus, if a person can swim, you can conclude they are a boy
Answer: Note that your translation of $p \rightarrow q$ as 'If a person can swiml, then the person is a boy' is inconsistent with your key. ... This is actually one good reason to use letters that are more descriptive of what statements they represent. So, I propose to use the following key:
$b$ the person is a boy
$s$ the person can swim
( I agree with Mauro from the comments that ideally you use predicates and quantifiers to deal with this statement, but you're probably still working with propositional logic at the moment, so I'll stick to that)
Now, how to translate 'only boys can swim'?
One way to think about this is that 'only boys can swim' means that any person that is not a boy cannot swim, which translates as:
$$\neg b \rightarrow \neg s$$
and by contraposition that is equivalent to:
$$s \rightarrow b$$
If you are thinking of using:
$$b \rightarrow s$$
please know that this effectively says that all boys can swim. But we don't know that!  Consider a situation where some of the boys can swim, some of the boys cannot swim, and where any person that is not a buy cannot swim. In that situation, it is true that 'only boys can swim', but it is not true that all boys can swim. And so no, you really do not want to use $b \rightarrow s$.
Answer: I think you can define
$P$ = the set of all people can swim.
and
$Q$ = the set of all boy.
Then you can write $p \in Q\rightarrow p \in P$.

Symbolic form of "Only boys can swim."?


How can I use "drush updb" to pass a value to the hook_update_N() implementation done from a module?

Question: I am trying to drush updb with an argument that is passed to the hook_update_N() implementation done from a module I wrote, which would use it to execute some update tasks.
function my_module_update_9001(&$value) {
  switch ($value) {
    case "unique":
      _set_unique_identifier();
      break;

    case "get":
      _get_field_typ_map();
      break;

    default:
      echo "Something went wrong...";
  }
}

function _set_unique_identifier() {
  _get_database_shame_connector()->uniqueIdentifier();
}

function _get_field_typ_map() {
  _get_database_shame_connector()->getFieldTypeMap();
}

How do I tell drush updb to pass an argument to an hook_update_N()  implementation?
Comment: Update hooks take $sandbox argument which contains information about the running batch. https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Extension%21module.api.php/function/hook_update_N/9.3.x - i.e. not a callable function you can invoke whenever, and will only run once. updb is not for this type of data manipulation. You need to write an actual Drush command that accepts arguments to build this out.
Comment: Agreed. As written, this is impossible.
Comment: Not really clear what you're trying to do/want to do.
Comment: It's also not possible because `drush updb` doesn't accept such parameter; it only accepts four options.
Comment: Updb is the wrong direction. Create your own custom Drush command.
Comment: But with an env var, maybe...
Answer: It's not possible because drush updb doesn't accept a parameter that is passed to hook_update_N() implementations, which makes sense, since the only parameter accepted from hook_update_N() is an array that allows that hook to use batch operations to run its updates. Passing a different argument to hook_update_N() implementations would cause issues with those implementations that use the batch API to run updates.
Even in the case drush updb would accept an argument to pass to an update hook and the function name for a hook_update_N() implementation, that implementation should be able to understand whether it's called by Drush with a different argument or by Drupal core with the expected argument.
There is a simpler way to execute code that runs after drush updb:  Implement a post-command hook, as described on How to invoke a post drush command? for example.
namespace Drush\Commands;

class PostUpdateCommands extends DrushCommands {

  /**
   * Executes commands right after `drush updatedb`.
   *
   * @hook post-command updatedb
   *
   */
  public function postUpdate() {
    // Your code
  }

}


JOIN between tables

Question: I have a little trouble with a query.
I have this DB structure
PROCESSES
process_id
process_name
ITENS
item_id
item_process_id
item_name
FILES
file_id
file_item_id
file_name
I run this query to obtain Process ID:
<code>$result = mysql_query("SELECT * FROM processes WHERE process_id='$process_id'");
$row = mysql_fetch_array($result, MYSQL_BOTH);
$process_id = $row['process_id'];
</code>
Put this ID in a LOOP to obtain all Itens from table:
<code>$query="SELECT * FROM itens WHERE item_process_id='$process_id'";
$result = mysql_query($query) or die(mysql_error());
$num = mysql_numrows($result);
</code>
And call Item name into a LOOP
Problem is when i try to call "file_name" from FILES table.
I have tried with a LEFT JOIN to table but with no success.
"file_name" variable is blank
Can anyone help me to understand how solve this?
TKS
Comment: what does your query to `files` look like? Are you sure there is data?
Comment: yes, i have data on FILES table
Comment: `"SELECT * FROM itens LEFT JOIN FILES ON FILES.item_id = itens.item_id WHERE itens.item_process_id='$process_id'"`. Remember that LEFT JOIN retrieve ALL result of the first table (itens) and join it to the table FILES and fill with NULL value on the FILES side if there is NO coressponding entry. That's may be why you could have empty value (if you don't have file for some iten)
Comment: @user2112020 post your files query, you probably have a join wrong
Comment: Oh My, Worked Now... I Dont't Understand, maybe I have worked a Lot for today...

I have added a simply JOIN.

TKS for ALL
Comment: Take care of INNER JOIN that could hide you some result from itens if there is no corresponding file. good luck
Comment: Here Working Query:
$query="SELECT * FROM itens
LEFT JOIN files ON file_id = file_item_id
WHERE process_id='$process_id'";
Answer: Try LEFT JOIN files ON file_id = file_item_id

JOIN between tables


Where does $\pi^2$ appear spontaneously within Physical Phenomenon and Mathematics Equations?

Question: The term $\pi$ is found to appear in many equations and natural phenomenon; however my question is related to $\pi^2$.
While trying to figure out the reason for some $\pi^2$ terms appearing in certain equalities that I came across, I have a question. And the question is this:

In which all mathematics/physics equation or contexts does $\pi^2$ appear inherently?

-- and (now this second part is merely a follow up question that did not form part of the original query but added later)  where that $\pi^2$ term can lend some interpretation of the underlying phenomenon, just like does $\pi$ whereby we can interpret (in most cases i.e.) that some type of circular ambulation in 1 dimension is involved??
As you can understand, the $\pi^2$ term is more complex and does not directly lend itself to an interpretation -- as opposed to $\pi$ which is very intuitive.
Thanks
Comment: volume of a $4$-ball?
Comment: [Basel problem](https://en.wikipedia.org/wiki/Basel_problem)
Comment: The volume of a torus.
Comment: @Kumar The question is still ambiguous unless you define what you mean by naturally as it may be interpreted as the physical/natural world. I suggest you re-title   it to something like - Where does $\pi^2$ appear in mathematics or physics?
Comment: I don't know what you mean, Kumar, by "spontaneously", or by "inherently". Also, $\pi$ appears in a great many places with nothing to do with circles.
Answer: $$
g \approx \pi^2\,\mathrm{m/s^2}
$$
The reason for this the original definition of the meter: the length of a pendulum whose half-period is 1 second. Much like the original definition of the Celsius scale, this allowed a person to easily calibrate equipment with common materials. (Of course, this calibration is hardly precise enough for modern measurements, so the meter has been redefined more than once to keep up with the times.) The $\pi^2$ then comes from the small-angle approximation to the period of a pendulum:
$$
T = 2\pi\sqrt{\frac{L}{g}}\;\; \Longrightarrow\;\; g = \pi^2 \frac{L}{(T/2)^2}
$$
Comment: Interesting.  The value of gravitational acceleration we use today implies the meter has grown by a few tenths of a percent.
Comment: I thought the meter was originally chosen to be a convenient fraction of the distance from the equator to the north pole (but using an inaccurate geoid).
Answer: A great example in my opinion is the Basel problem
$$
\sum_{k = 1}^{\infty} \frac{1}{k^2}
= \frac{\pi^2}{6}.
$$
A great intuitive and geometric explanation can be found in this video, which, along with the whole channel, I cannot recommend enough.
There's also a whole Stack Exchange post dedicated to it.
Answer: You can get arbitrarily-high powers of $\pi$ (though divided by factorials) using involutes:

A semicircle of radius $1$ has length $\pi$.
The involute of the semicircle has length $\tfrac12\pi^2$
The involute of that involute (emerging from the same point) has length $\tfrac1{6}\pi^3$.
The involute of that involute (emerging from the same point) has length $\tfrac1{24}\pi^4$.
The involute of that involute (emerging from the same point) has length $\tfrac1{120}\pi^5$.
... and so forth ...

Note: The polygonal spiral formed by joining the involutes' non-common endpoints has edge-lengths equal to those powers of $\pi$. (The right-most vertical edge has length $\pi$, the top-most horizontal edge has length $\tfrac12\pi^2$, etc.) Throw in a segment from the semicircle's right endpoint to its center, and you have an edge of length $1 = \tfrac{1}{1}\pi^0$.
Note: That polygonal spiral converges on the involutes' common endpoint. The back-and-forth-ing of the horizontal edges implies that
$$\tfrac11\pi^0 - \tfrac12\pi^2 + \tfrac1{24}\pi^4 - \cdots \;=\; -1$$
Likewise, the up-and-down-ing of the vertical edges implies
$$\tfrac11\pi^1 - \tfrac16\pi^3 + \tfrac1{120}\pi^5 - \cdots \;=\; 0$$
Of course, these values are, respectively, $\cos\pi$ and $\sin\pi$. The above is a special case of Chaikovsky's Involute Pinwheel for the power series of cosine and sine.
Answer: If you're looking for examples of $\pi^2$ occurring naturally, you should look at some of the common PDEs. The derivation of their solutions and solutions themselves lead to occurrences of $\pi^2$. I'll provide some examples, but I'm going to keep everything a bit loose to make it more digestible.
First, take the heat equation on a 1-D rod of length $L$ with no heat on the ends: $$\begin{cases}
\partial_t u=\kappa\partial_x^2 u\\
u(x,0)=f(x)\\
u(0,t)=u(L,t)=0
\end{cases}$$ If you assume a solution of the form $u(x,t)=X(x)T(t),$ then you will arrive at two ODEs to solve: $$\frac{d^2X}{dx^2}=-\lambda X$$ and $$\frac{dT}{dt}=-\kappa\lambda T,$$ with $X(0)=0$ and $X(L)=0$. The former is an eigenvalue problem with given boundary data, and solving this eigenvalue problem gives you $\pi^2$ presence, as we find the eigenvalues to be $$\lambda_n=\left(\frac{n\pi }{L}\right)^2,$$ and $$X_n(x)=\sin\left(\frac{n\pi x}{L}\right),$$ for $n=1,2,\cdots.$ Solving the $T$ equation gives you $$T(t)=ce^{-\kappa\lambda_n t},$$ and in the end, you get the solution $$u(x,t)=\sum\limits_{n=1}^\infty B_n\sin \left(\frac{n\pi x}{L}\right) e^{-\kappa\frac{n^2\pi^2}{L^2} t},$$ with $$B_n=\frac{2}{L}\int\limits_0^L f(x)\sin\left(\frac{n\pi x}{L}\right)\, dx.$$ This even has $\pi^2$ in the solution itself. Notice that, amongst other things, $\pi^2$ pops up in the decay rate of the solution to its steady state.
If we look at the wave equation $$\partial_t^2u=c^2\partial_x^2 u,$$ instead, then you will still get the same eigenvalues and eigenfunctions (since they are all eigenvalues/eigenfunctions of $\partial_x^2$). If we add in the additional initial condition $\partial_t u(x,0)=g(x),$ then we instead get the final solution $$u(x,t)=\sum\limits_{n=1}^\infty\left(A_n\cos\left(\frac{n\pi ct}{L}\right)+B_n\sin \left(\frac{n\pi ct}{L}\right)\right) \sin \left(\frac{n\pi x}{L}\right), $$ where $A_n$ is the same and $$B_n=\frac{2}{n\pi c}\int\limits_0^Lg(x)\sin \left(\frac{n\pi x}{L}\right)\, dx.$$ Here, the eigenvalues are related to the frequencies of the waves, so they have a nice physical interpretation.
Comment: Second one also, needs exponentials.
Comment: @SungjinKim Performing separation of variables, you get ODEs $X''+\lambda X=0$ and $T''+\lambda c^2 T=0.$ I wrote the solution to time ODE using $\sin$ and $\cos$ instead of complex exponentials. It's a pretty standard representation of the solution.
Comment: I see . Thank you for the clarification.
Answer: (1) Let $\mu$ denote the Mobius function:
$$ \sum_{n=1}^{\infty} \frac{\mu(n)}{n^{2}} = \frac{6}{\pi^{2}}$$
(2) Let $\sigma(n)$ denote the sum of the divisors of $n$:
$$ \lim_{n \to \infty} \frac{\sum_{i=1}^{n}\sigma(i)}{n^{2}} = \lim_{n \to \infty} \frac{\sigma(1) + \sigma(2) + \ldots + \sigma(n)}{n^{2}} = \frac{\pi^{2}}{12} $$
(3) Let $\phi$ denote Euler's $\phi$-function.
$$ \lim_{n \to \infty} \frac{\sum_{i=1}^{n}\phi(i)}{n^{2}} = \lim_{n \to \infty} \frac{\phi(1) + \phi(2) + \ldots + \phi(n)}{n^{2}} = \frac{3}{\pi^{2}} $$
Comment: "In which all mathematics/physics equation or contexts does π^{2} appear *naturally*?"
Comment: @Nil, what makes you so sure you know what OP meant?
Comment: @Nil, the keywords for me are "many equations" and "mathematics". Unless OP chooses to clarify, you have no better idea of what was intended than I do, so you may as well enjoy all the nice answers that have been posted.
Comment: OP asked about examples where $\pi^2$ occurred naturally. Your examples are not applicable unless you tell where they occur in nature.
Comment: By that statement statement OP meant which all equations in the mathematical formulas or derivations of known physics do we use $\pi^2$. I guess what you interpreted is - which all statements in mathematics or in physics do we  use $\pi^2$. These are two different questions. For example, there are parametric equations which the value of $\pi^2$ for infinite inputs. So if all you wanted was $\pi^2$, we can get infinitely many such equations or formulas. So the  physical context of mathematics in important here. Just see why other answers are focusing on physical use of $\pi^2$
Comment: @GerryMyerson OP said "The term π is found to appear in many equations and natural phenomenon". OP also said "In which all mathematics/physics equation or contexts does $\pi^2$ appear naturally?" The keywords for me here are "natural phenomenon" and "naturally"
Comment: @GerryMyerson Let be more picky: One natural phenomenon, one naturally and one physics makes it 3 key words vs one mathematics. Equations belong to both math and physics so not adding it to either. That's 3:1 hehe :)
Answer: Usually $(2 \pi)^{-n}$ appears in the $n$ dimensional Fourier transform: If the Fourier transform of a $L^1(\mathbb{R}^n)$ is defined as
$$\hat{f}(k)=\int_{\mathbb{R}^n} e^{-i k \cdot x} f(x) \,\mathrm{d}^n x$$
Then the inverse transform will be (if the integral is convergent)
$$f(x)= \frac{1}{(2 \pi)^n}\int_{\mathbb{R}^n} e^{i k \cdot x} \hat{f}(k) \,\mathrm{d}^n k$$
But you can also "spread" it to be more symmetric: if
$$\hat{f}(k)= \frac{1}{(2 \pi)^{n/2}}\int_{\mathbb{R}^n} e^{-i k \cdot x} f(x) \,\mathrm{d}^n x$$
then
$$f(x)= \frac{1}{(2 \pi)^{n/2}}\int_{\mathbb{R}^n} e^{i k \cdot x} \hat{f}(k) \,\mathrm{d}^n k$$
Answer: Also related to the Basel problem: the probability that two random integers are coprime is $$\frac{6}{\pi^2}$$
(in the sense that the probability that $a,b$ chosen uniformly from $\{1, 2, \ldots, N\}$ are coprime approaches $\frac{6}{\pi^2}$ as $N \rightarrow \infty$.)
Answer: Euler's solution of Basel problem:
Many of the answers posted here talk about Euler's solution of the Basel problem. But an important thing that is notably missing is that Euler's original solution to the Basel problem already had $\pi^2$ in it. Euler used the well know trigonometric identity
$$
\frac{\sin x}{x} = \prod_{n \ge 1} \bigg(1 - \frac{x^2}{\pi^2 n^2}\bigg)
$$
Ramanujan's first letter to Hardy:
This one is a part of mathematical folklore, appearing Ramanujan's first letter to Hardy and one of few that Hardy saild he could prove himself out of the many formulas in the letter. If $\alpha \beta = \pi^2$ then
$$
\alpha^{-1/4}\bigg(1 + 4\alpha\int_{0}^{\infty} \frac{xe^{-\alpha x^2}}{e^{2\pi x}-1}dx\bigg)
= \beta^{-1/4}\bigg(1 + 4\beta\int_{0}^{\infty} \frac{xe^{-\beta x^2}}{e^{2\pi x}-1}dx\bigg)
$$
Number of square free integers:
The number of square free integers $\le x$ is
$$\dfrac{6x}{\pi^2} + O(\sqrt{x})$$
Probability of coprime numbers:
The probability that $n$ positive integers chosen uniformly from $\{1, 2, \ldots, n\}$ have no common factors approaches
$$
\frac{1}{\zeta(2n)} = \frac{(-1)^{n+1}(2\pi)^{2n}B_{2n}}{2(2n)!}
$$
as $n \rightarrow \infty$, where $B_k$ is the $k$-th Bernoulli number.
Others:
$$
\sum_{n = 1}^{\infty}\frac{F_{2n}}{ n^2 {2n \choose n}} = \frac{4\pi^2}{25\sqrt 5}, \text{where $F_n$ is the $n$-th Fibonacci number}
$$
$$
\lim_{n \to \infty}\frac{1}{n^2} \sum_{r = 1}^n n(\textrm{mod } r) = 1 - \frac{\pi^2}{12}
$$
$$
\int_{0}^{1} \bigg(\frac{\tan^{-1}x}{x}\bigg)^2 dx = G - \frac{\pi^2}{16} + \frac{\pi \log 2}{4}, \text{ where $G$ is the Calatan constant}
$$
Answer: List of Places where π^2 can be seen-

π is present in some structural engineering formulae, such as the buckling formula derived by Euler, which gives the maximum axial load F that a long, slender column of length L, modulus of elasticity E, and area moment of inertia I can carry without buckling
The fact that π is approximately equal to 3 plays a role in the relatively long lifetime of orthopositronium. The inverse lifetime to lowest order in the fine-structure constant α has a term of π^2
Kepler's Third Law of Planetary Motion
Volume and Bounding Area of 4-D and 5-D Sphere
Basel Problem (As mentioned in another answer)
And many more as well

Sources-
https://en.m.wikipedia.org/wiki/Pi
https://en.m.wikipedia.org/wiki/Basel_problem
https://en.m.wikipedia.org/wiki/Buckling
https://en.m.wikipedia.org/wiki/Fine-structure_constant
https://en.m.wikipedia.org/wiki/Kepler's_laws_of_planetary_motion
https://en.m.wikipedia.org/wiki/N-sphere
Answer: The volume of a torus which consists of a tube with radius $r$ centered at a circle with radius $R$ is $4\pi^2Rr$.
Answer: How about an even higher power of $\pi$ in the Stefan-Boltzmann Law relating radiant energy flux from a black body to temperature?  This relation involves the fifth power of $\pi$:
$\text{Radiant flux}=((2\color{blue}{\pi^5}k^4)/(15c^2h^3))T^4$
The parameters $k, c, h$ are respectively Boltzmann's constant, the speed of light and Planck's constant from physics.  $T$ is absolute temperature.
Answer: The number of three-term geometric progressions of positive integers with no term exceeding $n$ is $${6\over\pi^2}n\log n+O(n)$$.
Answer: The $\text{Riemann Zeta function} \ \  \zeta(s)=\large \sum_{n=1}^{\infty} \frac{1}{n^s}$ is used in many branch of Science and mathematics. Replacing $s=2$, we have $ \zeta(2)=\sum \frac{1}{n^2}$. In $1735$ Leonard Euler showed that $$ \zeta(2)=\sum_{n=1}^{\infty} \frac{1}{n^2}=\frac{{\color{blue}{\pi^2}}}{6}.$$
This result leads to Number Theory and probability result as follows:
The $ \ {\color{blue}{ probability}} \ $ of two random number being $ \ {\color{blue}{ relatively \ \ prime}} \ $ is given by the following product over all primes
$$ \prod_{p}^{\infty} \left(1-\frac{1}{p^2} \right)=\left(\prod_{p}^{\infty}\frac{1}{1-p^{-2}} \right)^{-1}=\frac{1}{1+\frac{1}{2^2}+\frac{1}{3^2}+\cdots}=\frac{1}{\zeta(2)}=\frac{6}{{\color{blue}{\pi^2}}} \approx 61 \%.$$
This is an interesting result where $\pi^2$ is involved.
Comment: Both of these results are already mentioned in other answers, right?
Comment: @GerryMyerson, who mentioned the last and in fact main part of  my answer? I portrayed here that $\pi^2$ is used to mention the probability of two random number being relatively prime. I don't see anyone used in this question
Comment: https://math.stackexchange.com/questions/3298039/where-does-pi2-appear-spontaneously-within-physical-phenomenon-and-mathemati/3298242#3298242
Comment: @GerryMyerson, both answers have big difference in presentation, ok
Answer: Here is perhaps the newest result on $\pi^2$, proved only a few hours ago by Sungjin Kim in while working on one of our collaboration. Perhaps this is the fastest case of discovery and reference.
For any $\epsilon > 0$, the value of
$$
\frac{\sqrt{2n}}{2^n } \bigg\{{n\choose 1^2} + {n\choose 2^2} + {n\choose 3^2} + \cdots + {n\choose r^2}\bigg\}
$$
is more likely to be in the interval is $1 + 2e^{-\pi^2/4} \pm \epsilon$ than any other interval of the same width.
Comment: So perhaps you could apologize to those you criticized for doing what you are now doing yourself.
Comment: You wrote to another user, "Your examples are not applicable unless you tell where they occur in nature." That's not just "raising a valid point on the definition" – that's telling another user (and everyone reading it) that the user's answer is wrong. I reckon that deserves an apology. "not answering it before it was clarified" – but I think the timeline will show OP's clarification, such as it is, came well after you posted your answers.
Comment: What does "the most likely value" mean? I think that expression can't ever take on that value, so I'd say it's a *least* likely value. Also, is $r$ related to $n$ in any way? And aren't you the one who was complaining about answers not found in nature???
Comment: @GerryMyerson First part: Upper boundary of the class interval containing the mode. Second part: Yes I was. OP changed the title. But even in the re-title he did not define what he meant by naturally. So I notified him again.
Comment: So far as I can see, OP has never changed the title. What's a "class interval", please?
Comment: First part: OP deleted parts of the question saying he will post in a separate post to differentaite between this post and the new one. Second part: In the graph here https://math.stackexchange.com/questions/3287643/strange-shape-of-the-distribution-of-the-sum-of-the-binomial-coefficients-n-ch there are two spikes roughly at about $1 \pm 1/6$. The exact location of the spike turns out to be $1 \pm 2e^{-\pi^2/4}$.
Comment: "OP deleted parts of the question...." But OP didn't change the title. So, you are still doing what you have criticized others for doing. And you haven't told me what a "class interval" is. And why $1+2e^{-\pi^2/4}$ in the body, but $1\pm2e^{-\pi^2/4}$ in the comment?
Comment: First part: Yes exactly. Now I can do it because OP clearly did not demarcate between pure math and physical application. Only after that clarification I posted my answers, not before that so I am justified in posting my answers after that point. If he had not done that, I would have still taken natural phenomenon to imply real world or physical applications and so I would have still criticized non-physical answers. Second part: That's because the height of the left spike will be slightly less than the height of the right spike at $a = 1$ in the graph.
Comment: No, I raised a valid point on the definition of 'natural phenomenon'. Before that, everyone interpreted the question in their own way and answered it without clarifying the OP's which left room or ambiguity. So I don't think I owe an apology for following a better process. I should rather  be appreciated for raising it, getting it clarified and not answering it before it was clarified even though I had more than one answer form pure math.