(Inspirational speech by Carl Sagan – the Pale Blue Dot)
Look again at that dot. That’s here. That’s home. That’s us. On it everyone you love, everyone you know, everyone you ever heard of, every human being who ever was, lived out their lives. The aggregate of our joy and suffering, thousands of confident religions, ideologies, and economic doctrines, every hunter and forager, every hero and coward, every creator and destroyer of civilization, every king and peasant, every young couple in love, every mother and father, hopeful child, inventor and explorer, every teacher of morals, every corrupt politician, every “superstar,” every “supreme leader,” every saint and sinner in the history of our species lived there–on a mote of dust suspended in a sunbeam.
The Earth is a very small stage in a vast cosmic arena. Think of the rivers of blood spilled by all those generals and emperors so that, in glory and triumph, they could become the momentary masters of a fraction of a dot. Think of the endless cruelties visited by the inhabitants of one corner of this pixel on the scarcely distinguishable inhabitants of some other corner, how frequent their misunderstandings, how eager they are to kill one another, how fervent their hatreds.
Our posturings, our imagined self-importance, the delusion that we have some privileged position in the Universe, are challenged by this point of pale light. Our planet is a lonely speck in the great enveloping cosmic dark. In our obscurity, in all this vastness, there is no hint that help will come from elsewhere to save us from ourselves.
The Earth is the only world known so far to harbor life. There is nowhere else, at least in the near future, to which our species could migrate. Visit, yes. Settle, not yet. Like it or not, for the moment the Earth is where we make our stand.
It has been said that astronomy is a humbling and character-building experience. There is perhaps no better demonstration of the folly of human conceits than this distant image of our tiny world. To me, it underscores our responsibility to deal more kindly with one another, and to preserve and cherish the pale blue dot, the only home we’ve ever known.
— Carl Sagan, Pale Blue Dot, 1994
(Keeping notes here for ease of future references…)
10 x 3 array of random numbers (in range
[0,1]). For each row, pick the number closest to
argsortto find the column j closest for each row.
- Use fancy indexing to extract the numbers. (Hint:
a[i,j]– the array
imust contain the row numbers corresponding to stuff in
First version code to illustrate how things work:
Now we know how things work, let’s compact the solution (optional).
Major Learning Summary
This NumPy Array Broadcasting example is inspired by this SciPy Lecture Chapter on Array Broadcasting.
Note the use of
Note also that this example might be made much simplier with
np.mgrid. (See the SciPy-Lectures chapter on broadcasting)
Two really handy IPython Jupyter Notebook Tricks – Auto Module Reload and Inline Chart Plotting.
Auto Module Reload
Say in our main script we import an external module (that we own). By deafult if we’ve edited that module, it is not automatically reloaded (and therefore won’t take effect in the main script). To enable rapid iteration, it would be very handy to add this IPython magic command at the top – so whenever we’ve updated our module, it’s auto-reloaded in our main script.
Inline Chart Plotting
Another very handy magic command is inline chart plotting (via
matplotlib). By default if we do a plot in Jupyter Notebook, a new window will pop up with the chart. If we include the following IPython magic command at the top, our chart may get rendered directly inline within the Jupyter Notebook. (Note that qtconsole by default do the plot inline so we won’t need to worry about that there. Note also that Jupyter Console is text based only and a chart can only be plotted in a new window – i.e. no inline).
Happy rapid iteration!
Fortunately the answer is yes – this post documents how (once you know how it only takes less than a minute to setup – Read on!)
What we want to Achieve
- If we have a Python file, Sublime Text auto set tab size to 4.
(and some other desirable tab sizes for other languages – concept is the same).
Follow the step-by-step guide – should take about a minute.
First, make sure the Python file is opened in editor and active.
Sublime Text -> Preferences -> Settings - Syntax Specific.
(On Windows and Linux I presume it would be something similar)
Python.sublime-settings file will open. Simply copy and paste this JASON text to it:
Save and close the file.
Save and close the file.
Reference: This Stackoverflow forum
This visual example will show you how to a neatly select elements in a NumPy Matrix (2 dimensional array) in a pretty entertaining way (I promise).
(Caution: this is a NumPy array specific example with the aim of illustrating the a use case of “double colons”
:: for jumping of elements in multiple axes. This example does not cover native Python data structures like
One concrete example to rule them all…
Say we have a NumPy matrix that looks like this:
In : import numpy as np In : X = np.arange(100).reshape(10,10) In : X Out: array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [10, 11, 12, 13, 14, 15, 16, 17, 18, 19], [20, 21, 22, 23, 24, 25, 26, 27, 28, 29], [30, 31, 32, 33, 34, 35, 36, 37, 38, 39], [40, 41, 42, 43, 44, 45, 46, 47, 48, 49], [50, 51, 52, 53, 54, 55, 56, 57, 58, 59], [60, 61, 62, 63, 64, 65, 66, 67, 68, 69], [70, 71, 72, 73, 74, 75, 76, 77, 78, 79], [80, 81, 82, 83, 84, 85, 86, 87, 88, 89], [90, 91, 92, 93, 94, 95, 96, 97, 98, 99]])
Say for some reason, your boss wants you to select the following elements:
“But How???”… Read on! (We can do this in a 2-step approach)
Step 1 – Obtain subset
Specify the “start index” and “end index” in both row-wise and column-wise directions.
In : X2 = X[2:9,3:8] In : X2 Out: array([[23, 24, 25, 26, 27], [33, 34, 35, 36, 37], [43, 44, 45, 46, 47], [53, 54, 55, 56, 57], [63, 64, 65, 66, 67], [73, 74, 75, 76, 77], [83, 84, 85, 86, 87]])
Notice now we’ve just obtained our subset, with the use of simple start and end indexing technique. Next up, how to do that “jumping”… (read on!)
Step 2 – Select elements (with the “jump step” argument)
We can now specify the “jump steps” in both row-wise and column-wise directions (to select elements in a “jumping” way) like this:
In code (note the double colons):
In : X3 = X2[::3, ::2] In : X3 Out: array([[23, 25, 27], [53, 55, 57], [83, 85, 87]])
We have just selected all the elements as required! :)
Consolidate Step 1 (start and end) and Step 2 (“jumping”)
Now we know the concept, we can easily combine step 1 and step 2 into one consolidated step – for compactness:
In : X4 = X[2:9,3:8][::3,::2] In : X4 Out: array([[23, 25, 27], [53, 55, 57], [83, 85, 87]])
(I’ve also posted this trick in this Stackoverflow forum)